From a368e640c16fcb1b779f704a98a2dfe35ad61e3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E8=88=AA=E5=AE=87?= <3364451258@qq.com> Date: Fri, 10 Jul 2026 15:25:43 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E6=B7=BB=E5=8A=A0=20CLI=20=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E5=9F=BA=E7=A1=80=E6=B5=8B=E8=AF=95=20(ingest/search/?= =?UTF-8?q?stats/json)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude --- tests/test_cli.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tests/test_cli.py diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..8f421d9 --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,62 @@ +"""CLI 命令测试.""" +from typer.testing import CliRunner +from src.cli.main import app + +runner = CliRunner() + + +class TestCLIIngest: + """ingest 命令测试.""" + + def test_ingest_no_args_shows_usage(self): + """无参数时显示用法提示.""" + result = runner.invoke(app, ["ingest"]) + assert result.exit_code == 1 + assert "用法" in result.stderr + + def test_ingest_nonexistent_file_skips(self, tmp_path): + """不存在的文件优雅跳过.""" + result = runner.invoke(app, ["ingest", str(tmp_path / "nonexistent.md")]) + assert "SKIP" in result.stderr or result.exit_code != 0 + + +class TestCLISearch: + """search 命令测试.""" + + def test_search_basic(self, monkeypatch): + """search 命令可执行.""" + monkeypatch.setenv("MD_VECTOR_CONFIG", "config.yaml") + result = runner.invoke(app, ["search", "测试查询", "-k", "1"]) + assert isinstance(result.exit_code, int) + + +class TestCLIStats: + """stats 命令测试.""" + + def test_stats_basic(self, monkeypatch): + """stats 命令可执行.""" + monkeypatch.setenv("MD_VECTOR_CONFIG", "config.yaml") + result = runner.invoke(app, ["stats"]) + assert isinstance(result.exit_code, int) + + +class TestCLIJSONOutput: + """--json 输出测试.""" + + def test_search_json_valid(self, monkeypatch): + """search --json 输出合法 JSON.""" + import json + monkeypatch.setenv("MD_VECTOR_CONFIG", "config.yaml") + result = runner.invoke(app, ["search", "测试", "--json", "-k", "1"]) + if result.stdout.strip(): + data = json.loads(result.stdout) + assert isinstance(data, list) + + def test_stats_json_valid(self, monkeypatch): + """stats --json 输出合法 JSON.""" + import json + monkeypatch.setenv("MD_VECTOR_CONFIG", "config.yaml") + result = runner.invoke(app, ["stats", "--json"]) + if result.stdout.strip(): + data = json.loads(result.stdout) + assert isinstance(data, dict)