|
1 | | -"""cli.main — orchestrates paths.ensure_layout → bootstrap.ensure_engine → proxy.run.""" |
| 1 | +"""cli.main — orchestrates paths → cached_launcher → (install_shim or proxy).""" |
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
|
8 | 8 | import pytest |
9 | 9 |
|
10 | 10 | from codegraphagent import cli |
11 | | -from codegraphagent.errors import BootstrapError, LayoutConflictError |
| 11 | +from codegraphagent.errors import LayoutConflictError |
12 | 12 |
|
13 | 13 |
|
14 | | -def test_main_happy_path(tmp_path: Path, monkeypatch: pytest.MonkeyPatch): |
| 14 | +def test_main_proxies_immediately_when_engine_cached( |
| 15 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 16 | +): |
| 17 | + """Cached engine → straight to proxy, no install_shim involvement.""" |
15 | 18 | monkeypatch.setenv("BIOROUTER_WORKING_DIR", str(tmp_path)) |
16 | 19 | (tmp_path / ".git").mkdir() |
17 | 20 |
|
18 | | - fake_launcher = tmp_path / "fake-launcher" |
19 | | - fake_launcher.write_text("") |
| 21 | + cached_launcher = tmp_path / "cached-launcher" |
| 22 | + cached_launcher.write_text("") |
20 | 23 |
|
21 | 24 | proxy_run = MagicMock(return_value=0) |
| 25 | + install_shim_serve = MagicMock() |
22 | 26 |
|
23 | | - with patch.object(cli.bootstrap, "ensure_engine", return_value=fake_launcher), \ |
24 | | - patch.object(cli.proxy, "run", proxy_run): |
| 27 | + with patch.object(cli.bootstrap, "cached_launcher", return_value=cached_launcher), \ |
| 28 | + patch.object(cli.proxy, "run", proxy_run), \ |
| 29 | + patch.object(cli.install_shim, "serve", install_shim_serve): |
25 | 30 | rc = cli.main() |
26 | 31 |
|
27 | 32 | assert rc == 0 |
| 33 | + install_shim_serve.assert_not_called() |
28 | 34 | proxy_run.assert_called_once() |
29 | 35 | kwargs = proxy_run.call_args.kwargs |
30 | | - assert kwargs["launcher"] == fake_launcher |
| 36 | + assert kwargs["launcher"] == cached_launcher |
31 | 37 | assert kwargs["cwd"] == tmp_path.resolve() |
32 | 38 |
|
33 | 39 |
|
34 | | -def test_main_falls_back_to_error_shim_on_bootstrap_error( |
| 40 | +def test_main_serves_install_shim_when_not_cached_then_proxies( |
35 | 41 | tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
36 | 42 | ): |
| 43 | + """Not cached → install_shim runs, returns a launcher, then proxy runs.""" |
37 | 44 | monkeypatch.setenv("BIOROUTER_WORKING_DIR", str(tmp_path)) |
38 | 45 | (tmp_path / ".git").mkdir() |
39 | 46 |
|
40 | | - err = BootstrapError("nope", url="https://example.com/x") |
41 | | - error_shim_serve = MagicMock() |
| 47 | + installed_launcher = tmp_path / "freshly-installed" |
| 48 | + installed_launcher.write_text("") |
| 49 | + |
| 50 | + install_shim_serve = MagicMock(return_value=installed_launcher) |
| 51 | + proxy_run = MagicMock(return_value=0) |
42 | 52 |
|
43 | | - with patch.object(cli.bootstrap, "ensure_engine", side_effect=err), \ |
44 | | - patch.object(cli.error_shim, "serve", error_shim_serve): |
| 53 | + with patch.object(cli.bootstrap, "cached_launcher", return_value=None), \ |
| 54 | + patch.object(cli.install_shim, "serve", install_shim_serve), \ |
| 55 | + patch.object(cli.proxy, "run", proxy_run): |
45 | 56 | rc = cli.main() |
46 | 57 |
|
47 | 58 | assert rc == 0 |
48 | | - error_shim_serve.assert_called_once() |
49 | | - assert error_shim_serve.call_args.args[0] is err |
| 59 | + install_shim_serve.assert_called_once() |
| 60 | + proxy_run.assert_called_once() |
| 61 | + assert proxy_run.call_args.kwargs["launcher"] == installed_launcher |
| 62 | + |
| 63 | + |
| 64 | +def test_main_exits_zero_when_install_shim_returns_none( |
| 65 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 66 | +): |
| 67 | + """install_shim returns None (shutdown/EOF before install) → exit 0, no proxy.""" |
| 68 | + monkeypatch.setenv("BIOROUTER_WORKING_DIR", str(tmp_path)) |
| 69 | + (tmp_path / ".git").mkdir() |
| 70 | + |
| 71 | + install_shim_serve = MagicMock(return_value=None) |
| 72 | + proxy_run = MagicMock(return_value=0) |
| 73 | + |
| 74 | + with patch.object(cli.bootstrap, "cached_launcher", return_value=None), \ |
| 75 | + patch.object(cli.install_shim, "serve", install_shim_serve), \ |
| 76 | + patch.object(cli.proxy, "run", proxy_run): |
| 77 | + rc = cli.main() |
| 78 | + |
| 79 | + assert rc == 0 |
| 80 | + install_shim_serve.assert_called_once() |
| 81 | + proxy_run.assert_not_called() |
50 | 82 |
|
51 | 83 |
|
52 | 84 | def test_main_falls_back_to_error_shim_on_layout_conflict( |
53 | 85 | tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
54 | 86 | ): |
| 87 | + """Real .codegraph directory triggers LayoutConflictError → error_shim.""" |
55 | 88 | monkeypatch.setenv("BIOROUTER_WORKING_DIR", str(tmp_path)) |
56 | 89 | (tmp_path / ".git").mkdir() |
57 | 90 | (tmp_path / ".codegraph").mkdir() |
|
0 commit comments