from __future__ import annotations

from typing import Any

import scripts.plan_1c_saved_state_copy as copy_plan
import scripts.smoke_1c_code_write_saved_state as code_write_smoke


def test_resolve_object_uses_name_first_object_get(monkeypatch) -> None:
    calls: list[tuple[str, dict[str, Any]]] = []

    def fake_rpc(base_url: str, method: str, payload: dict[str, Any], timeout: float) -> dict[str, Any]:
        calls.append((method, payload))
        assert method == "metadata.object.get"
        return {
            "status": "ok",
            "object": {
                "guid": "40045984-a54c-42bd-8ea8-1c10672f40ec",
                "kind": "Catalog",
                "name": "АвансовыйОтчетПрисоединенныеФайлы",
            },
        }

    monkeypatch.setattr(copy_plan, "rpc", fake_rpc)
    selector = {
        "base_id": "upo_test",
        "ref": "Catalog.АвансовыйОтчетПрисоединенныеФайлы",
        "include_storage": True,
        "table": "Config",
    }

    result = copy_plan.resolve_object("http://adapter.example", selector, "Config", 1)

    assert result["kind"] == "Catalog"
    assert result["name"] == "АвансовыйОтчетПрисоединенныеФайлы"
    assert calls == [("metadata.object.get", selector)]


def test_optional_code_write_smoke_skips_only_policy_gate() -> None:
    assert code_write_smoke.is_optional_write_gate_block(
        {"status": "blocked", "error": "needs_repository_lock"}
    )
    assert not code_write_smoke.is_optional_write_gate_block(
        {"status": "blocked", "error": "write_plan_blocked"}
    )
    assert not code_write_smoke.is_optional_write_gate_block(
        {"status": "error", "error": "needs_repository_lock"}
    )
