package postgres import ( "context" "fmt" "knowledge-governance/internal/governance" "testing" "time" ) func TestContractSourceCandidateBeyondHistoryWindow(t *testing.T) { store, _ := newIdentityStore(t) ctx := context.Background() now := time.Now().UTC() source := governance.LibrarySourceIdentity{Project: "source-project", App: "source-app", Scope: "internal", TenantID: 10000, KnowledgeBaseID: "kb-source"} id, err := source.ID() if err != nil { t.Fatal(err) } for i := 0; i < 65; i++ { sid := "other-source" if i == 0 { sid = id } snapshot := fmt.Sprintf("candidate-%d", i) if err := store.RecordLibraryVersion(ctx, governance.LibraryVersion{ID: governance.LibraryVersionID(governance.LibraryVersionTestSealed, "internal", snapshot), Scope: "internal", Kind: governance.LibraryVersionTestSealed, SnapshotID: snapshot, Evidence: map[string]any{"source_id": sid}, CreatedAt: now.Add(time.Duration(i) * time.Second)}); err != nil { t.Fatal(err) } } candidate, found, err := store.LibrarySourceCandidate(ctx, "internal", id) if err != nil || !found || candidate.SnapshotID != "candidate-0" { t.Fatalf("exact candidate missing: %+v %v %v", candidate, found, err) } source.KnowledgeBaseID = "new-kb" newID, _ := source.ID() if _, found, err = store.LibrarySourceCandidate(ctx, "internal", newID); err != nil || found { t.Fatalf("new source inherited old candidate: %v %v", found, err) } if _, found, err = store.LibrarySourceCandidate(ctx, "external", id); err != nil || found { t.Fatalf("cross scope candidate: %v %v", found, err) } }