feat(evidence): project skill evidence from confirmed assessments
This commit is contained in:
1 parent
4761508f13
commit
c2a8d17195
94 files changed
+10077
-151
No files matched your search
@@ -0,0 +1,225 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"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/evidence"
|
||||
)
|
||||
|
||||
type evidenceOperationsStub struct {
|
||||
page outbound.EvidenceFactPage
|
||||
facts []domain.EvidenceOrganizationFact
|
||||
err error
|
||||
query outbound.EvidenceListQuery
|
||||
actorID uuid.UUID
|
||||
orgID uuid.UUID
|
||||
handle string
|
||||
listCalls int
|
||||
}
|
||||
|
||||
func (stub *evidenceOperationsStub) ListMine(_ context.Context, query outbound.EvidenceListQuery) (outbound.EvidenceFactPage, error) {
|
||||
stub.query = query
|
||||
return stub.page, stub.err
|
||||
}
|
||||
|
||||
func (stub *evidenceOperationsStub) ListForOrganization(_ context.Context, actorID, organizationID uuid.UUID, handle string) ([]domain.EvidenceOrganizationFact, error) {
|
||||
stub.listCalls++
|
||||
stub.actorID, stub.orgID, stub.handle = actorID, organizationID, handle
|
||||
return stub.facts, stub.err
|
||||
}
|
||||
|
||||
func evidenceTestFact(t *testing.T, candidateID uuid.UUID, at time.Time) domain.EvidenceFact {
|
||||
t.Helper()
|
||||
attempt := uuid.New()
|
||||
fact, err := domain.RestoreEvidenceFact(domain.EvidenceFactSnapshot{
|
||||
ID: uuid.New(), CandidateID: candidateID, Kind: domain.EvidenceKindMeasurement,
|
||||
SourceType: domain.EvidenceSourceAssessmentResult, EventID: attempt, ResultID: attempt,
|
||||
Skills: []domain.EvidenceSkillCode{"go", "backend"},
|
||||
Result: domain.EvidenceResult{
|
||||
Grade: domain.GradeJunior, Basis: domain.AssessmentCategorySourceSyntheticDemo,
|
||||
CalibrationStatus: domain.AssessmentCategoryCalibrationUncalibrated,
|
||||
},
|
||||
Attribution: domain.EvidenceAttributionPersonal, Trust: domain.EvidenceTrustOnlineUnconfirmed,
|
||||
OccurredAt: at, Visibility: domain.EvidenceVisibilityVisible,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("RestoreEvidenceFact() error = %v", err)
|
||||
}
|
||||
return fact
|
||||
}
|
||||
|
||||
func TestListMyEvidenceMapsFactsAndPagesWithAnOwnerBoundCursor(t *testing.T) {
|
||||
userID := uuid.New()
|
||||
at := time.Date(2026, 10, 8, 10, 15, 0, 0, time.UTC)
|
||||
stub := &evidenceOperationsStub{page: outbound.EvidenceFactPage{
|
||||
Items: []domain.EvidenceFact{evidenceTestFact(t, userID, at)},
|
||||
HasMore: true,
|
||||
}}
|
||||
handler := NewEvidenceHandler(stub)
|
||||
ctx := privacyHTTPContext(userID, "")
|
||||
|
||||
response, err := handler.ListMyEvidence(ctx, api.ListMyEvidenceParams{Limit: api.NewOptInt32(1)})
|
||||
if err != nil {
|
||||
t.Fatalf("ListMyEvidence() error = %v", err)
|
||||
}
|
||||
page, ok := response.(*api.EvidenceFactListHeaders)
|
||||
if !ok || page.CacheControl != api.CacheControlNoStoreNoStore || len(page.Response.Items) != 1 {
|
||||
t.Fatalf("ListMyEvidence() = %#v, want a no-store page of one fact", response)
|
||||
}
|
||||
item := page.Response.Items[0]
|
||||
if item.Trust != 1 || item.Kind != api.EvidenceKindMeasurement || item.Source.EventID != item.Source.ResultID ||
|
||||
len(item.Skills) != 2 || item.Skills[0] != api.EvidenceSkillCodeBackend || item.Result.Grade != api.GradeCodeJunior ||
|
||||
item.Visibility != api.EvidenceVisibilityVisible || !time.Time(item.OccurredAt).Equal(at) {
|
||||
t.Fatalf("fact mapping = %+v", item)
|
||||
}
|
||||
if stub.query.CandidateID != userID || stub.query.Limit != 1 || stub.query.After != nil {
|
||||
t.Fatalf("query = %+v, want the session owner and the first page", stub.query)
|
||||
}
|
||||
cursor, set := page.Response.Page.NextCursor.Get()
|
||||
if !page.Response.Page.HasMore || !set || cursor == "" {
|
||||
t.Fatalf("page info = %+v, want a next cursor", page.Response.Page)
|
||||
}
|
||||
|
||||
// The cursor continues the same owner's list at the last fact of the page.
|
||||
if _, err := handler.ListMyEvidence(ctx, api.ListMyEvidenceParams{Cursor: api.NewOptString(cursor)}); err != nil {
|
||||
t.Fatalf("ListMyEvidence(cursor) error = %v", err)
|
||||
}
|
||||
if stub.query.After == nil || stub.query.After.ID != stub.page.Items[0].Snapshot().ID || !stub.query.After.OccurredAt.Equal(at) {
|
||||
t.Fatalf("continuation = %+v, want the last fact of the previous page", stub.query.After)
|
||||
}
|
||||
|
||||
// A cursor of another candidate, a cursor of another list, and a limit out of range are validation errors.
|
||||
foreign, err := encodePrivacyCursor(uuid.New(), privacyCursorEvidence, at, uuid.New())
|
||||
if err != nil {
|
||||
t.Fatalf("encodePrivacyCursor() error = %v", err)
|
||||
}
|
||||
otherList, err := encodePrivacyCursor(userID, privacyCursorAccess, at, uuid.New())
|
||||
if err != nil {
|
||||
t.Fatalf("encodePrivacyCursor() error = %v", err)
|
||||
}
|
||||
for name, params := range map[string]api.ListMyEvidenceParams{
|
||||
"foreign cursor": {Cursor: api.NewOptString(foreign)},
|
||||
"other list": {Cursor: api.NewOptString(otherList)},
|
||||
"malformed cursor": {Cursor: api.NewOptString("not-a-cursor")},
|
||||
"limit too large": {Limit: api.NewOptInt32(outbound.MaxEvidencePageSize + 1)},
|
||||
} {
|
||||
response, err := handler.ListMyEvidence(ctx, params)
|
||||
if _, bad := response.(*api.ListMyEvidenceBadRequest); err != nil || !bad {
|
||||
t.Errorf("%s = %#v, %v, want 400", name, response, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestListMyEvidenceMapsAccessFailures(t *testing.T) {
|
||||
userID := uuid.New()
|
||||
stub := &evidenceOperationsStub{err: evidence.ErrForbidden}
|
||||
handler := NewEvidenceHandler(stub)
|
||||
response, err := handler.ListMyEvidence(privacyHTTPContext(userID, ""), api.ListMyEvidenceParams{})
|
||||
if _, forbidden := response.(*api.ListMyEvidenceForbidden); err != nil || !forbidden {
|
||||
t.Fatalf("forbidden actor = %#v, %v, want 403", response, err)
|
||||
}
|
||||
unauthenticated, err := handler.ListMyEvidence(context.Background(), api.ListMyEvidenceParams{})
|
||||
if _, ok := unauthenticated.(*api.ListMyEvidenceUnauthorized); err != nil || !ok {
|
||||
t.Fatalf("no principal = %#v, %v, want 401", unauthenticated, err)
|
||||
}
|
||||
stub.err = errors.New("storage failed")
|
||||
if _, err := handler.ListMyEvidence(privacyHTTPContext(userID, ""), api.ListMyEvidenceParams{}); err == nil {
|
||||
t.Fatal("an unexpected failure was swallowed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrganizationEvidenceMapsOnlyTheAllowlistedShape(t *testing.T) {
|
||||
userID, organizationID := uuid.New(), uuid.New()
|
||||
handle := "cv1_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||
fact, err := domain.NewEvidenceOrganizationFact(domain.EvidenceOrganizationFactInput{
|
||||
Skill: "backend", Grade: domain.GradeJunior, Trust: domain.EvidenceTrustOnlineUnconfirmed,
|
||||
Basis: domain.AssessmentCategorySourceSyntheticDemo, CalibrationStatus: domain.AssessmentCategoryCalibrationUncalibrated, ObservedYear: 2026,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewEvidenceOrganizationFact() error = %v", err)
|
||||
}
|
||||
stub := &evidenceOperationsStub{facts: []domain.EvidenceOrganizationFact{fact}}
|
||||
handler := NewEvidenceHandler(stub)
|
||||
ctx := privacyHTTPContext(userID, "")
|
||||
params := api.GetOrganizationCandidateEvidenceParams{OrgID: organizationID, CandidateHandle: api.CandidateHandle(handle)}
|
||||
|
||||
response, err := handler.GetOrganizationCandidateEvidence(ctx, params)
|
||||
if err != nil {
|
||||
t.Fatalf("GetOrganizationCandidateEvidence() error = %v", err)
|
||||
}
|
||||
body, ok := response.(*api.OrganizationEvidenceHeaders)
|
||||
if !ok || body.CacheControl != api.CacheControlNoStoreNoStore || len(body.Response.Items) != 1 {
|
||||
t.Fatalf("response = %#v, want a no-store projection of one fact", response)
|
||||
}
|
||||
if stub.actorID != userID || stub.orgID != organizationID || stub.handle != handle {
|
||||
t.Fatalf("scope = actor %s organization %s handle %q", stub.actorID, stub.orgID, stub.handle)
|
||||
}
|
||||
encoded, err := json.Marshal(body.Response)
|
||||
if err != nil {
|
||||
t.Fatalf("encode projection: %v", err)
|
||||
}
|
||||
if want := `{"items":[{"skill":"backend","grade":"junior","trust":1,"basis":"synthetic_demo","calibration_status":"uncalibrated","observed_year":2026}]}`; string(encoded) != want {
|
||||
t.Fatalf("projection = %s, want %s", encoded, want)
|
||||
}
|
||||
for _, private := range []string{userID.String(), "event_id", "result_id", "occurred_at", "observed_month", "answer", "score", "visibility"} {
|
||||
if strings.Contains(string(encoded), private) {
|
||||
t.Fatalf("organization projection exposes %q: %s", private, encoded)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrganizationEvidenceMapsCardFailuresWithoutDistinction(t *testing.T) {
|
||||
userID, organizationID := uuid.New(), uuid.New()
|
||||
handle := api.CandidateHandle("cv1_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
|
||||
stub := &evidenceOperationsStub{}
|
||||
handler := NewEvidenceHandler(stub)
|
||||
ctx := privacyHTTPContext(userID, "")
|
||||
params := api.GetOrganizationCandidateEvidenceParams{OrgID: organizationID, CandidateHandle: handle}
|
||||
|
||||
for name, test := range map[string]struct {
|
||||
err error
|
||||
want string
|
||||
}{
|
||||
"candidate account": {candidateview.ErrForbidden, "forbidden"},
|
||||
"hidden or unpublished": {candidateview.ErrCandidateCardNotFound, "not_found"},
|
||||
"no membership": {outbound.ErrOrganizationNotFound, "not_found"},
|
||||
"bad handle": {candidateview.ErrInvalidCandidateHandle, "bad_request"},
|
||||
"quota": {&candidateview.RateLimitedError{RetryAt: time.Now().Add(time.Minute)}, "too_many"},
|
||||
} {
|
||||
stub.err = test.err
|
||||
response, err := handler.GetOrganizationCandidateEvidence(ctx, params)
|
||||
if err != nil {
|
||||
t.Errorf("%s: error = %v", name, err)
|
||||
continue
|
||||
}
|
||||
var got string
|
||||
switch response.(type) {
|
||||
case *api.GetOrganizationCandidateEvidenceForbidden:
|
||||
got = "forbidden"
|
||||
case *api.GetOrganizationCandidateEvidenceNotFound:
|
||||
got = "not_found"
|
||||
case *api.GetOrganizationCandidateEvidenceBadRequest:
|
||||
got = "bad_request"
|
||||
case *api.TooManyRequestsHeaders:
|
||||
got = "too_many"
|
||||
}
|
||||
if got != test.want {
|
||||
t.Errorf("%s = %T, want %s", name, response, test.want)
|
||||
}
|
||||
}
|
||||
stub.err = errors.New("storage failed")
|
||||
if _, err := handler.GetOrganizationCandidateEvidence(ctx, params); err == nil {
|
||||
t.Fatal("an unexpected failure was swallowed")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user