Files
AiRouter/tests/test_config_workspace.py
2026-11-19 20:48:42 +07:00

102 lines
2.8 KiB
Python

from __future__ import annotations
import asyncio
from pathlib import Path
import pytest
from airouter.config import LimitsConfig, load_config, sample_config
from airouter.workspace import ToolError, Workspace
def run(coro):
return asyncio.run(coro)
def test_config_project_override(tmp_path: Path):
(tmp_path / ".airouter.toml").write_text(
"""
[models]
orchestrator = "main-x"
worker = "worker-y"
[limits]
max_workers = 2
[roles.reviewer]
model = "review-z"
description = "custom"
""",
encoding="utf-8",
)
config = load_config(tmp_path)
assert config.orchestrator_model == "main-x"
assert config.worker_model == "worker-y"
assert config.limits.max_workers == 2
assert config.roles["reviewer"].model == "review-z"
assert config.roles["reviewer"].description == "custom"
assert "coder" in config.roles
def test_sample_config_is_valid(tmp_path: Path):
path = tmp_path / ".airouter.toml"
path.write_text(sample_config(), encoding="utf-8")
config = load_config(tmp_path)
assert config.orchestrator_model
assert config.provider.base_url.startswith("https://")
def test_workspace_read_write_edit(tmp_path: Path):
ws = Workspace(tmp_path, LimitsConfig())
async def scenario():
await ws.write_file("src/a.txt", "hello world\nsecond line\n")
text = await ws.read_file("src/a.txt")
assert "1: hello world" in text
await ws.edit_file("src/a.txt", "hello", "hi")
assert (tmp_path / "src/a.txt").read_text() == "hi world\nsecond line\n"
listing = await ws.list_files(".")
assert "src/" in listing
result = await ws.bash("echo from-shell")
assert "from-shell" in result
assert ws.touched_files == {"src/a.txt"}
run(scenario())
def test_workspace_blocks_escape(tmp_path: Path):
root = tmp_path / "root"
root.mkdir()
ws = Workspace(root, LimitsConfig())
async def scenario():
with pytest.raises(ToolError):
await ws.write_file("../evil.txt", "no")
with pytest.raises(ToolError):
await ws.read_file("/etc/passwd")
run(scenario())
def test_workspace_edit_requires_unique_match(tmp_path: Path):
ws = Workspace(tmp_path, LimitsConfig())
async def scenario():
await ws.write_file("a.txt", "x\nx\n")
with pytest.raises(ToolError):
await ws.edit_file("a.txt", "x", "y")
await ws.edit_file("a.txt", "x", "y", replace_all=True)
assert (tmp_path / "a.txt").read_text() == "y\ny\n"
run(scenario())
def test_read_only_fs_rejects_writes(tmp_path: Path):
ws = Workspace(tmp_path, LimitsConfig(), read_only=True)
async def scenario():
with pytest.raises(ToolError):
await ws.write_file("a.txt", "x")
run(scenario())