package v1 import ( "context" "encoding/json" "errors" "fmt" "strings" "testing" "time" "github.com/google/uuid" "lincodex/backend/internal/domain" "lincodex/backend/internal/port/outbound" invitationusecase "lincodex/backend/internal/usecase/invitation" ) func TestInvitationDeclineFailuresMapToTheContractStatuses(t *testing.T) { ctx := context.Background() field := &domain.InvitationFieldError{Pointer: "/comment", Reason: "x"} for name, test := range map[string]struct { err error want string }{ "employer": {invitationusecase.ErrForbidden, "*api.DeclineMyInvitationForbidden"}, "unknown": {fmt.Errorf("wrapped: %w", outbound.ErrInvitationNotFound), "*api.DeclineMyInvitationNotFound"}, "reused key": {invitationusecase.ErrIdempotencyKeyReused, "*api.DeclineMyInvitationUnprocessableEntity"}, "bad key": {invitationusecase.ErrInvalidIdempotencyKey, "*api.DeclineMyInvitationBadRequest"}, "comment": {fmt.Errorf("wrapped: %w", field), "*api.DeclineMyInvitationBadRequest"}, "stale version": {domain.ErrInvitationVersionConflict, "*api.DeclineMyInvitationPreconditionFailed"}, "terminal or late": {fmt.Errorf("wrapped: %w", invitationusecase.ErrAnswerBlocked), "*api.DeclineMyInvitationConflict"}, } { response, unexpected := invitationDeclineFailure(ctx, test.err) if unexpected != nil || fmt.Sprintf("%T", response) != test.want { t.Errorf("%s: failure = %T, %v; want %s", name, response, unexpected, test.want) } } if _, unexpected := invitationDeclineFailure(ctx, errors.New("storage failed")); unexpected == nil { t.Error("an unknown failure produced a public response instead of reaching the responder") } } // The candidate's response carries the own decline and nothing of the // candidate: no identifier, handle, salary indicator, need, or contact. The // consent offer and the grant are null until an acceptance exists. func TestCandidateInvitationResponseCarriesTheDeclineAndNoCandidateData(t *testing.T) { candidateID, organizationID := uuid.Must(uuid.NewV7()), uuid.Must(uuid.NewV7()) policyID, err := domain.NewPolicyVersionID() if err != nil { t.Fatalf("NewPolicyVersionID() error = %v", err) } now := time.Date(2026, time.October, 9, 10, 0, 0, 0, time.UTC) draft, err := domain.NewInvitationDraft(domain.NewInvitationDraftInput{ ID: uuid.Must(uuid.NewV7()), OrganizationID: organizationID, CandidateID: candidateID, ActorID: uuid.Must(uuid.NewV7()), PolicyVersionID: policyID, At: now, Content: domain.InvitationDraftContent{ResponsePeriod: 7, Terms: invitationTermsFromRequest(invitationTestTerms())}, }) if err != nil { t.Fatalf("NewInvitationDraft() error = %v", err) } delivered, err := draft.Deliver(now) if err != nil { t.Fatalf("Deliver() error = %v", err) } declined, decline, err := delivered.Decline(domain.InvitationDeclineChoice{Reason: domain.InvitationDeclineOther}, now.Add(time.Hour)) if err != nil { t.Fatalf("Decline() error = %v", err) } view, err := declined.CandidateView(domain.InvitationOrganizationRef{ID: organizationID, Name: "Acme"}, &decline) if err != nil { t.Fatalf("CandidateView() error = %v", err) } encoded, err := json.Marshal(candidateInvitationResponse(view)) if err != nil { t.Fatalf("marshal response: %v", err) } text := string(encoded) if strings.Contains(text, candidateID.String()) { t.Errorf("response carries the candidate identifier: %s", text) } for _, key := range []string{"candidate_handle", "candidate_id", "salary_fit", "need", "email", "phone", "telegram", "full_name"} { if strings.Contains(text, `"`+key+`":`) { t.Errorf("response carries %s: %s", key, text) } } for _, want := range []string{ `"state":"declined"`, `"decline":{"reason":"other","comment":null,"declined_at":"2026-10-09T11:00:00Z"}`, `"contact_consent":null`, `"contact_grant":null`, `"organization":{"id":"` + organizationID.String() + `","name":"Acme"}`, `"response_deadline_at":"2026-10-16T10:00:00Z"`, } { if !strings.Contains(text, want) { t.Errorf("response lacks %s: %s", want, text) } } } func TestInvitationAcceptFailuresMapToTheContractStatuses(t *testing.T) { ctx := context.Background() text := &domain.InvitationFieldError{Pointer: "/consent_c4/text_version", Reason: "x"} for name, test := range map[string]struct { err error want string }{ "employer": {invitationusecase.ErrForbidden, "*api.AcceptMyInvitationForbidden"}, "unknown": {fmt.Errorf("wrapped: %w", outbound.ErrInvitationNotFound), "*api.AcceptMyInvitationNotFound"}, "reused key": {invitationusecase.ErrIdempotencyKeyReused, "*api.AcceptMyInvitationUnprocessableEntity"}, "bad key": {invitationusecase.ErrInvalidIdempotencyKey, "*api.AcceptMyInvitationBadRequest"}, "consent text": {fmt.Errorf("wrapped: %w", text), "*api.AcceptMyInvitationBadRequest"}, "stale version": {fmt.Errorf("wrapped: %w", domain.ErrInvitationVersionConflict), "*api.AcceptMyInvitationPreconditionFailed"}, "terminal or late": {fmt.Errorf("wrapped: %w", invitationusecase.ErrAnswerBlocked), "*api.AcceptMyInvitationConflict"}, } { response, unexpected := invitationAcceptFailure(ctx, test.err) if unexpected != nil || fmt.Sprintf("%T", response) != test.want { t.Errorf("%s: failure = %T, %v; want %s", name, response, unexpected, test.want) } } if _, unexpected := invitationAcceptFailure(ctx, errors.New("storage failed")); unexpected == nil { t.Error("an unknown failure produced a public response instead of reaching the responder") } }