Files
lincodex/backend/internal/usecase/matching/reveal_test.go
T

130 lines
5.4 KiB
Go

package matching
import (
"context"
"slices"
"testing"
"github.com/google/uuid"
"lincodex/backend/internal/domain"
"lincodex/backend/internal/port/outbound"
)
// viewsFake charges a fixed number of distinct candidates and records what it
// is asked to write.
type viewsFake struct {
allowance int
reserved map[uuid.UUID]bool
attempts []uuid.UUID
recorded map[uuid.UUID][]domain.CardField
order []uuid.UUID
}
func newViewsFake(allowance int) *viewsFake {
return &viewsFake{allowance: allowance, reserved: map[uuid.UUID]bool{}, recorded: map[uuid.UUID][]domain.CardField{}}
}
func (views *viewsFake) Reserve(_ context.Context, candidateID uuid.UUID) (bool, error) {
views.attempts = append(views.attempts, candidateID)
if views.reserved[candidateID] {
return true, nil
}
if len(views.reserved) >= views.allowance {
return false, nil
}
views.reserved[candidateID] = true
return true, nil
}
func (views *viewsFake) Record(_ context.Context, candidateID uuid.UUID, fields []domain.CardField) error {
views.recorded[candidateID] = fields
views.order = append(views.order, candidateID)
return nil
}
func revealService(views *viewsFake) *Service {
return &Service{views: func(context.Context, outbound.UnitOfWork, uuid.UUID, uuid.UUID) (ViewSession, error) {
return views, nil
}}
}
func confirmedItem(id uuid.UUID) rankedEntry {
band := domain.CandidateExperienceBandOneToThree
return rankedEntry{candidate: id, entry: Entry{
Specialization: domain.SpecializationBackend,
Category: &domain.CandidateViewCategory{Specialization: domain.SpecializationBackend, Grade: domain.GradeJunior},
PublicContext: PublicContext{ExperienceBand: &band, Stack: []domain.CandidateViewSkillCode{domain.CandidateViewSkillGo}},
}}
}
func declaredItem(id uuid.UUID, specialization domain.SpecializationCode) rankedEntry {
return rankedEntry{candidate: id, entry: Entry{Specialization: specialization}}
}
func TestRevealCutsEachGroupAndRecordsTheFieldsReturned(t *testing.T) {
first, second := uuid.New(), uuid.New()
policy := domain.DemoMatchingPolicy()
policy.MaxResultsPerGroup = 2
views := newViewsFake(10)
result, err := revealService(views).reveal(context.Background(), nil, Request{}, policy, needSemantics{}, groups{
main: []rankedEntry{confirmedItem(first), confirmedItem(second), confirmedItem(uuid.New())},
unconfirmed: []rankedEntry{declaredItem(first, domain.SpecializationNetworkEngineer)},
})
if err != nil {
t.Fatalf("reveal() error = %v", err)
}
if len(result.Main) != 2 || len(result.Unconfirmed) != 1 || !result.Truncated {
t.Fatalf("result = %d main, %d unconfirmed, truncated %v, want the group cut at 2 and flagged", len(result.Main), len(result.Unconfirmed), result.Truncated)
}
// The first candidate appears twice and is charged once; the dropped third
// entry is never charged.
if len(views.reserved) != 2 || !views.reserved[first] || !views.reserved[second] {
t.Fatalf("reserved = %v, want the two returned candidates", views.reserved)
}
// The fields are those that the returned entries show: the category, the
// stack, and the experience of the Backend entry plus the self-assessment of
// the Networks entry.
want := []domain.CardField{domain.CardFieldCategories, domain.CardFieldStack, domain.CardFieldExperience, domain.CardFieldSelfAssessment}
if !slices.Equal(views.recorded[first], want) {
t.Fatalf("recorded fields of the first candidate = %v, want %v", views.recorded[first], want)
}
if got := views.recorded[second]; !slices.Equal(got, want[:3]) {
t.Fatalf("recorded fields of the second candidate = %v, want %v", got, want[:3])
}
}
func TestRevealReturnsThePrefixThatIsPaidForWhenTheAllowanceEnds(t *testing.T) {
ids := []uuid.UUID{uuid.New(), uuid.New(), uuid.New(), uuid.New()}
views := newViewsFake(2)
result, err := revealService(views).reveal(context.Background(), nil, Request{}, domain.DemoMatchingPolicy(), needSemantics{}, groups{
main: []rankedEntry{confirmedItem(ids[0])},
near: []rankedEntry{confirmedItem(ids[1]), confirmedItem(ids[2])},
unconfirmed: []rankedEntry{declaredItem(ids[0], domain.SpecializationNetworkEngineer), declaredItem(ids[3], domain.SpecializationQA)},
})
if err != nil {
t.Fatalf("reveal() error = %v", err)
}
if len(result.Main) != 1 || len(result.Near) != 1 || len(result.Unconfirmed) != 0 || !result.Truncated {
t.Fatalf("result = %d/%d/%d truncated %v, want the entries before the refusal and nothing after it",
len(result.Main), len(result.Near), len(result.Unconfirmed), result.Truncated)
}
if len(views.reserved) != 2 || len(views.recorded) != 2 || views.reserved[ids[2]] || views.reserved[ids[3]] {
t.Fatalf("reserved %v, recorded %v, want only the two paid candidates", views.reserved, views.recorded)
}
}
func TestRevealDoesNotFlagACompleteResult(t *testing.T) {
views := newViewsFake(5)
result, err := revealService(views).reveal(context.Background(), nil, Request{}, domain.DemoMatchingPolicy(), needSemantics{}, groups{
main: []rankedEntry{confirmedItem(uuid.New())},
})
if err != nil || result.Truncated || len(result.Main) != 1 {
t.Fatalf("reveal() = %+v, %v, want one entry that is not truncated", result, err)
}
empty, err := revealService(newViewsFake(0)).reveal(context.Background(), nil, Request{}, domain.DemoMatchingPolicy(), needSemantics{}, groups{})
if err != nil || empty.Truncated || len(empty.Main)+len(empty.Near)+len(empty.Unconfirmed) != 0 {
t.Fatalf("an empty search = %+v, %v, want an empty result that is not truncated", empty, err)
}
}