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)