package libraryretention import ( "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) } }