knowledge-platform/tests/unit/test_index_rebuild_cli.py
2026-09-02 11:28:13 +08:00

83 lines
2.5 KiB
Python

"""Tests for the secret-safe shadow index command boundary."""
from typing import cast
import pytest
from ekp_api import index_rebuild
from ekp_core.index_rebuild import IndexBuildSummary, IndexRebuildService
class FakeRebuildService:
"""Return one stable summary for every explicit lifecycle operation."""
def __init__(self, *, failure: Exception | None = None) -> None:
self.failure = failure
self.calls: list[str] = []
def _result(self, command: str) -> IndexBuildSummary:
self.calls.append(command)
if self.failure is not None:
raise self.failure
return IndexBuildSummary(
profile_id="oceanbase-shadow-v1",
expected_documents=2,
built_documents=2 if command == "build" else 0,
skipped_documents=0,
expected_chunks=3,
indexed_chunks=3,
complete=True,
)
def rebuild(self) -> IndexBuildSummary:
return self._result("build")
def verify(self) -> IndexBuildSummary:
return self._result("verify")
def activate(self) -> IndexBuildSummary:
return self._result("activate")
@pytest.mark.parametrize("command", ["build", "verify", "activate"])
def test_index_rebuild_cli_executes_one_step_and_prints_safe_json(
command: str,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
service = FakeRebuildService()
monkeypatch.setattr(
index_rebuild,
"_create_service",
lambda settings: cast(IndexRebuildService, service),
)
exit_code = index_rebuild.main([command])
output = capsys.readouterr()
assert exit_code == 0
assert service.calls == [command]
assert '"complete":true' in output.out
assert '"profile_id":"oceanbase-shadow-v1"' in output.out
assert output.err == ""
def test_index_rebuild_cli_does_not_print_provider_failure_details(
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
service = FakeRebuildService(failure=RuntimeError("private-db.example password=secret-value"))
monkeypatch.setattr(
index_rebuild,
"_create_service",
lambda settings: cast(IndexRebuildService, service),
)
exit_code = index_rebuild.main(["build"])
output = capsys.readouterr()
assert exit_code == 1
assert "private-db.example" not in output.err
assert "secret-value" not in output.err
assert "command failed" in output.err