package policy import ( "context" "errors" "testing" "lincodex/backend/internal/domain" "lincodex/backend/internal/port/outbound" ) func seedVersion(t *testing.T, store *memoryPolicies, kind domain.PolicyKind, product domain.ProductPolicy) { t.Helper() publisher := NewPublisher(store, testClock) if _, err := publisher.Publish(context.Background(), PublishInput{ Key: domain.ProductPolicyKey, Kind: kind, Author: domain.DemoPolicySeedAuthor, Policy: product, }); err != nil { t.Fatalf("Publish() error = %v", err) } } func TestEnsureDemoMatchingSectionAddsOnlyTheSection(t *testing.T) { store := &memoryPolicies{} seedVersion(t, store, domain.PolicyKindDemo, domain.DemoProductPolicyV2()) publisher := NewPublisher(store, testClock) result, err := publisher.EnsureDemoMatchingSection(context.Background(), "operator:night") if err != nil || !result.Created || result.Version.Version() != 2 { t.Fatalf("EnsureDemoMatchingSection() = %+v, %v, want version 2 created", result, err) } extended := result.Version.Policy() want := domain.DemoProductPolicyV2() if extended.Matching == nil || extended.Support == nil || extended.Assessment.Attempt == nil || extended.Invitations.MaxPendingPerNeed != want.Invitations.MaxPendingPerNeed { t.Fatalf("extended policy = %+v, want the earlier sections kept and the matching section added", extended) } if result.Version.Kind() != domain.PolicyKindDemo || string(result.Version.Author()) != "operator:night" { t.Fatalf("version kind/author = %s/%s", result.Version.Kind(), result.Version.Author()) } again, err := publisher.EnsureDemoMatchingSection(context.Background(), "operator:night") if err != nil || again.Created || again.Version.Version() != 2 || len(store.versions) != 2 { t.Fatalf("second call = %+v, %v with %d versions, want no new version", again, err, len(store.versions)) } } func TestEnsureDemoMatchingSectionRefusesWhatItCannotExtend(t *testing.T) { empty := &memoryPolicies{} if _, err := NewPublisher(empty, testClock).EnsureDemoMatchingSection(context.Background(), "operator:night"); !errors.Is(err, outbound.ErrPolicyVersionNotFound) { t.Fatalf("empty family error = %v, want ErrPolicyVersionNotFound", err) } calibrated := &memoryPolicies{} product := domain.DemoProductPolicyV2() product.Assessment.Calibration = domain.AssessmentCalibrationCalibrated seedVersion(t, calibrated, domain.PolicyKindCalibrated, product) if _, err := NewPublisher(calibrated, testClock).EnsureDemoMatchingSection(context.Background(), "operator:night"); !errors.Is(err, ErrNotDemoPolicy) { t.Fatalf("calibrated family error = %v, want ErrNotDemoPolicy", err) } if len(calibrated.versions) != 1 { t.Fatalf("versions = %d, want the calibrated family untouched", len(calibrated.versions)) } } func TestEnsureDemoMatchingSectionDoesNotRetryAfterAConcurrentPublication(t *testing.T) { store := &memoryPolicies{} seedVersion(t, store, domain.PolicyKindDemo, domain.DemoProductPolicyV2()) store.beforeCreate = func(domain.PolicyVersionDraft) { store.beforeCreate = nil // Another operator publishes version 2 first. if _, err := NewPublisher(store, testClock).Publish(context.Background(), PublishInput{ Key: domain.ProductPolicyKey, Kind: domain.PolicyKindDemo, Author: "operator:other", Policy: domain.DemoProductPolicyV2(), }); err != nil { t.Errorf("concurrent Publish() error = %v", err) } } _, err := NewPublisher(store, testClock).EnsureDemoMatchingSection(context.Background(), "operator:night") if !errors.Is(err, ErrPolicyChanged) { t.Fatalf("EnsureDemoMatchingSection() error = %v, want ErrPolicyChanged", err) } if len(store.versions) != 2 || store.versions[1].Policy().Matching != nil { t.Fatalf("versions = %d, want only the concurrent publication stored and no built-on-stale version", len(store.versions)) } }