226 lines
10 KiB
Go
226 lines
10 KiB
Go
package v1
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/adapter/primary/http/api"
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
"lincodex/backend/internal/usecase/candidateview"
|
|
"lincodex/backend/internal/usecase/matching"
|
|
)
|
|
|
|
type matchOperationsStub struct {
|
|
result matching.Result
|
|
err error
|
|
request matching.Request
|
|
}
|
|
|
|
func (stub *matchOperationsStub) Search(_ context.Context, request matching.Request) (matching.Result, error) {
|
|
stub.request = request
|
|
return stub.result, stub.err
|
|
}
|
|
|
|
func TestSearchOrganizationMatchesMapsABankRequestAndEchoesTheNormalizedFilters(t *testing.T) {
|
|
userID, organizationID := uuid.New(), uuid.New()
|
|
stub := &matchOperationsStub{}
|
|
handler := NewMatchHandler(stub)
|
|
var request api.MatchSearchRequest
|
|
request.SetMatchSearchByBank(api.MatchSearchByBank{Filters: api.MatchSearchFilters{
|
|
Specialization: api.NewOptSpecializationCode(api.SpecializationCodeBackend),
|
|
ConfirmedCategory: api.NewOptMatchGradeFilter(api.MatchGradeFilter{Specialization: api.SpecializationCodeBackend, Grade: api.GradeCodeMiddle}),
|
|
Stack: []api.MatchSearchStackTag{api.MatchSearchStackTagPostgresql, api.MatchSearchStackTagGo},
|
|
Conditions: api.NewOptMatchSearchConditions(api.MatchSearchConditions{
|
|
WorkFormats: []api.WorkFormat{api.WorkFormatRemote},
|
|
SalaryCompatibility: api.NewOptCandidateSalaryCompatibility(api.CandidateSalaryCompatibilityCompatible),
|
|
}),
|
|
}})
|
|
stub.result.Truncated = false
|
|
stub.result.Source = matching.Source{Filters: matching.Filters{
|
|
Specialization: new(domain.SpecializationCode), Stack: []domain.CandidateViewSkillCode{domain.CandidateViewSkillGo},
|
|
}}
|
|
*stub.result.Source.Filters.Specialization = domain.SpecializationBackend
|
|
|
|
response, err := handler.SearchOrganizationMatches(privacyHTTPContext(userID, ""), request, api.SearchOrganizationMatchesParams{OrgID: organizationID})
|
|
if err != nil {
|
|
t.Fatalf("SearchOrganizationMatches() error = %v", err)
|
|
}
|
|
if stub.request.ActorID != userID || stub.request.OrganizationID != organizationID || stub.request.Need != nil {
|
|
t.Fatalf("command scope = %+v, want the session actor and the path organization", stub.request)
|
|
}
|
|
filters := stub.request.Filters
|
|
if filters.Specialization == nil || *filters.Specialization != domain.SpecializationBackend ||
|
|
filters.ConfirmedCategory == nil || filters.ConfirmedCategory.Grade != domain.GradeMiddle || filters.ConfirmedGrade != nil ||
|
|
filters.ConfirmationStatus != nil || filters.DeclaredGrade != nil || len(filters.Stack) != 2 ||
|
|
filters.Conditions == nil || len(filters.Conditions.WorkFormats) != 1 || filters.Conditions.SalaryCompatibility == nil {
|
|
t.Fatalf("mapped filters = %+v", filters)
|
|
}
|
|
body, ok := response.(*api.MatchSearchResponseHeaders)
|
|
if !ok || body.CacheControl != api.CacheControlNoStoreNoStore {
|
|
t.Fatalf("response = %#v, want a no-store result", response)
|
|
}
|
|
encoded, err := json.Marshal(body.Response)
|
|
if err != nil {
|
|
t.Fatalf("encode response: %v", err)
|
|
}
|
|
want := `{"source":{"kind":"bank","filters":{"specialization":"backend","confirmed_grade":null,"confirmed_category":null,` +
|
|
`"confirmation_status":null,"declared_grade":null,"stack":["go"],"conditions":null}},"groups":{"main":[],"near":[],"unconfirmed":[]},"truncated":false}`
|
|
if string(encoded) != want {
|
|
t.Fatalf("response = %s, want %s", encoded, want)
|
|
}
|
|
}
|
|
|
|
func TestSearchOrganizationMatchesMapsANeedRequestAndKeepsUnconfirmedEntriesStrict(t *testing.T) {
|
|
userID, organizationID, needID := uuid.New(), uuid.New(), uuid.New()
|
|
handle := "cv1_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
|
junior := domain.GradeJunior
|
|
stub := &matchOperationsStub{result: matching.Result{
|
|
Source: matching.Source{Need: &matching.NeedReference{NeedID: needID, RequirementsVersion: 3}},
|
|
Unconfirmed: []matching.Entry{{
|
|
CandidateHandle: handle, Specialization: domain.SpecializationNetworkEngineer, DeclaredGrade: &junior,
|
|
Components: matching.Components{Fit: 0.3125, Coverage: 0.25},
|
|
UnknownFlags: []matching.UnknownFlag{
|
|
{Component: matching.UnknownAssessment, Reason: matching.ReasonNoConfirmationForSpecialization},
|
|
},
|
|
EvidenceSummary: []matching.EvidenceItem{{Code: matching.EvidenceDeclaredGrade, State: matching.EvidenceStateDeclaredOnly}},
|
|
}},
|
|
}}
|
|
handler := NewMatchHandler(stub)
|
|
var request api.MatchSearchRequest
|
|
request.SetMatchSearchByNeed(api.MatchSearchByNeed{NeedID: needID, RequirementsVersion: 3})
|
|
|
|
response, err := handler.SearchOrganizationMatches(privacyHTTPContext(userID, ""), request, api.SearchOrganizationMatchesParams{OrgID: organizationID})
|
|
if err != nil {
|
|
t.Fatalf("SearchOrganizationMatches() error = %v", err)
|
|
}
|
|
if stub.request.Need == nil || stub.request.Need.NeedID != needID || stub.request.Need.RequirementsVersion != 3 {
|
|
t.Fatalf("need reference = %+v", stub.request.Need)
|
|
}
|
|
body, ok := response.(*api.MatchSearchResponseHeaders)
|
|
if !ok {
|
|
t.Fatalf("response = %#v", response)
|
|
}
|
|
encoded, err := json.Marshal(body.Response.Groups.Unconfirmed)
|
|
if err != nil {
|
|
t.Fatalf("encode response: %v", err)
|
|
}
|
|
want := `[{"candidate_handle":"` + handle + `","specialization":"network_engineer","confirmation_status":"unconfirmed","declared_grade":"junior",` +
|
|
`"category":null,"strength":null,"public_context":{"stack":null,"experience_band":null,"work_formats":null,"salary_compatibility":null},` +
|
|
`"evidence_summary":[{"code":"declared_grade","state":"declared_only"}],"semantic_evidence":[],"components":{"fit":0.3125,"coverage":0.25},` +
|
|
`"unknown_flags":[{"component":"assessment","reason":"no_confirmation_for_specialization"}]}]`
|
|
if string(encoded) != want {
|
|
t.Fatalf("unconfirmed entries = %s\nwant %s", encoded, want)
|
|
}
|
|
for _, forbidden := range []string{"percentile", "rank_score", "theta", "freshness"} {
|
|
if strings.Contains(string(encoded), forbidden) {
|
|
t.Fatalf("an unconfirmed entry exposes %q", forbidden)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSearchOrganizationMatchesMapsAConfirmedEntryWithANullPercentile(t *testing.T) {
|
|
handle := "cv1_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
|
stub := &matchOperationsStub{result: matching.Result{Main: []matching.Entry{{
|
|
CandidateHandle: handle, Specialization: domain.SpecializationBackend,
|
|
Category: &domain.CandidateViewCategory{
|
|
Specialization: domain.SpecializationBackend, Grade: domain.GradeMiddle, Strength: domain.CandidateViewStrengthStrong,
|
|
Basis: domain.AssessmentCategorySourceSyntheticDemo,
|
|
},
|
|
Components: matching.Components{
|
|
Fit: 0.5, Coverage: 0.5, Domain: 0.5, Semantics: 0.5, Freshness: 0.5, Responsiveness: 0.5, RankScore: 0.45125,
|
|
},
|
|
}}}}
|
|
response, err := NewMatchHandler(stub).SearchOrganizationMatches(privacyHTTPContext(uuid.New(), ""),
|
|
func() api.MatchSearchRequest {
|
|
var request api.MatchSearchRequest
|
|
request.SetMatchSearchByBank(api.MatchSearchByBank{})
|
|
return request
|
|
}(), api.SearchOrganizationMatchesParams{OrgID: uuid.New()})
|
|
if err != nil {
|
|
t.Fatalf("SearchOrganizationMatches() error = %v", err)
|
|
}
|
|
encoded, err := json.Marshal(response.(*api.MatchSearchResponseHeaders).Response.Groups.Main)
|
|
if err != nil {
|
|
t.Fatalf("encode response: %v", err)
|
|
}
|
|
for _, want := range []string{
|
|
`"confirmation_status":"confirmed"`, `"declared_grade":null`,
|
|
`"category":{"specialization":"backend","grade":"middle","basis":"synthetic_demo"}`, `"strength":"strong"`,
|
|
`"percentile":null`, `"competition":0`, `"rank_score":0.45125`,
|
|
} {
|
|
if !strings.Contains(string(encoded), want) {
|
|
t.Errorf("confirmed entry %s lacks %s", encoded, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSearchOrganizationMatchesMapsFailuresToTheContractProblems(t *testing.T) {
|
|
userID := uuid.New()
|
|
var request api.MatchSearchRequest
|
|
request.SetMatchSearchByBank(api.MatchSearchByBank{})
|
|
params := api.SearchOrganizationMatchesParams{OrgID: uuid.New()}
|
|
stub := &matchOperationsStub{}
|
|
handler := NewMatchHandler(stub)
|
|
|
|
unauthenticated, err := handler.SearchOrganizationMatches(context.Background(), request, params)
|
|
if _, ok := unauthenticated.(*api.SearchOrganizationMatchesUnauthorized); err != nil || !ok {
|
|
t.Fatalf("no principal = %#v, %v, want 401", unauthenticated, err)
|
|
}
|
|
for name, test := range map[string]struct {
|
|
err error
|
|
want string
|
|
}{
|
|
"not an owner": {matching.ErrForbidden, "forbidden"},
|
|
"candidate account": {candidateview.ErrForbidden, "forbidden"},
|
|
"membership is not an owner": {outbound.ErrOrganizationManagerRequired, "forbidden"},
|
|
"no membership": {outbound.ErrOrganizationNotFound, "not_found"},
|
|
"unknown need": {matching.ErrNeedNotFound, "not_found"},
|
|
} {
|
|
stub.err = test.err
|
|
response, err := handler.SearchOrganizationMatches(privacyHTTPContext(userID, ""), request, params)
|
|
if err != nil {
|
|
t.Errorf("%s: error = %v", name, err)
|
|
continue
|
|
}
|
|
var got string
|
|
switch response.(type) {
|
|
case *api.SearchOrganizationMatchesForbidden:
|
|
got = "forbidden"
|
|
case *api.SearchOrganizationMatchesNotFound:
|
|
got = "not_found"
|
|
}
|
|
if got != test.want {
|
|
t.Errorf("%s = %T, want %s", name, response, test.want)
|
|
}
|
|
}
|
|
for _, unexpected := range []error{errors.New("storage failed"), matching.ErrPolicyUnavailable} {
|
|
stub.err = unexpected
|
|
if _, err := handler.SearchOrganizationMatches(privacyHTTPContext(userID, ""), request, params); err == nil {
|
|
t.Fatalf("an unexpected failure %v was answered as a client problem", unexpected)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSearchOrganizationMatchesReportsATruncatedResultWithoutACount(t *testing.T) {
|
|
stub := &matchOperationsStub{result: matching.Result{Truncated: true}}
|
|
var request api.MatchSearchRequest
|
|
request.SetMatchSearchByBank(api.MatchSearchByBank{})
|
|
response, err := NewMatchHandler(stub).SearchOrganizationMatches(privacyHTTPContext(uuid.New(), ""), request, api.SearchOrganizationMatchesParams{OrgID: uuid.New()})
|
|
if err != nil {
|
|
t.Fatalf("SearchOrganizationMatches() error = %v", err)
|
|
}
|
|
encoded, err := json.Marshal(response.(*api.MatchSearchResponseHeaders).Response)
|
|
if err != nil {
|
|
t.Fatalf("encode response: %v", err)
|
|
}
|
|
if !strings.Contains(string(encoded), `"truncated":true`) || strings.Contains(string(encoded), "total") || strings.Contains(string(encoded), "count") {
|
|
t.Fatalf("response = %s, want the flag and no count", encoded)
|
|
}
|
|
}
|