Репозитории /
AiRouter /
tests/test_tui.py
test_tui.py
| 1 | from __future__ import annotations |
| 2 | |
| 3 | import asyncio |
| 4 | from pathlib import Path |
| 5 | |
| 6 | from airouter.config import Config |
| 7 | from airouter.models import ModelInfo |
| 8 | from airouter.tui.app import AirouterApp |
| 9 | from airouter.tui.screens import SettingsScreen, WorkerDetailScreen |
| 10 | |
| 11 | from .fake import FakeProvider, turn_text, turn_with_tool |
| 12 | |
| 13 | |
| 14 | def test_tui_chat_plan_and_worker(tmp_path: Path): |
| 15 | config = Config() |
| 16 | config.orchestrator_model = "main-fake" |
| 17 | config.worker_model = "worker-fake" |
| 18 | config.intelligence = { |
| 19 | "low": "worker-fake", |
| 20 | "medium": "worker-fake", |
| 21 | "high": "worker-fake", |
| 22 | } |
| 23 | provider = FakeProvider( |
| 24 | { |
| 25 | "main-fake": [ |
| 26 | turn_with_tool( |
| 27 | "update_plan", |
| 28 | { |
| 29 | "tasks": [ |
| 30 | { |
| 31 | "id": "t1", |
| 32 | "title": "Create out.txt", |
| 33 | "status": "in_progress", |
| 34 | "subtasks": [{"title": "write file", "status": "in_progress"}], |
| 35 | } |
| 36 | ] |
| 37 | }, |
| 38 | "p1", |
| 39 | ), |
| 40 | turn_with_tool( |
| 41 | "spawn_worker", |
| 42 | { |
| 43 | "task": "Create out.txt", |
| 44 | "workspace": ".", |
| 45 | "intelligence": "high", |
| 46 | }, |
| 47 | "s1", |
| 48 | ), |
| 49 | turn_with_tool("wait_for_workers", {"mode": "all"}, "w1"), |
| 50 | turn_with_tool( |
| 51 | "update_plan", |
| 52 | { |
| 53 | "tasks": [ |
| 54 | { |
| 55 | "id": "t1", |
| 56 | "title": "Create out.txt", |
| 57 | "status": "done", |
| 58 | "worker_id": "w1", |
| 59 | "subtasks": [{"title": "write file", "status": "done"}], |
| 60 | } |
| 61 | ] |
| 62 | }, |
| 63 | "p2", |
| 64 | ), |
| 65 | turn_text("Готово."), |
| 66 | ], |
| 67 | "worker-fake": [ |
| 68 | turn_with_tool("write_file", {"path": "out.txt", "content": "hi"}), |
| 69 | turn_text("Status: done\nSummary: wrote out.txt"), |
| 70 | ], |
| 71 | } |
| 72 | ) |
| 73 | models = [ |
| 74 | ModelInfo(id="main-fake"), |
| 75 | ModelInfo(id="worker-fake"), |
| 76 | ModelInfo(id="glm-5.3-flash"), |
| 77 | ] |
| 78 | |
| 79 | async def scenario(): |
| 80 | app = AirouterApp( |
| 81 | config=config, |
| 82 | provider=provider, |
| 83 | sessions_root=tmp_path / ".airouter" / "sessions", |
| 84 | cwd=tmp_path, |
| 85 | models=models, |
| 86 | ) |
| 87 | async with app.run_test(size=(130, 42)) as pilot: |
| 88 | await pilot.pause() |
| 89 | for _ in range(40): |
| 90 | if app._ui_ready: |
| 91 | break |
| 92 | await pilot.pause(0.05) |
| 93 | assert app._ui_ready |
| 94 | assert app.session is not None |
| 95 | prompt = app.query_one("#prompt") |
| 96 | prompt.value = "Создай out.txt" |
| 97 | prompt.focus() |
| 98 | await pilot.press("enter") |
| 99 | for _ in range(60): |
| 100 | await pilot.pause(0.05) |
| 101 | if app.session and not app._busy: |
| 102 | break |
| 103 | assert (tmp_path / "out.txt").read_text() == "hi" |
| 104 | assert app.session.workers["w1"].status == "done" |
| 105 | assert app.session.todos[0]["status"] == "done" |
| 106 | await pilot.click("#worker-w1") |
| 107 | await pilot.pause() |
| 108 | assert isinstance(app.screen, WorkerDetailScreen) |
| 109 | app.screen.action_close() |
| 110 | await pilot.pause() |
| 111 | await pilot.click("#settings") |
| 112 | await pilot.pause() |
| 113 | assert isinstance(app.screen, SettingsScreen) |
| 114 | max_select = app.screen.query_one("#setting-max-workers") |
| 115 | max_select.value = 4 |
| 116 | await pilot.click("#settings-save") |
| 117 | await pilot.pause() |
| 118 | assert app.session.config.limits.max_workers == 4 |
| 119 | await app.action_quit_app() |
| 120 | await pilot.pause() |
| 121 | |
| 122 | asyncio.run(asyncio.wait_for(scenario(), timeout=20)) |