|
| 1 | +# Python imports |
| 2 | +import json |
| 3 | + |
| 4 | +# Pip imports |
| 5 | +import pytest |
| 6 | +from typer.testing import CliRunner |
| 7 | + |
| 8 | +# Internal imports |
| 9 | +from beans.cli import app, find_beans_dir |
| 10 | + |
| 11 | +runner = CliRunner() |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture() |
| 15 | +def project_dir(tmp_path, monkeypatch): |
| 16 | + d = tmp_path / "myproject" |
| 17 | + d.mkdir() |
| 18 | + monkeypatch.chdir(d) |
| 19 | + return d |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture() |
| 23 | +def invoke(project_dir): |
| 24 | + def invoke(*args): |
| 25 | + result = runner.invoke(app, ["--json", *args]) |
| 26 | + data = json.loads(result.output) if result.output.strip() else None |
| 27 | + return result.exit_code, data |
| 28 | + |
| 29 | + return invoke |
| 30 | + |
| 31 | + |
| 32 | +def init_project(): |
| 33 | + return runner.invoke(app, ["init"]) |
| 34 | + |
| 35 | + |
| 36 | +class TestInitCommand: |
| 37 | + """'beans init' creates .beans/ directory with db and AGENTS.md.""" |
| 38 | + |
| 39 | + def test_init_creates_beans_dir(self, project_dir): |
| 40 | + result = init_project() |
| 41 | + |
| 42 | + assert result.exit_code == 0 |
| 43 | + assert (project_dir / ".beans").is_dir() |
| 44 | + |
| 45 | + def test_init_creates_db(self, project_dir): |
| 46 | + init_project() |
| 47 | + |
| 48 | + assert (project_dir / ".beans" / "beans.db").exists() |
| 49 | + |
| 50 | + def test_init_creates_agents_md(self, project_dir): |
| 51 | + init_project() |
| 52 | + |
| 53 | + agents_file = project_dir / ".beans" / "AGENTS.md" |
| 54 | + assert agents_file.exists() |
| 55 | + assert "beans" in agents_file.read_text().lower() |
| 56 | + |
| 57 | + def test_init_idempotent(self, project_dir): |
| 58 | + init_project() |
| 59 | + result = init_project() |
| 60 | + |
| 61 | + assert result.exit_code == 0 |
| 62 | + |
| 63 | + def test_init_does_not_overwrite_existing_agents_md(self, project_dir): |
| 64 | + beans_dir = project_dir / ".beans" |
| 65 | + beans_dir.mkdir() |
| 66 | + agents_file = beans_dir / "AGENTS.md" |
| 67 | + agents_file.write_text("custom content") |
| 68 | + |
| 69 | + init_project() |
| 70 | + |
| 71 | + assert agents_file.read_text() == "custom content" |
| 72 | + |
| 73 | + |
| 74 | +class TestFindBeansDir: |
| 75 | + """find_beans_dir() walks up from cwd to find .beans/.""" |
| 76 | + |
| 77 | + def test_finds_beans_dir_in_cwd(self, project_dir): |
| 78 | + (project_dir / ".beans").mkdir() |
| 79 | + assert find_beans_dir(project_dir) == project_dir / ".beans" |
| 80 | + |
| 81 | + def test_finds_beans_dir_in_parent(self, project_dir): |
| 82 | + (project_dir / ".beans").mkdir() |
| 83 | + subdir = project_dir / "src" / "app" |
| 84 | + subdir.mkdir(parents=True) |
| 85 | + assert find_beans_dir(subdir) == project_dir / ".beans" |
| 86 | + |
| 87 | + def test_raises_when_not_found(self, tmp_path, monkeypatch): |
| 88 | + empty = tmp_path / "empty" |
| 89 | + empty.mkdir() |
| 90 | + monkeypatch.chdir(empty) |
| 91 | + with pytest.raises(FileNotFoundError, match=r"\.beans"): |
| 92 | + find_beans_dir(empty) |
| 93 | + |
| 94 | + |
| 95 | +class TestProjectDiscovery: |
| 96 | + """CLI commands use project discovery to find .beans/beans.db.""" |
| 97 | + |
| 98 | + def test_create_uses_discovered_db(self, project_dir, invoke): |
| 99 | + init_project() |
| 100 | + |
| 101 | + exit_code, _ = invoke("create", "Fix auth") |
| 102 | + assert exit_code == 0 |
| 103 | + |
| 104 | + _, data = invoke("list") |
| 105 | + assert len(data) == 1 |
| 106 | + assert data[0]["title"] == "Fix auth" |
| 107 | + |
| 108 | + def test_db_flag_overrides_discovery(self, project_dir, invoke): |
| 109 | + init_project() |
| 110 | + |
| 111 | + db_path = str(project_dir / "custom.db") |
| 112 | + runner.invoke(app, ["--db", db_path, "--json", "create", "Custom"]) |
| 113 | + |
| 114 | + _, data = invoke("list") |
| 115 | + assert len(data) == 0 |
0 commit comments