package anonymousprofile import ( "bytes" "context" "encoding/json" "errors" "strings" "testing" "time" "github.com/google/uuid" "lincodex/backend/internal/domain" ) type privacyCohortReaderFunc func(context.Context, uuid.UUID) ([]domain.CandidateViewSource, error) func (reader privacyCohortReaderFunc) ListVisibleCandidateViewSources(ctx context.Context, organizationID uuid.UUID) ([]domain.CandidateViewSource, error) { return reader(ctx, organizationID) } func TestListVisibleReturnsOrganizationScopedAliasesAndSuppressesSmallCohort(t *testing.T) { t.Parallel() organizationID := uuid.New() candidateIDs := make([]uuid.UUID, 5) sources := make([]domain.CandidateViewSource, 0, len(candidateIDs)) fields, err := domain.NewCardFieldSet([]domain.CardField{domain.CardFieldStack}) if err != nil { t.Fatalf("NewCardFieldSet() error = %v", err) } for index := range candidateIDs { candidateIDs[index] = uuid.New() profile, err := domain.EmptyCandidateProfile(candidateIDs[index]) if err != nil { t.Fatalf("EmptyCandidateProfile() error = %v", err) } source, err := domain.NewCandidateViewSource( candidateIDs[index], fields, domain.CandidateViewProfileFromContent(profile.Content()), nil, nil, nil, ) if err != nil { t.Fatalf("NewCandidateViewSource() error = %v", err) } sources = append(sources, source) } key, err := domain.NewCandidateViewPseudonymKey(bytes.Repeat([]byte("purpose-specific-key"), 2)) if err != nil { t.Fatalf("NewCandidateViewPseudonymKey() error = %v", err) } service := NewService(privacyCohortReaderFunc(func(_ context.Context, got uuid.UUID) ([]domain.CandidateViewSource, error) { if got != organizationID { t.Fatalf("reader organization = %s, want %s", got, organizationID) } return sources, nil }), key, func() time.Time { return time.Date(2026, 10, 7, 12, 0, 0, 0, time.UTC) }) views, err := service.ListVisible(context.Background(), organizationID) if err != nil { t.Fatalf("ListVisible() error = %v", err) } if len(views) != 5 { t.Fatalf("ListVisible() returned %d views, want five", len(views)) } aliases := make([]string, 0, len(views)) for _, view := range views { aliases = append(aliases, view.Pseudonym) } seenAliases := make(map[string]struct{}, len(aliases)) for _, alias := range aliases { if _, duplicate := seenAliases[alias]; duplicate { t.Fatalf("pseudonyms are not unique within the organization: %v", aliases) } seenAliases[alias] = struct{}{} } encoded, err := json.Marshal(views) if err != nil { t.Fatalf("Marshal() error = %v", err) } for _, candidateID := range candidateIDs { if bytes.Contains(encoded, []byte(candidateID.String())) { t.Fatalf("organization-facing projection contains source ID %s", candidateID) } } otherOrganization := uuid.New() otherService := NewService(privacyCohortReaderFunc(func(context.Context, uuid.UUID) ([]domain.CandidateViewSource, error) { return sources, nil }), key, func() time.Time { return time.Date(2026, 10, 7, 12, 0, 0, 0, time.UTC) }) otherViews, err := otherService.ListVisible(context.Background(), otherOrganization) if err != nil { t.Fatalf("ListVisible(other organization) error = %v", err) } if otherViews[0].Pseudonym == views[0].Pseudonym { t.Fatal("candidate pseudonym was reused across organizations") } smallService := NewService(privacyCohortReaderFunc(func(context.Context, uuid.UUID) ([]domain.CandidateViewSource, error) { return sources[:4], nil }), key, time.Now) small, err := smallService.ListVisible(context.Background(), organizationID) if err != nil { t.Fatalf("ListVisible(small cohort) error = %v", err) } if len(small) != 0 { t.Fatalf("small cohort returned %d views, want suppression", len(small)) } } func TestListVisibleRejectsInvalidScopeAndSourceFailures(t *testing.T) { t.Parallel() key, err := domain.NewCandidateViewPseudonymKey(bytes.Repeat([]byte("purpose-specific-key"), 2)) if err != nil { t.Fatalf("NewCandidateViewPseudonymKey() error = %v", err) } service := NewService(privacyCohortReaderFunc(func(context.Context, uuid.UUID) ([]domain.CandidateViewSource, error) { return nil, errors.New("storage unavailable") }), key, time.Now) if _, err := service.ListVisible(context.Background(), uuid.Nil); !errors.Is(err, ErrInvalidOrganization) { t.Fatalf("ListVisible(empty organization) error = %v, want ErrInvalidOrganization", err) } if _, err := service.ListVisible(context.Background(), uuid.New()); err == nil || !strings.Contains(err.Error(), "read visible candidate sources") { t.Fatalf("ListVisible(storage error) = %v, want wrapped read error", err) } invalidSourceService := NewService(privacyCohortReaderFunc(func(context.Context, uuid.UUID) ([]domain.CandidateViewSource, error) { return []domain.CandidateViewSource{{}}, nil }), key, time.Now) if _, err := invalidSourceService.ListVisible(context.Background(), uuid.New()); !errors.Is(err, domain.ErrInvalidAnonymousCandidateView) { t.Fatalf("ListVisible(invalid source) error = %v, want invalid projection", err) } }