package commandcenter import ( "reflect" "strings" "testing" "github.com/shopspring/decimal" ) func dec(s string) *decimal.Decimal { d := decimal.RequireFromString(s) return &d } func findGap(gaps []Gap, lcat string) *Gap { for i := range gaps { if gaps[i].LaborCategory == lcat { return &gaps[i] } } return nil } // reqs that all share one labor category and level, one per opportunity, all fully unmet (no assignment). func unmetReqs(lcat, level string, oppIDs ...string) []reqMeta { out := make([]reqMeta, 0, len(oppIDs)) for i, o := range oppIDs { out = append(out, reqMeta{id: "r" + o, oppID: o, lcat: lcat, level: level, quantity: 1}) _ = i } return out } func TestRevenueAtRiskCountedBasisMixed(t *testing.T) { reqs := unmetReqs("Cyber", "secret", "o1", "o2", "o3") opp := map[string]oppInfo{ "o1": {name: "Alpha", value: dec("1000000.00"), valueSource: "representative"}, "o2": {name: "Bravo", value: dec("2000000.00"), valueSource: "representative"}, "o3": {name: "Charlie", value: nil, valueSource: "representative"}, // unpriced } gaps, _, _ := gapsFromAssignment(reqs, map[string]int{}, opp) g := findGap(gaps, "Cyber") if g == nil { t.Fatal("expected a Cyber gap") } if g.PricedOpportunities != 2 || g.UnpricedOpportunities != 1 || g.TotalOpportunities != 3 { t.Fatalf("counts: priced=%d unpriced=%d total=%d, want 2/1/3", g.PricedOpportunities, g.UnpricedOpportunities, g.TotalOpportunities) } if g.ValueAtRisk == nil || *g.ValueAtRisk != "3000000.00" { t.Fatalf("value at risk: got %v want 3000000.00", g.ValueAtRisk) } if !strings.Contains(g.ValueBasis, "Summed across 2 of 3 priced") || !strings.Contains(g.ValueBasis, "1 excluded as unpriced") { t.Fatalf("basis line not auditable: %q", g.ValueBasis) } if !g.ValueRepresentative { t.Error("synthetic values must be representative") } } func TestRevenueAtRiskAllUnpriced(t *testing.T) { reqs := unmetReqs("Cyber", "secret", "o1", "o2") opp := map[string]oppInfo{ "o1": {name: "Alpha", value: nil, valueSource: "representative"}, "o2": {name: "Bravo", value: nil, valueSource: "representative"}, } gaps, _, _ := gapsFromAssignment(reqs, map[string]int{}, opp) g := findGap(gaps, "Cyber") if g.ValueAtRisk != nil { t.Fatalf("all unpriced must emit no dollar, got %v", *g.ValueAtRisk) } if g.PricedOpportunities != 0 || g.UnpricedOpportunities != 2 { t.Fatalf("counts: priced=%d unpriced=%d, want 0/2", g.PricedOpportunities, g.UnpricedOpportunities) } if !g.ValueRepresentative { t.Error("no real basis means representative") } if !strings.Contains(g.ValueBasis, "2 of 2 opportunities excluded as unpriced") { t.Fatalf("basis: %q", g.ValueBasis) } } func TestRevenueAtRiskNoOpportunities(t *testing.T) { // A req with no oppID ties the gap to zero opportunities. reqs := []reqMeta{{id: "r1", oppID: "", lcat: "Cyber", level: "secret", quantity: 2}} gaps, _, _ := gapsFromAssignment(reqs, map[string]int{}, map[string]oppInfo{}) g := findGap(gaps, "Cyber") if g.TotalOpportunities != 0 || g.ValueAtRisk != nil { t.Fatalf("no opportunities: total=%d value=%v", g.TotalOpportunities, g.ValueAtRisk) } if g.ValueBasis != "No priced opportunities tied to this gap." { t.Fatalf("basis: %q", g.ValueBasis) } } func TestRevenueAtRiskDecimalSumExact(t *testing.T) { reqs := unmetReqs("Cyber", "secret", "o1", "o2") opp := map[string]oppInfo{ "o1": {name: "Alpha", value: dec("1234567.89"), valueSource: "representative"}, "o2": {name: "Bravo", value: dec("2000000.10"), valueSource: "representative"}, } gaps, _, _ := gapsFromAssignment(reqs, map[string]int{}, opp) g := findGap(gaps, "Cyber") // 1234567.89 + 2000000.10 = 3234567.99 exactly; float64 addition would drift off the last cent. if g.ValueAtRisk == nil { t.Fatal("decimal sum: got nil want 3234567.99") } if *g.ValueAtRisk != "3234567.99" { t.Fatalf("decimal sum: got %s want 3234567.99", *g.ValueAtRisk) } } func TestRevenueAtRiskRealVsRepresentative(t *testing.T) { cases := []struct { name string sources []string wantRepres bool }{ {"all federal real", []string{"sam", "usaspending"}, false}, {"mixed real and synthetic", []string{"sam", "representative"}, true}, {"all operator entered", []string{"operator", "operator"}, true}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { reqs := unmetReqs("Cyber", "secret", "o1", "o2") opp := map[string]oppInfo{ "o1": {name: "Alpha", value: dec("1000000.00"), valueSource: tc.sources[0]}, "o2": {name: "Bravo", value: dec("2000000.00"), valueSource: tc.sources[1]}, } gaps, _, _ := gapsFromAssignment(reqs, map[string]int{}, opp) g := findGap(gaps, "Cyber") if g.ValueRepresentative != tc.wantRepres { t.Errorf("representative: got %v want %v", g.ValueRepresentative, tc.wantRepres) } if !tc.wantRepres && !strings.Contains(g.ValueBasis, "cached federal values") { t.Errorf("real basis should cite cached federal values: %q", g.ValueBasis) } }) } } func TestIsRealValueSource(t *testing.T) { for src, want := range map[string]bool{ "sam": true, "usaspending": true, "representative": false, "operator": false, "bogus": false, "": false, } { if got := isRealValueSource(src); got != want { t.Errorf("isRealValueSource(%q): got %v want %v", src, got, want) } } } func TestMissionReadinessImpactPct(t *testing.T) { // demandedTotal = 20, gap unmet = 5 => 25.00 points of overall coverage. reqs := []reqMeta{{id: "r1", oppID: "o1", lcat: "Cyber", level: "secret", quantity: 20}} opp := map[string]oppInfo{"o1": {name: "Alpha", value: dec("1000000.00"), valueSource: "representative"}} gaps, _, _ := gapsFromAssignment(reqs, map[string]int{"r1": 15}, opp) g := findGap(gaps, "Cyber") if g.Unmet != 5 { t.Fatalf("unmet: got %d want 5", g.Unmet) } if g.ImpactModeledPct != "25.00" { t.Fatalf("impact: got %s want 25.00", g.ImpactModeledPct) } if !strings.Contains(g.ImpactModeledLabel, "Modeled coverage delta") || !strings.Contains(g.ImpactModeledLabel, "not a guarantee") { t.Fatalf("impact label must be modeled and not a guarantee: %q", g.ImpactModeledLabel) } // Rounding: unmet 1 of demanded 3 => 33.33. reqs2 := []reqMeta{{id: "r1", oppID: "", lcat: "Data", level: "secret", quantity: 3}} gaps2, _, _ := gapsFromAssignment(reqs2, map[string]int{"r1": 2}, map[string]oppInfo{}) if g2 := findGap(gaps2, "Data"); g2.ImpactModeledPct != "33.33" { t.Fatalf("rounding: got %s want 33.33", g2.ImpactModeledPct) } } func TestRevenueAtRiskDeterministic(t *testing.T) { reqs := unmetReqs("Cyber", "secret", "o1", "o2", "o3") opp := map[string]oppInfo{ "o1": {name: "Alpha", value: dec("1000000.00"), valueSource: "sam"}, "o2": {name: "Bravo", value: dec("2000000.00"), valueSource: "representative"}, "o3": {name: "Charlie", value: nil, valueSource: "representative"}, } a, _, _ := gapsFromAssignment(reqs, map[string]int{}, opp) b, _, _ := gapsFromAssignment(reqs, map[string]int{}, opp) if len(a) != len(b) { t.Fatalf("length differs: %d vs %d", len(a), len(b)) } for i := range a { if !reflect.DeepEqual(a[i], b[i]) { t.Fatalf("gap %d differs across runs:\n%+v\n%+v", i, a[i], b[i]) } } }