"""Tests for the monitoring layer. The ledger is the house's only source of truth, so it is the one thing that must not lie: these tests assert the honesty properties, not just the happy path.""" import json import tempfile import unittest from pathlib import Path from blue_morpho.monitor import GATES, Monitor class MonitorTest(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() self.ledger = Path(self.tmp.name) / "events.jsonl" self.mon = Monitor(self.ledger) def tearDown(self): self.tmp.cleanup() def test_record_and_read_back(self): self.mon.record("film", "author", "Story", "consulted", engine="Qwen3-VL-8B", result="ok") rows = self.mon.events() self.assertEqual(len(rows), 1) self.assertEqual(rows[0]["seat"], "author") self.assertIn("ts", rows[0]) def test_events_are_filtered_by_production(self): self.mon.record("a", "author", "Story", "consulted") self.mon.record("b", "director", "Direction", "consulted") self.assertEqual(len(self.mon.events("a")), 1) self.assertEqual(len(self.mon.events()), 2) self.assertEqual(self.mon.productions(), ["a", "b"]) def test_a_torn_line_is_dropped_not_guessed(self): self.mon.record("film", "author", "Story", "consulted") with self.ledger.open("a") as fh: fh.write('{"production": "film", "seat": "tru') # torn write rows = self.mon.events() self.assertEqual(len(rows), 1, "an unparseable line must be skipped, never inferred") def test_a_gate_is_not_run_until_someone_gates_it(self): state = self.mon.gates("film") self.assertEqual(set(state), set(GATES)) self.assertTrue(all(v["result"] == "not run" for v in state.values()), "silence is never a pass") def test_a_failed_gate_stays_failed(self): self.mon.record("film", "executive_producer", "Development", "gated", gate="G1", result="fail", note="5.0 < 7.0") self.assertEqual(self.mon.gates("film")["G1"]["result"], "fail") self.assertEqual(self.mon.gates("film")["G1"]["note"], "5.0 < 7.0") def test_hook_series_keeps_order(self): for score in (4.0, 6.0, 8.0): self.mon.record("film", "eyes", "Story", "scored", artifact="cut", score=score) self.assertEqual([s for _, s in self.mon.hook_series("film")], [4.0, 6.0, 8.0]) def test_rollup_counts_acts_failures_and_engines(self): self.mon.record("film", "author", "Story", "consulted", engine="A", duration_s=2.0) self.mon.record("film", "author", "Story", "consulted", engine="B", duration_s=3.0) self.mon.record("film", "camera", "Camera", "rendered", engine="C", result="fail") roll = self.mon.rollup() self.assertEqual(roll["author"]["acts"], 2) self.assertEqual(roll["author"]["engines"], ["A", "B"]) self.assertAlmostEqual(roll["author"]["seconds"], 5.0) self.assertEqual(roll["camera"]["failures"], 1) def test_health_refuses_to_report_an_impossible_disk(self): health = self.mon.health() self.assertIn("disk_note", health) if health["disk_free_tb"] is not None: self.assertLess(health["disk_free_tb"], 1e6, "overlayfs reports a synthetic capacity; it must never be quoted") def test_report_is_printable_and_names_the_house(self): self.mon.record("film", "author", "Story", "consulted") text = self.mon.report("film") self.assertIn("BlueMorpho", text) self.assertIn("gates:", text) if __name__ == "__main__": unittest.main()