Files
dxdesigner-part-replacement/tests/part_replace/test_cli.py
T

1255 lines
45 KiB
Python

from __future__ import annotations
from dataclasses import replace
from decimal import Decimal
import json
import os
from pathlib import Path
import subprocess
import sys
import pytest
import dx_productivity.part_replace.controller as controller_module
from dx_productivity.part_replace.certification import CertificationStore, interface_fingerprint
from dx_productivity.part_replace.cli import main
from dx_productivity.part_replace.controller import BatchController
from dx_productivity.part_replace.models import (
ComponentSnapshot,
DesignSnapshot,
ExternalChecks,
MappingRule,
PinSnapshot,
UnitSnapshot,
)
from dx_productivity.part_replace.protocol import (
atomic_write_text,
load_status,
snapshot_fingerprint,
write_snapshot,
)
from dx_productivity.part_replace.verification import verify_replacement
ROOT = Path(__file__).resolve().parents[2]
PROFILE = ROOT / "tests" / "fixtures" / "part_replace" / "compatibility-profile-v1.json"
def _project_inputs(tmp_path: Path) -> tuple[Path, Path, Path]:
project_root = tmp_path / "project"
project_root.mkdir()
marker = project_root / "demo.prj"
marker.write_text("demo\n", encoding="utf-8")
mapping = tmp_path / "mapping.csv"
mapping.write_text("old_part_number,new_part_number\nOLD-1,NEW-1\n", encoding="utf-8")
projects = tmp_path / "projects.txt"
projects.write_text(str(marker.resolve()) + "\n", encoding="utf-8")
return marker, mapping, projects
def _snapshot(project_id: str, marker: Path, part: str, fingerprint: str, *, net: str | None = "N1") -> DesignSnapshot:
return DesignSnapshot(
format_version=2,
project_id=project_id,
project_path=str(marker.resolve()),
fingerprint=fingerprint,
components=(
ComponentSnapshot(
logical_key="/MAIN|U1",
refdes="U1",
part_number=part,
units=(
UnitSnapshot(
"A", "/MAIN", "LIB:NEW" if part == "NEW-1" else "LIB:OLD",
Decimal("0.5"), Decimal("1.5"),
Decimal("1"), Decimal("2"), 0, False,
(PinSnapshot("1", "P", Decimal("1"), Decimal("2"), net),),
),
),
),
),
)
def _certify(path: Path, before: DesignSnapshot) -> None:
rule = MappingRule("OLD-1", "NEW-1")
after = replace(before, fingerprint="after", components=(_snapshot(before.project_id, Path(before.project_path), "NEW-1", "after").components[0],))
result = verify_replacement(before, after, rule, ("/MAIN|U1",))
CertificationStore(
path,
before=before,
after=after,
verification=result,
evidence_paths=("before", "after", "packager", "erc", "netlist"),
).record(
rule,
interface_fingerprint(after, ("/MAIN|U1",)),
"lib-r42",
ExternalChecks(True, True, True),
)
def _bridge_snapshot(snapshot: DesignSnapshot) -> DesignSnapshot:
return replace(snapshot, fingerprint=snapshot_fingerprint(snapshot))
def _write_correlated_status(
path: Path,
*,
command: dict[str, object],
state: str,
snapshot: Path,
) -> None:
atomic_write_text(
path,
json.dumps(
{
"command_id": command["command_id"],
"details": [
["action", command["action"]],
["expected_count", str(command["expected_count"])],
["job_id", command["job_id"]],
],
"error_code": None,
"error_message": None,
"format_version": 1,
"project_id": command["project_id"],
"snapshot_path": str(snapshot.resolve()),
"state": state,
},
sort_keys=True,
separators=(",", ":"),
)
+ "\n",
)
def _publish_active_command_snapshot(
run: Path,
project_id: str,
snapshot: DesignSnapshot,
state: str,
*,
status_action: str | None = None,
) -> tuple[dict[str, object], DesignSnapshot, Path]:
mailbox = run / "mailbox" / project_id
command = json.loads((mailbox / "command.json").read_text(encoding="utf-8"))
observed = _bridge_snapshot(snapshot)
snapshot_path = mailbox / "snapshot.json"
write_snapshot(snapshot_path, observed)
status_command = dict(command)
if status_action is not None:
status_command["action"] = status_action
_write_correlated_status(
mailbox / "status.json",
command=status_command,
state=state,
snapshot=snapshot_path,
)
return command, observed, snapshot_path
def _publish_inventory(
run: Path,
project_id: str,
snapshot: DesignSnapshot,
) -> DesignSnapshot:
BatchController.open(run).prepare_inventory(project_id)
_, observed, _ = _publish_active_command_snapshot(
run,
project_id,
snapshot,
"inventory_exported",
)
return observed
def _ready_for_apply_cli(
run: Path,
project_id: str,
before: DesignSnapshot,
capsys,
) -> None:
assert main(["--json", "prepare-close", str(run), project_id]) == 4
capsys.readouterr()
_publish_active_command_snapshot(run, project_id, before, "project_closed")
assert main(["--json", "confirm-close", str(run), project_id]) == 0
capsys.readouterr()
assert main(["--json", "prepare-open", str(run), project_id]) == 4
capsys.readouterr()
_publish_active_command_snapshot(run, project_id, before, "project_opened")
assert main(["--json", "confirm-open", str(run), project_id]) == 0
capsys.readouterr()
def _write_manual_action_status(
path: Path,
*,
command: dict[str, object],
) -> None:
atomic_write_text(
path,
json.dumps(
{
"command_id": command["command_id"],
"details": [
["action", "replace_part"],
["expected_count", str(command["expected_count"])],
["job_id", command["job_id"]],
["instruction", "Complete the native Replace Part dialog"],
["manual_route", "manual_native_dialog"],
["new_part_number", command["new_part_number"]],
["old_part_number", command["old_part_number"]],
],
"error_code": None,
"error_message": None,
"format_version": 1,
"project_id": command["project_id"],
"snapshot_path": None,
"state": "manual_action_required",
},
sort_keys=True,
separators=(",", ":"),
)
+ "\n",
)
def _durable_intent(run: Path, event_name: str) -> dict[str, object]:
events = [
json.loads(line)
for line in (run / "events.jsonl").read_text(encoding="utf-8").splitlines()
]
return next(event["payload"]["command"] for event in events if event["event"] == event_name)
def test_discover_json_is_machine_readable_and_does_not_create_a_job(tmp_path: Path, capsys) -> None:
# Catches JSON mode that emits prose or discovery that silently mutates batch state.
marker, _, _ = _project_inputs(tmp_path)
code = main(["--json", "discover", str(tmp_path), "--suffix", ".prj"])
payload = json.loads(capsys.readouterr().out)
assert code == 0
assert payload == {"projects": [str(marker.resolve())]}
assert not (tmp_path / "run").exists()
def test_invalid_create_returns_exit_2_with_stable_json_error(tmp_path: Path, capsys) -> None:
# Catches invalid input escaping as a traceback, exit 1, or non-machine-readable output.
marker, mapping, projects = _project_inputs(tmp_path)
mapping.write_text("old_part_number,new_part_number\nOLD-1,OLD-1\n", encoding="utf-8")
code = main([
"--json", "create", str(tmp_path / "run"), "--mapping", str(mapping),
"--projects", str(projects), "--certifications", str(tmp_path / "certifications.json"),
"--suffix", marker.suffix,
])
payload = json.loads(capsys.readouterr().out)
assert code == 2
assert payload["error"]["code"] == "equal_rule"
def test_prepare_returns_manual_exit_4_and_exact_command_json(tmp_path: Path, capsys) -> None:
# Catches treating a prepared native dialog as completed unattended work.
marker, mapping, projects = _project_inputs(tmp_path)
run = tmp_path / "run"
certifications = tmp_path / "certifications.json"
assert main([
"--json", "create", str(run), "--mapping", str(mapping), "--projects", str(projects),
"--certifications", str(certifications), "--suffix", ".prj", "--library-identity", "lib-r42",
]) == 0
created = json.loads(capsys.readouterr().out)
project_id = created["projects"][0]["project_id"]
before = _snapshot(project_id, marker, "OLD-1", "inventory")
before = _publish_inventory(run, project_id, before)
_certify(certifications, before)
assert main(["--json", "ingest", str(run), project_id]) == 0
capsys.readouterr()
_ready_for_apply_cli(run, project_id, before, capsys)
code = main(["--json", "prepare", str(run), project_id, "0"])
payload = json.loads(capsys.readouterr().out)
assert code == 4
assert payload["action"] == "replace_part"
assert payload["expected_logical_keys"] == ["/MAIN|U1"]
def test_prepare_inventory_returns_the_complete_bridge_command_json(
tmp_path: Path,
capsys,
) -> None:
# Catches CLI output omitting a protocol field that exists in the mailbox command.
marker, mapping, projects = _project_inputs(tmp_path)
run = tmp_path / "run"
assert main(
[
"--json",
"create",
str(run),
"--mapping",
str(mapping),
"--projects",
str(projects),
"--certifications",
str(tmp_path / "certifications.json"),
"--suffix",
marker.suffix,
]
) == 0
created = json.loads(capsys.readouterr().out)
project_id = created["projects"][0]["project_id"]
code = main(["--json", "prepare-inventory", str(run), project_id])
payload = json.loads(capsys.readouterr().out)
assert code == 0
assert set(payload) == {
"action",
"command_id",
"expected_count",
"expected_fingerprint",
"expected_logical_keys",
"format_version",
"job_id",
"new_part_number",
"old_part_number",
"project_id",
"scope",
}
assert payload["format_version"] == 1
assert payload["action"] == "inventory"
assert payload["project_id"] == project_id
assert payload["scope"] == "full_project"
assert payload["expected_count"] == 0
assert payload["expected_logical_keys"] == []
assert payload["expected_fingerprint"] == ""
assert payload["old_part_number"] == payload["new_part_number"] == ""
def test_operator_step_returns_one_manifest_ready_current_command(
tmp_path: Path,
capsys,
) -> None:
# Catches the public operator loop omitting a command/project binding or using exit 0 early.
marker, mapping, projects = _project_inputs(tmp_path)
run = tmp_path / "run"
assert main([
"--json", "create", str(run), "--mapping", str(mapping),
"--projects", str(projects), "--certifications",
str(tmp_path / "certifications.json"), "--suffix", marker.suffix,
]) == 0
created = json.loads(capsys.readouterr().out)
project = created["projects"][0]
code = main(["--json", "operator-step", str(run)])
payload = json.loads(capsys.readouterr().out)
assert code == 4
assert payload == {
"bridge_state": None,
"command": {
"action": "inventory",
"command_id": payload["command"]["command_id"],
"expected_count": 0,
"expected_fingerprint": "",
"expected_logical_keys": [],
"format_version": 1,
"job_id": run.resolve().name,
"new_part_number": "",
"old_part_number": "",
"project_id": project["project_id"],
"scope": "full_project",
},
"complete": False,
"consumed_action": None,
"instruction": None,
"manual_route": None,
"project": project,
"state": "inventory_pending",
}
def test_operator_step_returns_exit_zero_after_consuming_final_noop_inventory(
tmp_path: Path,
capsys,
) -> None:
# Catches a completed no-op job retaining a stale current command in the launcher manifest.
marker, mapping, projects = _project_inputs(tmp_path)
run = tmp_path / "run"
assert main([
"--json", "create", str(run), "--mapping", str(mapping),
"--projects", str(projects), "--certifications",
str(tmp_path / "certifications.json"), "--suffix", marker.suffix,
]) == 0
project_id = json.loads(capsys.readouterr().out)["projects"][0]["project_id"]
assert main(["--json", "operator-step", str(run)]) == 4
command = json.loads(capsys.readouterr().out)["command"]
snapshot = _bridge_snapshot(_snapshot(project_id, marker, "KEEP-1", "noop"))
mailbox = run / "mailbox" / project_id
write_snapshot(mailbox / "snapshot.json", snapshot)
_write_correlated_status(
mailbox / "status.json",
command=command,
state="inventory_exported",
snapshot=mailbox / "snapshot.json",
)
code = main(["--json", "operator-step", str(run)])
assert code == 0
assert json.loads(capsys.readouterr().out) == {
"bridge_state": None,
"command": None,
"complete": True,
"consumed_action": "inventory",
"instruction": None,
"manual_route": None,
"project": None,
"state": None,
}
def test_cli_runs_correlated_close_open_save_and_external_checks(
tmp_path: Path,
capsys,
) -> None:
# Catches an operator command bypassing its prepared mailbox evidence or accepting caller check flags.
marker, mapping, projects = _project_inputs(tmp_path)
run = tmp_path / "run"
certifications = tmp_path / "certifications.json"
assert main(
[
"--json",
"create",
str(run),
"--mapping",
str(mapping),
"--projects",
str(projects),
"--certifications",
str(certifications),
"--suffix",
marker.suffix,
"--library-identity",
"lib-r42",
]
) == 0
created = json.loads(capsys.readouterr().out)
project_id = created["projects"][0]["project_id"]
before = _publish_inventory(
run,
project_id,
_snapshot(project_id, marker, "OLD-1", "inventory"),
)
_certify(certifications, before)
assert main(["--json", "ingest", str(run), project_id]) == 0
capsys.readouterr()
assert main(["--json", "prepare-close", str(run), project_id]) == 4
close_command = json.loads(capsys.readouterr().out)
assert close_command["action"] == "close_project"
_publish_active_command_snapshot(run, project_id, before, "project_closed")
assert main(["--json", "confirm-close", str(run), project_id]) == 0
assert json.loads(capsys.readouterr().out) == {
"project_id": project_id,
"state": "apply_pending",
}
assert main(["--json", "prepare-open", str(run), project_id]) == 4
open_command = json.loads(capsys.readouterr().out)
assert open_command["action"] == "open_project"
_publish_active_command_snapshot(run, project_id, before, "project_opened")
assert main(["--json", "confirm-open", str(run), project_id]) == 0
assert json.loads(capsys.readouterr().out) == {
"project_id": project_id,
"state": "apply_pending",
}
assert main(["--json", "prepare", str(run), project_id, "0"]) == 4
apply_command = json.loads(capsys.readouterr().out)
after = _snapshot(project_id, marker, "NEW-1", "after")
_publish_active_command_snapshot(run, project_id, after, "applied")
assert main(["--json", "verify", str(run), project_id, "0"]) == 0
assert json.loads(capsys.readouterr().out)["passed"] is True
assert main(["--json", "prepare-save", str(run), project_id]) == 4
save_command = json.loads(capsys.readouterr().out)
assert save_command["action"] == "save"
_publish_active_command_snapshot(
run,
project_id,
after,
"saved",
status_action="replace_part",
)
assert main(["--json", "confirm-saved", str(run), project_id]) == 4
assert json.loads(capsys.readouterr().out) == {
"project_id": project_id,
"state": "awaiting_external_checks",
}
for index, check in enumerate(("packager", "erc", "netlist")):
assert main(
["--json", "prepare-check", str(run), project_id, check]
) == 4
check_command = json.loads(capsys.readouterr().out)
assert check_command["action"] == check
_publish_active_command_snapshot(
run,
project_id,
after,
"external_check_passed",
)
expected_code = 0 if index == 2 else 4
assert main(
["--json", "confirm-check", str(run), project_id, check]
) == expected_code
expected_state = (
"verified_saved" if index == 2 else "awaiting_external_checks"
)
assert json.loads(capsys.readouterr().out) == {
"project_id": project_id,
"state": expected_state,
}
def test_cli_requires_correlated_undo_before_reconcile(
tmp_path: Path,
capsys,
) -> None:
# Catches a failed native replacement being reconciled without exact Undo evidence.
marker, mapping, projects = _project_inputs(tmp_path)
run = tmp_path / "run"
certifications = tmp_path / "certifications.json"
assert main(
[
"--json",
"create",
str(run),
"--mapping",
str(mapping),
"--projects",
str(projects),
"--certifications",
str(certifications),
"--suffix",
marker.suffix,
"--library-identity",
"lib-r42",
]
) == 0
project_id = json.loads(capsys.readouterr().out)["projects"][0]["project_id"]
before = _publish_inventory(
run,
project_id,
_snapshot(project_id, marker, "OLD-1", "inventory"),
)
_certify(certifications, before)
assert main(["--json", "ingest", str(run), project_id]) == 0
capsys.readouterr()
_ready_for_apply_cli(run, project_id, before, capsys)
assert main(["--json", "prepare", str(run), project_id, "0"]) == 4
apply_command = json.loads(capsys.readouterr().out)
wrong_target = _snapshot(project_id, marker, "WRONG-1", "wrong")
_publish_active_command_snapshot(run, project_id, wrong_target, "applied")
assert main(["--json", "verify", str(run), project_id, "0"]) == 3
failed = json.loads(capsys.readouterr().out)
assert {issue["code"] for issue in failed["issues"]} == {
"uncertified_target",
"wrong_target_part_number",
}
assert main(["--json", "prepare-undo", str(run), project_id]) == 4
undo_command = json.loads(capsys.readouterr().out)
assert undo_command["action"] == "undo"
assert undo_command["command_id"] == apply_command["command_id"]
_publish_active_command_snapshot(
run,
project_id,
before,
"undo_verified",
status_action="replace_part",
)
assert main(["--json", "confirm-undo", str(run), project_id]) == 0
assert json.loads(capsys.readouterr().out) == {
"project_id": project_id,
"state": "blocked",
}
assert main(["--json", "reconcile", str(run), project_id]) == 0
assert json.loads(capsys.readouterr().out) == {
"project_id": project_id,
"state": "inventory_pending",
}
def test_cli_restores_verified_backup_after_closed_disk_mismatch(
tmp_path: Path,
capsys,
) -> None:
# Catches a changed on-disk project being trusted after close-without-save or overwritten without preservation.
marker, mapping, projects = _project_inputs(tmp_path)
run = tmp_path / "run"
certifications = tmp_path / "certifications.json"
assert main(
[
"--json",
"create",
str(run),
"--mapping",
str(mapping),
"--projects",
str(projects),
"--certifications",
str(certifications),
"--suffix",
marker.suffix,
"--library-identity",
"lib-r42",
]
) == 0
project_id = json.loads(capsys.readouterr().out)["projects"][0]["project_id"]
before = _publish_inventory(
run,
project_id,
_snapshot(project_id, marker, "OLD-1", "inventory"),
)
_certify(certifications, before)
assert main(["--json", "ingest", str(run), project_id]) == 0
capsys.readouterr()
_ready_for_apply_cli(run, project_id, before, capsys)
assert main(["--json", "prepare", str(run), project_id, "0"]) == 4
capsys.readouterr()
wrong_target = _snapshot(project_id, marker, "WRONG-1", "wrong")
_publish_active_command_snapshot(run, project_id, wrong_target, "applied")
assert main(["--json", "verify", str(run), project_id, "0"]) == 3
capsys.readouterr()
marker.write_text("failed on-disk change\n", encoding="utf-8")
assert main(["--json", "prepare-close", str(run), project_id]) == 4
close_command = json.loads(capsys.readouterr().out)
assert close_command["action"] == "close_without_save"
_publish_active_command_snapshot(
run,
project_id,
wrong_target,
"project_closed",
)
assert main(["--json", "confirm-close", str(run), project_id]) == 3
assert json.loads(capsys.readouterr().out)["error"]["code"] == (
"closed_disk_mismatch"
)
assert main(["--json", "restore", str(run), project_id]) == 0
restored = json.loads(capsys.readouterr().out)
recovery = Path(restored["recovery_path"])
assert restored["state"] == "blocked"
assert marker.read_text(encoding="utf-8") == "demo\n"
assert (recovery / marker.name).read_text(encoding="utf-8") == (
"failed on-disk change\n"
)
assert main(["--json", "reconcile", str(run), project_id]) == 0
assert json.loads(capsys.readouterr().out)["state"] == "inventory_pending"
assert main(["--json", "next", str(run)]) == 4
assert json.loads(capsys.readouterr().out)["action"] == "prepare-open"
def test_prepare_inventory_recovers_malformed_command_from_correlated_outputs_via_cli(
tmp_path: Path,
capsys,
monkeypatch: pytest.MonkeyPatch,
) -> None:
# Catches command corruption stranding a valid inventory export outside every CLI route.
marker, mapping, projects = _project_inputs(tmp_path)
run = tmp_path / "run"
assert main(
[
"--json",
"create",
str(run),
"--mapping",
str(mapping),
"--projects",
str(projects),
"--certifications",
str(tmp_path / "certifications.json"),
"--suffix",
marker.suffix,
]
) == 0
created = json.loads(capsys.readouterr().out)
project_id = created["projects"][0]["project_id"]
real_write_command = controller_module.write_command
def interrupt_after_publish(path: Path, command: object) -> None:
real_write_command(path, command)
raise RuntimeError("inventory publication interrupted")
monkeypatch.setattr(controller_module, "write_command", interrupt_after_publish)
with pytest.raises(RuntimeError, match="inventory publication interrupted"):
main(["--json", "prepare-inventory", str(run), project_id])
monkeypatch.setattr(controller_module, "write_command", real_write_command)
intended = _durable_intent(run, "inventory_command_publication_started")
mailbox = run / "mailbox" / project_id
command_path = mailbox / "command.json"
command_path.write_bytes(b"{malformed-command")
snapshot_path = mailbox / "snapshot.json"
write_snapshot(
snapshot_path,
_bridge_snapshot(_snapshot(project_id, marker, "OLD-1", "ignored")),
)
status_path = mailbox / "status.json"
_write_correlated_status(
status_path,
command=intended,
state="inventory_exported",
snapshot=snapshot_path,
)
status_bytes = status_path.read_bytes()
snapshot_bytes = snapshot_path.read_bytes()
retry_code = main(["--json", "prepare-inventory", str(run), project_id])
recovered = json.loads(capsys.readouterr().out)
ingest_code = main(["--json", "ingest", str(run), project_id])
ingested = json.loads(capsys.readouterr().out)
assert retry_code == 0
assert recovered == intended
assert json.loads(command_path.read_text(encoding="utf-8")) == intended
assert status_path.read_bytes() == status_bytes
assert snapshot_path.read_bytes() == snapshot_bytes
assert ingest_code == 0
assert ingested == {"project_id": project_id, "state": "apply_pending"}
def test_prepare_apply_recovers_malformed_command_from_correlated_outputs_via_cli(
tmp_path: Path,
capsys,
monkeypatch: pytest.MonkeyPatch,
) -> None:
# Catches command corruption stranding a completed native Apply before hard verification.
marker, mapping, projects = _project_inputs(tmp_path)
run = tmp_path / "run"
certifications = tmp_path / "certifications.json"
assert main(
[
"--json",
"create",
str(run),
"--mapping",
str(mapping),
"--projects",
str(projects),
"--certifications",
str(certifications),
"--suffix",
marker.suffix,
"--library-identity",
"lib-r42",
]
) == 0
created = json.loads(capsys.readouterr().out)
project_id = created["projects"][0]["project_id"]
before = _bridge_snapshot(_snapshot(project_id, marker, "OLD-1", "ignored"))
before = _publish_inventory(run, project_id, before)
_certify(certifications, before)
assert main(["--json", "ingest", str(run), project_id]) == 0
capsys.readouterr()
_ready_for_apply_cli(run, project_id, before, capsys)
real_write_command = controller_module.write_command
def interrupt_after_publish(path: Path, command: object) -> None:
real_write_command(path, command)
raise RuntimeError("apply publication interrupted")
monkeypatch.setattr(controller_module, "write_command", interrupt_after_publish)
with pytest.raises(RuntimeError, match="apply publication interrupted"):
main(["--json", "prepare", str(run), project_id, "0"])
monkeypatch.setattr(controller_module, "write_command", real_write_command)
intended = _durable_intent(run, "apply_command_publication_started")
mailbox = run / "mailbox" / project_id
command_path = mailbox / "command.json"
command_path.write_bytes(b"{malformed-command")
after_path = mailbox / "snapshot.json"
after = _bridge_snapshot(
_snapshot(project_id, marker, "NEW-1", "ignored", net="N1")
)
write_snapshot(after_path, after)
status_path = mailbox / "status.json"
_write_correlated_status(
status_path,
command=intended,
state="applied",
snapshot=after_path,
)
status_bytes = status_path.read_bytes()
snapshot_bytes = after_path.read_bytes()
retry_code = main(["--json", "prepare", str(run), project_id, "0"])
recovered = json.loads(capsys.readouterr().out)
verify_code = main(["--json", "verify", str(run), project_id, "0"])
verified = json.loads(capsys.readouterr().out)
assert retry_code == 4
assert recovered == intended
assert json.loads(command_path.read_text(encoding="utf-8")) == intended
assert status_path.read_bytes() == status_bytes
assert after_path.read_bytes() == snapshot_bytes
assert verify_code == 0
assert verified["passed"] is True
@pytest.mark.parametrize("phase", ("inventory", "apply"))
def test_uncorrelated_publication_outputs_reconcile_and_prepare_via_cli(
tmp_path: Path,
capsys,
monkeypatch: pytest.MonkeyPatch,
phase: str,
) -> None:
# Catches a reconcile cycle leaving uncertain mailbox files active for the next prepare.
marker, mapping, projects = _project_inputs(tmp_path)
run = tmp_path / "run"
certifications = tmp_path / "certifications.json"
assert main(
[
"--json",
"create",
str(run),
"--mapping",
str(mapping),
"--projects",
str(projects),
"--certifications",
str(certifications),
"--suffix",
marker.suffix,
"--library-identity",
"lib-r42",
]
) == 0
created = json.loads(capsys.readouterr().out)
project_id = created["projects"][0]["project_id"]
before = _bridge_snapshot(_snapshot(project_id, marker, "OLD-1", "ignored"))
_certify(certifications, before)
if phase == "apply":
_publish_inventory(run, project_id, before)
assert main(["--json", "ingest", str(run), project_id]) == 0
capsys.readouterr()
_ready_for_apply_cli(run, project_id, before, capsys)
command_args = (
["--json", "prepare-inventory", str(run), project_id]
if phase == "inventory"
else ["--json", "prepare", str(run), project_id, "0"]
)
real_write_command = controller_module.write_command
def interrupt_after_publish(path: Path, command: object) -> None:
real_write_command(path, command)
raise RuntimeError(f"{phase} publication interrupted")
monkeypatch.setattr(controller_module, "write_command", interrupt_after_publish)
with pytest.raises(RuntimeError, match=f"{phase} publication interrupted"):
main(command_args)
monkeypatch.setattr(controller_module, "write_command", real_write_command)
intended = _durable_intent(
run,
"inventory_command_publication_started"
if phase == "inventory"
else "apply_command_publication_started",
)
mailbox = run / "mailbox" / project_id
command_path = mailbox / "command.json"
status_path = mailbox / "status.json"
snapshot_path = mailbox / "snapshot.json"
command_bytes = b"{malformed-command"
status_bytes = b"{malformed-status"
command_path.write_bytes(command_bytes)
status_path.write_bytes(status_bytes)
write_snapshot(snapshot_path, before)
snapshot_bytes = snapshot_path.read_bytes()
failed_code = main(command_args)
failed = json.loads(capsys.readouterr().out)
next_code = main(["--json", "next", str(run)])
next_action = json.loads(capsys.readouterr().out)
if phase == "apply":
assert main(["--json", "prepare-close", str(run), project_id]) == 4
capsys.readouterr()
_publish_active_command_snapshot(
run,
project_id,
before,
"project_closed",
)
assert main(["--json", "confirm-close", str(run), project_id]) == 0
capsys.readouterr()
reconcile_code = main(["--json", "reconcile", str(run), project_id])
reconciled = json.loads(capsys.readouterr().out)
mailbox_outputs_cleared = not any(
path.exists() for path in (status_path, snapshot_path)
)
active_command = (
json.loads(command_path.read_text(encoding="utf-8"))
if command_path.exists()
else None
)
if phase == "apply":
assert main(["--json", "prepare-open", str(run), project_id]) == 4
capsys.readouterr()
_publish_active_command_snapshot(
run,
project_id,
before,
"project_opened",
)
assert main(["--json", "confirm-open", str(run), project_id]) == 0
capsys.readouterr()
fresh_inventory_code = main(
["--json", "prepare-inventory", str(run), project_id]
)
fresh_inventory = json.loads(capsys.readouterr().out)
_publish_active_command_snapshot(
run,
project_id,
before,
"inventory_exported",
)
ingest_code = main(["--json", "ingest", str(run), project_id])
ingested = json.loads(capsys.readouterr().out)
_ready_for_apply_cli(run, project_id, before, capsys)
prepare_next_code = main(["--json", "next", str(run)])
prepare_next = json.loads(capsys.readouterr().out)
fresh_prepare_code = main(
["--json", "prepare", str(run), project_id, "0"]
)
fresh_command = json.loads(capsys.readouterr().out)
quarantine = (
run
/ "history"
/ "mailbox"
/ project_id
/ str(intended["command_id"])
/ "uncertain"
)
events = [
json.loads(line)
for line in (run / "events.jsonl").read_text(encoding="utf-8").splitlines()
]
quarantine_events = [
event for event in events if event["event"] == "mailbox_evidence_quarantined"
]
assert failed_code == 3
assert failed["error"]["code"] == "mailbox_reconcile_required"
assert next_code == 4
assert next_action["action"] == (
"prepare-close" if phase == "apply" else "reconcile"
)
assert reconcile_code == 0
assert reconciled == {"project_id": project_id, "state": "inventory_pending"}
assert mailbox_outputs_cleared
if phase == "apply":
assert active_command["action"] == "close_without_save"
else:
assert active_command is None
assert fresh_inventory_code == 0
assert fresh_inventory["action"] == "inventory"
assert fresh_inventory["command_id"] != intended["command_id"]
assert ingest_code == 0
assert ingested == {"project_id": project_id, "state": "apply_pending"}
assert prepare_next_code == 4
assert prepare_next["action"] == "prepare"
assert fresh_prepare_code == 4
assert fresh_command["action"] == "replace_part"
assert fresh_command["command_id"] != intended["command_id"]
assert (quarantine / "command.json").read_bytes() == command_bytes
assert (quarantine / "status.json").read_bytes() == status_bytes
assert (quarantine / "snapshot.json").read_bytes() == snapshot_bytes
assert len(quarantine_events) == 1
quarantined = quarantine_events[0]
assert quarantined["payload"]["command_id"] == intended["command_id"]
assert set(quarantined["payload"]["paths"]) == {
path.relative_to(run).as_posix() for path in quarantine.iterdir()
}
def test_valid_manual_action_status_enters_reachable_cli_reconcile_path(
tmp_path: Path,
capsys,
monkeypatch: pytest.MonkeyPatch,
) -> None:
# Catches incomplete but valid manual evidence becoming neither resumable nor reconcilable.
marker, mapping, projects = _project_inputs(tmp_path)
run = tmp_path / "run"
certifications = tmp_path / "certifications.json"
assert main(
[
"--json",
"create",
str(run),
"--mapping",
str(mapping),
"--projects",
str(projects),
"--certifications",
str(certifications),
"--suffix",
marker.suffix,
"--library-identity",
"lib-r42",
]
) == 0
created = json.loads(capsys.readouterr().out)
project_id = created["projects"][0]["project_id"]
before = _bridge_snapshot(_snapshot(project_id, marker, "OLD-1", "ignored"))
_publish_inventory(run, project_id, before)
_certify(certifications, before)
assert main(["--json", "ingest", str(run), project_id]) == 0
capsys.readouterr()
_ready_for_apply_cli(run, project_id, before, capsys)
real_write_command = controller_module.write_command
def interrupt_after_publish(path: Path, command: object) -> None:
real_write_command(path, command)
raise RuntimeError("apply publication interrupted")
monkeypatch.setattr(controller_module, "write_command", interrupt_after_publish)
with pytest.raises(RuntimeError, match="apply publication interrupted"):
main(["--json", "prepare", str(run), project_id, "0"])
monkeypatch.setattr(controller_module, "write_command", real_write_command)
intended = _durable_intent(run, "apply_command_publication_started")
mailbox = run / "mailbox" / project_id
command_path = mailbox / "command.json"
status_path = mailbox / "status.json"
malformed_command = b"{malformed-command"
command_path.write_bytes(malformed_command)
_write_manual_action_status(status_path, command=intended)
manual_status = load_status(status_path)
status_bytes = status_path.read_bytes()
failed_code = main(["--json", "prepare", str(run), project_id, "0"])
failed = json.loads(capsys.readouterr().out)
reconcile_next_code = main(["--json", "next", str(run)])
reconcile_next = json.loads(capsys.readouterr().out)
assert main(["--json", "prepare-close", str(run), project_id]) == 4
capsys.readouterr()
_publish_active_command_snapshot(
run,
project_id,
before,
"project_closed",
)
assert main(["--json", "confirm-close", str(run), project_id]) == 0
capsys.readouterr()
reconcile_code = main(["--json", "reconcile", str(run), project_id])
reconciled = json.loads(capsys.readouterr().out)
assert main(["--json", "prepare-open", str(run), project_id]) == 4
capsys.readouterr()
_publish_active_command_snapshot(
run,
project_id,
before,
"project_opened",
)
assert main(["--json", "confirm-open", str(run), project_id]) == 0
capsys.readouterr()
assert main(["--json", "prepare-inventory", str(run), project_id]) == 0
capsys.readouterr()
_publish_active_command_snapshot(
run,
project_id,
before,
"inventory_exported",
)
assert main(["--json", "ingest", str(run), project_id]) == 0
capsys.readouterr()
_ready_for_apply_cli(run, project_id, before, capsys)
prepare_next_code = main(["--json", "next", str(run)])
prepare_next = json.loads(capsys.readouterr().out)
fresh_prepare_code = main(
["--json", "prepare", str(run), project_id, "0"]
)
fresh_command = json.loads(capsys.readouterr().out)
quarantine = (
run
/ "history"
/ "mailbox"
/ project_id
/ str(intended["command_id"])
/ "uncertain"
)
assert manual_status.state == "manual_action_required"
assert manual_status.error_code is None
assert manual_status.snapshot_path is None
assert failed_code == 3
assert failed["error"]["code"] == "mailbox_reconcile_required"
assert reconcile_next_code == 4
assert reconcile_next["action"] == "prepare-close"
assert reconcile_code == 0
assert reconciled == {"project_id": project_id, "state": "inventory_pending"}
assert prepare_next_code == 4
assert prepare_next["action"] == "prepare"
assert fresh_prepare_code == 4
assert fresh_command["command_id"] != intended["command_id"]
assert (quarantine / "command.json").read_bytes() == malformed_command
assert (quarantine / "status.json").read_bytes() == status_bytes
assert not (quarantine / "snapshot.json").exists()
assert not status_path.exists()
def test_deploy_profile_cli_publishes_exact_bytes_and_reports_destination(
tmp_path: Path,
capsys,
) -> None:
# Catches the installer-facing command rewriting or publishing elsewhere.
source = tmp_path / "source profile.json"
destination = tmp_path / "runs" / "compatibility-profile.json"
source.write_bytes(PROFILE.read_bytes())
code = main(["--json", "deploy-profile", str(source), str(destination)])
assert code == 0
assert json.loads(capsys.readouterr().out) == {
"compatibility_profile": str(destination.resolve())
}
assert destination.read_bytes() == source.read_bytes()
def test_deploy_profile_cli_rejects_duplicate_keys_without_replacing_destination(
tmp_path: Path,
capsys,
) -> None:
source = tmp_path / "source.json"
destination = tmp_path / "runs" / "compatibility-profile.json"
destination.parent.mkdir()
source.write_bytes(b'{"format_version":1,"format_version":1}\n')
destination.write_bytes(b"previous-profile")
code = main(["--json", "deploy-profile", str(source), str(destination)])
payload = json.loads(capsys.readouterr().out)
assert code == 2
assert payload["error"]["code"] == "invalid_compatibility_profile"
assert destination.read_bytes() == b"previous-profile"
assert not list(destination.parent.glob(".*.tmp"))
def test_promote_profile_cli_requires_a_bound_acceptance_manifest(
tmp_path: Path,
capsys,
) -> None:
# Catches supported tooling exposing only unchecked deployment or a bare trust flag.
source = tmp_path / "probe.json"
source.write_bytes(PROFILE.read_bytes())
destination = tmp_path / "runs" / "compatibility-profile.json"
code = main(
[
"--json",
"promote-profile",
str(source),
str(tmp_path / "missing-acceptance.json"),
str(destination),
]
)
payload = json.loads(capsys.readouterr().out)
assert code == 2
assert payload["error"]["code"] == "invalid_compatibility_profile"
assert not destination.exists()
def test_failed_verify_returns_blocking_exit_3_and_issue_json(tmp_path: Path, capsys) -> None:
# Catches hard verification failures being reported as successful CLI commands.
marker, mapping, projects = _project_inputs(tmp_path)
run = tmp_path / "run"
certifications = tmp_path / "certifications.json"
main([
"--json", "create", str(run), "--mapping", str(mapping), "--projects", str(projects),
"--certifications", str(certifications), "--suffix", ".prj", "--library-identity", "lib-r42",
])
created = json.loads(capsys.readouterr().out)
project_id = created["projects"][0]["project_id"]
before = _snapshot(project_id, marker, "OLD-1", "inventory")
before = _publish_inventory(run, project_id, before)
_certify(certifications, before)
main(["--json", "ingest", str(run), project_id])
capsys.readouterr()
_ready_for_apply_cli(run, project_id, before, capsys)
main(["--json", "prepare", str(run), project_id, "0"])
capsys.readouterr()
unsafe = _snapshot(project_id, marker, "NEW-1", "after", net="CHANGED")
_publish_active_command_snapshot(run, project_id, unsafe, "applied")
code = main(["--json", "verify", str(run), project_id, "0"])
payload = json.loads(capsys.readouterr().out)
assert code == 3
assert payload["passed"] is False
assert "connectivity_changed" in {issue["code"] for issue in payload["issues"]}
@pytest.mark.parametrize("json_output", (False, True))
def test_ingest_returns_blocking_exit_3_when_impact_blocks_project(
tmp_path: Path,
capsys,
json_output: bool,
) -> None:
# Catches successful CLI status after durable impact analysis has blocked the project.
marker, mapping, projects = _project_inputs(tmp_path)
run = tmp_path / "run"
certifications = tmp_path / "certifications.json"
create_args = [
"create", str(run), "--mapping", str(mapping), "--projects", str(projects),
"--certifications", str(certifications), "--suffix", ".prj",
]
assert main(["--json", *create_args]) == 0
created = json.loads(capsys.readouterr().out)
project_id = created["projects"][0]["project_id"]
incomplete = _snapshot(project_id, marker, "OLD-1", "inventory", net=None)
_publish_inventory(run, project_id, incomplete)
ingest_args = ["ingest", str(run), project_id]
if json_output:
ingest_args.insert(0, "--json")
code = main(ingest_args)
output = capsys.readouterr().out.strip()
assert code == 3
if json_output:
payload = json.loads(output)
assert payload["state"] == "blocked"
assert payload["error"]["code"] == "impact_blocked"
else:
assert output == "impact_blocked: inventory impact contains blocking issues"
def test_package_module_exposes_same_cli_help(tmp_path: Path) -> None:
# Catches python -m dx_productivity failing to route to the reproducible CLI.
environment = os.environ.copy()
source = str(Path(__file__).parents[2] / "src")
environment["PYTHONPATH"] = source
completed = subprocess.run(
[sys.executable, "-m", "dx_productivity", "--help"],
cwd=tmp_path,
env=environment,
text=True,
capture_output=True,
check=False,
)
assert completed.returncode == 0
assert (
"{discover,create,deploy-profile,promote-profile,ingest,next,operator-step,prepare-inventory,prepare-close,"
"confirm-close,prepare-open,confirm-open,prepare-undo,confirm-undo,prepare-save,confirm-saved,"
"restore,reconcile,prepare-check,confirm-check,prepare,verify,report,status}"
) in completed.stdout