from api.routers import policy


async def test_policy_router_reads_and_updates_policy(client, tmp_path, monkeypatch) -> None:
    monkeypatch.setattr(policy, "POLICY_DIR", tmp_path)
    (tmp_path / "example.md").write_text("old content")
    read_response = await client.get("/policy/example")
    update_response = await client.put("/policy/example", json={"content": "new content"})

    assert read_response.status_code == 200
    assert read_response.json() == {"name": "example", "content": "old content"}
    assert update_response.status_code == 200
    assert update_response.json() == {"name": "example", "content": "new content"}
    assert (tmp_path / "example.md").read_text() == "new content"


async def test_policy_router_rejects_invalid_policy_name(client, tmp_path, monkeypatch) -> None:
    monkeypatch.setattr(policy, "POLICY_DIR", tmp_path)
    response = await client.get("/policy/BadName")

    assert response.status_code == 400
