68 lines
2.2 KiB
Python
68 lines
2.2 KiB
Python
# tests/test_library_api.py
|
|
"""Tests for GET/POST /api/library endpoints."""
|
|
from __future__ import annotations
|
|
|
|
import sqlite3
|
|
|
|
|
|
def _add_doc(db_path: str, title: str, path: str, status: str = "ready") -> str:
|
|
conn = sqlite3.connect(db_path)
|
|
doc_id = conn.execute(
|
|
"INSERT INTO documents(title, file_path, status) VALUES (?,?,?) RETURNING id",
|
|
[title, path, status],
|
|
).fetchone()[0]
|
|
conn.commit()
|
|
conn.close()
|
|
return doc_id
|
|
|
|
|
|
def test_list_library_empty(client):
|
|
resp = client.get("/api/library")
|
|
assert resp.status_code == 200
|
|
assert resp.json() == []
|
|
|
|
|
|
def test_list_library_returns_documents(client, test_db):
|
|
_add_doc(test_db, "Player's Handbook", "/books/phb.pdf")
|
|
resp = client.get("/api/library")
|
|
assert resp.status_code == 200
|
|
docs = resp.json()
|
|
assert len(docs) == 1
|
|
assert docs[0]["title"] == "Player's Handbook"
|
|
assert "status" in docs[0]
|
|
|
|
|
|
def test_delete_document_removes_record(client, test_db):
|
|
doc_id = _add_doc(test_db, "Monster Manual", "/books/mm.pdf")
|
|
resp = client.delete(f"/api/library/{doc_id}")
|
|
assert resp.status_code == 204
|
|
resp2 = client.get("/api/library")
|
|
assert resp2.json() == []
|
|
|
|
|
|
def test_delete_nonexistent_returns_404(client):
|
|
resp = client.delete("/api/library/does-not-exist")
|
|
assert resp.status_code == 404
|
|
|
|
|
|
def test_reingest_returns_task_id(client, test_db, tmp_path):
|
|
pdf_path = str(tmp_path / "books" / "test.pdf")
|
|
open(pdf_path, "wb").write(b"%PDF-1.4")
|
|
doc_id = _add_doc(test_db, "Test Book", pdf_path)
|
|
resp = client.post(f"/api/library/{doc_id}/reingest")
|
|
assert resp.status_code == 202
|
|
assert "task_id" in resp.json()
|
|
|
|
|
|
def test_reingest_updates_status_to_processing(client, test_db, tmp_path):
|
|
from pathlib import Path
|
|
pdf_path = str(tmp_path / "books" / "dm_guide.pdf")
|
|
Path(pdf_path).write_bytes(b"%PDF-1.4 empty fixture")
|
|
doc_id = _add_doc(test_db, "DM Guide", pdf_path)
|
|
|
|
resp = client.post(f"/api/library/{doc_id}/reingest")
|
|
assert resp.status_code == 202
|
|
|
|
# Document should be in processing state (or beyond if stub ingest ran instantly)
|
|
status_resp = client.get(f"/api/library/{doc_id}/status")
|
|
assert status_resp.json()["status"] in ("processing", "error", "ready")
|