Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
42 lines
1.8 KiB
Python
42 lines
1.8 KiB
Python
"""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")
|