package worker import ( "context" "testing" "time" "mtusi2026lab6/internal/adapter/objectstore" "mtusi2026lab6/internal/domain" ) type cleanupReference struct { id string status domain.AttachmentStatus updatedAt time.Time } type cleanupStoreFake struct { references map[string]cleanupReference cleared map[string]bool } func (store *cleanupStoreFake) LookupAttachmentByStagingKey( _ context.Context, key string, ) (string, domain.AttachmentStatus, time.Time, bool, error) { reference, found := store.references[key] return reference.id, reference.status, reference.updatedAt, found, nil } func (store *cleanupStoreFake) ListReadyStagingKeys( _ context.Context, _ int, ) (map[string]string, error) { keys := make(map[string]string) for key, reference := range store.references { if reference.status == domain.AttachmentStatusReady { keys[key] = reference.id } } return keys, nil } func (store *cleanupStoreFake) ClearAttachmentStagingKey( _ context.Context, _ string, key string, ) error { store.cleared[key] = true return nil } type cleanupObjectsFake struct { objects []objectstore.StoredObject deleted map[string]bool } func (objects *cleanupObjectsFake) List( context.Context, string, ) ([]objectstore.StoredObject, error) { return objects.objects, nil } func (objects *cleanupObjectsFake) Delete(_ context.Context, key string) error { objects.deleted[key] = true return nil } func TestStagingCleanerPreservesActiveWorkAndAppliesRetention(t *testing.T) { now := time.Date(2026, time.November, 5, 12, 0, 0, 0, time.UTC) old := now.Add(-25 * time.Hour) recent := now.Add(-time.Hour) store := &cleanupStoreFake{ references: map[string]cleanupReference{ "staging/ready": {id: "ready", status: domain.AttachmentStatusReady, updatedAt: recent}, "staging/ready-missing": {id: "ready-missing", status: domain.AttachmentStatusReady, updatedAt: recent}, "staging/failed-old": {id: "failed-old", status: domain.AttachmentStatusFailed, updatedAt: old}, "staging/failed-recent": {id: "failed-recent", status: domain.AttachmentStatusFailed, updatedAt: recent}, "staging/queued-old": {id: "queued-old", status: domain.AttachmentStatusQueued, updatedAt: old}, }, cleared: make(map[string]bool), } objects := &cleanupObjectsFake{ objects: []objectstore.StoredObject{ {Key: "staging/ready", LastModified: recent}, {Key: "staging/failed-old", LastModified: old}, {Key: "staging/failed-recent", LastModified: recent}, {Key: "staging/queued-old", LastModified: old}, {Key: "staging/orphan-old", LastModified: old}, {Key: "staging/orphan-recent", LastModified: recent}, }, deleted: make(map[string]bool), } cleaner, err := NewStagingCleaner(store, objects, 24*time.Hour) if err != nil { t.Fatal(err) } deleted, err := cleaner.RunOnce(context.Background(), now) if err != nil { t.Fatal(err) } if deleted != 3 { t.Fatalf("deleted %d objects, want 3", deleted) } for _, key := range []string{"staging/ready", "staging/failed-old", "staging/orphan-old"} { if !objects.deleted[key] { t.Errorf("expected %s to be deleted", key) } } for _, key := range []string{"staging/failed-recent", "staging/queued-old", "staging/orphan-recent"} { if objects.deleted[key] { t.Errorf("active/recent %s was deleted", key) } } if !store.cleared["staging/ready"] || !store.cleared["staging/ready-missing"] || !store.cleared["staging/failed-old"] { t.Fatal("deleted referenced staging keys were not cleared") } if store.cleared["staging/orphan-old"] { t.Fatal("orphan must not clear nonexistent metadata") } }