fix: align tests with model validation and API error reporting

This commit is contained in:
2026-04-13 01:43:52 -04:00
parent 89abb6ecc6
commit f2cc585d72
22 changed files with 494 additions and 1698 deletions

View File

@@ -29,7 +29,7 @@ class TestMutateInterval:
async def test_unauthenticated_returns_401(self, client: httpx.AsyncClient):
resp = await client.put(
"/api/v1/deckies/decky-01/mutate-interval",
json={"mutate_interval": 60},
json={"mutate_interval": "60m"},
)
assert resp.status_code == 401
@@ -40,9 +40,9 @@ class TestMutateInterval:
resp = await client.put(
"/api/v1/deckies/decky-01/mutate-interval",
headers={"Authorization": f"Bearer {auth_token}"},
json={"mutate_interval": 60},
json={"mutate_interval": "60m"},
)
assert resp.status_code == 400
assert resp.status_code == 404
@pytest.mark.asyncio
async def test_decky_not_found(self, client: httpx.AsyncClient, auth_token: str):
@@ -52,7 +52,7 @@ class TestMutateInterval:
resp = await client.put(
"/api/v1/deckies/nonexistent/mutate-interval",
headers={"Authorization": f"Bearer {auth_token}"},
json={"mutate_interval": 60},
json={"mutate_interval": "60m"},
)
assert resp.status_code == 404
@@ -64,11 +64,14 @@ class TestMutateInterval:
resp = await client.put(
"/api/v1/deckies/decky-01/mutate-interval",
headers={"Authorization": f"Bearer {auth_token}"},
json={"mutate_interval": 120},
json={"mutate_interval": "120m"},
)
assert resp.status_code == 200
assert resp.json()["message"] == "Mutation interval updated"
mock_repo.set_state.assert_awaited_once()
saved = mock_repo.set_state.call_args[0][1]
saved_interval = saved["config"]["deckies"][0]["mutate_interval"]
assert saved_interval == 120
@pytest.mark.asyncio
async def test_null_interval_removes_mutation(self, client: httpx.AsyncClient, auth_token: str):
@@ -82,3 +85,47 @@ class TestMutateInterval:
)
assert resp.status_code == 200
mock_repo.set_state.assert_awaited_once()
@pytest.mark.asyncio
async def test_invalid_format_returns_422(self, client: httpx.AsyncClient, auth_token: str):
"""Seconds ('s') and raw integers are not accepted.
Note: The API returns 400 for structural violations (wrong type) and 422 for semantic/pattern violations.
"""
cases = [
("1s", 422),
("60", 422),
(60, 400),
(False, 400),
("1h", 422),
]
for bad, expected_status in cases:
resp = await client.put(
"/api/v1/deckies/decky-01/mutate-interval",
headers={"Authorization": f"Bearer {auth_token}"},
json={"mutate_interval": bad},
)
assert resp.status_code == expected_status, f"Expected {expected_status} for {bad!r}, got {resp.status_code}"
@pytest.mark.asyncio
async def test_duration_units_stored_as_minutes(self, client: httpx.AsyncClient, auth_token: str):
"""Each unit suffix is parsed to the correct number of minutes."""
cases = [
("2m", 2),
("1d", 1440),
("1M", 43200),
("1y", 525600),
("1Y", 525600),
]
for duration, expected_minutes in cases:
config = _config()
with patch("decnet.web.router.fleet.api_mutate_interval.repo", new_callable=AsyncMock) as mock_repo:
mock_repo.get_state.return_value = {"config": config.model_dump(), "compose_path": "c.yml"}
resp = await client.put(
"/api/v1/deckies/decky-01/mutate-interval",
headers={"Authorization": f"Bearer {auth_token}"},
json={"mutate_interval": duration},
)
assert resp.status_code == 200, f"Expected 200 for {duration!r}"
saved = mock_repo.set_state.call_args[0][1]
saved_interval = saved["config"]["deckies"][0]["mutate_interval"]
assert saved_interval == expected_minutes, f"{duration!r} → expected {expected_minutes} min, got {saved_interval}"