192 lines
10 KiB
Python
192 lines
10 KiB
Python
import copy
|
|
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
|
|
spec = importlib.util.spec_from_file_location("workflow", Path(__file__).parents[1] / "scripts/workflow.py")
|
|
w = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(w)
|
|
|
|
|
|
class WorkflowTests(unittest.TestCase):
|
|
def test_completed_week_on_monday_and_midweek(self):
|
|
expected = {"start": "2026-10-12", "end_exclusive": "2026-10-19", "timezone": "America/Denver"}
|
|
self.assertEqual(w.window("2026-10-19"), expected)
|
|
self.assertEqual(w.window("2026-10-21"), expected)
|
|
|
|
def test_dst_weeks_use_local_calendar(self):
|
|
self.assertEqual(w.window("2026-11-02")["start"], "2026-10-26")
|
|
self.assertEqual(w.window("2026-03-09")["start"], "2026-03-02")
|
|
|
|
def test_path_escape(self):
|
|
with self.assertRaises(ValueError):
|
|
w.inside(Path.cwd(), "../outside.json")
|
|
|
|
def fixture(self, root):
|
|
for d in ("config", "reports", "evidence", "receipts", "state"):
|
|
(root / d).mkdir()
|
|
w.atomic_json(root / "config/research.json", {"baseline_searches_per_method": 6})
|
|
w.atomic_json(root / "repository.json", {"version": "0.1.0", "entrypoints": {}})
|
|
(root / "VERSION").write_text("0.1.0")
|
|
w.atomic_json(root / "content-index.json", {"items": [], "updated_at": "2026-10-14"})
|
|
(root / "playbook.md").write_text("# Strategy")
|
|
body = "\n".join("## " + h for h in w.HEADINGS)
|
|
body += "\n[S001](https://example.com/news)"
|
|
(root / "reports/run.md").write_text(body)
|
|
receipt = {"run_id": "run", "status": "complete", "validation_passed": True, "gaps": [], "capture_completeness": "complete", "started_at": "2026-10-19T07:00:00-06:00", "ended_at": "2026-10-19T07:30:00-06:00", "usage": None}
|
|
w.atomic_json(root / "receipts/run.json", receipt)
|
|
e = {"run_id": "run", "run_date": "2026-10-19", "window": w.window("2026-10-19"), "status": "complete", "report_path": "reports/run.md", "receipt_path": "receipts/run.json", "coverage": {p: "complete" for p in w.POOLS}, "playbook": {"path": str(root / "playbook.md"), "sha256": w.hashlib.sha256((root / "playbook.md").read_bytes()).hexdigest(), "sections": ["strategy"]}, "searches": [{"method": m, "query": "fixture", "status": "ok", "returned_count": 0} for m in ("standard_web", "exa") for _ in range(6)], "pool_checks": {p: [{"url": "https://example.com/index", "status": "ok"}] for p in ("sec_edgar", "rock_fight")}, "sources": [{"id": "S001", "url": "https://example.com/news", "title": "fixture", "published_date": "2026-10-12", "discovery_methods": ["standard_web", "exa"], "retrieval_method": "web_open", "format": "article", "claim": "fixture", "north_america_relevance": "U.S. retail", "evidence_level": "explicit", "confidence": "high", "kind": "news"}]}
|
|
w.atomic_json(root / "evidence/run.json", e)
|
|
return e
|
|
|
|
def test_success_is_idempotent(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root = Path(tmp)
|
|
self.fixture(root)
|
|
w.finalize(root, "evidence/run.json")
|
|
w.finalize(root, "evidence/run.json")
|
|
self.assertEqual(len(w.read(root / "content-index.json")["items"]), 1)
|
|
self.assertTrue((root / "state/last-successful-run.json").exists())
|
|
|
|
def test_bad_evidence_never_advances_checkpoint(self):
|
|
for mutation in ("missing_exa", "stale", "partial", "podcast", "sec", "hash", "unknown_date"):
|
|
with self.subTest(mutation=mutation), tempfile.TemporaryDirectory() as tmp:
|
|
root = Path(tmp)
|
|
e = self.fixture(root)
|
|
if mutation == "missing_exa":
|
|
e["searches"] = [s for s in e["searches"] if s["method"] != "exa"]
|
|
elif mutation == "stale":
|
|
e["sources"][0]["published_date"] = "2025-10-12"
|
|
elif mutation == "partial":
|
|
e["coverage"]["sec_edgar"] = "unavailable"
|
|
elif mutation == "podcast":
|
|
e["sources"][0]["kind"] = "podcast"
|
|
elif mutation == "sec":
|
|
e["sources"][0]["kind"] = "sec"
|
|
elif mutation == "hash":
|
|
e["playbook"]["sha256"] = "bad"
|
|
else:
|
|
e["sources"][0]["published_date"] = "unknown"
|
|
w.atomic_json(root / "evidence/run.json", e)
|
|
with self.assertRaises((ValueError, KeyError)):
|
|
w.finalize(root, "evidence/run.json")
|
|
self.assertFalse((root / "state/last-successful-run.json").exists())
|
|
self.assertEqual(w.read(root / "content-index.json")["items"], [])
|
|
|
|
def test_failure_count_is_unavailable(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root = Path(tmp)
|
|
e = self.fixture(root)
|
|
e["searches"][0].update(status="error", returned_count=0)
|
|
w.atomic_json(root / "evidence/run.json", e)
|
|
with self.assertRaises(ValueError):
|
|
w.validate_run(root, root / "evidence/run.json")
|
|
|
|
# Gmail contract tests use synthetic aggregate counts and public evidence only.
|
|
class GmailCoverageTests(unittest.TestCase):
|
|
fixture = WorkflowTests.fixture
|
|
|
|
def gmail_fixture(self, root, status="complete"):
|
|
e = self.fixture(root)
|
|
config = w.read(root / "config/research.json")
|
|
config["gmail_candidate_source"] = {"enabled_from_run_date": "2026-10-19"}
|
|
w.atomic_json(root / "config/research.json", config)
|
|
e["coverage"]["gmail_brandemails"] = status
|
|
e["gmail_collection"] = {"status": status, "pages_read": 2, "messages_enumerated": 3,
|
|
"messages_screened": 3, "enumeration_complete": True, "screening_complete": True, "limitations": []}
|
|
return e
|
|
|
|
def test_complete_gmail_and_merged_public_evidence(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root = Path(tmp)
|
|
e = self.gmail_fixture(root)
|
|
e["sources"][0]["discovery_methods"].append("gmail")
|
|
w.atomic_json(root / "evidence/run.json", e)
|
|
w.finalize(root, "evidence/run.json")
|
|
|
|
def test_gmail_cannot_claim_complete_when_truncated_or_missing(self):
|
|
for mutation in ("missing", "truncated", "screening", "unavailable", "unchecked_empty"):
|
|
with self.subTest(mutation=mutation), tempfile.TemporaryDirectory() as tmp:
|
|
root = Path(tmp)
|
|
e = self.gmail_fixture(root)
|
|
if mutation == "missing":
|
|
del e["coverage"]["gmail_brandemails"]
|
|
elif mutation == "truncated":
|
|
e["gmail_collection"]["enumeration_complete"] = False
|
|
elif mutation == "screening":
|
|
e["gmail_collection"]["messages_screened"] = 1
|
|
elif mutation == "unchecked_empty":
|
|
e["gmail_collection"].update(pages_read=0, messages_enumerated=0, messages_screened=0)
|
|
else:
|
|
e["coverage"]["gmail_brandemails"] = "unavailable"
|
|
e["gmail_collection"].update(status="unavailable", limitations=["Connector unavailable"])
|
|
w.atomic_json(root / "evidence/run.json", e)
|
|
with self.assertRaises(ValueError):
|
|
w.finalize(root, "evidence/run.json")
|
|
self.assertFalse((root / "state/last-successful-run.json").exists())
|
|
|
|
def test_unavailable_gmail_retains_valid_partial_artifacts(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root = Path(tmp)
|
|
e = self.gmail_fixture(root, "unavailable")
|
|
e["status"] = "incomplete"
|
|
e["gmail_collection"].update(pages_read=0, messages_enumerated=0, messages_screened=0,
|
|
enumeration_complete=False, screening_complete=False, limitations=["Connector unavailable"])
|
|
receipt = w.read(root / "receipts/run.json")
|
|
receipt.update(status="incomplete", capture_completeness="partial", gaps=["Gmail unavailable"])
|
|
w.atomic_json(root / "receipts/run.json", receipt)
|
|
w.atomic_json(root / "evidence/run.json", e)
|
|
w.validate_run(root, root / "evidence/run.json")
|
|
|
|
def test_checked_empty_scan_is_valid(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root = Path(tmp)
|
|
e = self.gmail_fixture(root)
|
|
e["gmail_collection"].update(pages_read=1, messages_enumerated=0, messages_screened=0)
|
|
w.atomic_json(root / "evidence/run.json", e)
|
|
w.finalize(root, "evidence/run.json")
|
|
|
|
|
|
class SiaTranscriptTests(unittest.TestCase):
|
|
fixture = WorkflowTests.fixture
|
|
|
|
def sia_fixture(self, root):
|
|
e = self.fixture(root)
|
|
e["sources"][0].update(kind="podcast", podcast_series="sia_inside_line", format="publisher_transcript",
|
|
episode_title="Synthetic SIA episode", speaker="Verified speaker", timestamps="00:01-01:00",
|
|
transcript_url="https://example.com/transcript", transcript_provider="Publisher",
|
|
transcript_completeness="full", transcript_limitations=[])
|
|
return e
|
|
|
|
def test_provided_transcript_formats(self):
|
|
for fmt in ("publisher_transcript", "provided_speech_to_text"):
|
|
with self.subTest(fmt=fmt), tempfile.TemporaryDirectory() as tmp:
|
|
root = Path(tmp)
|
|
e = self.sia_fixture(root)
|
|
e["sources"][0]["format"] = fmt
|
|
w.atomic_json(root / "evidence/run.json", e)
|
|
w.finalize(root, "evidence/run.json")
|
|
|
|
def test_rejects_audio_notes_and_missing_provenance(self):
|
|
for mutation in ("audio_transcribed", "show_notes", "outline", "missing_url", "partial_without_limits"):
|
|
with self.subTest(mutation=mutation), tempfile.TemporaryDirectory() as tmp:
|
|
root = Path(tmp)
|
|
e = self.sia_fixture(root)
|
|
source = e["sources"][0]
|
|
if mutation == "missing_url":
|
|
del source["transcript_url"]
|
|
elif mutation == "partial_without_limits":
|
|
source["transcript_completeness"] = "partial"
|
|
else:
|
|
source["format"] = mutation
|
|
w.atomic_json(root / "evidence/run.json", e)
|
|
with self.assertRaises(ValueError):
|
|
w.finalize(root, "evidence/run.json")
|
|
self.assertFalse((root / "state/last-successful-run.json").exists())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|