|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import json |
6 | | -from unittest.mock import AsyncMock, patch |
| 6 | +from unittest.mock import AsyncMock, MagicMock, patch |
7 | 7 |
|
8 | 8 | import pytest |
9 | 9 |
|
@@ -341,3 +341,77 @@ async def test_close_kitchen_drains_orphaned_github_api_entries(tmp_path, monkey |
341 | 341 | data = json.loads(orphan_path.read_text()) |
342 | 342 | assert data["total_requests"] == 1 |
343 | 343 | assert not log._entries |
| 344 | + |
| 345 | + |
| 346 | +@pytest.mark.anyio |
| 347 | +async def test_open_kitchen_fail_closed_empty_content(monkeypatch, tmp_path): |
| 348 | + """open_kitchen returns fail-closed when content is empty (caught at validity gate).""" |
| 349 | + monkeypatch.chdir(tmp_path) |
| 350 | + ctx = _make_mock_ctx() |
| 351 | + ctx.gate.enabled = True |
| 352 | + ctx.gate_infrastructure_ready = True |
| 353 | + _load_result = {"valid": True, "content": "", "dispatch_feasible": True} |
| 354 | + ctx.recipes.load_and_validate.return_value = _load_result |
| 355 | + ctx.recipes.find.return_value = MagicMock(path=tmp_path / "r.yaml") |
| 356 | + ctx.config.providers = MagicMock() |
| 357 | + ctx.backend = MagicMock() |
| 358 | + ctx.backend.name = "test" |
| 359 | + with ( |
| 360 | + patch("autoskillit.server._get_ctx", return_value=ctx), |
| 361 | + patch("autoskillit.server.logger"), |
| 362 | + patch( |
| 363 | + "autoskillit.server.tools.tools_kitchen._apply_triage_gate", |
| 364 | + new=AsyncMock(return_value=_load_result), |
| 365 | + ), |
| 366 | + patch( |
| 367 | + "autoskillit.server.tools.tools_kitchen._prime_quota_cache", |
| 368 | + new_callable=AsyncMock, |
| 369 | + ), |
| 370 | + patch("autoskillit.server.tools.tools_kitchen._write_hook_config"), |
| 371 | + ): |
| 372 | + from autoskillit.server.tools.tools_kitchen import open_kitchen |
| 373 | + |
| 374 | + raw = await open_kitchen(name="test-recipe") |
| 375 | + |
| 376 | + parsed = json.loads(raw) |
| 377 | + assert parsed["success"] is False |
| 378 | + assert parsed.get("kitchen") == "failed" |
| 379 | + assert "error" in parsed |
| 380 | + assert "failed validation" in parsed["error"] |
| 381 | + |
| 382 | + |
| 383 | +@pytest.mark.anyio |
| 384 | +async def test_open_kitchen_fail_closed_missing_content(monkeypatch, tmp_path): |
| 385 | + """open_kitchen returns fail-closed when content key is missing (caught at validity gate).""" |
| 386 | + monkeypatch.chdir(tmp_path) |
| 387 | + ctx = _make_mock_ctx() |
| 388 | + ctx.gate.enabled = True |
| 389 | + ctx.gate_infrastructure_ready = True |
| 390 | + _load_result = {"valid": True, "dispatch_feasible": True} |
| 391 | + ctx.recipes.load_and_validate.return_value = _load_result |
| 392 | + ctx.recipes.find.return_value = MagicMock(path=tmp_path / "r.yaml") |
| 393 | + ctx.config.providers = MagicMock() |
| 394 | + ctx.backend = MagicMock() |
| 395 | + ctx.backend.name = "test" |
| 396 | + with ( |
| 397 | + patch("autoskillit.server._get_ctx", return_value=ctx), |
| 398 | + patch("autoskillit.server.logger"), |
| 399 | + patch( |
| 400 | + "autoskillit.server.tools.tools_kitchen._apply_triage_gate", |
| 401 | + new=AsyncMock(return_value=_load_result), |
| 402 | + ), |
| 403 | + patch( |
| 404 | + "autoskillit.server.tools.tools_kitchen._prime_quota_cache", |
| 405 | + new_callable=AsyncMock, |
| 406 | + ), |
| 407 | + patch("autoskillit.server.tools.tools_kitchen._write_hook_config"), |
| 408 | + ): |
| 409 | + from autoskillit.server.tools.tools_kitchen import open_kitchen |
| 410 | + |
| 411 | + raw = await open_kitchen(name="test-recipe") |
| 412 | + |
| 413 | + parsed = json.loads(raw) |
| 414 | + assert parsed["success"] is False |
| 415 | + assert parsed.get("kitchen") == "failed" |
| 416 | + assert "error" in parsed |
| 417 | + assert "failed validation" in parsed["error"] |
0 commit comments