import json import subprocess import sys import tempfile import unittest import zipfile from pathlib import Path from xml.etree import ElementTree as ET ROOT = Path(__file__).resolve().parents[1] class ScriptTests(unittest.TestCase): def test_csv_analysis_separates_maturity_dates(self): with tempfile.TemporaryDirectory() as tmp: output = Path(tmp) / "analysis.json" subprocess.run([ sys.executable, str(ROOT / "scripts/analyze_report.py"), str(ROOT / "tests/fixtures/sample-report.csv"), "--analysis-date", "2026-08-25", "--retention-lag-days", "1", "--output", str(output), ], check=True, capture_output=True, text=True) data = json.loads(output.read_text(encoding="utf-8")) self.assertEqual(data["dates"]["analysis_date"], "2026-08-25") self.assertEqual(data["dates"]["retention_mature_date"], "2026-08-24") self.assertEqual(len(data["ads"]), 2) self.assertNotIn("tier", data["ads"][0]) self.assertAlmostEqual(data["portfolio"]["analysis_day"]["spend"], 72) def test_docx_contract_and_forbidden_headings(self): with tempfile.TemporaryDirectory() as tmp: output = Path(tmp) / "brief.docx" subprocess.run([ sys.executable, str(ROOT / "scripts/build_brief.py"), str(ROOT / "examples/brief.example.json"), "--output", str(output), ], check=True, capture_output=True, text=True) self.assertTrue(zipfile.is_zipfile(output)) with zipfile.ZipFile(output) as archive: root = ET.fromstring(archive.read("word/document.xml")) text = "".join(root.itertext()) for heading in ("1. 是否达标", "2. 问题分析", "3. 建议动作", "4. 近7天表现好的 Ads"): self.assertIn(heading, text) for forbidden in ("核心判断", "规则一", "规则二", "Campaign层级", "层级判断"): self.assertNotIn(forbidden, text) self.assertIn("决定", text) if __name__ == "__main__": unittest.main()