|
@@ -1,30 +1,50 @@
|
|
|
#!/usr/bin/env python3
|
|
#!/usr/bin/env python3
|
|
|
-"""RED contract tests for the temporary ACK-only Normal-v3 host path."""
|
|
|
|
|
|
|
+"""RED contract tests for the minimal ACK-only Normal-v3 wire path."""
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import importlib.util
|
|
import importlib.util
|
|
|
-import json
|
|
|
|
|
import sys
|
|
import sys
|
|
|
import unittest
|
|
import unittest
|
|
|
from pathlib import Path
|
|
from pathlib import Path
|
|
|
-from unittest import mock
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
|
TOOLS = ROOT / "tools"
|
|
TOOLS = ROOT / "tools"
|
|
|
sys.path.insert(0, str(TOOLS))
|
|
sys.path.insert(0, str(TOOLS))
|
|
|
|
|
|
|
|
|
|
+import prod2u_p4_normal_v3_model as model_v3 # noqa: E402
|
|
|
import prod2u_p4_protocol as protocol # noqa: E402
|
|
import prod2u_p4_protocol as protocol # noqa: E402
|
|
|
|
|
|
|
|
|
|
|
|
|
-ACK_ONLY_CAPABILITY = 0x00001F8F
|
|
|
|
|
|
|
+GOLDEN_REQUEST_HEX = {
|
|
|
|
|
+ "enable": (
|
|
|
|
|
+ "a55a0204070000000000000000222a00"
|
|
|
|
|
+ "000000000000000000000000000000000000000000000000"
|
|
|
|
|
+ "1e1e1e1e00000000d6df"
|
|
|
|
|
+ ),
|
|
|
|
|
+ "jog_x": "a55a020411000000000000000008350001000100000086c3",
|
|
|
|
|
+ "jog_y": "a55a0204110000000000000000083501000001000000473e",
|
|
|
|
|
+ "mark_x": "a55a0204110000000000000000083600010000000000477d",
|
|
|
|
|
+ "mark_y": "a55a02041100000000000000000836010000000000008680",
|
|
|
|
|
+ "open": (
|
|
|
|
|
+ "a55a0204070000000000000000222a00"
|
|
|
|
|
+ "0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a"
|
|
|
|
|
+ "46464646646464642c75"
|
|
|
|
|
+ ),
|
|
|
|
|
+ "close": (
|
|
|
|
|
+ "a55a0204070000000000000000222a00"
|
|
|
|
|
+ "0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a"
|
|
|
|
|
+ "4646464600000000684a"
|
|
|
|
|
+ ),
|
|
|
|
|
+ "disable": "a55a02040500000000000000000426000000c4ae",
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_cli():
|
|
def load_cli():
|
|
|
path = TOOLS / "prod2u_p4_cli.py"
|
|
path = TOOLS / "prod2u_p4_cli.py"
|
|
|
spec = importlib.util.spec_from_file_location(
|
|
spec = importlib.util.spec_from_file_location(
|
|
|
- "test_prod2u_p4_ack_only_host_contract_owned",
|
|
|
|
|
|
|
+ "test_prod2u_p4_minimal_ack_only_cli",
|
|
|
path,
|
|
path,
|
|
|
)
|
|
)
|
|
|
if spec is None or spec.loader is None:
|
|
if spec is None or spec.loader is None:
|
|
@@ -36,7 +56,7 @@ def load_cli():
|
|
|
|
|
|
|
|
|
|
|
|
|
class ScriptedEndpoint:
|
|
class ScriptedEndpoint:
|
|
|
- """Expose one response only after the first command write."""
|
|
|
|
|
|
|
+ """Expose scripted response bytes only after the request is written."""
|
|
|
|
|
|
|
|
def __init__(self, response: bytes) -> None:
|
|
def __init__(self, response: bytes) -> None:
|
|
|
self._response = bytearray(response)
|
|
self._response = bytearray(response)
|
|
@@ -55,99 +75,23 @@ class ScriptedEndpoint:
|
|
|
return result
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
-def build_legacy_command_result(request: bytes, ack: bytes) -> bytes:
|
|
|
|
|
- """Build a CRC-valid legacy 0x0F frame that ACK-only hosts must reject."""
|
|
|
|
|
-
|
|
|
|
|
- ack_payload = ack[protocol.HEADER_LEN : -protocol.CRC_LEN]
|
|
|
|
|
- payload = (1).to_bytes(4, "little")
|
|
|
|
|
- payload += bytes((0x02, request[4]))
|
|
|
|
|
- payload += int.from_bytes(request[-2:], "little").to_bytes(2, "little")
|
|
|
|
|
- payload += int.from_bytes(ack_payload, "little").to_bytes(8, "little")
|
|
|
|
|
- payload += int.from_bytes(ack_payload[4:6], "little").to_bytes(2, "little")
|
|
|
|
|
|
|
+def legacy_temperature_request() -> bytes:
|
|
|
return protocol.encode_frame(
|
|
return protocol.encode_frame(
|
|
|
- frame_type=0x0F,
|
|
|
|
|
|
|
+ frame_type=protocol.FrameType.HOST_CMD,
|
|
|
seq=0,
|
|
seq=0,
|
|
|
timestamp_tick=0,
|
|
timestamp_tick=0,
|
|
|
- payload=payload,
|
|
|
|
|
|
|
+ payload=b"\x29\x00\x00\x00",
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
-class Prod2UP4AckOnlyHostContractTests(unittest.TestCase):
|
|
|
|
|
- def test_capability_clears_trace_bit_and_boot_wire_uses_1f8f(self) -> None:
|
|
|
|
|
- self.assertEqual(protocol.CAPABILITY_FLAGS, ACK_ONLY_CAPABILITY)
|
|
|
|
|
- self.assertEqual(protocol.CAPABILITY_FLAGS & (1 << 4), 0)
|
|
|
|
|
-
|
|
|
|
|
- boot = protocol.build_boot_build_id(build_id=0x6A5DB6D3)
|
|
|
|
|
- self.assertEqual(boot[18:22], bytes.fromhex("8F 1F 00 00"))
|
|
|
|
|
-
|
|
|
|
|
- def test_host_contract_requires_boot_without_epoch(self) -> None:
|
|
|
|
|
- contract = json.loads(
|
|
|
|
|
- (ROOT / "config" / "prod2u_p4_normal_host_contract_v4.json").read_text(
|
|
|
|
|
- encoding="utf-8"
|
|
|
|
|
- )
|
|
|
|
|
- )
|
|
|
|
|
- self.assertEqual(contract["boot_build_id"]["capability"], ACK_ONLY_CAPABILITY)
|
|
|
|
|
- self.assertEqual(contract["boot_build_id"]["capability_wire_le"], "8F 1F 00 00")
|
|
|
|
|
- self.assertFalse(contract["capability_registry"]["TRACE_EVENT_V1"]["enabled"])
|
|
|
|
|
-
|
|
|
|
|
- startup = contract["boot_build_id"].get("startup_order")
|
|
|
|
|
- if startup is None:
|
|
|
|
|
- startup = contract["boot_build_id"].get("startup_epoch_order")
|
|
|
|
|
- self.assertIsNotNone(startup, "host contract must define startup ordering")
|
|
|
|
|
- self.assertEqual(startup["required_prefix"], ["BOOT_BUILD_ID"])
|
|
|
|
|
- self.assertEqual(startup.get("epoch_count", 0), 0)
|
|
|
|
|
-
|
|
|
|
|
- def test_session_never_constructs_trace_host_policy(self) -> None:
|
|
|
|
|
- cli = load_cli()
|
|
|
|
|
- endpoint = ScriptedEndpoint(b"")
|
|
|
|
|
-
|
|
|
|
|
- with mock.patch.object(
|
|
|
|
|
- cli,
|
|
|
|
|
- "TraceHostPolicy",
|
|
|
|
|
- create=True,
|
|
|
|
|
- side_effect=AssertionError("ACK-only CLI must not instantiate TraceHostPolicy"),
|
|
|
|
|
- ) as constructor:
|
|
|
|
|
- session = cli.NormalSession(endpoint)
|
|
|
|
|
-
|
|
|
|
|
- constructor.assert_not_called()
|
|
|
|
|
- self.assertFalse(hasattr(session, "_trace_policy"))
|
|
|
|
|
-
|
|
|
|
|
- def test_startup_accepts_boot_then_host_ack_without_epoch(self) -> None:
|
|
|
|
|
- cli = load_cli()
|
|
|
|
|
- request = protocol.build_host_command(command=protocol.CommandId.GET_STATUS)
|
|
|
|
|
- boot = protocol.build_boot_build_id(
|
|
|
|
|
- build_id=0x6A5DB6D3,
|
|
|
|
|
- capability_flags=ACK_ONLY_CAPABILITY,
|
|
|
|
|
- )
|
|
|
|
|
- ack = protocol.build_host_ack(
|
|
|
|
|
- ok=True,
|
|
|
|
|
- command=protocol.CommandId.GET_STATUS,
|
|
|
|
|
- status=protocol.StatusCode.OK,
|
|
|
|
|
- state_hash16=0x1234,
|
|
|
|
|
- )
|
|
|
|
|
- endpoint = ScriptedEndpoint(boot + ack)
|
|
|
|
|
- session = cli.NormalSession(
|
|
|
|
|
- endpoint,
|
|
|
|
|
- timeout_seconds=0.05,
|
|
|
|
|
- expected_build_id=0x6A5DB6D3,
|
|
|
|
|
- expected_capability_flags=ACK_ONLY_CAPABILITY,
|
|
|
|
|
- )
|
|
|
|
|
-
|
|
|
|
|
- result = session.command(
|
|
|
|
|
- request,
|
|
|
|
|
- expected_command=protocol.CommandId.GET_STATUS,
|
|
|
|
|
- allow_boot_preamble=True,
|
|
|
|
|
- )
|
|
|
|
|
-
|
|
|
|
|
|
|
+class Prod2UP4MinimalAckOnlyContractTests(unittest.TestCase):
|
|
|
|
|
+ def test_existing_input_request_hex_vectors_remain_byte_exact(self) -> None:
|
|
|
self.assertEqual(
|
|
self.assertEqual(
|
|
|
- [frame.frame_type for frame in result.frames],
|
|
|
|
|
- [protocol.FrameType.BOOT_BUILD_ID, protocol.FrameType.HOST_ACK],
|
|
|
|
|
|
|
+ {name: value.hex() for name, value in model_v3.GOLDEN_VECTORS.items()},
|
|
|
|
|
+ GOLDEN_REQUEST_HEX,
|
|
|
)
|
|
)
|
|
|
- self.assertEqual(session.startup_boot_identity.build_id, 0x6A5DB6D3)
|
|
|
|
|
- self.assertIsNone(getattr(session, "startup_epoch", None))
|
|
|
|
|
- self.assertEqual(endpoint.writes, [request])
|
|
|
|
|
|
|
|
|
|
- def test_transaction_accepts_host_ack_without_command_result_trace(self) -> None:
|
|
|
|
|
|
|
+ def test_ordinary_command_accepts_ack_as_first_and_only_response(self) -> None:
|
|
|
cli = load_cli()
|
|
cli = load_cli()
|
|
|
request = protocol.build_host_command(command=protocol.CommandId.GET_STATUS)
|
|
request = protocol.build_host_command(command=protocol.CommandId.GET_STATUS)
|
|
|
ack = protocol.build_host_ack(
|
|
ack = protocol.build_host_ack(
|
|
@@ -162,11 +106,16 @@ class Prod2UP4AckOnlyHostContractTests(unittest.TestCase):
|
|
|
result = session.command(request, expected_command=protocol.CommandId.GET_STATUS)
|
|
result = session.command(request, expected_command=protocol.CommandId.GET_STATUS)
|
|
|
|
|
|
|
|
self.assertEqual(result.raw_response, ack)
|
|
self.assertEqual(result.raw_response, ack)
|
|
|
- self.assertEqual([frame.frame_type for frame in result.frames], [protocol.FrameType.HOST_ACK])
|
|
|
|
|
|
|
+ self.assertEqual(
|
|
|
|
|
+ [frame.frame_type for frame in result.frames],
|
|
|
|
|
+ [protocol.FrameType.HOST_ACK],
|
|
|
|
|
+ )
|
|
|
self.assertEqual(result.ack.state_hash16, 0xBEEF)
|
|
self.assertEqual(result.ack.state_hash16, 0xBEEF)
|
|
|
self.assertEqual(endpoint.writes, [request])
|
|
self.assertEqual(endpoint.writes, [request])
|
|
|
|
|
+ self.assertFalse(hasattr(session, "_trace_policy"))
|
|
|
|
|
+ self.assertFalse(hasattr(result, "command_result_trace"))
|
|
|
|
|
|
|
|
- def test_motor_status_accepts_host_ack_then_optional_followup(self) -> None:
|
|
|
|
|
|
|
+ def test_motor_status_response_is_ack_then_one_ten_byte_status(self) -> None:
|
|
|
cli = load_cli()
|
|
cli = load_cli()
|
|
|
request = protocol.build_get_motor_status()
|
|
request = protocol.build_get_motor_status()
|
|
|
ack = protocol.build_host_ack(
|
|
ack = protocol.build_host_ack(
|
|
@@ -176,7 +125,7 @@ class Prod2UP4AckOnlyHostContractTests(unittest.TestCase):
|
|
|
response_type=protocol.FrameType.MOTION_STATUS,
|
|
response_type=protocol.FrameType.MOTION_STATUS,
|
|
|
result_flags=0x08,
|
|
result_flags=0x08,
|
|
|
)
|
|
)
|
|
|
- status = protocol.build_motion_status(
|
|
|
|
|
|
|
+ status_wire = protocol.build_motion_status(
|
|
|
flags=0,
|
|
flags=0,
|
|
|
position_valid_mask=0,
|
|
position_valid_mask=0,
|
|
|
busy_mask=0,
|
|
busy_mask=0,
|
|
@@ -184,42 +133,75 @@ class Prod2UP4AckOnlyHostContractTests(unittest.TestCase):
|
|
|
target_xyz=(0xFF, 0xFF, 0xFF),
|
|
target_xyz=(0xFF, 0xFF, 0xFF),
|
|
|
fault_mask=0,
|
|
fault_mask=0,
|
|
|
)
|
|
)
|
|
|
- endpoint = ScriptedEndpoint(ack + status)
|
|
|
|
|
|
|
+ endpoint = ScriptedEndpoint(ack + status_wire)
|
|
|
session = cli.NormalSession(endpoint, timeout_seconds=0.05)
|
|
session = cli.NormalSession(endpoint, timeout_seconds=0.05)
|
|
|
|
|
|
|
|
result = session.motor_status()
|
|
result = session.motor_status()
|
|
|
|
|
+ frames, remainder = protocol.decode_stream(result.raw_response)
|
|
|
|
|
|
|
|
- self.assertEqual(result.status.current_xyz, (0xFF, 0xFF, 0xFF))
|
|
|
|
|
|
|
+ self.assertEqual(remainder, b"")
|
|
|
self.assertEqual(
|
|
self.assertEqual(
|
|
|
- [frame.frame_type for frame in result.command.frames],
|
|
|
|
|
|
|
+ [frame.frame_type for frame in frames],
|
|
|
[protocol.FrameType.HOST_ACK, protocol.FrameType.MOTION_STATUS],
|
|
[protocol.FrameType.HOST_ACK, protocol.FrameType.MOTION_STATUS],
|
|
|
)
|
|
)
|
|
|
|
|
+ self.assertEqual(len(frames[1].payload), protocol.MOTION_STATUS_PAYLOAD_LEN)
|
|
|
|
|
+ self.assertEqual(result.status.current_xyz, (0xFF, 0xFF, 0xFF))
|
|
|
self.assertEqual(endpoint.writes, [request])
|
|
self.assertEqual(endpoint.writes, [request])
|
|
|
|
|
|
|
|
- def test_legacy_0x0f_frame_is_unexpected_not_a_compatibility_prefix(self) -> None:
|
|
|
|
|
- cli = load_cli()
|
|
|
|
|
- request = protocol.build_host_command(command=protocol.CommandId.GET_STATUS)
|
|
|
|
|
- ack = protocol.build_host_ack(
|
|
|
|
|
- ok=True,
|
|
|
|
|
- command=protocol.CommandId.GET_STATUS,
|
|
|
|
|
- status=protocol.StatusCode.OK,
|
|
|
|
|
- state_hash16=0x1234,
|
|
|
|
|
|
|
+ def test_mark_closed_is_unsupported_and_has_no_model_side_effect(self) -> None:
|
|
|
|
|
+ subject = model_v3.NormalV3Model()
|
|
|
|
|
+ before = subject.state_fingerprint()
|
|
|
|
|
+
|
|
|
|
|
+ result = subject.apply_frame(bytes.fromhex(GOLDEN_REQUEST_HEX["mark_x"]))
|
|
|
|
|
+
|
|
|
|
|
+ self.assertFalse(result.accepted)
|
|
|
|
|
+ self.assertEqual(result.command, 0x36)
|
|
|
|
|
+ self.assertEqual(result.status, protocol.StatusCode.UNSUPPORTED_IN_PROFILE)
|
|
|
|
|
+ self.assertEqual(subject.state_fingerprint(), before)
|
|
|
|
|
+
|
|
|
|
|
+ def test_legacy_temperature_request_is_unsupported_and_has_no_side_effect(self) -> None:
|
|
|
|
|
+ subject = model_v3.NormalV3Model()
|
|
|
|
|
+ before = subject.state_fingerprint()
|
|
|
|
|
+
|
|
|
|
|
+ result = subject.apply_frame(legacy_temperature_request())
|
|
|
|
|
+
|
|
|
|
|
+ self.assertFalse(result.accepted)
|
|
|
|
|
+ self.assertEqual(result.command, 0x29)
|
|
|
|
|
+ self.assertEqual(result.status, protocol.StatusCode.UNSUPPORTED_IN_PROFILE)
|
|
|
|
|
+ self.assertEqual(subject.state_fingerprint(), before)
|
|
|
|
|
+
|
|
|
|
|
+ def test_jog_step_field_keeps_zero_invalid_and_65535_valid(self) -> None:
|
|
|
|
|
+ with self.assertRaisesRegex(ValueError, "steps must be from 1 to 65535"):
|
|
|
|
|
+ protocol.build_motor_jog(axis=0, raw_dir=1, speed_code=3, steps=0)
|
|
|
|
|
+
|
|
|
|
|
+ maximum = protocol.build_motor_jog(
|
|
|
|
|
+ axis=1,
|
|
|
|
|
+ raw_dir=1,
|
|
|
|
|
+ speed_code=3,
|
|
|
|
|
+ steps=65535,
|
|
|
)
|
|
)
|
|
|
- legacy_trace = build_legacy_command_result(request, ack)
|
|
|
|
|
- endpoint = ScriptedEndpoint(legacy_trace + ack)
|
|
|
|
|
- session = cli.NormalSession(endpoint, timeout_seconds=0.05)
|
|
|
|
|
|
|
+ decoded = protocol.decode_motion_command(protocol.decode_frame(maximum))
|
|
|
|
|
|
|
|
- with self.assertRaisesRegex(
|
|
|
|
|
- RuntimeError,
|
|
|
|
|
- r"(?i)(0x0f|trace_event|trace frame|unsupported|unexpected)",
|
|
|
|
|
- ):
|
|
|
|
|
- session.command(request, expected_command=protocol.CommandId.GET_STATUS)
|
|
|
|
|
|
|
+ self.assertEqual(decoded.steps, 65535)
|
|
|
|
|
+ self.assertEqual(protocol.decode_frame(maximum).payload[4:6], b"\xFF\xFF")
|
|
|
|
|
|
|
|
- self.assertEqual(
|
|
|
|
|
- endpoint.writes,
|
|
|
|
|
- [request, protocol.build_host_command(command=protocol.CommandId.DISABLE)],
|
|
|
|
|
|
|
+ def test_partial_ack_preserves_solenoid_skip_bit_and_actual_state_hash(self) -> None:
|
|
|
|
|
+ wire = protocol.build_host_ack(
|
|
|
|
|
+ ok=True,
|
|
|
|
|
+ command=protocol.CommandId.SET_OUTPUT_SNAPSHOT,
|
|
|
|
|
+ status=protocol.StatusCode.PARTIAL,
|
|
|
|
|
+ detail=0x41,
|
|
|
|
|
+ state_hash16=0xE177,
|
|
|
|
|
+ result_flags=0x0D,
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+ ack = protocol.decode_host_ack(protocol.decode_frame(wire))
|
|
|
|
|
+
|
|
|
|
|
+ self.assertEqual(ack.detail & 0x40, 0x40)
|
|
|
|
|
+ self.assertEqual(ack.detail & 0x07, 0x01)
|
|
|
|
|
+ self.assertEqual(ack.state_hash16, 0xE177)
|
|
|
|
|
+ self.assertEqual(ack.result_flags, 0x0D)
|
|
|
|
|
+
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if __name__ == "__main__":
|
|
|
unittest.main()
|
|
unittest.main()
|