|
| 1 | +"""Tests for slash commands in the chat REPL.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import json |
| 5 | +from pathlib import Path |
| 6 | +from unittest.mock import AsyncMock, patch |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from prompt_toolkit.styles import Style |
| 11 | + |
| 12 | +from openkb.agent.chat import _handle_slash, _run_add |
| 13 | +from openkb.agent.chat_session import ChatSession |
| 14 | + |
| 15 | + |
| 16 | +def _setup_kb(tmp_path: Path) -> Path: |
| 17 | + """Create a minimal KB structure and return kb_dir.""" |
| 18 | + kb_dir = tmp_path |
| 19 | + (kb_dir / "raw").mkdir() |
| 20 | + (kb_dir / "wiki" / "sources" / "images").mkdir(parents=True) |
| 21 | + (kb_dir / "wiki" / "summaries").mkdir(parents=True) |
| 22 | + (kb_dir / "wiki" / "concepts").mkdir(parents=True) |
| 23 | + (kb_dir / "wiki" / "reports").mkdir(parents=True) |
| 24 | + openkb_dir = kb_dir / ".openkb" |
| 25 | + openkb_dir.mkdir() |
| 26 | + (openkb_dir / "config.yaml").write_text("model: gpt-4o-mini\n") |
| 27 | + (openkb_dir / "hashes.json").write_text(json.dumps({})) |
| 28 | + return kb_dir |
| 29 | + |
| 30 | + |
| 31 | +def _make_session(kb_dir: Path) -> ChatSession: |
| 32 | + return ChatSession.new(kb_dir, "gpt-4o-mini", "en") |
| 33 | + |
| 34 | + |
| 35 | +_STYLE = Style.from_dict({}) |
| 36 | + |
| 37 | + |
| 38 | +def _collect_fmt(): |
| 39 | + """Return (patch, collected) where collected is a list of printed strings.""" |
| 40 | + collected: list[str] = [] |
| 41 | + |
| 42 | + def _fake_fmt(_style, *fragments): |
| 43 | + for _cls, text in fragments: |
| 44 | + collected.append(text) |
| 45 | + |
| 46 | + return patch("openkb.agent.chat._fmt", _fake_fmt), collected |
| 47 | + |
| 48 | + |
| 49 | +# --- /status and /list use click.echo, captured by capsys --- |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.asyncio |
| 53 | +async def test_slash_status(tmp_path, capsys): |
| 54 | + kb_dir = _setup_kb(tmp_path) |
| 55 | + session = _make_session(kb_dir) |
| 56 | + result = await _handle_slash("/status", kb_dir, session, _STYLE) |
| 57 | + assert result is None |
| 58 | + output = capsys.readouterr().out |
| 59 | + assert "Knowledge Base Status" in output |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.asyncio |
| 63 | +async def test_slash_list_empty(tmp_path, capsys): |
| 64 | + kb_dir = _setup_kb(tmp_path) |
| 65 | + session = _make_session(kb_dir) |
| 66 | + result = await _handle_slash("/list", kb_dir, session, _STYLE) |
| 67 | + assert result is None |
| 68 | + output = capsys.readouterr().out |
| 69 | + assert "No documents indexed yet" in output |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.asyncio |
| 73 | +async def test_slash_list_with_docs(tmp_path, capsys): |
| 74 | + kb_dir = _setup_kb(tmp_path) |
| 75 | + hashes = {"abc": {"name": "paper.pdf", "type": "pdf"}} |
| 76 | + (kb_dir / ".openkb" / "hashes.json").write_text(json.dumps(hashes)) |
| 77 | + session = _make_session(kb_dir) |
| 78 | + result = await _handle_slash("/list", kb_dir, session, _STYLE) |
| 79 | + assert result is None |
| 80 | + output = capsys.readouterr().out |
| 81 | + assert "paper.pdf" in output |
| 82 | + |
| 83 | + |
| 84 | +# --- /add, /exit, /clear, /help, /unknown use _fmt → need patching --- |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.asyncio |
| 88 | +async def test_slash_add_missing_arg(tmp_path): |
| 89 | + kb_dir = _setup_kb(tmp_path) |
| 90 | + session = _make_session(kb_dir) |
| 91 | + p, collected = _collect_fmt() |
| 92 | + with p: |
| 93 | + result = await _handle_slash("/add", kb_dir, session, _STYLE) |
| 94 | + assert result is None |
| 95 | + assert any("Usage: /add <path>" in s for s in collected) |
| 96 | + |
| 97 | + |
| 98 | +@pytest.mark.asyncio |
| 99 | +async def test_slash_add_nonexistent_path(tmp_path): |
| 100 | + kb_dir = _setup_kb(tmp_path) |
| 101 | + session = _make_session(kb_dir) |
| 102 | + p, collected = _collect_fmt() |
| 103 | + with p: |
| 104 | + result = await _handle_slash("/add /no/such/path", kb_dir, session, _STYLE) |
| 105 | + assert result is None |
| 106 | + assert any("Path does not exist" in s for s in collected) |
| 107 | + |
| 108 | + |
| 109 | +@pytest.mark.asyncio |
| 110 | +async def test_slash_add_unsupported_type(tmp_path): |
| 111 | + kb_dir = _setup_kb(tmp_path) |
| 112 | + bad_file = tmp_path / "file.xyz" |
| 113 | + bad_file.write_text("data") |
| 114 | + session = _make_session(kb_dir) |
| 115 | + p, collected = _collect_fmt() |
| 116 | + with p: |
| 117 | + result = await _handle_slash(f"/add {bad_file}", kb_dir, session, _STYLE) |
| 118 | + assert result is None |
| 119 | + assert any("Unsupported file type" in s for s in collected) |
| 120 | + |
| 121 | + |
| 122 | +@pytest.mark.asyncio |
| 123 | +async def test_slash_add_single_file(tmp_path): |
| 124 | + kb_dir = _setup_kb(tmp_path) |
| 125 | + doc = tmp_path / "test.md" |
| 126 | + doc.write_text("# Hello") |
| 127 | + p, _collected = _collect_fmt() |
| 128 | + with p, patch("openkb.cli.add_single_file") as mock_add: |
| 129 | + await _run_add(str(doc), kb_dir, _STYLE) |
| 130 | + mock_add.assert_called_once_with(doc, kb_dir) |
| 131 | + |
| 132 | + |
| 133 | +@pytest.mark.asyncio |
| 134 | +async def test_slash_add_directory_with_progress(tmp_path): |
| 135 | + kb_dir = _setup_kb(tmp_path) |
| 136 | + docs_dir = tmp_path / "docs" |
| 137 | + docs_dir.mkdir() |
| 138 | + (docs_dir / "a.md").write_text("# A") |
| 139 | + (docs_dir / "b.txt").write_text("B") |
| 140 | + (docs_dir / "skip.xyz").write_text("skip") |
| 141 | + p, collected = _collect_fmt() |
| 142 | + with p, patch("openkb.cli.add_single_file") as mock_add: |
| 143 | + await _run_add(str(docs_dir), kb_dir, _STYLE) |
| 144 | + assert mock_add.call_count == 2 |
| 145 | + output = "".join(collected) |
| 146 | + assert "Found 2 supported file(s)" in output |
| 147 | + assert "[1/2]" in output |
| 148 | + assert "[2/2]" in output |
| 149 | + |
| 150 | + |
| 151 | +@pytest.mark.asyncio |
| 152 | +async def test_slash_lint(tmp_path): |
| 153 | + kb_dir = _setup_kb(tmp_path) |
| 154 | + session = _make_session(kb_dir) |
| 155 | + with patch("openkb.cli.run_lint", new_callable=AsyncMock, return_value=tmp_path / "report.md"): |
| 156 | + result = await _handle_slash("/lint", kb_dir, session, _STYLE) |
| 157 | + assert result is None |
| 158 | + |
| 159 | + |
| 160 | +@pytest.mark.asyncio |
| 161 | +async def test_slash_unknown(tmp_path): |
| 162 | + kb_dir = _setup_kb(tmp_path) |
| 163 | + session = _make_session(kb_dir) |
| 164 | + p, collected = _collect_fmt() |
| 165 | + with p: |
| 166 | + result = await _handle_slash("/foobar", kb_dir, session, _STYLE) |
| 167 | + assert result is None |
| 168 | + assert any("Unknown command" in s for s in collected) |
| 169 | + |
| 170 | + |
| 171 | +@pytest.mark.asyncio |
| 172 | +async def test_slash_exit(tmp_path): |
| 173 | + kb_dir = _setup_kb(tmp_path) |
| 174 | + session = _make_session(kb_dir) |
| 175 | + p, _collected = _collect_fmt() |
| 176 | + with p: |
| 177 | + result = await _handle_slash("/exit", kb_dir, session, _STYLE) |
| 178 | + assert result == "exit" |
| 179 | + |
| 180 | + |
| 181 | +@pytest.mark.asyncio |
| 182 | +async def test_slash_clear(tmp_path): |
| 183 | + kb_dir = _setup_kb(tmp_path) |
| 184 | + session = _make_session(kb_dir) |
| 185 | + p, _collected = _collect_fmt() |
| 186 | + with p: |
| 187 | + result = await _handle_slash("/clear", kb_dir, session, _STYLE) |
| 188 | + assert result == "new_session" |
0 commit comments