**Phase 118 final verification pass — complete.** All criteria verified; 4 pre-existing defects found and fixed.
- **Verified:** summary-seed wiring (`select_suggested` top-5 no-floor → summary blocks, no full text in HIGH prompt), all-doc markdown summaries + NULL backfill (`summary_backfilled`, no `sources_meta` bump), `read` adds full text with `read_docs`-only dedupe, `done.sources` = suggested+read / durable record = suggested+related+read + `suggested=N` log line (seen live in E2E), byte-locked PERSONA/LOW/TOOLS_SECTION, battery gate PASS recorded in `TOOL_CALLING_TESTING.md` §10 (turbo 2026-09-16: 1/2/4 GREEN, cond-3 reported 9/10 per A7, contract 21/21, caps 0).
- **Defects fixed (all pre-existing, none phase-118):** ① `ChatMessage` schema missing the phase-113 `related` key → `extra="forbid"` 422'd every done-time auto-save of grounded turns with a related tier, leaving `message_count=1` (root cause of `test_share_chat` 3F; browser-level instrumentation proved the PUT 422) — added the field + unit/integration pins; ② `test_theme_semantic_completion` pins stale vs phase-117 debox (border/chip removed) — re-targeted to assert border/chip *absence*; ③ `test_header_consistency` `<26`px pin red on 26.125px native date-input line — bound relaxed to `<34` (wrap-detection intent kept); ④ `test_navbar_refresh` bor.chat.v1 key set updated for `related`.
- **Test/lint/coverage:** `uv run pytest --cov=app --cov-report=term-missing` → **2506 passed, app/ 99%** (>90%); `uv run ruff check . && uv run pyright` → clean, 0 errors.
- **E2E:** new story suite in isolation → **2 passed**; full 103-suite matrix sweep (each isolated) → **all 103 green** after the fixes; `test_share_chat` 4 passed, `test_theme_semantic_completion` 8 passed, `test_header_consistency` 3 passed, `test_navbar_refresh` 7 passed.
- **Deviations:** none from LOCKED decisions. Note: orphaned diagnostic uvicorn processes briefly made E2E sessions exercise stale code — killed and re-verified; a sweep-regenerated tracked screenshot was restored. No commits made (harness commits).
- **Completion criteria:** all 7 ✅ (commit/phase-move is the harness's step).
- **Next pending phase:** none — `todo/` holds only this phase's overview pending the harness move.
116 lines
5.1 KiB
Python
116 lines
5.1 KiB
Python
"""Integration test: importer end-to-end against ``tests/fixtures/docs/``.
|
||
|
||
Runs the real import pipeline (walk → chunk → embed → upsert) into the
|
||
local compose Postgres, then checks the DB state *and* the API shape a
|
||
browser would consume. Embeddings come from a deterministic in-process
|
||
fake, so no network is needed.
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import asyncio
|
||
from pathlib import Path
|
||
|
||
from sqlalchemy import func, select, text
|
||
|
||
from app.models import Chunk, Document
|
||
from app.rag.importer import import_sources
|
||
from tests.fakes import FakeEmbedder
|
||
|
||
FIXTURES = Path(__file__).resolve().parents[1] / "fixtures" / "docs"
|
||
|
||
EXPECTED_DOCS = {
|
||
("docs", "homelab/kubernetes.md"),
|
||
("docs", "homelab/backups.md"),
|
||
("docs", "deployments/new-service.md"),
|
||
("docs", "homelab/container_gitlab/gitlab.md"),
|
||
("docs", "homelab/container_gitlab/gitlab-compose.yaml"),
|
||
("docs", "homelab/networking/static-dns.json"),
|
||
("docs", "homelab/scripts/uptime_probe.py"),
|
||
("docs", "homelab/ssh/ssh_aliases.txt"),
|
||
("docs", "homelab/tables.md"), # phase 44: the markdown-tables fixture
|
||
# phase 47 (A9 revised 2026-08-27): quadlet family + jinja fixtures
|
||
("docs", "homelab/quadlet/compose.container"),
|
||
("docs", "homelab/quadlet/lan.network"),
|
||
("docs", "homelab/quadlet/cache.volume"),
|
||
("docs", "homelab/templates/deploy.j2"),
|
||
}
|
||
|
||
|
||
def test_import_fixtures_end_to_end(admin_client, db) -> None:
|
||
db.execute(text("TRUNCATE chunks, documents, query_log"))
|
||
db.commit()
|
||
llm = FakeEmbedder()
|
||
|
||
summary = asyncio.run(import_sources([FIXTURES], llm, session=db))
|
||
# Thirteen A9-format files (phase 44 added homelab/tables.md, phase 47
|
||
# added the quadlet + j2 fixtures); .hidden/junk.md is out of scope
|
||
# (A9 revised).
|
||
assert (summary.files, summary.added, summary.unchanged) == (13, 13, 0)
|
||
assert summary.chunks >= 13
|
||
assert summary.formats == {
|
||
"md": 5, "yaml": 1, "json": 1, "py": 1, "txt": 1,
|
||
"container": 1, "network": 1, "volume": 1, "j2": 1, # phase 47
|
||
}
|
||
# PLAN §9 per-format summary line: highest count first, then alpha.
|
||
assert summary.format_counts() == (
|
||
"md:5,container:1,j2:1,json:1,network:1,py:1,txt:1,volume:1,yaml:1"
|
||
)
|
||
|
||
docs = db.scalars(select(Document)).all()
|
||
assert {(d.source, d.path) for d in docs} == EXPECTED_DOCS
|
||
titles = {d.path: d.title for d in docs}
|
||
assert titles["homelab/kubernetes.md"] == "Kubernetes Homelab Cluster"
|
||
assert titles["deployments/new-service.md"] == "Deploying a New Service"
|
||
assert titles["homelab/container_gitlab/gitlab.md"] == "Gitlab"
|
||
# Non-markdown titles come from the file stem (a leading ``#`` or docstring
|
||
# line is a comment there, not a heading).
|
||
assert titles["homelab/container_gitlab/gitlab-compose.yaml"] == "gitlab-compose"
|
||
assert titles["homelab/scripts/uptime_probe.py"] == "uptime_probe"
|
||
assert titles["homelab/networking/static-dns.json"] == "static-dns"
|
||
assert titles["homelab/ssh/ssh_aliases.txt"] == "ssh_aliases"
|
||
assert titles["homelab/quadlet/compose.container"] == "compose"
|
||
assert titles["homelab/quadlet/lan.network"] == "lan"
|
||
assert titles["homelab/quadlet/cache.volume"] == "cache"
|
||
assert titles["homelab/templates/deploy.j2"] == "deploy"
|
||
# Hidden junk was never imported.
|
||
assert not any(".hidden" in d.path for d in docs)
|
||
# Full content is stored — that is what the RAG context will be.
|
||
k8s = next(d for d in docs if d.path == "homelab/kubernetes.md")
|
||
assert "Talos Linux" in k8s.content and k8s.content_hash
|
||
|
||
# Phase 30; phase 118 (A2): EVERY fixture gained one embedded
|
||
# ``is_summary`` chunk (markdown included), so the DB holds content +
|
||
# summary chunks.
|
||
n_chunks = db.scalar(select(func.count()).select_from(Chunk))
|
||
assert n_chunks == summary.chunks + summary.summaries
|
||
for c in db.scalars(select(Chunk)).all():
|
||
assert c.embedding is not None and len(c.embedding) == 768
|
||
|
||
assert summary.summary_errors == 0
|
||
for d in docs:
|
||
# Phase 118 (A2): EVERY document — markdown included — carries a
|
||
# stored summary + exactly one embedded summary chunk (−1).
|
||
schunks = [c for c in d.chunks if c.is_summary]
|
||
assert d.summary is not None, f"{d.path} should have a summary"
|
||
assert len(schunks) == 1
|
||
assert schunks[0].position == -1
|
||
assert schunks[0].content == d.summary
|
||
assert schunks[0].embedding is not None
|
||
assert summary.summaries == len(docs) # one per doc, no exceptions
|
||
|
||
# The Sources page consumes exactly this shape.
|
||
r = admin_client.get("/api/docs") # phase 16: the catalog is admin-only
|
||
assert r.status_code == 200
|
||
body = r.json()
|
||
assert len(body["documents"]) == 13
|
||
assert all(d["chunks"] >= 1 for d in body["documents"])
|
||
|
||
# Idempotent re-run: nothing re-embedded.
|
||
calls_before = len(llm.calls)
|
||
s2 = asyncio.run(import_sources([FIXTURES], llm, session=db))
|
||
assert s2.unchanged == 13 and s2.added == 0
|
||
assert len(llm.calls) == calls_before # unchanged → no embedding requests
|
||
|
||
db.execute(text("TRUNCATE chunks, documents, query_log"))
|
||
db.commit()
|