127 lines
5.0 KiB
Go
127 lines
5.0 KiB
Go
package matching
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
// cohortEvidence answers the cohort count and no fact.
|
|
type cohortEvidence struct {
|
|
outbound.EvidenceRepository
|
|
|
|
counts map[outbound.CategoryKey]int64
|
|
asked [][]outbound.CategoryKey
|
|
}
|
|
|
|
func (evidence *cohortEvidence) CountComparableRealCategories(
|
|
_ context.Context, categories []outbound.CategoryKey,
|
|
) (map[outbound.CategoryKey]int64, error) {
|
|
evidence.asked = append(evidence.asked, categories)
|
|
return evidence.counts, nil
|
|
}
|
|
|
|
type cohortUnit struct {
|
|
outbound.UnitOfWork
|
|
|
|
evidence *cohortEvidence
|
|
}
|
|
|
|
func (unit cohortUnit) Evidence() outbound.EvidenceRepository { return unit.evidence }
|
|
|
|
func confirmedCard(specialization domain.SpecializationCode, grade domain.GradeCode) domain.VisibleCandidateCard {
|
|
return domain.VisibleCandidateCard{CandidateID: uuid.New(), Card: domain.AnonymousCandidateViewDTO{
|
|
Pseudonym: "cv1_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
|
|
Categories: []domain.CandidateViewCategory{{Specialization: specialization, Grade: grade, Strength: domain.CandidateViewStrengthStandard}},
|
|
}}
|
|
}
|
|
|
|
func TestRankKeepsTheNeutralTWhenTheRealCohortReachesTheMinimum(t *testing.T) {
|
|
policy := domain.DemoMatchingPolicy()
|
|
backendIntern := outbound.CategoryKey{Specialization: domain.SpecializationBackend, Grade: domain.GradeIntern}
|
|
frontendJunior := outbound.CategoryKey{Specialization: domain.SpecializationFrontend, Grade: domain.GradeJunior}
|
|
evidence := &cohortEvidence{counts: map[outbound.CategoryKey]int64{
|
|
backendIntern: int64(policy.PercentileMinCohort),
|
|
frontendJunior: int64(policy.PercentileMinCohort) - 1,
|
|
}}
|
|
var logs bytes.Buffer
|
|
service := &Service{logger: slog.New(slog.NewTextHandler(&logs, nil))}
|
|
cards := []domain.VisibleCandidateCard{
|
|
confirmedCard(domain.SpecializationBackend, domain.GradeIntern),
|
|
confirmedCard(domain.SpecializationBackend, domain.GradeIntern),
|
|
confirmedCard(domain.SpecializationFrontend, domain.GradeJunior),
|
|
}
|
|
ranked, err := service.rank(context.Background(), cohortUnit{evidence: evidence}, Request{}, searchBasis{}, policy, needSemantics{}, cards)
|
|
if err != nil {
|
|
t.Fatalf("rank() error = %v, want a result whatever the real cohort size", err)
|
|
}
|
|
if len(ranked.main) != 3 {
|
|
t.Fatalf("main = %d entries, want 3", len(ranked.main))
|
|
}
|
|
// Only the categories of the accepted entries are counted, each once.
|
|
if len(evidence.asked) != 1 || len(evidence.asked[0]) != 2 {
|
|
t.Fatalf("cohort questions = %v, want one question for the two categories of the entries", evidence.asked)
|
|
}
|
|
reasons := map[domain.SpecializationCode]UnknownReason{}
|
|
for _, item := range ranked.main {
|
|
for _, flag := range item.entry.UnknownFlags {
|
|
if flag.Component == UnknownPercentile {
|
|
reasons[item.entry.Specialization] = flag.Reason
|
|
}
|
|
}
|
|
if item.entry.Components.Percentile != nil || microToFloat(item.rank) != item.entry.Components.RankScore {
|
|
t.Fatalf("entry %+v has a percentile or a rank that is not the neutral-T rank", item.entry.Components)
|
|
}
|
|
// T is the neutral 0.5 in every case: Cov, Dom, Sem 0.5 give S = 0.45125.
|
|
if item.entry.Components.RankScore != 0.45125 {
|
|
t.Fatalf("rank score = %v, want 0.45125 with the neutral T", item.entry.Components.RankScore)
|
|
}
|
|
}
|
|
if reasons[domain.SpecializationBackend] != ReasonPercentileNotComputed || reasons[domain.SpecializationFrontend] != ReasonCohortBelow30 {
|
|
t.Fatalf("percentile reasons = %v, want not computed at the minimum and below 30 under it", reasons)
|
|
}
|
|
if strings.Count(logs.String(), "reached the percentile minimum") != 1 || !strings.Contains(logs.String(), "specialization=backend") {
|
|
t.Fatalf("log = %q, want one warning for the one category that reached the minimum", logs.String())
|
|
}
|
|
}
|
|
|
|
// repeatingTransactor fails with a visibility change a number of times and then
|
|
// succeeds without running the work.
|
|
type repeatingTransactor struct {
|
|
failures int
|
|
calls int
|
|
}
|
|
|
|
func (transactor *repeatingTransactor) WithinTransaction(context.Context, func(context.Context, outbound.UnitOfWork) error) error {
|
|
transactor.calls++
|
|
if transactor.calls <= transactor.failures {
|
|
return fmt.Errorf("lock the cohort: %w", outbound.ErrConcurrentVisibilityChange)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func TestSearchRepeatsAfterAConcurrentVisibilityChangeAFewTimes(t *testing.T) {
|
|
request := Request{ActorID: uuid.New(), OrganizationID: uuid.New()}
|
|
for _, test := range []struct {
|
|
failures, calls int
|
|
wantErr bool
|
|
}{{0, 1, false}, {2, 3, false}, {3, 3, true}} {
|
|
transactor := &repeatingTransactor{failures: test.failures}
|
|
service := &Service{transactor: transactor}
|
|
_, err := service.Search(context.Background(), request)
|
|
if transactor.calls != test.calls || (err != nil) != test.wantErr ||
|
|
(err != nil && !errors.Is(err, outbound.ErrConcurrentVisibilityChange)) {
|
|
t.Errorf("%d failures: %d attempts, error %v; want %d attempts, error %v", test.failures, transactor.calls, err, test.calls, test.wantErr)
|
|
}
|
|
}
|
|
}
|