~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""HTTP-contract tests for the ported FastAPI Notes router.
The Flask ``notes_bp`` blueprint was ported to a FastAPI ``APIRouter`` at
``web/routers/notes.py`` (prefix ``/notes``). These tests drive that router
through a real ``TestClient`` and pin its key HTTP contracts: auth-gating,
the note CRUD lifecycle, body validation, collections, research/document
links + annotations, wiki-links (backlinks/outgoing/resolve), version
history, the rendered pages, and the AI endpoints (with ``NoteAIService``
mocked so no model backend is required).
Harness notes:
* The note service opens the user's ENCRYPTED per-user DB, so a real
register + login is required — auth cannot be faked with a dependency
override here (the DB password only exists after a real login). A fresh
username is minted per module with ``uuid``.
* Every state-changing request carries an ``X-CSRFToken`` header fetched
from ``GET /auth/csrf-token`` — the app's CSRF middleware runs before
``require_auth``, so an unauthenticated mutating request without a token
would 403 (CSRF) rather than 401 (auth). To assert *auth*-gating on a
mutating route we mint a token on the unauthenticated session first, so
CSRF passes and the request reaches ``require_auth``.
* Rate limiting must be DISABLED (``LDR_DISABLE_RATE_LIMITING=true``); no
assertion here depends on a 429.
"""
import os
# Rate limiting is read once at import time in
# ``web/dependencies/rate_limit.py``; make sure it is disabled before the app
# (and that module) is imported. The CI/local runner also exports this, but
# set it defensively so a stray direct run can't flake on the write/AI
# per-minute buckets.
os.environ.setdefault("LDR_DISABLE_RATE_LIMITING", "true")
import uuid # noqa: E402
import pytest # noqa: E402
from fastapi.testclient import TestClient # noqa: E402
PASSWORD = "TestPass123!"
# ---------------------------------------------------------------------------
# Fixtures + helpers
# ---------------------------------------------------------------------------
def _csrf(client):
"""Mint (or fetch) the session CSRF token for a client."""
client.get("/auth/login")
r = client.get("/auth/csrf-token")
return r.json().get("csrf_token", "") if r.status_code == 200 else ""
@pytest.fixture(scope="module")
def client():
"""A REAL registered + logged-in user (module-scoped).
The note service opens the user's encrypted DB, so the session must carry
a genuine DB password from a real login — mirrors ``notes_smoke.py``.
"""
from local_deep_research.web.fastapi_app import app
c = TestClient(app, raise_server_exceptions=False)
username = "notes_" + uuid.uuid4().hex[:8]
reg = c.post(
"/auth/register",
data={
"username": username,
"password": PASSWORD,
"confirm_password": PASSWORD,
"acknowledge": "true",
"csrf_token": _csrf(c),
},
follow_redirects=False,
)
assert reg.status_code in (200, 302), f"register failed: {reg.status_code}"
login = c.post(
"/auth/login",
data={
"username": username,
"password": PASSWORD,
"csrf_token": _csrf(c),
},
follow_redirects=False,
)
assert login.status_code == 302, f"login failed: {login.status_code}"
return c
@pytest.fixture(scope="module")
def unauth_client():
"""A fresh client that is never logged in (for auth-gating tests)."""
from local_deep_research.web.fastapi_app import app
return TestClient(app, raise_server_exceptions=False)
def _create_note(client, title=None, content="hello world", tags=None):
"""Create a note and return the (response, note_id) pair."""
body = {
"title": title or ("Note " + uuid.uuid4().hex[:8]),
"content": content,
}
if tags is not None:
body["tags"] = tags
r = client.post(
"/notes/api/notes", json=body, headers={"X-CSRFToken": _csrf(client)}
)
note_id = r.json().get("id") if r.status_code == 201 else None
return r, note_id
def _create_collection(client, name=None):
"""Create a user collection via the RAG API; return its id (or None)."""
r = client.post(
"/library/api/collections",
json={
"name": name or ("coll-" + uuid.uuid4().hex[:8]),
"type": "user_collection",
},
headers={"X-CSRFToken": _csrf(client)},
)
if r.status_code == 200 and r.json().get("success"):
return r.json()["collection"]["id"]
return None
def _install_fake_ai(monkeypatch, **returns):
"""Replace ``notes.NoteAIService`` with a fake whose named methods return
fixed values, so AI endpoints can be exercised without a model backend.
The NoteAIService methods these routes await are ``async def`` (#5854),
so the fake methods are async too.
"""
class _FakeAI:
def __init__(self, *args, **kwargs):
pass
for name, value in returns.items():
def _make(v):
async def _method(self, *args, **kwargs):
return v
return _method
setattr(_FakeAI, name, _make(value))
monkeypatch.setattr(
"local_deep_research.web.routers.notes.NoteAIService", _FakeAI
)
return _FakeAI
def _rand_id():
return uuid.uuid4().hex
def _validation_warnings(cap):
"""Audit-log lines from the notes router's validation-reject warnings.
The router logs through loguru, not stdlib logging, so a bare ``caplog``
sees nothing here -- the ``loguru_caplog_full`` fixture (tests/conftest.py)
bridges loguru into caplog via a PropagateHandler.
"""
return [
line
for line in cap.text.splitlines()
if "notes_api validation reject" in line
]
# ---------------------------------------------------------------------------
# 1. Auth-gating — a representative sample across endpoint groups
# ---------------------------------------------------------------------------
class TestAuthGating:
def test_list_requires_auth(self, unauth_client):
assert unauth_client.get("/notes/api/notes").status_code == 401
def test_get_note_requires_auth(self, unauth_client):
r = unauth_client.get(f"/notes/api/notes/{_rand_id()}")
assert r.status_code == 401
def test_create_requires_auth(self, unauth_client):
# Mint a CSRF token on the unauth session so the request passes the
# CSRF middleware and actually reaches require_auth -> 401 (not 403).
tok = _csrf(unauth_client)
r = unauth_client.post(
"/notes/api/notes",
json={"title": "x", "content": "y"},
headers={"X-CSRFToken": tok},
)
assert r.status_code == 401
def test_collections_route_requires_auth(self, unauth_client):
r = unauth_client.get(f"/notes/api/notes/{_rand_id()}/collections")
assert r.status_code == 401
def test_research_annotations_route_requires_auth(self, unauth_client):
r = unauth_client.get(f"/notes/api/research/{_rand_id()}/annotations")
assert r.status_code == 401
def test_semantic_search_requires_auth(self, unauth_client):
r = unauth_client.get("/notes/api/notes/semantic-search?q=hello")
assert r.status_code == 401
def test_versions_route_requires_auth(self, unauth_client):
r = unauth_client.get(f"/notes/api/notes/{_rand_id()}/versions")
assert r.status_code == 401
def test_synthesize_route_requires_auth(self, unauth_client):
tok = _csrf(unauth_client)
r = unauth_client.post(
"/notes/api/notes/synthesize",
json={"note_ids": [_rand_id(), _rand_id()]},
headers={"X-CSRFToken": tok},
)
assert r.status_code == 401
# ---------------------------------------------------------------------------
# 2. Note CRUD lifecycle
# ---------------------------------------------------------------------------
class TestNoteCRUD:
def test_create_returns_201_and_id(self, client):
r, note_id = _create_note(client, title="Create Me")
assert r.status_code == 201
body = r.json()
assert body["success"] is True
assert isinstance(note_id, str) and note_id
def test_get_returns_note_with_expected_keys(self, client):
_, note_id = _create_note(client, title="Fetch Me", content="body text")
r = client.get(f"/notes/api/notes/{note_id}")
assert r.status_code == 200
payload = r.json()
assert payload["success"] is True
note = payload["note"]
assert note["id"] == note_id
assert note["title"] == "Fetch Me"
assert note["content"] == "body text"
# Documented note-dict shape.
for key in ("tags", "pinned", "created_at", "updated_at"):
assert key in note
def test_list_contains_created_note(self, client):
_, note_id = _create_note(client, title="Listed Note")
r = client.get("/notes/api/notes")
assert r.status_code == 200
payload = r.json()
assert payload["success"] is True
assert payload["total"] >= 1
assert note_id in {n["id"] for n in payload["notes"]}
def test_update_reflects_changed_title_and_content(self, client):
_, note_id = _create_note(client, title="Old Title", content="old")
r = client.put(
f"/notes/api/notes/{note_id}",
json={"title": "New Title", "content": "new body"},
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 200
assert r.json()["success"] is True
got = client.get(f"/notes/api/notes/{note_id}").json()["note"]
assert got["title"] == "New Title"
assert got["content"] == "new body"
def test_update_pin_toggle_reflected(self, client):
_, note_id = _create_note(client, title="Pin Me")
r = client.put(
f"/notes/api/notes/{note_id}",
json={"pinned": True},
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 200
got = client.get(f"/notes/api/notes/{note_id}").json()["note"]
assert got["pinned"] is True
def test_delete_then_get_is_404(self, client):
_, note_id = _create_note(client, title="Delete Me")
d = client.delete(
f"/notes/api/notes/{note_id}",
headers={"X-CSRFToken": _csrf(client)},
)
assert d.status_code == 200
assert d.json()["success"] is True
g = client.get(f"/notes/api/notes/{note_id}")
assert g.status_code == 404
assert g.json()["success"] is False
def test_update_unknown_note_404(self, client):
r = client.put(
f"/notes/api/notes/{_rand_id()}",
json={"title": "nope"},
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 404
def test_delete_unknown_note_404(self, client):
r = client.delete(
f"/notes/api/notes/{_rand_id()}",
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 404
# ---------------------------------------------------------------------------
# 3. Validation / body caps
# ---------------------------------------------------------------------------
class TestValidation:
def test_create_non_object_body_400(self, client):
r = client.post(
"/notes/api/notes",
json=[1, 2, 3],
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 400
assert r.json()["error"] == "Request body must be a JSON object"
def test_create_empty_object_body_400_not_500(self, client):
r = client.post(
"/notes/api/notes",
json={},
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 400
assert r.json()["error"] == "No data provided"
def test_create_missing_title_400(self, client):
r = client.post(
"/notes/api/notes",
json={"content": "no title here"},
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 400
assert r.json()["error"] == "Title is required"
def test_create_missing_content_400(self, client):
r = client.post(
"/notes/api/notes",
json={"title": "no content here"},
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 400
assert r.json()["error"] == "Content is required"
def test_update_non_object_body_400(self, client):
r = client.put(
f"/notes/api/notes/{_rand_id()}",
json="just a string",
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 400
assert r.json()["error"] == "Request body must be a JSON object"
def test_update_empty_object_body_400(self, client):
_, note_id = _create_note(client)
r = client.put(
f"/notes/api/notes/{note_id}",
json={},
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 400
assert r.json()["error"] == "No data provided"
def test_update_non_bool_pinned_400(self, client):
_, note_id = _create_note(client)
r = client.put(
f"/notes/api/notes/{note_id}",
json={"pinned": "yes"},
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 400
assert r.json()["error"] == "pinned must be a boolean"
def test_list_invalid_limit_400(self, client):
r = client.get("/notes/api/notes?limit=not-a-number")
assert r.status_code == 400
assert r.json()["success"] is False
def test_index_non_bool_force_reindex_400(self, client, loguru_caplog_full):
# Validated before the note_exists/RAG-service work — a nonexistent
# note id still hits the 400, not a 404.
with loguru_caplog_full.at_level("WARNING"):
r = client.post(
f"/notes/api/notes/{_rand_id()}/index",
json={"collection_id": _rand_id(), "force_reindex": "yes"},
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 400
assert r.json()["error"] == "force_reindex must be a boolean"
# The reject is audit-logged (visibility into probing of the
# tightened input contract) with the rejected value type-tagged.
msgs = _validation_warnings(loguru_caplog_full)
assert msgs, "expected a validation-reject audit line"
assert "force_reindex" in msgs[0]
assert "type=str" in msgs[0]
@pytest.mark.skip(
reason=(
"The 413 guard keys off a declared Content-Length > 100MB "
"(2x the 50MB note cap). Triggering it deterministically would "
"require either allocating a >100MB body (wasteful, forbidden by "
"the harness) or forging the Content-Length header, which httpx "
"recomputes from the actual body — so it cannot be exercised "
"reliably at the HTTP layer. The non-object 400 path is covered "
"above; the 413 branch is unit-covered at the helper level."
)
)
def test_oversized_body_413(self, client): # pragma: no cover
pass
# ---------------------------------------------------------------------------
# 4. Collections
# ---------------------------------------------------------------------------
class TestCollections:
def test_new_note_is_in_default_notes_collection(self, client):
_, note_id = _create_note(client)
r = client.get(f"/notes/api/notes/{note_id}/collections")
assert r.status_code == 200
payload = r.json()
assert payload["success"] is True
types = {c["collection_type"] for c in payload["collections"]}
assert "notes" in types
def test_add_list_and_remove_user_collection(self, client):
_, note_id = _create_note(client)
collection_id = _create_collection(client)
if collection_id is None:
pytest.skip("collection creation API unavailable in this build")
add = client.post(
f"/notes/api/notes/{note_id}/collections",
json={"collection_id": collection_id},
headers={"X-CSRFToken": _csrf(client)},
)
assert add.status_code == 200
assert add.json()["success"] is True
listing = client.get(f"/notes/api/notes/{note_id}/collections")
assert listing.status_code == 200
assert collection_id in {c["id"] for c in listing.json()["collections"]}
remove = client.delete(
f"/notes/api/notes/{note_id}/collections/{collection_id}",
headers={"X-CSRFToken": _csrf(client)},
)
assert remove.status_code == 200
assert remove.json()["success"] is True
def test_add_missing_collection_id_400(self, client):
_, note_id = _create_note(client)
r = client.post(
f"/notes/api/notes/{note_id}/collections",
json={},
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 400
assert r.json()["error"] == "collection_id is required"
def test_add_unknown_collection_404(self, client):
_, note_id = _create_note(client)
r = client.post(
f"/notes/api/notes/{note_id}/collections",
json={"collection_id": _rand_id()},
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 404
assert r.json()["error"] == "Collection not found"
def test_add_to_collection_unknown_note_404(self, client):
collection_id = _create_collection(client)
if collection_id is None:
pytest.skip("collection creation API unavailable in this build")
r = client.post(
f"/notes/api/notes/{_rand_id()}/collections",
json={"collection_id": collection_id},
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 404
assert r.json()["error"] == "Note not found"
def test_remove_from_notes_collection_is_guarded_400(self, client):
_, note_id = _create_note(client)
colls = client.get(f"/notes/api/notes/{note_id}/collections").json()[
"collections"
]
notes_cid = next(
c["id"] for c in colls if c["collection_type"] == "notes"
)
r = client.delete(
f"/notes/api/notes/{note_id}/collections/{notes_cid}",
headers={"X-CSRFToken": _csrf(client)},
)
# The system Notes collection is a note's permanent home; the service
# raises ValueError -> the route maps it to 400.
assert r.status_code == 400
assert r.json()["success"] is False
def test_remove_from_collection_not_a_member_404(self, client):
_, note_id = _create_note(client)
r = client.delete(
f"/notes/api/notes/{note_id}/collections/{_rand_id()}",
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 404
assert r.json()["error"] == "Not in collection"
# ---------------------------------------------------------------------------
# 5. Research links + annotations + document notes
# ---------------------------------------------------------------------------
class TestResearchAndDocuments:
def test_get_note_research_empty_for_new_note(self, client):
_, note_id = _create_note(client)
r = client.get(f"/notes/api/notes/{note_id}/research")
assert r.status_code == 200
payload = r.json()
assert payload["success"] is True
assert payload["research"] == []
def test_get_note_research_unknown_note_404(self, client):
r = client.get(f"/notes/api/notes/{_rand_id()}/research")
assert r.status_code == 404
assert r.json()["error"] == "Note not found"
def test_link_research_missing_id_400(self, client):
_, note_id = _create_note(client)
r = client.post(
f"/notes/api/notes/{note_id}/research",
json={},
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 400
assert r.json()["error"] == "research_id is required"
def test_patch_research_non_bool_is_collapsed_400(
self, client, loguru_caplog_full
):
# Validated before service.update_note_research is called — a
# nonexistent note/research pair still hits the 400, not a 404.
with loguru_caplog_full.at_level("WARNING"):
r = client.patch(
f"/notes/api/notes/{_rand_id()}/research/{_rand_id()}",
json={"is_collapsed": "yes"},
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 400
assert r.json()["error"] == "is_collapsed must be a boolean"
msgs = _validation_warnings(loguru_caplog_full)
assert msgs, "expected a validation-reject audit line"
assert "is_collapsed" in msgs[0]
assert "type=str" in msgs[0]
def test_get_research_notes_unknown_research_404(self, client):
r = client.get(f"/notes/api/research/{_rand_id()}/notes")
assert r.status_code == 404
assert r.json()["error"] == "Research not found"
def test_get_research_annotations_unknown_research_404(self, client):
r = client.get(f"/notes/api/research/{_rand_id()}/annotations")
assert r.status_code == 404
assert r.json()["error"] == "Research not found"
def test_create_research_annotation_unknown_research_404(self, client):
# A well-formed annotation body: validation passes, then the missing
# research row produces the 404 (not a validation 400).
r = client.post(
f"/notes/api/research/{_rand_id()}/annotations",
json={"comment": "a remark", "quote": "some quoted text"},
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 404
assert r.json()["error"] == "Research not found"
def test_create_research_annotation_missing_comment_400(self, client):
r = client.post(
f"/notes/api/research/{_rand_id()}/annotations",
json={"quote": "only a quote"},
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 400
# Validator text is no longer forwarded; the 400 still separates a
# malformed annotation from the 404 above.
assert r.json()["error"] == "Invalid annotation data"
def test_get_document_notes_unknown_document_404(self, client):
r = client.get(f"/notes/api/documents/{_rand_id()}/notes")
assert r.status_code == 404
assert r.json()["error"] == "Document not found"
def test_get_document_annotations_unknown_document_404(self, client):
r = client.get(f"/notes/api/documents/{_rand_id()}/annotations")
assert r.status_code == 404
assert r.json()["error"] == "Document not found"
# ---------------------------------------------------------------------------
# 6. Backlinks / outgoing-links / resolve-link / unlinked-mentions
# ---------------------------------------------------------------------------
class TestLinks:
def test_backlinks_shape_for_new_note(self, client):
_, note_id = _create_note(client)
r = client.get(f"/notes/api/notes/{note_id}/backlinks")
assert r.status_code == 200
payload = r.json()
assert payload["success"] is True
assert payload["backlinks"] == []
assert payload["total"] == 0
def test_outgoing_links_shape_for_new_note(self, client):
_, note_id = _create_note(client)
r = client.get(f"/notes/api/notes/{note_id}/outgoing-links")
assert r.status_code == 200
payload = r.json()
assert payload["success"] is True
assert payload["links"] == []
assert payload["total"] == 0
def test_backlinks_unknown_note_404(self, client):
r = client.get(f"/notes/api/notes/{_rand_id()}/backlinks")
assert r.status_code == 404
def test_outgoing_links_unknown_note_404(self, client):
r = client.get(f"/notes/api/notes/{_rand_id()}/outgoing-links")
assert r.status_code == 404
def test_unlinked_mentions_shape_for_new_note(self, client):
_, note_id = _create_note(client)
r = client.get(f"/notes/api/notes/{note_id}/unlinked-mentions")
assert r.status_code == 200
payload = r.json()
assert payload["success"] is True
assert "mentions" in payload
def test_resolve_link_found(self, client):
title = "Resolvable " + uuid.uuid4().hex[:8]
_, note_id = _create_note(client, title=title)
r = client.post(
"/notes/api/notes/resolve-link",
json={"link_text": title},
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 200
payload = r.json()
assert payload["success"] is True
assert payload["note"]["id"] == note_id
def test_resolve_link_not_found_404(self, client):
r = client.post(
"/notes/api/notes/resolve-link",
json={"link_text": "no-such-note-" + uuid.uuid4().hex},
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 404
assert r.json()["error"] == "Note not found"
def test_resolve_link_missing_text_400(self, client):
r = client.post(
"/notes/api/notes/resolve-link",
json={},
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 400
assert r.json()["error"] == "link_text is required"
# ---------------------------------------------------------------------------
# 7. Pages
# ---------------------------------------------------------------------------
class TestPages:
def test_notes_list_page_renders_html(self, client):
r = client.get("/notes/", follow_redirects=False)
assert r.status_code == 200
assert r.headers.get("content-type", "").startswith("text/html")
def test_note_detail_page_renders_html(self, client):
_, note_id = _create_note(client)
r = client.get(f"/notes/{note_id}", follow_redirects=False)
assert r.status_code == 200
assert r.headers.get("content-type", "").startswith("text/html")
# ---------------------------------------------------------------------------
# 8. Version history
# ---------------------------------------------------------------------------
class TestVersions:
def test_versions_present_for_new_note(self, client):
# create_note writes an INITIAL version snapshot, so a fresh note
# already has >= 1 version.
_, note_id = _create_note(client)
r = client.get(f"/notes/api/notes/{note_id}/versions")
assert r.status_code == 200
payload = r.json()
assert payload["success"] is True
assert payload["total"] >= 1
assert payload["versions"]
first = payload["versions"][0]
assert "id" in first and "version_number" in first
def test_versions_unknown_note_404(self, client):
r = client.get(f"/notes/api/notes/{_rand_id()}/versions")
assert r.status_code == 404
def test_specific_version_unknown_id_404(self, client):
_, note_id = _create_note(client)
r = client.get(f"/notes/api/notes/{note_id}/versions/{_rand_id()}")
assert r.status_code == 404
assert r.json()["error"] == "Version not found"
# ---------------------------------------------------------------------------
# 9. AI endpoints (NoteAIService mocked)
# ---------------------------------------------------------------------------
class TestAIEndpoints:
def test_summarize_returns_mocked_summary(self, client, monkeypatch):
_, note_id = _create_note(client, content="something to summarize")
_install_fake_ai(monkeypatch, summarize_note="MOCK SUMMARY")
r = client.post(
f"/notes/api/notes/{note_id}/summarize",
json={},
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 200
payload = r.json()
assert payload["success"] is True
assert payload["summary"] == "MOCK SUMMARY"
def test_summarize_note_not_found_404(self, client, monkeypatch):
# summarize_note returns None for a missing/empty note -> 404.
_install_fake_ai(monkeypatch, summarize_note=None)
r = client.post(
f"/notes/api/notes/{_rand_id()}/summarize",
json={},
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 404
assert r.json()["error"] == "Note not found or empty"
def test_suggest_tags_returns_mocked_tags(self, client, monkeypatch):
_install_fake_ai(monkeypatch, suggest_tags=["mock-tag-a", "mock-tag-b"])
r = client.post(
"/notes/api/notes/suggest-tags",
json={"content": "content to analyze for tags"},
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 200
payload = r.json()
assert payload["success"] is True
assert payload["tags"] == ["mock-tag-a", "mock-tag-b"]
def test_suggest_tags_missing_content_400(self, client):
# Validated before any AI call — no mock required.
r = client.post(
"/notes/api/notes/suggest-tags",
json={},
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 400
assert r.json()["error"] == "content is required"
def test_semantic_search_missing_query_400(self, client):
# Query validation happens before the AI service is constructed.
r = client.get("/notes/api/notes/semantic-search")
assert r.status_code == 400
assert r.json()["error"] == "Query is required"
@pytest.mark.parametrize("value", ["nan", "inf", "-inf"])
def test_semantic_search_non_finite_min_similarity_400(
self, client, value, loguru_caplog_full
):
# NaN survives max()/min() unchanged, so a non-finite min_similarity
# needs its own math.isfinite() guard, checked before the AI call.
with loguru_caplog_full.at_level("WARNING"):
r = client.get(
f"/notes/api/notes/semantic-search?q=topic&min_similarity={value}"
)
assert r.status_code == 400
assert r.json()["error"] == "Invalid limit or min_similarity"
msgs = _validation_warnings(loguru_caplog_full)
assert msgs, "expected a validation-reject audit line"
assert "min_similarity" in msgs[0]
assert "non-finite" in msgs[0]
def test_similar_unknown_note_404(self, client):
# note_exists gate fires before any AI/embeddings call.
r = client.get(f"/notes/api/notes/{_rand_id()}/similar")
assert r.status_code == 404
assert r.json()["error"] == "Note not found"
def test_similar_passages_unknown_note_404(
self, client, loguru_caplog_full
):
# note_exists gate fires before any AI/embeddings call.
with loguru_caplog_full.at_level("WARNING"):
r = client.post(
f"/notes/api/notes/{_rand_id()}/similar-passages",
json={"text": "some selected passage text"},
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 404
assert r.json()["error"] == "Note not found"
msgs = _validation_warnings(loguru_caplog_full)
assert msgs, "expected a validation-reject audit line"
assert "similar-passages" in msgs[0]
assert "note_id" in msgs[0]
def test_synthesize_non_bool_create_note_400(
self, client, loguru_caplog_full
):
# Validated before the AI service is invoked — no mock required.
with loguru_caplog_full.at_level("WARNING"):
r = client.post(
"/notes/api/notes/synthesize",
json={
"note_ids": [_rand_id(), _rand_id()],
"synthesis_type": "merge",
"create_note": "yes",
},
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 400
assert r.json()["error"] == "create_note must be a boolean"
msgs = _validation_warnings(loguru_caplog_full)
assert msgs, "expected a validation-reject audit line"
assert "create_note" in msgs[0]
assert "type=str" in msgs[0]
# ------------------------------------------------------------------
# Omitted-default controls, ported from main's #5500.
#
# main added these to tests/notes/test_notes_api_validation_followups.py,
# a Flask-shaped file this branch deletes, so the merge surfaced them as
# a modify/delete conflict. Resolving "ours" would have silently dropped
# the coverage. The two tests above pin that a NON-BOOL value is
# REJECTED; these pin the other half -- that OMITTING the key is
# accepted and takes the documented default. A guard written as
# `if not isinstance(x, bool)` on `data.get("x")` (no default) rejects
# the omitted case too, and only this direction catches that.
#
# Driven through the real HTTP router rather than main's direct-handler
# call, so the default is asserted where the user meets it.
# ------------------------------------------------------------------
def test_index_omitted_force_reindex_defaults_to_false(
self, client, monkeypatch
):
calls = []
class _FakeRag:
def index_document(self, **kwargs):
calls.append(kwargs)
return {"status": "indexed"}
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
return False
monkeypatch.setattr(
"local_deep_research.web.routers.rag.get_rag_service",
lambda request, username, collection_id: _FakeRag(),
)
_, note_id = _create_note(client, title="Index Me")
r = client.post(
f"/notes/api/notes/{note_id}/index",
json={"collection_id": _rand_id()}, # force_reindex omitted
headers={"X-CSRFToken": _csrf(client)},
)
assert r.status_code == 200, r.text
assert r.json()["success"] is True
# The point of the test: the omitted key reaches the RAG service as
# False rather than being rejected by the boolean guard or arriving
# as None.
assert len(calls) == 1, calls
assert calls[0]["force_reindex"] is False
assert calls[0]["document_id"] == note_id
def test_synthesize_omitted_create_note_defaults_to_true(
self, client, monkeypatch
):
_, first_id = _create_note(client, title="Source A", content="alpha")
_, second_id = _create_note(client, title="Source B", content="beta")
_install_fake_ai(
monkeypatch,
synthesize_notes={
"suggested_title": "Synthesized Title",
"content": "Synthesized body",
# Real ids: the handler persists these as NoteSynthesisSource
# rows, so bogus ones would FK-violate.
"source_notes": [{"id": first_id}, {"id": second_id}],
},
)
r = client.post(
"/notes/api/notes/synthesize",
json={ # create_note omitted
"note_ids": [first_id, second_id],
"synthesis_type": "merge",
},
headers={"X-CSRFToken": _csrf(client)},
)
# 201 rather than 200 IS the default taking effect: the handler
# returns 200 when create_note is false.
assert r.status_code == 201, r.text
body = r.json()
assert body["success"] is True
# Stronger than asserting a mock call: the note is really persisted,
# so a default that reached the branch but failed to write would
# still fail here.
new_note_id = body["result"]["note_id"]
fetched = client.get(f"/notes/api/notes/{new_note_id}")
assert fetched.status_code == 200, fetched.text
assert fetched.json()["note"]["title"] == "Synthesized Title"