131 lines
5.9 KiB
Go
131 lines
5.9 KiB
Go
package domain
|
|
|
|
import (
|
|
"errors"
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func validEvidenceSnapshot() EvidenceFactSnapshot {
|
|
attempt := uuid.New()
|
|
return EvidenceFactSnapshot{
|
|
ID: uuid.New(), CandidateID: uuid.New(), Kind: EvidenceKindMeasurement,
|
|
SourceType: EvidenceSourceAssessmentResult, EventID: attempt, ResultID: attempt,
|
|
Skills: []EvidenceSkillCode{"go", "backend"},
|
|
Result: EvidenceResult{
|
|
Grade: GradeJunior, Basis: AssessmentCategorySourceSyntheticDemo,
|
|
CalibrationStatus: AssessmentCategoryCalibrationUncalibrated,
|
|
},
|
|
Attribution: EvidenceAttributionPersonal, Trust: EvidenceTrustOnlineUnconfirmed,
|
|
OccurredAt: time.Date(2026, 10, 8, 10, 15, 0, 123456789, time.FixedZone("test", 3*60*60)),
|
|
Visibility: EvidenceVisibilityVisible,
|
|
}
|
|
}
|
|
|
|
func TestRestoreEvidenceFactKeepsOneFactWithOrderedTags(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
snapshot := validEvidenceSnapshot()
|
|
fact, err := RestoreEvidenceFact(snapshot)
|
|
if err != nil {
|
|
t.Fatalf("RestoreEvidenceFact() error = %v", err)
|
|
}
|
|
got := fact.Snapshot()
|
|
if !slices.Equal(got.Skills, []EvidenceSkillCode{"backend", "go"}) {
|
|
t.Fatalf("Skills = %v, want the tags of one event in ascending order", got.Skills)
|
|
}
|
|
if got.OccurredAt.Location() != time.UTC || got.OccurredAt.Nanosecond()%1000 != 0 {
|
|
t.Fatalf("OccurredAt = %v, want UTC at microsecond precision", got.OccurredAt)
|
|
}
|
|
got.Skills[0] = "qa"
|
|
if fact.Snapshot().Skills[0] != "backend" {
|
|
t.Fatal("Snapshot() shares its skill slice with the fact")
|
|
}
|
|
if EvidenceTrustOnlineUnconfirmed != 1 {
|
|
t.Fatalf("online trust = %d, want 1", EvidenceTrustOnlineUnconfirmed)
|
|
}
|
|
}
|
|
|
|
func TestRestoreEvidenceFactRejectsBrokenModelRules(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cases := map[string]func(*EvidenceFactSnapshot){
|
|
"claim above level 0": func(s *EvidenceFactSnapshot) { s.Kind = EvidenceKindClaim; s.Trust = EvidenceTrustOnlineUnconfirmed },
|
|
"measurement at level 0": func(s *EvidenceFactSnapshot) { s.Trust = EvidenceTrustStated },
|
|
"online result at the in-person level": func(s *EvidenceFactSnapshot) { s.Trust = EvidenceTrustInPerson },
|
|
"assessment claim": func(s *EvidenceFactSnapshot) { s.Kind = EvidenceKindClaim; s.Trust = EvidenceTrustStated },
|
|
"team attribution": func(s *EvidenceFactSnapshot) { s.Attribution = EvidenceAttributionTeamUnattributed },
|
|
"event differs from result": func(s *EvidenceFactSnapshot) { s.ResultID = uuid.New() },
|
|
"unknown source": func(s *EvidenceFactSnapshot) { s.SourceType = "demo_express" },
|
|
"no skill": func(s *EvidenceFactSnapshot) { s.Skills = nil },
|
|
"repeated tag": func(s *EvidenceFactSnapshot) { s.Skills = []EvidenceSkillCode{"go", "go"} },
|
|
"unknown tag": func(s *EvidenceFactSnapshot) { s.Skills = []EvidenceSkillCode{"rate-limiting"} },
|
|
"unknown grade": func(s *EvidenceFactSnapshot) { s.Result.Grade = "principal" },
|
|
"calibration missing": func(s *EvidenceFactSnapshot) { s.Result.CalibrationStatus = "" },
|
|
"unknown visibility": func(s *EvidenceFactSnapshot) { s.Visibility = "public" },
|
|
"missing candidate": func(s *EvidenceFactSnapshot) { s.CandidateID = uuid.Nil },
|
|
"missing time": func(s *EvidenceFactSnapshot) { s.OccurredAt = time.Time{} },
|
|
}
|
|
for name, mutate := range cases {
|
|
t.Run(name, func(t *testing.T) {
|
|
t.Parallel()
|
|
snapshot := validEvidenceSnapshot()
|
|
mutate(&snapshot)
|
|
if _, err := RestoreEvidenceFact(snapshot); !errors.Is(err, ErrInvalidEvidence) {
|
|
t.Fatalf("RestoreEvidenceFact() error = %v, want ErrInvalidEvidence", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEvidenceSkillCodesAreTheSpecializationsAndTheCardTechnologies(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
codes := EvidenceSkillCodes()
|
|
if len(codes) != len(specializationCodes)+len(CandidateViewSkillCodes()) {
|
|
t.Fatalf("EvidenceSkillCodes() has %d codes", len(codes))
|
|
}
|
|
for _, code := range codes {
|
|
if !code.IsKnown() {
|
|
t.Errorf("%q is listed but not known", code)
|
|
}
|
|
}
|
|
if EvidenceSkillCode("rate-limiting").IsKnown() || EvidenceSkillCode("").IsKnown() {
|
|
t.Fatal("a code outside the closed set is known")
|
|
}
|
|
}
|
|
|
|
func TestNewEvidenceOrganizationFactChecksTheAllowlistedShape(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
valid := EvidenceOrganizationFactInput{
|
|
Skill: "backend", Grade: GradeJunior, Trust: EvidenceTrustOnlineUnconfirmed,
|
|
Basis: AssessmentCategorySourceSyntheticDemo, CalibrationStatus: AssessmentCategoryCalibrationUncalibrated, ObservedYear: 2026,
|
|
}
|
|
fact, err := NewEvidenceOrganizationFact(valid)
|
|
if err != nil || fact.Input() != valid {
|
|
t.Fatalf("NewEvidenceOrganizationFact() = %+v, %v, want the input back", fact.Input(), err)
|
|
}
|
|
for name, mutate := range map[string]func(*EvidenceOrganizationFactInput){
|
|
"year before the range": func(i *EvidenceOrganizationFactInput) { i.ObservedYear = 1999 },
|
|
"year after the range": func(i *EvidenceOrganizationFactInput) { i.ObservedYear = 10000 },
|
|
"no year": func(i *EvidenceOrganizationFactInput) { i.ObservedYear = 0 },
|
|
"claim level": func(i *EvidenceOrganizationFactInput) { i.Trust = EvidenceTrustStated },
|
|
"trust above in person": func(i *EvidenceOrganizationFactInput) { i.Trust = 4 },
|
|
"unknown skill": func(i *EvidenceOrganizationFactInput) { i.Skill = "rate-limiting" },
|
|
"no skill": func(i *EvidenceOrganizationFactInput) { i.Skill = "" },
|
|
"unknown grade": func(i *EvidenceOrganizationFactInput) { i.Grade = "principal" },
|
|
"no basis": func(i *EvidenceOrganizationFactInput) { i.Basis = "" },
|
|
"no calibration": func(i *EvidenceOrganizationFactInput) { i.CalibrationStatus = "" },
|
|
} {
|
|
input := valid
|
|
mutate(&input)
|
|
if _, err := NewEvidenceOrganizationFact(input); !errors.Is(err, ErrInvalidEvidence) {
|
|
t.Errorf("%s: error = %v, want ErrInvalidEvidence", name, err)
|
|
}
|
|
}
|
|
}
|