版本: 1.0
更新日期: 2026-02-28
# 編譯
go build -o xero-cli.exe
# 或使用 go run
go run .go run . config預期輸出:
No configuration found
Run 'xero-cli config --init' to initialize
go run . config --json預期輸出:
{}go run . config --init交互流程:
Initialize Xero CLI Configuration
==================================
Step 1/5: Client ID
-------------------
Enter your Xero Client ID from Xero Developer Portal:
> [輸入 client_id]
Step 2/5: Client Secret (Optional for PKCE)
-------------------------------------------
Enter your Xero Client Secret (leave empty for PKCE flow, recommended):
> [輸入或直接 Enter 跳過]
Step 3/5: Redirect URI
----------------------
Enter Redirect URI [http://localhost:3456/callback]:
> [Enter 使用預設]
Step 4/5: OAuth Scopes
----------------------
Enter OAuth Scopes [openid profile email accounting.transactions accounting.settings offline_access]:
> [Enter 使用預設]
Step 5/5: Callback Server Port
------------------------------
Enter Callback Server Port (3000-9000) [3456]:
> [Enter 使用預設]
Configuration Summary
---------------------
Client ID: abc123-def456-ghi789
Client Secret: ****789
Redirect URI: http://localhost:3456/callback
Scopes: openid profile email accounting.transactions accounting.settings offline_access
Port: 3456
Save this configuration? [Y/n]:
> [Enter 確認]
✓ Configuration saved successfully
Config file: C:\Users\<user>\AppData\Roaming\xero-cli\config.json (encrypted)
Next step: Run 'xero-cli auth' to authenticate
檢查點:
- ✅ 配置文件已創建並加密
- ✅ 文件權限為 0600
- ✅ 可以再次查看配置
go run . config預期輸出:
Xero CLI Configuration
======================
Client ID: abc123-def456-ghi789
Client Secret: ****789
Redirect URI: http://localhost:3456/callback
Scopes: openid profile email accounting.transactions accounting.settings offline_access
Port: 3456
Config File: C:\Users\<user>\AppData\Roaming\xero-cli\config.json (encrypted)
Token File: (not authenticated)
Commands:
Use 'xero-cli config --init' to initialize configuration
Use 'xero-cli config --update' to update configuration
Use 'xero-cli config --reset' to reset configuration
Use 'xero-cli auth' to authenticate with Xero
go run . config --json預期輸出:
{
"client_id": "abc123-def456-ghi789",
"redirect_uri": "http://localhost:3456/callback",
"scopes": "openid profile email accounting.transactions accounting.settings offline_access",
"port": "3456",
"client_secret": "***masked***"
}go run . config --update交互流程:
Update Xero CLI Configuration
==============================
Current Configuration
---------------------
Client ID: abc123-def456-ghi789
Client Secret: ****789
Redirect URI: http://localhost:3456/callback
Scopes: openid profile email accounting.transactions accounting.settings offline_access
Port: 3456
Press Enter to keep current value, or enter new value
Update Client ID [abc123-def456-ghi789]:
> [Enter 保持不變]
Update Client Secret [****789] (leave empty to keep current):
> [Enter 保持不變]
Update Redirect URI [http://localhost:3456/callback]:
> http://localhost:4000/callback
Update Scopes [openid profile email ...]:
> [Enter 保持不變]
Update Port [3456]:
> 4000
Configuration will be updated:
Client ID: abc123-def456-ghi789
Client Secret: ****789
Redirect URI: http://localhost:4000/callback (changed)
Scopes: openid profile email accounting.transactions accounting.settings offline_access
Port: 4000 (changed)
Save changes? [Y/n]:
> [Enter 確認]
✓ Configuration updated
Token cleared (re-authentication required)
Next: Run 'xero-cli auth' to authenticate with new configuration
檢查點:
- ✅ 配置文件已更新
- ✅ Token 已被清空(如果存在)
go run . config --reset交互流程:
Reset Xero CLI Configuration
=============================
WARNING: This will delete your configuration and clear saved tokens.
This action will:
- Delete config file: C:\Users\<user>\AppData\Roaming\xero-cli\config.json
- Delete token file: C:\Users\<user>\AppData\Roaming\xero-cli\tokens.json
Are you sure you want to continue? [y/N]:
> y
✓ Configuration reset successfully
Next step: Run 'xero-cli config --init' to set up new configuration
檢查點:
- ✅ Config 文件已刪除
- ✅ Token 文件已刪除(如果存在)
go run . config --reset交互流程:
...
Are you sure you want to continue? [y/N]:
> n
Reset cancelled
檢查點:
- ✅ Config 文件仍然存在
- ✅ Token 文件仍然存在
go run . auth預期錯誤:
Error: failed to load config: no configuration found
Run 'xero-cli config --init' or provide --client-id flag
go run . auth預期輸出:
Already authenticated
Tenant: ABC Company Ltd
Expires: 2026-02-28T15:30:00Z
Scopes: openid profile email accounting.transactions accounting.settings offline_access
Use --force to re-authenticate or --switch to change tenant
go run . auth --force預期: 重新執行 OAuth flow
go run . auth --switch預期:
- 如果有多個 tenant,顯示選擇列表
- 選擇後更新 Token 中的 TenantID
go run . config --init
# 輸入 port: 2000預期錯誤:
Invalid port: port must be between 3000-9000. Please try again.
go run . config --init
# 直接 Enter預期錯誤:
Error: client ID is required
go run . config --init
# 完成輸入後,在確認時輸入 n預期:
Configuration cancelled
# 查看 config 文件內容
cat ~/.config/xero-cli/config.json
# 或 Windows
type %APPDATA%\xero-cli\config.json預期格式:
{
"version": 1,
"data": "base64_encrypted_data...",
"nonce": "base64_nonce...",
"platform": "windows"
}# 查看 token 文件內容
cat ~/.config/xero-cli/tokens.json預期格式: 同上,加密格式
# Linux/macOS
ls -la ~/.config/xero-cli/
# Windows (PowerShell)
Get-Acl %APPDATA%\xero-cli\config.json | Format-List預期: 僅所有者可讀寫(0600)
-
config- 查看配置(無配置) -
config --json- JSON 輸出(無配置) -
config --init- 初始化配置 -
config- 查看配置(有配置) -
config --json- JSON 輸出(有配置) -
config --update- 更新配置 -
config --reset- 重置配置(確認) -
config --reset- 重置配置(取消)
-
auth- 無配置 -
auth- 已有有效 token -
auth --force- 強制重新認證 -
auth --switch- 切換 tenant -
auth- 使用 CLI flags
- 無效 Port
- 空 Client ID
- 取消配置保存
- Config 文件加密格式
- Token 文件加密格式
- 文件權限 0600
無
- 多 Tenant 選擇流程(需要真實 Xero 賬號有多個 organization)
- PKCE flow vs Standard OAuth flow(需要有 client secret)
- Token Refresh 流程
- 跨機器測試(機器綁定驗證)
### 測試結果
**測試日期**: YYYY-MM-DD
**測試環境**: Windows 11 / Go 1.24.0
**測試者**: [姓名]
#### 通過測試
- [ ] Config 查看
- [ ] Config 初始化
- [ ] Config 更新
- [ ] Config 重置
- [ ] Auth 基本流程
#### 失敗測試
- [ ] [測試名稱] - [失敗原因]
#### 發現問題
1. [問題描述]
2. [問題描述]
#### 建議改進
1. [建議內容]
2. [建議內容]# 運行所有測試
go test ./...
# 詳細輸出
go test ./... -v
# 覆蓋率報告
go test ./... -cover
# 單一測試
go test -run ^TestGetPayment_Mock$ ./...
go test -run ^TestBuildOrderClause$ ./...
go test -run ^TestBuildCreditNoteWhereClause$ ./...| 測試類別 | 測試函數 | 子測試數量 | 狀態 |
|---|---|---|---|
| API 測試 | |||
| GetPayment | TestGetPayment_Mock | 1 | ✅ |
| GetPayment NotFound | TestGetPayment_NotFound | 1 | ✅ |
| GetPaymentsPaginated | TestGetPaymentsPaginated_WithOrder | 1 | ✅ |
| GetCreditNotes | TestGetCreditNotes_Mock | 1 | ✅ |
| GetCreditNote | TestGetCreditNote_Mock | 2 | ✅ |
| WHERE Clause 測試 | |||
| normalizeWhereClause | TestNormalizeWhereClause | 10 | ✅ |
| buildInvoiceWhereClause | TestBuildInvoiceWhereClause | 5 | ✅ |
| buildContactWhereClause | TestBuildContactWhereClause | 7 | ✅ |
| buildAccountWhereClause | TestBuildAccountWhereClause | 6 | ✅ |
| buildPaymentWhereClause | TestBuildPaymentWhereClause | 8 | ✅ |
| buildCreditNoteWhereClause | TestBuildCreditNoteWhereClause | 7 | ✅ |
| 排序測試 | |||
| buildOrderClause | TestBuildOrderClause | 7 | ✅ |
| 認證測試 | |||
| OAuth2 | TestGenerateCodeVerifier | 1 | ✅ |
| OAuth2 | TestGenerateCodeChallenge | 1 | ✅ |
| Token | TestTokenStoreFunctions | 1 | ✅ |
| Config | TestConfigValidation | 1 | ✅ |
| API Client 測試 | |||
| CallAPI | TestAPIClient_CallAPI_Success | 1 | ✅ |
| CallAPI Error | TestAPIClient_CallAPI_Error | 1 | ✅ |
| AutoRefresh | TestAPIClient_CallWithAutoRefresh | 3 | ✅ |
| 總計 | 21 個測試函數 | 70+ 子測試 | 100% 通過 |
| 測試類別 | 測試數量 | 說明 |
|---|---|---|
| API 測試 | 10+ | GetPayment, GetCreditNote, GetConnections, etc. |
| WHERE Clause 測試 | 50+ | buildInvoiceWhereClause, buildContactWhereClause, etc. |
| 排序測試 | 7+ | buildOrderClause |
| 認證測試 | 5+ | OAuth2 PKCE, Token 管理 |
| 工具函數測試 | 10+ | normalizeWhereClause, ConfigValidation |
| 總計 | 70+ |
api_payments_test.go- Payments API 測試api_creditnotes_test.go- CreditNotes API 測試cmd_helpers_test.go- WHERE clause 和排序測試api_test.go- API Client 基礎測試auth_test.go- OAuth2 認證測試config_test.go- 配置測試