"""The device_commands audit table: one row per change attempt, pending -> result.""" from __future__ import annotations import os import sys sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from sfm.audit import DbAudit # noqa: E402 from sfm.database import SeismoDb # noqa: E402 ROW = dict(serial="UM20147", series=4, address="serial:/dev/ttyACM0", action="start monitoring", tier="change", request={"plan": {"steps": [{"sub": "0x96"}]}}, caller="test") def test_a_command_is_pending_until_finished(tmp_path): db = SeismoDb(tmp_path / "s.db") cid = db.begin_device_command(**ROW) row = db.query_device_commands(serial="UM20147")[0] assert (row["id"], row["outcome"], row["verified"]) == (cid, "pending", None) assert row["request"]["plan"]["steps"][0]["sub"] == "0x96" db.finish_device_command(cid, outcome="sent", verified="ok", ack={"subs": ["0x69"]}, error=None) row = db.query_device_commands(serial="UM20147")[0] assert (row["outcome"], row["verified"], row["ack"]) == ("sent", "ok", {"subs": ["0x69"]}) assert row["finished_ts"] def test_the_table_survives_reopening(tmp_path): SeismoDb(tmp_path / "s.db").begin_device_command(**ROW) assert len(SeismoDb(tmp_path / "s.db").query_device_commands()) == 1 def test_db_audit_is_a_thin_sink(tmp_path): db = SeismoDb(tmp_path / "s.db") audit = DbAudit(lambda: db) cid = audit.begin(**ROW) audit.finish(cid, outcome="failed", verified="mismatch", ack=None, error="unit reads idle") row = db.query_device_commands()[0] assert (row["outcome"], row["verified"], row["error"]) == ("failed", "mismatch", "unit reads idle") def test_finishing_an_unknown_id_raises_and_creates_nothing(tmp_path): import pytest db = SeismoDb(tmp_path / "s.db") with pytest.raises(ValueError, match="no-such-id"): db.finish_device_command("no-such-id", outcome="sent", verified="ok", ack=None, error=None) assert db.query_device_commands() == [] def test_a_second_finish_raises_and_keeps_the_first_result(tmp_path): import pytest db = SeismoDb(tmp_path / "s.db") cid = db.begin_device_command(**ROW) db.finish_device_command(cid, outcome="failed", verified="mismatch", ack={"subs": ["0x69"]}, error="unit reads idle") with pytest.raises(ValueError, match=cid): db.finish_device_command(cid, outcome="sent", verified="ok", ack={"other": 1}, error=None) row = db.query_device_commands()[0] assert (row["outcome"], row["verified"], row["ack"], row["error"]) == \ ("failed", "mismatch", {"subs": ["0x69"]}, "unit reads idle") def test_a_finish_records_whether_a_frame_went_out_and_the_states(tmp_path): db = SeismoDb(tmp_path / "s.db") states = {"before": "idle", "seen": ["idle", "monitoring"], "after": "monitoring"} cid = db.begin_device_command(**ROW) db.finish_device_command(cid, outcome="sent", verified="ok", ack={"subs": ["0x69"]}, error=None, sent=1, states=states) row = db.query_device_commands()[0] assert (row["sent"], row["states"]) == (1, states) def test_sent_and_states_default_to_null(tmp_path): db = SeismoDb(tmp_path / "s.db") cid = db.begin_device_command(**ROW) db.finish_device_command(cid, outcome="failed", verified="not_checked", ack=None, error="no clean ack") row = db.query_device_commands()[0] assert (row["sent"], row["states"]) == (None, None) def test_db_audit_passes_sent_and_states_through(tmp_path): db = SeismoDb(tmp_path / "s.db") audit = DbAudit(lambda: db) cid = audit.begin(**ROW) audit.finish(cid, outcome="skipped", verified="not_checked", ack=None, error="already idle", sent=0, states={"before": "idle", "seen": ["idle"], "after": None}) row = db.query_device_commands()[0] assert row["sent"] == 0 and row["states"]["before"] == "idle" # The device_commands table as Task 3 created it: no `sent`, no `states`. _TASK3_TABLE = """ CREATE TABLE device_commands ( id TEXT PRIMARY KEY, ts TEXT NOT NULL, finished_ts TEXT, serial TEXT NOT NULL, series INTEGER NOT NULL, address TEXT NOT NULL, action TEXT NOT NULL, tier TEXT NOT NULL, request TEXT, ack TEXT, verified TEXT, outcome TEXT NOT NULL, error TEXT, caller TEXT ); INSERT INTO device_commands (id, ts, serial, series, address, action, tier, outcome) VALUES ('old', '2026-10-22T00:00:00+00:00', 'UM20147', 4, 'tcp:x', 'stop monitoring', 'change', 'pending'); """ def test_an_existing_task3_table_gains_sent_and_states(tmp_path): import sqlite3 path = tmp_path / "s.db" with sqlite3.connect(path) as conn: conn.executescript(_TASK3_TABLE) db = SeismoDb(path) with sqlite3.connect(path) as conn: cols = {r[1] for r in conn.execute("PRAGMA table_info(device_commands)")} assert {"sent", "states"} <= cols old = db.query_device_commands()[0] assert (old["id"], old["sent"], old["states"]) == ("old", None, None) db.finish_device_command("old", outcome="failed", verified="not_checked", ack=None, error="x", sent=0, states={"before": "idle", "seen": ["idle"], "after": None}) assert db.query_device_commands()[0]["sent"] == 0 SeismoDb(path) # a second open is a no-op