package libraryretention import ( "math" "strings" "testing" "time" ) func day(n int) time.Time { return time.Date(2026, 10, 20, 0, 0, 0, 0, time.UTC).AddDate(0, 0, n) } func fixture() []Version { return []Version{ {ID: "v-new", Kind: "test_sealed", CreatedAt: day(5), ExclusiveBytes: 100, SharedBytes: 50}, {ID: "v-mid", Kind: "test_sealed", CreatedAt: day(3), ExclusiveBytes: 200}, {ID: "v-old", Kind: "test_sealed", CreatedAt: day(1), ExclusiveBytes: 300}, {ID: "v-prod", Kind: "production_effective", CreatedAt: day(0), Protected: true, ProtectedBy: []string{"生产生效版本"}}, } } func TestBuildWithoutPolicyKeepsEverything(t *testing.T) { plan := Build(fixture(), 1000, Policy{}, day(6)) if plan.PolicyConfigured { t.Fatal("zero policy must not look configured") } if len(plan.Candidates) != 0 { t.Fatalf("no policy must not produce candidates: %+v", plan.Candidates) } if len(plan.Blocked) == 0 { t.Fatal("no policy must explain why no preview is available") } if plan.AutomaticCleanup { t.Fatal("automatic cleanup must stay off") } } func TestBuildProtectsVersionRegardlessOfAge(t *testing.T) { plan := Build(fixture(), 1000, Policy{KeepVersions: 1}, day(6)) for _, candidate := range plan.Candidates { if candidate.ID == "v-prod" { t.Fatal("protected version must never be a candidate") } } found := false for _, id := range plan.Protected { if id == "v-prod" { found = true } } if !found { t.Fatalf("protected list must include v-prod: %+v", plan.Protected) } // 保留 1 个版本(最近)之后,除受保护版本外都应成为候选 if len(plan.Candidates) != 2 { t.Fatalf("candidates = %+v, want v-mid and v-old", plan.Candidates) } } func TestBuildKeepsRecentPlusProtected(t *testing.T) { plan := Build(fixture(), 1000, Policy{KeepVersions: 2}, day(6)) // 保留最近 2 个(v-new、v-mid)+ 受保护的 v-prod;只有 v-old 成为候选。 if len(plan.Keep) != 3 { t.Fatalf("keep = %+v, want 3 versions", plan.Keep) } if len(plan.Candidates) != 1 || plan.Candidates[0].ID != "v-old" { t.Fatalf("candidates = %+v, want only v-old", plan.Candidates) } } func TestReclaimPreviewCountsOnlyExclusiveBytes(t *testing.T) { plan := Build([]Version{ {ID: "a", CreatedAt: day(0), ExclusiveBytes: 100, SharedBytes: 900}, {ID: "b", CreatedAt: day(1), ExclusiveBytes: 20}, }, 1020, Policy{KeepVersions: 1}, day(2)) if plan.ReclaimPreviewBytes != 100 { t.Fatalf("reclaim preview = %d, want 100 (shared bytes must not count)", plan.ReclaimPreviewBytes) } } func TestBuildRejectsNegativePolicy(t *testing.T) { plan := Build(fixture(), 1, Policy{KeepVersions: -1}, day(6)) if len(plan.Blocked) == 0 || len(plan.Candidates) != 0 { t.Fatalf("invalid policy must block preview: %+v", plan) } } func TestCheckCapacityFailsClosed(t *testing.T) { if result := CheckCapacity(100, 100, 0); result.OK || result.Configured { t.Fatalf("unset budget must fail closed: %+v", result) } if result := CheckCapacity(100, 100, 150); result.OK { t.Fatalf("over budget must fail: %+v", result) } if result := CheckCapacity(100, 20, 150); !result.OK || !result.Configured { t.Fatalf("within budget must pass: %+v", result) } } func TestCheckCapacityRejectsOverflowInsteadOfWrapping(t *testing.T) { // 两个大正数相加会绕回负数;门禁必须拒绝,而不是把负数当成"没超预算"。 result := CheckCapacity(math.MaxInt64-10, 100, math.MaxInt64) if result.OK { t.Fatalf("overflow must be refused, got %+v", result) } if result.Reason == "" || !strings.Contains(result.Reason, "溢出") { t.Fatalf("overflow must name the reason, got %q", result.Reason) } // 恰好等于预算:允许(不超),并且不溢出。 exact := CheckCapacity(100, 50, 150) if !exact.OK || exact.ProjectedByte != 150 { t.Fatalf("exactly at budget must pass: %+v", exact) } // 相差 1 字节超预算:阻断。 if over := CheckCapacity(100, 51, 150); over.OK { t.Fatalf("one byte over budget must be refused: %+v", over) } // 负数输入不能算出"更小"的占用。 if neg := CheckCapacity(-1, 10, 1<<30); neg.OK { t.Fatalf("negative current must be refused: %+v", neg) } } func TestPolicyCombinationUsesConjunctionOfSetDimensions(t *testing.T) { now := time.Date(2026, 10, 20, 0, 0, 0, 0, time.UTC) versions := []Version{ {ID: "new", CreatedAt: now.AddDate(0, 0, -1)}, {ID: "old", CreatedAt: now.AddDate(0, 0, -30)}, } // 同时设 3 个版本 + 7 天:old 两个维度都超限 → 候选;new 太新 → 保留。 both := Build(versions, 0, Policy{KeepVersions: 1, KeepDays: 7}, now) if len(both.Candidates) != 1 || both.Candidates[0].ID != "old" { t.Fatalf("only the doubly-exceeded version may be a candidate: %+v", both.Candidates) } // 只设天数:数量维度不约束,old 超 7 天即为候选。 daysOnly := Build(versions, 0, Policy{KeepDays: 7}, now) if len(daysOnly.Candidates) != 1 || daysOnly.Candidates[0].ID != "old" { t.Fatalf("days-only policy must use the day dimension: %+v", daysOnly.Candidates) } // 数量宽松(保留 5 个)但天数很紧(1 天):同时超限才删 → 都不候选。 wide := Build(versions, 0, Policy{KeepVersions: 5, KeepDays: 1}, now) if len(wide.Candidates) != 0 { t.Fatalf("a version kept by either dimension must not be a candidate: %+v", wide.Candidates) } if both.PolicyNote == "" { t.Fatal("policy note must describe the combination explicitly") } } func TestLockedVersionIsNeverACandidate(t *testing.T) { now := time.Date(2026, 10, 20, 0, 0, 0, 0, time.UTC) locked := Version{ID: "locked", CreatedAt: now.AddDate(0, 0, -90), Protected: true, ProtectedBy: []string{"人工锁定", "备份恢复依赖"}} plan := Build([]Version{locked}, 0, Policy{KeepVersions: 1, KeepDays: 1}, now) if len(plan.Candidates) != 0 { t.Fatalf("locked version must never be a candidate: %+v", plan.Candidates) } if len(plan.Protected) != 1 || len(plan.Blocked) != 0 { t.Fatalf("plan must record protection: %+v", plan) } }