Files
arcrun-collector/cloud_version_test.go
T
Leo 1e139a2e7e t177 更正:取回 08-02 的原版修法,撤掉我重寫的次級版本
【leo 08-04 質問】「這不是先前改過為什麼又出現?你去查看 wiki,是不是你沒看 wiki 就改?」
——**是。查證屬實,而且比沒查更糟。**

【事實】08-02 commit `dde7a5b` 已完整修過這個 bug(t170 daemon 側),
但它**只在 `fix/cis-round3-landing-favicon` 分支**,`feat/daemon` 不含它。
我在 feat/daemon 上看到沒修過的舊 code,就當新 bug 從頭修了一遍。

【而且我修得比原版差】
  08-02 原版:isSemverLike 辨格式 → compareSemver **逐段整數比較**
              (能擋 "1.10.0" < "1.9.0" 這個經典坑)+ minCloudRelease 最低版本常數
  我今天版:  只判「是不是 semver」,是就放行 ⇒ 無法表達「semver 世代的最低相容版本」
⇒ 本 commit **取回 08-02 原版**(git checkout dde7a5b -- ...),
   刪掉我另建的重複測試檔 cloudversion_test.go,改用原版在 main_test.go 的 8 則。

【測試】兩模組全綠;8 則逐項通過,含「semver 1.10.0 比 1.9.0 新(防字串比較)」
——那則正是我的版本表達不出來的。

【病根】hook 給的是 wiki grep 命中行,而那幾行**不含「已修過」的線索**
(關鍵字 compareSemver/isSemverLike 不在搜尋詞裡)。我把「hook 沒提」當成「沒人修過」,
且跳過了 `git log --all -S` 這個五秒就能做的機械檢查。
⇒ 已在頂層 InkStoneCo 補 hook:改檔時一併列出**該檔在所有分支的近期 commit**。

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-04 09:51:48 +08:00

73 lines
2.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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,
},
// 🔴 2026-08-02 迴歸守衛(leo 實撞:兩帳號都更新到 1.4.1,卻**都**顯示需要更新且消不掉)。
// 病根:舊實作 `head < minCloudBuilt` 是**字串比較**semver 進來後
// "1.4.1" < "2026-07-28" 恆為真 ⇒ 最新版被判過舊。
// 這幾則若變紅,代表版本比較又退回字串比較,或新格式沒被辨識。
{
name: "semver 最新版(1.4.1)不該被判過舊", version: "1.4.1", checkOK: true, wantStale: false,
},
{
name: "semver 剛好達標(1.4.0", version: "1.4.0", checkOK: true, wantStale: false,
},
{
name: "semver 過舊(1.3.9)需要更新", version: "1.3.9", checkOK: true, wantStale: true,
},
{
// 字串比較會誤判成 "1.10.0" < "1.9.0";整數逐段比較才對
name: "semver 1.10.0 比 1.9.0 新(防字串比較)", version: "1.10.0", checkOK: true, wantStale: false,
},
{
// 舊格式仍須正確辨識——兩種格式並存期間,老實例不可被誤判成最新而收不到提示
name: "舊格式過舊仍要判過舊(相容)", version: "2026-07-01+deadbee", checkOK: true, wantStale: true,
},
}
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) = %vwant %v", tc.version, tc.checkOK, got, tc.wantStale)
}
})
}
}