feat(t103): daemon 偵測雲端過舊+托盤更新入口——連動閉環
每輪 GET /health 取 bundle_version(5s timeout 失敗靜默);空或日期<minCloudBuilt → 托盤「⚠ 知識庫需要更新(點我)」開 install.arcrun.dev;結果進 status.json。 兩模組 go test 全綠(總管親跑)。leo:「daemon 和雲端是連動的」——自此用戶只看托盤。 (實作=子 CC;驗證+commit=總管)
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
// cloud_version_test.go — t103 雲端版本判定函式 4 案單元測試。
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestMain 在所有測試執行前把 fetchCloudVersion 換成 stub(回 ("", false)),
|
||||
// 避免各測試的假 httptest.Server 或離線環境因真實 /health 呼叫炸掉測試。
|
||||
// TestCloudVersionStale 直接測 cloudVersionStale 邏輯,不受此 stub 影響。
|
||||
func TestMain(m *testing.M) {
|
||||
fetchCloudVersion = func(string) (string, bool) { return "", false }
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
// TestCloudVersionStale 驗證四種判定情境:空版本/過舊/正常/解析失敗。
|
||||
func TestCloudVersionStale(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
version string
|
||||
checkOK bool
|
||||
wantStale bool
|
||||
}{
|
||||
{
|
||||
// 老實例:/health 可達但 bundle_version 為空字串 → 需要更新
|
||||
name: "空版本(老實例)", version: "", checkOK: true, wantStale: true,
|
||||
},
|
||||
{
|
||||
// 日期早於 minCloudBuilt → 需要更新
|
||||
name: "過舊日期", version: "2026-07-27+abc1234", checkOK: true, wantStale: true,
|
||||
},
|
||||
{
|
||||
// 日期等於 minCloudBuilt → 正常(剛好達標)
|
||||
name: "正常(剛好達標)", version: "2026-07-28+abc1234", checkOK: true, wantStale: false,
|
||||
},
|
||||
{
|
||||
// /health 不可達(解析失敗/逾時)→ 靜默不判定
|
||||
name: "解析失敗(/health 不可達)", version: "", checkOK: false, wantStale: false,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := cloudVersionStale(tc.version, tc.checkOK)
|
||||
if got != tc.wantStale {
|
||||
t.Errorf("cloudVersionStale(%q, %v) = %v,want %v", tc.version, tc.checkOK, got, tc.wantStale)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user