Files
knowledge-governance/internal/libraryretention/retention_test.go
T
姚章浩 f91359f881
build / build (push) Successful in 1m15s
feat(library): 存储管理真实化——容量门禁、依赖保护与清理预览
为什么:存储页原来只有占用数字,保留/依赖保护/清理预览写"未接线";这些都不需要生产写权限,
按"未设置就不做决定"的口径实现成只读计算。

- internal/libraryretention:按保留数量/天数生成预览;生产生效、每库最近封存、每次生产预备份、
  在飞作业引用一律受保护;回收预览只算独占字节,共享 blob 不计。
- internal/libraryretention.CheckCapacity:预算未设置即不放行(无法证明装得下),超预算阻断;
  已并入 POST /library/syncs 的 gate 缺项。
- 新增 GET /api/v1/library/storage:真实归档用量 + 三类版本保护依据 + 策略 + 预览 + 容量,只读。
- 系统设置→存储管理改为读该接口:策略未设置时明确"全部保留、不生成候选",自动清理恒关闭。
- 配置项 LIBRARY_CAPACITY_BUDGET_BYTES / LIBRARY_RETENTION_VERSIONS / LIBRARY_RETENTION_DAYS(未设=0)。
2026-10-28 15:04:44 +08:00

96 lines
3.1 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.
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)
}
}