"""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) def test_ingest_help(): """ingest --help 正常输出.""" result = runner.invoke(app, ["ingest", "--help"]) assert result.exit_code == 0 def test_search_help(): """search --help 正常输出.""" result = runner.invoke(app, ["search", "--help"]) assert result.exit_code == 0 def test_export_help(): """export --help 正常输出.""" result = runner.invoke(app, ["export", "--help"]) assert result.exit_code == 0