package repository import "context" import ( "testing" "time" "supply-intelligence/internal/domain" ) func TestMemoryRepositoryRoutingState(t *testing.T) { repo := NewMemoryRepository() state := domain.AccountRoutingState{AccountID: 1, Platform: "openai", AccountStatus: domain.AccountStatusActive, RoutingEnabled: true, Version: 1} repo.UpsertRoutingState(context.Background(), state) got, ok := repo.GetRoutingState(context.Background(), 1) if !ok { t.Fatalf("expected routing state") } if got.AccountStatus != domain.AccountStatusActive { t.Fatalf("unexpected status: %q", got.AccountStatus) } } func TestMemoryRepositoryPackageEventsAndAck(t *testing.T) { repo := NewMemoryRepository() evt := domain.PackageChangeEvent{EventID: "evt-1", EventType: "supply_package_published", PackageID: 1, Platform: "openai", Model: "gpt-4.1-mini", OccurredAt: time.Unix(10, 0).UTC(), Version: 2} repo.AppendPackageEvent(context.Background(), evt) items := repo.ListPackageEvents(context.Background(), ) if len(items) != 1 { t.Fatalf("expected 1 event, got %d", len(items)) } ackedAt := time.Unix(20, 0).UTC() updated, err := repo.AckPackageEvent(context.Background(), "evt-1", "gateway", domain.GatewayAckResultApplied, "ok", ackedAt) if err != nil { t.Fatalf("unexpected ack error: %v", err) } if updated.GatewaySyncStatus != domain.GatewaySyncStatusApplied { t.Fatalf("unexpected ack status: %+v", updated) } if updated.Consumer != "gateway" || updated.ConsumerDetail != "ok" { t.Fatalf("unexpected consumer metadata: %+v", updated) } if updated.AckedAt == nil || !updated.AckedAt.Equal(ackedAt) { t.Fatalf("unexpected ack time: %+v", updated) } } func TestMemoryRepositoryListPackageEventsAfterCursor(t *testing.T) { repo := NewMemoryRepository() repo.AppendPackageEvent(context.Background(), domain.PackageChangeEvent{EventID: "evt-1", EventType: "supply_package_published", PackageID: 1, Platform: "openai", Model: "a", OccurredAt: time.Unix(10, 0).UTC(), Version: 1}) repo.AppendPackageEvent(context.Background(), domain.PackageChangeEvent{EventID: "evt-2", EventType: "supply_package_published", PackageID: 2, Platform: "openai", Model: "b", OccurredAt: time.Unix(20, 0).UTC(), Version: 2}) items, nextCursor := repo.ListPackageEventsAfter(context.Background(), "") if len(items) != 2 || nextCursor != "" { t.Fatalf("unexpected initial page: len=%d next=%q", len(items), nextCursor) } items, nextCursor = repo.ListPackageEventsAfter(context.Background(), "evt-1") if len(items) != 1 || items[0].EventID != "evt-2" || nextCursor != "" { t.Fatalf("unexpected cursor page: items=%+v next=%q", items, nextCursor) } } func TestMemoryRepositoryDiscoveryCandidateCRUD(t *testing.T) { repo := NewMemoryRepository() candidate := domain.DiscoveryCandidate{ CandidateID: "cand-1", AccountID: 1, Platform: "openai", Model: "gpt-4.1-mini", Source: "seed", Status: domain.DiscoveryCandidateStatusPendingAdmission, DiscoveredAt: time.Unix(10, 0).UTC(), UpdatedAt: time.Unix(10, 0).UTC(), Version: 1, } repo.UpsertDiscoveryCandidateContext(nil, candidate) got, ok := repo.GetDiscoveryCandidateByIDContext(nil, "cand-1") if !ok || got.CandidateID != "cand-1" { t.Fatalf("expected candidate, got %+v ok=%v", got, ok) } } func TestMemoryRepositoryFindDiscoveryCandidateByBusinessKey(t *testing.T) { repo := NewMemoryRepository() repo.UpsertDiscoveryCandidateContext(nil, domain.DiscoveryCandidate{ CandidateID: "cand-1", AccountID: 1, Platform: "openai", Model: "gpt-4.1-mini", Source: "seed", Status: domain.DiscoveryCandidateStatusPendingAdmission, DiscoveredAt: time.Unix(10, 0).UTC(), UpdatedAt: time.Unix(10, 0).UTC(), Version: 1, }) got, ok := repo.FindDiscoveryCandidateContext(nil, 1, "openai", "gpt-4.1-mini") if !ok || got.CandidateID != "cand-1" { t.Fatalf("expected candidate by business key, got %+v ok=%v", got, ok) } } func TestMemoryRepositoryGetLatestDiscoveryCandidate(t *testing.T) { repo := NewMemoryRepository() repo.UpsertDiscoveryCandidateContext(nil, domain.DiscoveryCandidate{ CandidateID: "cand-1", AccountID: 1, Platform: "openai", Model: "gpt-4.1-mini", Source: "seed", Status: domain.DiscoveryCandidateStatusDiscovered, DiscoveredAt: time.Unix(10, 0).UTC(), UpdatedAt: time.Unix(10, 0).UTC(), Version: 1, }) repo.UpsertDiscoveryCandidateContext(nil, domain.DiscoveryCandidate{ CandidateID: "cand-2", AccountID: 2, Platform: "openai", Model: "gpt-4.1-mini", Source: "seed", Status: domain.DiscoveryCandidateStatusTestPassed, DiscoveredAt: time.Unix(20, 0).UTC(), UpdatedAt: time.Unix(20, 0).UTC(), Version: 2, }) got, ok := repo.GetLatestDiscoveryCandidateContext(nil, "openai", "gpt-4.1-mini") if !ok || got.CandidateID != "cand-2" { t.Fatalf("expected latest candidate, got %+v ok=%v", got, ok) } } func TestMemoryRepositoryListDiscoveryCandidatesByStatusAndOrder(t *testing.T) { repo := NewMemoryRepository() repo.UpsertDiscoveryCandidateContext(nil, domain.DiscoveryCandidate{ CandidateID: "cand-2", AccountID: 2, Platform: "openai", Model: "b", Source: "seed", Status: domain.DiscoveryCandidateStatusAdmitted, DiscoveredAt: time.Unix(20, 0).UTC(), UpdatedAt: time.Unix(20, 0).UTC(), Version: 1, }) repo.UpsertDiscoveryCandidateContext(nil, domain.DiscoveryCandidate{ CandidateID: "cand-1", AccountID: 1, Platform: "openai", Model: "a", Source: "seed", Status: domain.DiscoveryCandidateStatusPendingAdmission, DiscoveredAt: time.Unix(10, 0).UTC(), UpdatedAt: time.Unix(10, 0).UTC(), Version: 1, }) items := repo.ListDiscoveryCandidatesContext(nil, domain.DiscoveryCandidateStatusPendingAdmission) if len(items) != 1 || items[0].CandidateID != "cand-1" { t.Fatalf("unexpected filtered items: %+v", items) } all := repo.ListDiscoveryCandidatesContext(nil, "") if len(all) != 2 || all[0].CandidateID != "cand-1" || all[1].CandidateID != "cand-2" { t.Fatalf("unexpected ordering: %+v", all) } }