package magstore import ( "reflect" "testing" ) func mem(slate, cand string, approved bool, f ...float64) SlateMember { return SlateMember{SlateID: slate, CandidateID: cand, Approved: approved, Features: f} } func TestRerankCoeffsInFeatureOrder(t *testing.T) { m := Model{Coeffs: map[string]float64{ "clearance_score": 1, "skills_score": 2, "certs_score": 3, "customer_score": 4, "experience_score": 5, "location_score": 6, }} got := RerankCoeffs(m) if !reflect.DeepEqual(got, []float64{1, 2, 3, 4, 5, 6}) { t.Fatalf("coeffs must come back in RerankFeatureOrder, got %v", got) } } func TestOrderMembersByUtilityStableTiebreak(t *testing.T) { // weight only the first feature; a higher first feature sorts first. coeffs := []float64{1, 0, 0, 0, 0, 0} members := []SlateMember{ mem("s", "low", false, 10, 0, 0, 0, 0, 0), mem("s", "high", true, 90, 0, 0, 0, 0, 0), mem("s", "mid", false, 50, 0, 0, 0, 0, 0), } ordered := OrderMembers(coeffs, members) got := []string{ordered[0].CandidateID, ordered[1].CandidateID, ordered[2].CandidateID} if !reflect.DeepEqual(got, []string{"high", "mid", "low"}) { t.Fatalf("must order by utility descending, got %v", got) } } func TestOrderMembersZeroWeightPreservesEngineOrder(t *testing.T) { // zero weights => every utility ties => the stable incoming (engine) order is preserved exactly. coeffs := []float64{0, 0, 0, 0, 0, 0} members := []SlateMember{ mem("s", "a", true, 1, 0, 0, 0, 0, 0), mem("s", "b", false, 9, 0, 0, 0, 0, 0), mem("s", "c", true, 5, 0, 0, 0, 0, 0), } ordered := OrderMembers(coeffs, members) got := []string{ordered[0].CandidateID, ordered[1].CandidateID, ordered[2].CandidateID} if !reflect.DeepEqual(got, []string{"a", "b", "c"}) { t.Fatalf("a zero weight must preserve the engine order, got %v", got) } } func TestTopKSelectionUsesApprovedCount(t *testing.T) { // two approved on this slate => the top 2 by utility are the selection the ranking nudges. coeffs := []float64{1, 0, 0, 0, 0, 0} groups := []SlateGroup{{SlateID: "s", Members: []SlateMember{ mem("s", "top", true, 90, 0, 0, 0, 0, 0), mem("s", "second", false, 80, 0, 0, 0, 0, 0), mem("s", "third", true, 10, 0, 0, 0, 0, 0), }}} sel := TopKSelection(coeffs, groups) // two approved => top 2 by the weight (top=90, second=80) are selected; third (10) is not. if !sel["s|top"] || !sel["s|second"] { t.Fatalf("the top two by utility must be selected, got %v", sel) } if sel["s|third"] { t.Fatalf("the third ranked must not be in a top two selection, got %v", sel) } if len(sel) != 2 { t.Fatalf("the selection size must equal the approved count (2), got %d", len(sel)) } } func TestTopKSelectionSkipsUnanimousReject(t *testing.T) { // no approvals => nothing is selected (no ordering signal to gate). coeffs := []float64{1, 0, 0, 0, 0, 0} groups := []SlateGroup{{SlateID: "s", Members: []SlateMember{ mem("s", "a", false, 90, 0, 0, 0, 0, 0), mem("s", "b", false, 10, 0, 0, 0, 0, 0), }}} if sel := TopKSelection(coeffs, groups); len(sel) != 0 { t.Fatalf("a slate with no approvals must select nothing, got %v", sel) } }