Files
outdoor-industry-intelligen…/tests/test_workflow.py
T
2026-10-14 12:05:54 -06:00

90 lines
5.1 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")
if __name__ == "__main__":
unittest.main()