test: scaffold isolated grpc data docker fixtures

This commit is contained in:
Joseph Doherty
2026-11-16 04:31:00 -05:00
parent 862dbc8e90
commit daea02b271
7 changed files with 747 additions and 0 deletions
@@ -0,0 +1,78 @@
"""Pure Modbus fixture tests; no sockets, Docker, or production process starts."""
from pathlib import Path
import struct
import sys
import unittest
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "device"))
from server import DeviceState, apply_control, handle_modbus_pdu
def request(function, address, value):
return bytes([function]) + struct.pack(">HH", address, value)
def words(response):
return struct.unpack(f">{response[1] // 2}H", response[2:])
class ModbusFixtureTests(unittest.TestCase):
def setUp(self):
self.state = DeviceState()
def test_counter_advances_only_by_explicit_bounded_control(self):
self.assertEqual(words(handle_modbus_pdu(self.state, 1, request(3, 0, 1))), (0,))
apply_control(self.state, {"action": "advance", "steps": 3})
self.assertEqual(words(handle_modbus_pdu(self.state, 1, request(3, 0, 1))), (3,))
self.assertEqual(words(handle_modbus_pdu(self.state, 1, request(3, 0, 1))), (3,))
with self.assertRaises(ValueError):
apply_control(self.state, {"action": "advance", "steps": 1001})
self.assertEqual(self.state.snapshot()["counter"], 3)
def test_scalar_and_array_registers_have_deterministic_readback(self):
self.assertEqual(words(handle_modbus_pdu(self.state, 1, request(3, 10, 3))), (7, 11, 13))
self.assertEqual(handle_modbus_pdu(self.state, 1, request(6, 20, 42)), request(6, 20, 42))
self.assertEqual(words(handle_modbus_pdu(self.state, 1, request(3, 20, 1))), (42,))
payload = bytes([16]) + struct.pack(">HHBHH", 30, 2, 4, 0x1234, 0x00FF)
self.assertEqual(handle_modbus_pdu(self.state, 1, payload), request(16, 30, 2))
self.assertEqual(words(handle_modbus_pdu(self.state, 1, request(3, 30, 2))), (0x1234, 0x00FF))
def test_each_committed_write_has_one_effect_and_repeated_value_is_a_new_request(self):
write = request(6, 20, 42)
handle_modbus_pdu(self.state, 1, write)
handle_modbus_pdu(self.state, 1, write)
ledger = self.state.snapshot()["effects"]
self.assertEqual([item["sequence"] for item in ledger], [1, 2])
self.assertEqual([item["values"] for item in ledger], [[42], [42]])
self.assertEqual(self.state.snapshot()["effect_total"], 2)
def test_response_loss_is_armed_for_one_write_after_effect(self):
apply_control(self.state, {"action": "drop_next_write_response"})
self.assertIsNone(handle_modbus_pdu(self.state, 1, request(6, 20, 42)))
snapshot = self.state.snapshot()
self.assertEqual(snapshot["effect_total"], 1)
self.assertEqual(snapshot["effects"][0]["values"], [42])
self.assertEqual(handle_modbus_pdu(self.state, 1, request(6, 20, 43)), request(6, 20, 43))
self.assertEqual(self.state.snapshot()["effect_total"], 2)
def test_malformed_and_out_of_range_requests_never_mutate(self):
invalid = [b"", b"\x06\x00", request(3, 0, 0), request(3, 0, 126),
request(6, 999, 1), bytes([16]) + struct.pack(">HHB", 30, 2, 2) + b"\x00\x01"]
before = self.state.snapshot()
for payload in invalid:
with self.subTest(payload=payload), self.assertRaises(ValueError):
handle_modbus_pdu(self.state, 1, payload)
self.assertEqual(self.state.snapshot(), before)
def test_control_rejects_unknown_actions_and_unbounded_input(self):
for command in ({"action": "set", "address": 20, "value": -1},
{"action": "set", "address": 999, "value": 3},
{"action": "advance", "steps": True},
{"action": "unknown"}):
with self.subTest(command=command), self.assertRaises(ValueError):
apply_control(self.state, command)
self.assertEqual(self.state.snapshot()["effect_total"], 0)
if __name__ == "__main__":
unittest.main()