"""Compatibility adapter coverage for RMGR-WP-0008-T01."""

from api.services import repo_manager_dual_run as adapter


def test_rm_update_workplan_builds_governed_cli_command(monkeypatch):
    seen = {}

    def fake_run(args, *, timeout=120):
        seen["args"] = args
        return 0, '{"status":"applied","evidence":{"git_sha":"abc"}}', ""

    monkeypatch.setattr(adapter, "run_rmgr", fake_run)
    result = adapter.rm_update_workplan(
        repo_path="/repos/demo",
        workplan_id="DEMO-WP-0001",
        operation="update",
        title="Renamed",
        status="active",
        repo_slug="demo",
        correlation_id="00000000-0000-4000-8000-000000000001",
        push=True,
    )

    assert result["status"] == "applied"
    assert seen["args"][:5] == [
        "workplan",
        "update",
        "--path",
        "/repos/demo",
        "--workplan-id",
    ]
    assert "--title" in seen["args"]
    assert "--status" in seen["args"]
    assert "--push" in seen["args"]


def test_rm_update_workplan_maps_delete_to_confirmed_archive(monkeypatch):
    seen = {}

    def fake_run(args, *, timeout=120):
        seen["args"] = args
        return 0, '{"status":"applied"}', ""

    monkeypatch.setattr(adapter, "run_rmgr", fake_run)
    result = adapter.rm_update_workplan(
        repo_path="/repos/demo",
        workplan_id="DEMO-WP-0001",
        operation="archive",
        confirm_archive=True,
        push=False,
    )

    assert result["status"] == "applied"
    assert seen["args"][0:2] == ["workplan", "delete"]
    assert "--confirm" in seen["args"]
    assert "--push" not in seen["args"]


def test_rm_update_workplan_rejects_unknown_operation_without_invocation(monkeypatch):
    def fail_run(*args, **kwargs):
        raise AssertionError("rmgr must not run")

    monkeypatch.setattr(adapter, "run_rmgr", fail_run)
    result = adapter.rm_update_workplan(
        repo_path="/repos/demo",
        workplan_id="DEMO-WP-0001",
        operation="erase",
    )
    assert result["status"] == "rejected"
    assert result["error"]["code"] == "validation_error"


def test_rm_update_register_entry_builds_shared_spine_command(monkeypatch):
    seen = {}

    def fake_run(args, *, timeout=120):
        seen["args"] = args
        return 0, '{"status":"applied"}', ""

    monkeypatch.setattr(adapter, "run_rmgr", fake_run)
    result = adapter.rm_update_register_entry(
        repo_path="/repos/demo",
        kind="technical-debt",
        entry_id="TD-001",
        operation="put",
        title="Debt",
        data={"severity": "high"},
        push=False,
    )

    assert result["status"] == "applied"
    assert seen["args"][0:2] == ["register", "put"]
    assert "technical-debt" in seen["args"]
    assert '{"severity":"high"}' in seen["args"]


def test_rm_scan_sbom_builds_derived_snapshot_command(monkeypatch):
    seen = {}

    def fake_run(args, *, timeout=120):
        seen["args"] = args
        return (
            0,
            '{"schema":"repo-manager.sbom-snapshot.v1","ok":true,"entry_count":3}',
            "",
        )

    monkeypatch.setattr(adapter, "run_rmgr", fake_run)
    result = adapter.rm_scan_sbom(repo_path="/repos/demo", repo_slug="demo")

    assert result["ok"] is True
    assert result["schema"] == "repo-manager.sbom-snapshot.v1"
    assert result["exit_code"] == 0
    assert seen["args"] == ["sbom", "scan", "--path", "/repos/demo", "--slug", "demo"]
