Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
"""Tests for Phase B server endpoints — sidebar completeness, FTS fan-out,
``/api/search`` ``repo_id`` filter, and the new ``/api/workspace/sync``
job-fan-out endpoint.
The fixtures here build a minimal "workspace mode" by populating
``app.state.workspace_config`` + ``app.state.workspace_root`` directly,
since spinning up a real ``lifespan()`` would require on-disk repos and
multi-engine fixtures the rest of the suite doesn't use.
"""
from __future__ import annotations
import json
import sqlite3
from pathlib import Path
import pytest
from httpx import AsyncClient
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from sqlalchemy.pool import StaticPool
from repowise.core.persistence.database import init_db
from repowise.core.persistence.search import FullTextSearch
from repowise.core.workspace.config import RepoEntry, WorkspaceConfig
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _build_workspace_dir(tmp_path: Path) -> tuple[Path, WorkspaceConfig]:
"""Create a workspace root with two child repos.
backend/ is "indexed" (has .repowise/wiki.db) and frontend/ is not.
The wiki.db is created with the bare schema so we can later attach a
fresh SQLAlchemy engine and have :func:`init_db` accept it.
"""
ws_root = tmp_path / "ws"
backend = ws_root / "backend"
frontend = ws_root / "frontend"
for d in (backend / ".git", frontend / ".git"):
d.mkdir(parents=True)
(backend / ".repowise").mkdir()
(backend / ".repowise" / "wiki.db").write_bytes(b"") # placeholder
(backend / ".repowise" / "state.json").write_text(
json.dumps(
{
"last_sync_commit": "abc",
"docs_enabled": False,
"docs_skip_reason": "cost gate declined",
}
)
)
cfg = WorkspaceConfig(
version=1,
repos=[
RepoEntry(path="backend", alias="backend", is_primary=True),
RepoEntry(path="frontend", alias="frontend"),
],
default_repo="backend",
)
cfg.save(ws_root)
return ws_root, cfg
async def _build_repo_engine(repo_path: Path):
"""Create an in-memory engine pretending to be the per-repo DB and
seed it with one Repository row + a couple of pages so FTS queries
return something."""
engine = create_async_engine(
"sqlite+aiosqlite:///:memory:",
connect_args={"check_same_thread": False},
poolclass=StaticPool,
)
await init_db(engine)
sf = async_sessionmaker(engine, expire_on_commit=False)
from repowise.core.persistence import crud, upsert_repository
from repowise.core.persistence.database import get_session
async with get_session(sf) as session:
repo = await upsert_repository(
session, name=repo_path.name, local_path=str(repo_path)
)
from repowise.core.persistence.models import GraphNode
session.add(
GraphNode(
repository_id=repo.id,
node_id="src/main.py",
node_type="file",
)
)
await crud.upsert_page(
session,
page_id="p1",
repository_id=repo.id,
page_type="file_page",
title="Auth module",
content="Authentication and JWT verification flow.",
summary="Auth module",
target_path="auth.py",
source_hash="h1",
model_name="mock",
provider_name="mock",
)
await session.commit()
fts = FullTextSearch(engine)
await fts.ensure_index()
await fts.index(
"p1",
"Auth module",
"Authentication and JWT verification flow.",
summary="Auth module",
target_path="auth.py",
)
return engine, sf, fts, repo.id
# ---------------------------------------------------------------------------
# Fixtures
# ---------------------------------------------------------------------------
@pytest.fixture
async def workspace_app(tmp_path, app):
"""Augment the standard ``app`` fixture with workspace state."""
ws_root, ws_config = _build_workspace_dir(tmp_path)
# Build one real-ish "backend" engine, register it as a workspace
# session, and patch its FTS into the registry.
backend_path = ws_root / "backend"
engine, sf, fts, repo_id = await _build_repo_engine(backend_path)
orig_sf = app.state.session_factory
orig_engine = app.state.engine
orig_fts = app.state.fts
orig_config = getattr(app.state, "workspace_config", None)
orig_root = getattr(app.state, "workspace_root", None)
orig_sessions = getattr(app.state, "workspace_sessions", {})
orig_engines = getattr(app.state, "workspace_engines", [])
orig_wf_fts = getattr(app.state, "workspace_fts", {})
orig_vs = getattr(app.state, "workspace_vector_stores", {})
app.state.session_factory = sf
app.state.engine = engine
app.state.fts = fts
app.state.workspace_config = ws_config
app.state.workspace_root = str(ws_root)
app.state.workspace_sessions = {repo_id: sf}
app.state.workspace_engines = [engine]
app.state.workspace_fts = {repo_id: fts}
app.state.workspace_vector_stores = {}
try:
yield app, ws_root, ws_config, repo_id
finally:
app.state.session_factory = orig_sf
app.state.engine = orig_engine
app.state.fts = orig_fts
app.state.workspace_config = orig_config
app.state.workspace_root = orig_root
app.state.workspace_sessions = orig_sessions
app.state.workspace_engines = orig_engines
app.state.workspace_fts = orig_wf_fts
app.state.workspace_vector_stores = orig_vs
await engine.dispose()
# ---------------------------------------------------------------------------
# B1 — sidebar shows indexed + unindexed + status
# ---------------------------------------------------------------------------
async def test_list_repos_includes_unindexed_workspace_entries(
workspace_app, client: AsyncClient
):
"""Frontend pulled /api/repos and used to silently drop unindexed
workspace repos. Now they must appear as ``ws:<alias>`` synthetic
rows with status="needs_index"."""
_, _ws_root, _ws_config, _ = workspace_app
resp = await client.get("/api/repos")
assert resp.status_code == 200
items = resp.json()
by_alias = {r.get("workspace_alias"): r for r in items if r.get("workspace_alias")}
assert "backend" in by_alias
assert "frontend" in by_alias
assert by_alias["backend"]["workspace_status"] == "indexed"
assert by_alias["backend"]["is_primary"] is True
assert by_alias["backend"]["docs_enabled"] is False
assert "cost gate declined" in (by_alias["backend"]["docs_skip_reason"] or "")
assert by_alias["frontend"]["workspace_status"] == "needs_index"
assert by_alias["frontend"]["id"].startswith("ws:")
assert by_alias["frontend"]["docs_enabled"] is False
# ---------------------------------------------------------------------------
# B2 — search fans across workspace, accepts repo_id filter
# ---------------------------------------------------------------------------
async def test_search_fulltext_fans_across_workspace(workspace_app, client: AsyncClient):
_, _, _, _repo_id = workspace_app
resp = await client.get(
"/api/search",
params={"query": "JWT", "search_type": "fulltext", "limit": 5},
)
assert resp.status_code == 200
body = resp.json()
assert len(body) >= 1
assert any("Auth" in r["title"] for r in body)
async def test_search_with_repo_id_scopes_to_one_repo(workspace_app, client: AsyncClient):
_, _, _, repo_id = workspace_app
resp = await client.get(
"/api/search",
params={"query": "JWT", "search_type": "fulltext", "repo_id": repo_id},
)
assert resp.status_code == 200
assert len(resp.json()) >= 1
async def test_search_with_synthetic_repo_id_returns_empty(workspace_app, client: AsyncClient):
"""Synthetic ``ws:<alias>`` IDs point at unindexed repos — search
must return [] rather than silently fall back to the primary."""
resp = await client.get(
"/api/search",
params={"query": "JWT", "search_type": "fulltext", "repo_id": "ws:frontend"},
)
assert resp.status_code == 200
assert resp.json() == []
# ---------------------------------------------------------------------------
# B4 — /api/workspace/sync — wire up the conftest router include first
# ---------------------------------------------------------------------------
@pytest.fixture
async def workspace_app_with_workspace_router(workspace_app):
"""The shared ``_create_test_app`` doesn't include the workspace
router, so add it here for the few tests that need it."""
app, *rest = workspace_app
from repowise.server.routers import workspace as ws_router
# Including the same router twice is harmless — FastAPI dedupes by
# path, but to keep the test isolation tidy we check membership.
if not any(getattr(r, "path", "").startswith("/api/workspace") for r in app.routes):
app.include_router(ws_router.router)
return (app, *rest)
async def test_workspace_sync_endpoint_indexes_unindexed(
workspace_app_with_workspace_router, client: AsyncClient, monkeypatch
):
_app, ws_root, _ws_config, repo_id = workspace_app_with_workspace_router
# Stub the executor: this test covers the fan-out decisions, not the pipeline.
async def _noop_execute(job_id, app_state, session_factory_override=None):
return None
from repowise.server.routers import repos as repos_module
monkeypatch.setattr(repos_module, "execute_job", _noop_execute)
# Repo path resolution needs the fake wiki.db to point at the right
# repo_id; the endpoint uses sqlite3.connect on disk to discover it.
# We seeded a placeholder; replace with a real schema containing the
# repo_id we generated in the fixture.
db_path = ws_root / "backend" / ".repowise" / "wiki.db"
conn = sqlite3.connect(str(db_path))
conn.execute(
"CREATE TABLE repositories (id TEXT PRIMARY KEY, name TEXT, local_path TEXT)"
)
conn.execute(
"INSERT INTO repositories(id, name, local_path) VALUES (?, ?, ?)",
(repo_id, "backend", str(ws_root / "backend")),
)
conn.commit()
conn.close()
resp = await client.post("/api/workspace/sync")
assert resp.status_code == 202, resp.text
body = resp.json()
# frontend has no wiki.db → a repo-local DB is created and the first
# full index is enqueued through the normal job machinery.
aliases = {r["alias"]: r for r in body["results"]}
assert aliases["frontend"]["status"] == "accepted"
assert aliases["frontend"]["job_id"]
assert (ws_root / "frontend" / ".repowise" / "wiki.db").is_file()
# backend got a job (or was accepted) — exact value depends on
# whether the dummy job table is wired, but it must not error.
assert aliases["backend"]["status"] in {"accepted", "error", "skipped"}
async def test_workspace_sync_unknown_alias_404s(
workspace_app_with_workspace_router, client: AsyncClient
):
resp = await client.post("/api/workspace/sync", params={"repo_alias": "bogus"})
assert resp.status_code == 404