import unittest from market_proposals import validate_proposal, insert_proposal REPORT = "https://example.org/research\nEvidence: product listing lacks dosage instructions." BASE = {"source_url": "https://example.org/research", "evidence_quote": "product listing lacks dosage instructions", "action_type": "CATALOG_AUDIT", "department": "CONTENT"} class FakeDB: def __init__(self, output): self.output = output self.statements = [] def sql(self, statement): self.statements.append(statement) return self.output class MarketProposalTests(unittest.TestCase): def test_verified_citation_produces_stable_fingerprint_and_pending_payload(self): first = validate_proposal(BASE, REPORT, "scout-2026-10-20.md") second = validate_proposal(BASE, REPORT, "scout-2026-10-22.md") self.assertEqual(first["fingerprint"], second["fingerprint"]) self.assertEqual(first["payload"]["sourceUrl"], BASE["source_url"]) self.assertEqual(first["payload"]["evidenceQuote"], BASE["evidence_quote"]) self.assertEqual(first["payload"]["reviewRequired"], True) self.assertNotIn("price", str(first).lower()) def test_unsubstantiated_or_unsafe_sources_rejected(self): for item in ({**BASE, "source_url": "https://other.example/page"}, {**BASE, "evidence_quote": "invented price 123"}, {**BASE, "source_url": "file:///etc/passwd"}): with self.subTest(item=item), self.assertRaises(ValueError): validate_proposal(item, REPORT, "scout.md") def test_quote_must_be_in_same_citation_block_as_url(self): report = "https://example.org/research\nSome unrelated finding.\n\nhttps://example.org/other\nproduct listing lacks dosage instructions" with self.assertRaises(ValueError): validate_proposal(BASE, report, "scout.md") def test_human_and_physical_routes_rejected(self): for item in ({**BASE, "department": "B2B"}, {**BASE, "department": "CLINICAL_RD"}, {**BASE, "action_type": "FIELD_VISIT"}, {**BASE, "action_type": "CATALOG_AUDIT", "department": "SUPPORT"}): with self.subTest(item=item), self.assertRaises(ValueError): validate_proposal(item, REPORT, "scout.md") def test_untrusted_model_narrative_cannot_enter_payload_or_title(self): item = {**BASE, "title": "Guaranteed cure 999", "payload": {"price": "invented"}} result = validate_proposal(item, REPORT, "scout.md") self.assertNotIn("Guaranteed", str(result)) self.assertNotIn("invented", str(result)) def test_insert_counts_only_returned_uuid_and_sql_is_atomic_and_pending(self): proposal = validate_proposal(BASE, REPORT, "scout.md") db = FakeDB("BEGIN\n123e4567-e89b-12d3-a456-426614174000\nINSERT 0 1\nCOMMIT") self.assertTrue(insert_proposal(db, proposal)) sql = db.statements[0] self.assertIn("pg_advisory_xact_lock", sql) self.assertIn("sourceFingerprint", sql) self.assertIn("RETURNING id", sql) self.assertIn("'PENDING'", sql) self.assertNotIn("APPROVED", sql) self.assertFalse(insert_proposal(FakeDB("BEGIN\nINSERT 0 0\nCOMMIT"), proposal)) self.assertFalse(insert_proposal(FakeDB("BEGIN\nCOMMIT"), proposal)) if __name__ == "__main__": unittest.main()