from __future__ import annotations from copy import deepcopy import pytest from dx_productivity.part_replace.command_schema import command_data, command_from_data from dx_productivity.part_replace.controller import ControllerError from dx_productivity.part_replace.models import Scope from dx_productivity.part_replace.protocol import BridgeCommand def _command_document() -> dict[str, object]: return { "action": "replace_part", "command_id": "a" * 32, "expected_count": 2, "expected_fingerprint": "f" * 64, "expected_logical_keys": ["/MAIN|R1", "/MAIN|R2"], "format_version": 1, "job_id": "job-1", "new_part_number": "NEW-1", "old_part_number": "OLD-1", "project_id": "project-1", "scope": "full_project", } def test_command_codec_round_trips_the_exact_durable_schema() -> None: expected = _command_document() command = BridgeCommand( format_version=1, command_id="a" * 32, action="replace_part", job_id="job-1", project_id="project-1", scope=Scope.FULL_PROJECT, old_part_number="OLD-1", new_part_number="NEW-1", expected_logical_keys=("/MAIN|R1", "/MAIN|R2"), expected_count=2, expected_fingerprint="f" * 64, ) assert command_data(command) == expected assert command_from_data(expected) == command @pytest.mark.parametrize( ("field", "value", "message"), ( ("unexpected", 1, "fields"), ("format_version", True, "values"), ("scope", "selection", "scope"), ("command_id", "G" * 32, "values"), ), ) def test_command_codec_rejects_noncanonical_durable_documents( field: str, value: object, message: str, ) -> None: document = deepcopy(_command_document()) document[field] = value with pytest.raises(ControllerError, match=message) as captured: command_from_data(document) assert captured.value.code == "corrupt_publication"