80 lines
2.5 KiB
Python
80 lines
2.5 KiB
Python
"""Integration tests: GET /api/docs — empty shape + populated shape.
|
|
|
|
Uses the real compose Postgres (``db`` fixture) and FastAPI's TestClient.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import uuid
|
|
from datetime import UTC, datetime
|
|
|
|
from sqlalchemy import text
|
|
|
|
from app.models import Chunk, Document
|
|
|
|
|
|
def test_docs_empty_shape(admin_client, db) -> None:
|
|
db.execute(text("TRUNCATE chunks, documents"))
|
|
db.commit()
|
|
r = admin_client.get("/api/docs")
|
|
assert r.status_code == 200
|
|
assert r.json() == {"documents": []}
|
|
|
|
|
|
def test_docs_populated_shape_sorted_with_chunk_counts(admin_client, db) -> None:
|
|
db.execute(text("TRUNCATE chunks, documents"))
|
|
db.commit()
|
|
now = datetime.now(UTC)
|
|
k8s = Document(
|
|
source="Homelab",
|
|
path="kubernetes.md",
|
|
full_path="/tmp/kubernetes.md",
|
|
title="Kubernetes Homelab Cluster",
|
|
content="# Kubernetes Homelab Cluster\n\nTalos on 3 nodes.",
|
|
content_hash="a" * 64,
|
|
indexed_at=now,
|
|
)
|
|
empty = Document(
|
|
source="Deployments",
|
|
path="empty.md",
|
|
full_path="/tmp/empty.md",
|
|
title="No Chunks Yet",
|
|
content="two-phase: doc exists, embeddings pending",
|
|
content_hash="b" * 64,
|
|
indexed_at=now,
|
|
)
|
|
db.add_all([empty, k8s])
|
|
db.flush()
|
|
db.add_all(
|
|
Chunk(document_id=k8s.id, position=i, content=f"chunk {i}", embedding=[0.01] * 768)
|
|
for i in range(3)
|
|
)
|
|
db.commit()
|
|
|
|
r = admin_client.get("/api/docs")
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
# Ordered by (source, path): Deployments < Homelab.
|
|
assert [d["path"] for d in body["documents"]] == ["empty.md", "kubernetes.md"]
|
|
by_path = {d["path"]: d for d in body["documents"]}
|
|
|
|
k = by_path["kubernetes.md"]
|
|
assert k["source"] == "Homelab"
|
|
assert k["title"] == "Kubernetes Homelab Cluster"
|
|
assert k["chunks"] == 3
|
|
datetime.fromisoformat(k["indexed_at"]) # raises if not valid ISO-8601
|
|
uuid.UUID(k["id"]) # raises if not a valid UUID
|
|
assert by_path["empty.md"]["chunks"] == 0 # outerjoin → zero, not missing
|
|
|
|
db.execute(text("TRUNCATE chunks, documents"))
|
|
db.commit()
|
|
|
|
|
|
def test_docs_response_matches_schema_shape(admin_client, db) -> None:
|
|
r = admin_client.get("/api/docs")
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert set(body) == {"documents"}
|
|
for d in body["documents"]:
|
|
assert set(d) == {"id", "source", "path", "title", "chunks", "indexed_at"}
|
|
assert isinstance(d["chunks"], int) and d["chunks"] >= 0
|