feat: two-store reconciler with safe repair and cost measurement (grade 5, track 2)

This commit is contained in:
2026-11-20 05:01:20 +03:00
parent 1c3011bf28
commit 60e8f4474b
36 changed files with 807 additions and 212 deletions
+33 -9
View File
@@ -8,7 +8,7 @@ import (
func TestPlanDetectsOrphansAndMissing(t *testing.T) {
now := time.Date(2026, 11, 20, 12, 0, 0, 0, time.UTC)
usedID, missingID := uuid.New(), uuid.New()
usedID, missingID, unreferencedID := uuid.New(), uuid.New(), uuid.New()
objects := []Object{
{Key: "chats/a/used", Size: 10, LastModified: now.Add(-2 * time.Hour)},
{Key: "chats/a/orphan-old", Size: 20, LastModified: now.Add(-2 * time.Hour)},
@@ -17,16 +17,28 @@ func TestPlanDetectsOrphansAndMissing(t *testing.T) {
}
rows := []Metadata{
{ID: usedID, ObjectKey: "chats/a/used", ChatID: uuid.New(), CreatedAt: now.Add(-2 * time.Hour)},
{ID: missingID, ObjectKey: "chats/a/missing", ChatID: uuid.New(), CreatedAt: now.Add(-2 * time.Hour)},
{ID: missingID, ObjectKey: "chats/a/missing-referenced", ChatID: uuid.New(), CreatedAt: now.Add(-2 * time.Hour)},
{ID: unreferencedID, ObjectKey: "chats/a/missing-orphan-row", ChatID: uuid.New(), CreatedAt: now.Add(-2 * time.Hour)},
}
referenced := map[uuid.UUID]bool{missingID: true}
orphans, missing := Plan(objects, rows, "chats/", now, time.Hour)
orphans, missing := Plan(objects, rows, referenced, "chats/", now, time.Hour)
if len(orphans) != 1 || orphans[0].Key != "chats/a/orphan-old" {
t.Fatalf("orphans = %+v, want only chats/a/orphan-old", orphans)
}
if len(missing) != 1 || missing[0].ObjectKey != "chats/a/missing" {
t.Fatalf("missing = %+v, want only chats/a/missing", missing)
if len(missing) != 2 {
t.Fatalf("missing = %+v, want two rows", missing)
}
byKey := map[string]Missing{}
for _, item := range missing {
byKey[item.ObjectKey] = item
}
if !byKey["chats/a/missing-referenced"].Referenced {
t.Error("referenced missing row must be flagged")
}
if byKey["chats/a/missing-orphan-row"].Referenced {
t.Error("unreferenced missing row must not be flagged")
}
}
@@ -36,12 +48,12 @@ func TestPlanHonoursGraceAndPrefix(t *testing.T) {
{Key: "chats/a/fresh", Size: 1, LastModified: now.Add(-30 * time.Second)},
{Key: "outside/key", Size: 1, LastModified: now.Add(-48 * time.Hour)},
}
orphans, _ := Plan(objects, nil, "chats/", now, time.Hour)
orphans, _ := Plan(objects, nil, nil, "chats/", now, time.Hour)
if len(orphans) != 0 {
t.Fatalf("orphans = %+v, want none", orphans)
}
orphans, _ = Plan(objects, nil, "chats/", now, 0)
orphans, _ = Plan(objects, nil, nil, "chats/", now, 0)
if len(orphans) != 1 || orphans[0].Key != "chats/a/fresh" {
t.Fatalf("orphans with zero grace = %+v, want chats/a/fresh", orphans)
}
@@ -52,7 +64,7 @@ func TestPlanNeverDeletesReferencedObjects(t *testing.T) {
objects := []Object{{Key: "chats/a/used", Size: 1, LastModified: now.Add(-100 * time.Hour)}}
rows := []Metadata{{ID: uuid.New(), ObjectKey: "chats/a/used", ChatID: uuid.New(), CreatedAt: now.Add(-100 * time.Hour)}}
orphans, missing := Plan(objects, rows, "chats/", now, 0)
orphans, missing := Plan(objects, rows, nil, "chats/", now, 0)
if len(orphans) != 0 {
t.Fatalf("orphans = %+v, want none for referenced object", orphans)
}
@@ -61,6 +73,18 @@ func TestPlanNeverDeletesReferencedObjects(t *testing.T) {
}
}
func TestPlanOnlyReportsRowsInsidePrefix(t *testing.T) {
now := time.Date(2026, 11, 20, 12, 0, 0, 0, time.UTC)
rows := []Metadata{
{ID: uuid.New(), ObjectKey: "chats/a/inside", ChatID: uuid.New(), CreatedAt: now},
{ID: uuid.New(), ObjectKey: "bench/outside", ChatID: uuid.New(), CreatedAt: now},
}
_, missing := Plan(nil, rows, nil, "chats/", now, 0)
if len(missing) != 1 || missing[0].ObjectKey != "chats/a/inside" {
t.Fatalf("missing = %+v, want only rows under the scanned prefix", missing)
}
}
func TestPlanOutputIsSorted(t *testing.T) {
now := time.Date(2026, 11, 20, 12, 0, 0, 0, time.UTC)
objects := []Object{
@@ -68,7 +92,7 @@ func TestPlanOutputIsSorted(t *testing.T) {
{Key: "chats/a/b", LastModified: now.Add(-time.Hour)},
{Key: "chats/a/m", LastModified: now.Add(-time.Hour)},
}
orphans, _ := Plan(objects, nil, "chats/", now, 0)
orphans, _ := Plan(objects, nil, nil, "chats/", now, 0)
if len(orphans) != 3 || orphans[0].Key != "chats/a/b" || orphans[2].Key != "chats/a/z" {
t.Fatalf("orphans are not sorted: %+v", orphans)
}