package opportunities import ( "strings" "testing" ) const srcText = `Statement of Work. The contractor shall provide senior data engineering support to the Department of Veterans Affairs. Personnel must hold an active Secret clearance. The contractor shall deliver a monthly status report. Two Senior Data Engineers are required at the Austin, Texas facility.` // TestUncitedModelOutputIsRejected is the Gate 2 criterion: an item the model produced without a quote // cannot become a scope item, no matter how plausible it reads. func TestUncitedModelOutputIsRejected(t *testing.T) { raw := []byte(`{"items":[{"scope_type":"task","title":"Provide data engineering","quote":""}]}`) got, rejected, err := ValidateScopeCandidates(raw, srcText) if err != nil { t.Fatalf("validation should not error on a well formed payload: %v", err) } if len(got) != 0 { t.Fatalf("an uncited item must be rejected, got %d accepted", len(got)) } if len(rejected) != 1 || !strings.Contains(rejected[0].Reason, "uncited") { t.Fatalf("expected one uncited rejection, got %+v", rejected) } } // TestFabricatedCitationIsRejected is the strongest grounding guarantee here: a quote that does not appear // in the source text is a FABRICATED citation, which is worse than no citation because it reads as evidence. // The check is exact containment against the source, so the model cannot invent support for a claim. func TestFabricatedCitationIsRejected(t *testing.T) { raw := []byte(`{"items":[{"scope_type":"clearance","title":"TS SCI required", "quote":"Personnel must hold an active TS SCI clearance with polygraph."}]}`) got, rejected, err := ValidateScopeCandidates(raw, srcText) if err != nil { t.Fatalf("unexpected error: %v", err) } if len(got) != 0 { t.Fatal("a quote absent from the source must be rejected as a fabricated citation") } if len(rejected) != 1 || !strings.Contains(rejected[0].Reason, "not found in the source") { t.Fatalf("expected a fabricated citation rejection, got %+v", rejected) } } // TestGroundedItemIsAcceptedAndHashed proves a properly cited item passes and carries the hash of the exact // quote, which is what the scope item stores as its citation. func TestGroundedItemIsAcceptedAndHashed(t *testing.T) { raw := []byte(`{"items":[{"scope_type":"clearance","title":"Secret clearance required", "quote":"Personnel must hold an active Secret clearance.","confidence":0.9}]}`) got, _, err := ValidateScopeCandidates(raw, srcText) if err != nil { t.Fatalf("unexpected error: %v", err) } if len(got) != 1 { t.Fatalf("a grounded item must be accepted, got %d", len(got)) } if got[0].QuoteHash == "" || len(got[0].QuoteHash) != 64 { t.Fatalf("expected a sha256 hex quote hash, got %q", got[0].QuoteHash) } if got[0].ScopeType != "clearance" { t.Errorf("scope type not preserved: %q", got[0].ScopeType) } } // TestInvalidScopeTypeIsRejected proves the closed vocabulary is enforced before the database sees it, so a // model inventing a category is refused rather than becoming an 'other' catch all. func TestInvalidScopeTypeIsRejected(t *testing.T) { raw := []byte(`{"items":[{"scope_type":"vibes","title":"x","quote":"The contractor shall deliver a monthly status report."}]}`) got, rejected, err := ValidateScopeCandidates(raw, srcText) if err != nil { t.Fatalf("unexpected error: %v", err) } if len(got) != 0 || len(rejected) != 1 { t.Fatalf("an out of vocabulary scope type must be rejected, got %d accepted", len(got)) } if !strings.Contains(rejected[0].Reason, "scope_type") { t.Errorf("expected a scope_type rejection, got %q", rejected[0].Reason) } } // TestMalformedPayloadFailsClosed proves an unparseable model response yields an error and zero items, // never a partial write. func TestMalformedPayloadFailsClosed(t *testing.T) { if _, _, err := ValidateScopeCandidates([]byte("not json at all"), srcText); err == nil { t.Fatal("a malformed model payload must fail closed") } } // TestEmptySourceRejectsEverything proves that with no source text nothing can be grounded, so nothing is // accepted. A normalization run against an empty notice writes no scope. func TestEmptySourceRejectsEverything(t *testing.T) { raw := []byte(`{"items":[{"scope_type":"task","title":"x","quote":"anything at all"}]}`) got, _, err := ValidateScopeCandidates(raw, "") if err != nil { t.Fatalf("unexpected error: %v", err) } if len(got) != 0 { t.Fatal("nothing can be grounded against an empty source") } }