package domain import ( "bytes" "errors" "reflect" "testing" ) func TestDemoProductPolicyV4CarriesTheMatchingSection(t *testing.T) { policy := DemoProductPolicyV4() if err := policy.Validate(); err != nil { t.Fatalf("DemoProductPolicyV4().Validate() error = %v", err) } if policy.Matching == nil || policy.Assessment.Signals == nil || policy.Support == nil { t.Fatalf("DemoProductPolicyV4() sections = matching:%v signals:%v support:%v, want all", policy.Matching != nil, policy.Assessment.Signals != nil, policy.Support != nil) } if DemoProductPolicyV3().Matching != nil { t.Fatal("DemoProductPolicyV3() gained a matching section: a stored version must not change") } encoded, _, err := encodeProductPolicy(policy) if err != nil { t.Fatalf("encodeProductPolicy() error = %v", err) } decoded, err := decodeProductPolicy(encoded) if err != nil { t.Fatalf("decodeProductPolicy() error = %v", err) } if decoded.Matching == nil || !reflect.DeepEqual(*decoded.Matching, *policy.Matching) { t.Fatalf("decoded matching policy = %+v, want %+v", decoded.Matching, policy.Matching) } } func TestMatchingPolicyIsAnIndependentSchemaTwoExtension(t *testing.T) { policy := DemoProductPolicyV1() matching := DemoMatchingPolicy() policy.Matching = &matching encoded, _, err := encodeProductPolicy(policy) if err != nil { t.Fatalf("encodeProductPolicy() error = %v", err) } decoded, err := decodeProductPolicy(encoded) if err != nil { t.Fatalf("decodeProductPolicy() error = %v", err) } if decoded.Matching == nil || decoded.Assessment.Attempt != nil || decoded.Assessment.Training != nil || decoded.Assessment.Signals != nil || decoded.Support != nil { t.Fatalf("decoded extensions = matching:%v attempt:%v training:%v signals:%v support:%v, want only matching", decoded.Matching != nil, decoded.Assessment.Attempt != nil, decoded.Assessment.Training != nil, decoded.Assessment.Signals != nil, decoded.Support != nil) } legacy := editPolicySchemaDocument(t, []byte(demoProductPolicyV1Document), func(document map[string]any) { document["matching"] = nil }) if _, err := decodeProductPolicy(legacy); !errors.Is(err, ErrInvalidPolicy) { t.Fatalf("decodeProductPolicy(schema 1 with a matching key) error = %v, want ErrInvalidPolicy", err) } } func TestMatchingPolicyDocumentRequiresEveryFieldAndRejectsUnknownOnes(t *testing.T) { encoded, _, err := encodeProductPolicy(DemoProductPolicyV4()) if err != nil { t.Fatalf("encodeProductPolicy() error = %v", err) } missing := bytes.Replace(encoded, []byte(`"neutral_unknown_bp":5000,`), nil, 1) if bytes.Equal(missing, encoded) { t.Fatal("encoded matching section did not contain neutral_unknown_bp") } if _, err := decodeProductPolicy(missing); !errors.Is(err, ErrInvalidPolicy) { t.Fatalf("decodeProductPolicy(missing neutral_unknown_bp) error = %v, want ErrInvalidPolicy", err) } unknown := bytes.Replace(encoded, []byte(`"neutral_unknown_bp":5000`), []byte(`"neutral_unknown_bp":5000,"unknown":1`), 1) if _, err := decodeProductPolicy(unknown); !errors.Is(err, ErrInvalidPolicy) { t.Fatalf("decodeProductPolicy(unknown matching field) error = %v, want ErrInvalidPolicy", err) } } func TestMatchingPolicyRejectsInconsistentValues(t *testing.T) { tests := []struct { name string change func(*MatchingPolicy) }{ {name: "fit weights do not add up", change: func(policy *MatchingPolicy) { policy.FitCoverageWeight = 7000 }}, {name: "rank weights do not add up", change: func(policy *MatchingPolicy) { policy.RankFitWeight = 6000 }}, {name: "share above one", change: func(policy *MatchingPolicy) { policy.NeutralUnknown = 10001 }}, {name: "zero freshness floor", change: func(policy *MatchingPolicy) { policy.FreshnessFloor = 0 }}, {name: "zero responsiveness floor", change: func(policy *MatchingPolicy) { policy.ResponsivenessFloor = 0 }}, {name: "trust falls with the level", change: func(policy *MatchingPolicy) { policy.TrustOnline = 2000 }}, {name: "team result above a personal one", change: func(policy *MatchingPolicy) { policy.AttributionTeamRole = 10001 }}, {name: "broader relation above exact", change: func(policy *MatchingPolicy) { policy.RelationBroader = 10000 policy.RelationNarrower = 9000 policy.RelationExact = 8000 }}, {name: "zero required weight", change: func(policy *MatchingPolicy) { policy.RequiredSkillWeight = 0 }}, {name: "zero cohort minimum", change: func(policy *MatchingPolicy) { policy.PercentileMinCohort = 0 }}, {name: "no results allowed", change: func(policy *MatchingPolicy) { policy.MaxResultsPerGroup = 0 }}, {name: "unbounded results", change: func(policy *MatchingPolicy) { policy.MaxResultsPerGroup = 1001 }}, {name: "unknown specialization in the taxonomy", change: func(policy *MatchingPolicy) { policy.SkillTaxonomy[0].Specialization = "chef" }}, {name: "unknown technology in the taxonomy", change: func(policy *MatchingPolicy) { policy.SkillTaxonomy[0].Skills[0] = "cobol" }}, {name: "repeated technology in a group", change: func(policy *MatchingPolicy) { policy.SkillTaxonomy[0].Skills = []CandidateViewSkillCode{"go", "go"} }}, {name: "repeated specialization in the taxonomy", change: func(policy *MatchingPolicy) { policy.SkillTaxonomy[1].Specialization = SpecializationBackend }}, {name: "empty group in the taxonomy", change: func(policy *MatchingPolicy) { policy.SkillTaxonomy[1].Skills = nil }}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { policy := DemoProductPolicyV4() test.change(policy.Matching) if err := policy.Validate(); !errors.Is(err, ErrInvalidPolicy) { t.Fatalf("ProductPolicy.Validate() error = %v, want ErrInvalidPolicy", err) } }) } } func TestMatchingPolicyDemoValuesAreTheDesignValuesOfThePRD(t *testing.T) { policy := DemoMatchingPolicy() for level, want := range map[EvidenceTrust]BasisPoints{ EvidenceTrustStated: 2500, EvidenceTrustOnlineUnconfirmed: 6000, EvidenceTrustIdentityTied: 8500, EvidenceTrustInPerson: 10000, } { if got, ok := policy.TrustCoefficient(level); !ok || got != want { t.Errorf("TrustCoefficient(%d) = %d, %v, want %d", level, got, ok, want) } } if _, ok := policy.TrustCoefficient(EvidenceTrust(4)); ok { t.Error("TrustCoefficient(4) reported a coefficient for an unknown level") } for attribution, want := range map[EvidenceAttribution]BasisPoints{ EvidenceAttributionPersonal: 10000, EvidenceAttributionTeamRole: 8000, EvidenceAttributionTeamUnattributed: 5000, } { if got, ok := policy.AttributionCoefficient(attribution); !ok || got != want { t.Errorf("AttributionCoefficient(%s) = %d, %v, want %d", attribution, got, ok, want) } } for relation, want := range map[EvidenceSkillRelation]BasisPoints{ EvidenceSkillRelationExact: 10000, EvidenceSkillRelationNarrower: 8000, EvidenceSkillRelationBroader: 4000, EvidenceSkillRelationNone: 0, } { if got := policy.RelationCoefficient(relation); got != want { t.Errorf("RelationCoefficient(%d) = %d, want %d", relation, got, want) } } } func TestMatchingPolicyRelatesSkillsByItsOwnTaxonomy(t *testing.T) { policy := DemoMatchingPolicy() tests := []struct { name string requested, offered string want EvidenceSkillRelation }{ {"same technology", "go", "go", EvidenceSkillRelationExact}, {"specialization evidence for a technology", "go", "backend", EvidenceSkillRelationBroader}, {"technology evidence for a specialization", "backend", "go", EvidenceSkillRelationNarrower}, {"another technology", "go", "kafka", EvidenceSkillRelationNone}, {"unrelated specialization", "go", "frontend", EvidenceSkillRelationNone}, {"devops covers docker only", "go", "devops_sre", EvidenceSkillRelationNone}, {"devops covers docker", "docker", "devops_sre", EvidenceSkillRelationBroader}, {"unmapped requested skill", "", "backend", EvidenceSkillRelationNone}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { if got := policy.RelateSkill(test.requested, test.offered); got != test.want { t.Fatalf("RelateSkill(%q, %q) = %d, want %d", test.requested, test.offered, got, test.want) } }) } // A version without a taxonomy relates nothing but equal codes, and a // changed version changes the relation: the version decides the result. policy.SkillTaxonomy = nil if got := policy.RelateSkill("go", "backend"); got != EvidenceSkillRelationNone { t.Fatalf("RelateSkill without a taxonomy = %d, want none", got) } policy.SkillTaxonomy = []MatchingSkillGroup{{Specialization: SpecializationFrontend, Skills: []CandidateViewSkillCode{"docker"}}} if got := policy.RelateSkill("docker", "frontend"); got != EvidenceSkillRelationBroader { t.Fatalf("RelateSkill with a changed taxonomy = %d, want broader", got) } } func TestMatchingPolicyCloneDoesNotShareTheTaxonomy(t *testing.T) { original := DemoMatchingPolicy() copied := original.Clone() copied.SkillTaxonomy[0].Skills[0] = "kafka" copied.SkillTaxonomy[1].Specialization = SpecializationQA if original.SkillTaxonomy[0].Skills[0] == "kafka" || original.SkillTaxonomy[1].Specialization == SpecializationQA { t.Fatal("a clone of the matching policy shares its taxonomy with the original") } }