Репозитории /
AiRouter /
tests/test_config_workspace.py
test_config_workspace.py
| 1 | from __future__ import annotations |
| 2 | |
| 3 | import asyncio |
| 4 | from pathlib import Path |
| 5 | |
| 6 | import pytest |
| 7 | |
| 8 | from airouter.config import LimitsConfig, load_config, sample_config |
| 9 | from airouter.workspace import ToolError, Workspace |
| 10 | |
| 11 | |
| 12 | def run(coro): |
| 13 | return asyncio.run(coro) |
| 14 | |
| 15 | |
| 16 | def test_config_project_override(tmp_path: Path): |
| 17 | (tmp_path / ".airouter.toml").write_text( |
| 18 | """ |
| 19 | [models] |
| 20 | orchestrator = "main-x" |
| 21 | worker = "worker-y" |
| 22 | |
| 23 | [limits] |
| 24 | max_workers = 2 |
| 25 | |
| 26 | [roles.reviewer] |
| 27 | model = "review-z" |
| 28 | description = "custom" |
| 29 | """, |
| 30 | encoding="utf-8", |
| 31 | ) |
| 32 | config = load_config(tmp_path) |
| 33 | assert config.orchestrator_model == "main-x" |
| 34 | assert config.worker_model == "worker-y" |
| 35 | assert config.limits.max_workers == 2 |
| 36 | assert config.roles["reviewer"].model == "review-z" |
| 37 | assert config.roles["reviewer"].description == "custom" |
| 38 | assert "coder" in config.roles |
| 39 | |
| 40 | |
| 41 | def test_sample_config_is_valid(tmp_path: Path): |
| 42 | path = tmp_path / ".airouter.toml" |
| 43 | path.write_text(sample_config(), encoding="utf-8") |
| 44 | config = load_config(tmp_path) |
| 45 | assert config.orchestrator_model |
| 46 | assert config.provider.base_url.startswith("https://") |
| 47 | |
| 48 | |
| 49 | def test_workspace_read_write_edit(tmp_path: Path): |
| 50 | ws = Workspace(tmp_path, LimitsConfig()) |
| 51 | |
| 52 | async def scenario(): |
| 53 | await ws.write_file("src/a.txt", "hello world\nsecond line\n") |
| 54 | text = await ws.read_file("src/a.txt") |
| 55 | assert "1: hello world" in text |
| 56 | await ws.edit_file("src/a.txt", "hello", "hi") |
| 57 | assert (tmp_path / "src/a.txt").read_text() == "hi world\nsecond line\n" |
| 58 | listing = await ws.list_files(".") |
| 59 | assert "src/" in listing |
| 60 | result = await ws.bash("echo from-shell") |
| 61 | assert "from-shell" in result |
| 62 | assert ws.touched_files == {"src/a.txt"} |
| 63 | |
| 64 | run(scenario()) |
| 65 | |
| 66 | |
| 67 | def test_workspace_blocks_escape(tmp_path: Path): |
| 68 | root = tmp_path / "root" |
| 69 | root.mkdir() |
| 70 | ws = Workspace(root, LimitsConfig()) |
| 71 | |
| 72 | async def scenario(): |
| 73 | with pytest.raises(ToolError): |
| 74 | await ws.write_file("../evil.txt", "no") |
| 75 | with pytest.raises(ToolError): |
| 76 | await ws.read_file("/etc/passwd") |
| 77 | |
| 78 | run(scenario()) |
| 79 | |
| 80 | |
| 81 | def test_workspace_edit_requires_unique_match(tmp_path: Path): |
| 82 | ws = Workspace(tmp_path, LimitsConfig()) |
| 83 | |
| 84 | async def scenario(): |
| 85 | await ws.write_file("a.txt", "x\nx\n") |
| 86 | with pytest.raises(ToolError): |
| 87 | await ws.edit_file("a.txt", "x", "y") |
| 88 | await ws.edit_file("a.txt", "x", "y", replace_all=True) |
| 89 | assert (tmp_path / "a.txt").read_text() == "y\ny\n" |
| 90 | |
| 91 | run(scenario()) |
| 92 | |
| 93 | |
| 94 | def test_read_only_fs_rejects_writes(tmp_path: Path): |
| 95 | ws = Workspace(tmp_path, LimitsConfig(), read_only=True) |
| 96 | |
| 97 | async def scenario(): |
| 98 | with pytest.raises(ToolError): |
| 99 | await ws.write_file("a.txt", "x") |
| 100 | |
| 101 | run(scenario()) |