"""P0-4 恢复演练脚本的只读检查逻辑。""" import importlib.util import json import os import shutil import tempfile import unittest ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) SCRIPT = os.path.join(ROOT, "scripts", "evidence_drill.py") def load_drill(): spec = importlib.util.spec_from_file_location("evidence_drill", SCRIPT) module = importlib.util.module_from_spec(spec) assert spec.loader is not None spec.loader.exec_module(module) return module class EvidenceDrillTests(unittest.TestCase): def setUp(self): self.tmp = tempfile.mkdtemp(prefix="portcv_drill_") self.drill = load_drill() def tearDown(self): shutil.rmtree(self.tmp, ignore_errors=True) def write(self, name, rows): path = os.path.join(self.tmp, name) with open(path, "w", encoding="utf-8") as fh: for row in rows: fh.write(json.dumps(row) + "\n") return path def test_restart_resets_frame_id_but_keeps_sessions(self): self.write("frames.jsonl", [ {"session": "a", "frame_id": 1, "ts": 10.0}, {"session": "a", "frame_id": 2, "ts": 10.1}, {"session": "b", "frame_id": 1, "ts": 20.0}, {"session": "b", "frame_id": 2, "ts": 20.1}, ]) summary = self.drill.check_jsonl(self.tmp) self.assertEqual(summary["records"], 4) self.assertEqual(summary["sessions"], 2) self.assertEqual(summary["frame_id_regressions"], 0) self.assertEqual(summary["bad_lines"], 0) def test_bad_line_and_regression_are_reported(self): path = os.path.join(self.tmp, "frames.jsonl") with open(path, "w", encoding="utf-8") as fh: fh.write(json.dumps({"session": "a", "frame_id": 5, "ts": 1.0}) + "\n") fh.write('{"session":"a","frame_id":3,"ts":1.1}\n') fh.write('{"broken' ) summary = self.drill.check_jsonl(self.tmp) self.assertEqual(summary["bad_lines"], 1) self.assertEqual(summary["frame_id_regressions"], 1) self.assertFalse( summary["records"] > 0 and summary["bad_lines"] == 0 and summary["frame_id_regressions"] == 0 ) def test_events_summary_without_clips(self): summary = self.drill.check_events(self.tmp) self.assertEqual(summary["event_meta"], 0) self.assertEqual(summary["event_clips"], 0) self.assertEqual(summary["unreadable_clips"], 0) if __name__ == "__main__": unittest.main()