Skip to content

~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.

44.0 KB
Raw Blame
"""COMPLETE USER JOURNEYS driven end to end over real HTTP.

Everything else on this branch tests units, single endpoints, or one
subsystem at a time.  This module drives whole journeys the way a person
uses the product, and asserts that **each step's output is actually
consumed by the next step** — the exact place a Flask -> FastAPI port
breaks, because a handler can keep its status code while quietly losing
the value the following handler needs.

Five journeys:

1. ``TestJourneyResearchLifecycle`` — register -> login -> start research
   -> poll status -> read report -> export (LaTeX / Quarto / RIS) ->
   delete.  Every hand-off is asserted on content, never on a status
   code: the report carries the synthesized answer AND the source the
   stubbed engine returned; the history listing carries the research the
   POST minted; each export body carries that same answer text (LaTeX
   escaped, Quarto unzipped from the archive) and the exported filename
   derives from the query the user typed; after delete the report, the
   status endpoint and the history listing all agree it is gone.

2. ``TestJourneyCollectionRetrieval`` — create collection -> upload
   document -> index -> search -> find the document.  The known adjacent
   defect (a mismatched search config builds an EMPTY index and PROMOTES
   it, so search silently returns nothing;
   ``tests/web/routers/test_rag_indexing_pipeline.py`` pins the
   mechanism) makes "the search returned 200" worthless here, so the
   assertion is that retrieval returns **the document id the upload
   minted**, with a snippet containing the uploaded bytes — and that a
   SECOND search still returns it, i.e. the first query did not demote
   the real index out from under the collection.

3. ``TestJourneyChatSession`` — create session -> send message -> get
   research -> follow up -> delete an attempt -> send again.  The
   follow-up assertion is that the first question actually reached the
   LLM prompt of the second run (context carried), not merely that a
   second run started.  ``tests/chat/test_chat_service_contracts.py``
   established that deleting a NON-newest attempt bricks the session;
   the strict xfail here is the HTTP-level consequence — the next send
   500s — while the rest of the journey (including deleting the NEWEST
   attempt and sending again) is proven to work around it.

4. ``TestJourneyNotesAnnotation`` — create note -> annotate a library
   document -> edit -> view the annotation.  Asserts the text-quote
   anchor round-trips, that the quote genuinely occurs in the document
   text the library serves, that editing the annotation's note is
   reflected in the annotation view and in note version history, and
   records what "editing the document" actually does in this product
   (there is no document-edit endpoint; re-uploading forks a SECOND
   document and the annotation does not follow it).

5. ``TestJourneySettingsBehaviour`` — change a setting -> verify the
   behaviour it names actually changes -> reset -> verify it reverts.
   ``llm.model`` is chosen because its behaviour is observable without
   ambiguity: with the shipped default (empty) a submission is REFUSED
   at the boundary; once set, the value reaches the LLM factory the
   background run instantiates (recorded by the stub); after
   ``reset_to_defaults`` the refusal comes back and the factory is never
   reached again.

NOT re-covered here (already covered, deliberately not duplicated):
``tests/web/test_long_integration_flows.py`` and
``…_followup.py`` (register/login/settings/collection/upload/logout
lifecycle, password change, queue lifecycle, collection cascade
deletion, ``policy.egress_scope`` observed across routers, restart
recovery), ``tests/web/routers/test_rag_indexing_pipeline.py`` (index
identity, force-reindex destruction, concurrent indexing),
``tests/chat/test_chat_service_contracts.py`` (the attempt-deletion
mechanism at service level), ``tests/web/services/
test_research_execution_boundary.py`` (worker ownership/cancellation).

HARNESS RULES
* Real HTTP via ``TestClient``, real CSRF, real per-user encrypted DBs,
  real background research threads, real FAISS index files, real
  exporters, real settings storage.
* The ONLY fakes are the three network/model boundaries: the chat model
  (an in-process ``BaseChatModel`` registered through the production LLM
  registry), the search engine's outbound HTTP (``WikipediaSearchEngine``
  preview/content fetch), and the embedding backend
  (``LocalEmbeddingManager._initialize_embeddings``).  No model is ever
  loaded and no socket is ever opened.
* Every test takes the ``app`` fixture from ``tests/conftest.py`` (fresh
  ``LDR_DATA_DIR`` and a fresh user per test), so journeys never share
  state and each stays well inside the per-user connection budget.
* Polling is bounded and always ends at a terminal status; no bare
  sleeps outside the poll helper.
"""

import io
import time
import uuid
import zipfile

import numpy as np
import pytest
from fastapi.testclient import TestClient
from langchain_core.embeddings import Embeddings
from langchain_core.language_models import BaseChatModel
from langchain_core.messages import AIMessage
from langchain_core.outputs import ChatGeneration, ChatResult

from local_deep_research.llm import register_llm, unregister_llm

TEST_PASSWORD = "JourneyPass123!"  # noqa: S105

# Deliberately alphanumeric: these markers are asserted inside LaTeX and
# Quarto output, and LaTeX escapes "_" to "\_" — a marker with an
# underscore would make the export assertions test the escaper instead of
# the hand-off.
PROVIDER = "journey_stub_llm"
ANSWER = "JOURNEYSYNTHESISMARKER"
SOURCE_TITLE = "Journey Stub Source"
SOURCE_URL = "https://journey.invalid/stub-source"
SNIPPET = "JOURNEYSEARCHSNIPPET"

# Text uploaded in the collection / notes journeys. The annotation journey
# anchors onto the middle sentence, so the three sentences must stay
# distinct.
DOCUMENT_TEXT = (
    "JOURNEYUPLOADMARKER The quick brown fox jumps over the lazy dog. "
    "Photosynthesis converts light energy into chemical energy in plants. "
    "The mitochondrion is the powerhouse of the cell.\n"
)
UPLOAD_MARKER = "JOURNEYUPLOADMARKER"
ANCHOR_QUOTE = "Photosynthesis converts light energy"
ANCHOR_PREFIX = "lazy dog. "
ANCHOR_SUFFIX = " into chemical"


# ---------------------------------------------------------------------------
# The three stubbed boundaries
# ---------------------------------------------------------------------------


class _StubChatModel(BaseChatModel):
    """In-process chat model. Never opens a socket."""

    @property
    def _llm_type(self) -> str:
        return "journey-stub-chat-model"

    def _generate(self, messages, stop=None, run_manager=None, **kwargs):
        _PROMPTS.append("\n".join(str(m.content) for m in messages))
        return ChatResult(
            generations=[ChatGeneration(message=AIMessage(content=ANSWER))]
        )


class _RecordingLLMFactory:
    """Registered as a FACTORY (not an instance) so the journey can observe
    which ``model_name`` the production ``get_llm`` dispatch handed it —
    that is the observable behaviour behind the ``llm.model`` setting."""

    def __call__(
        self, model_name=None, temperature=None, settings_snapshot=None
    ):
        _MODEL_NAMES.append(model_name)
        return _StubChatModel()


class _StubEmbeddings(Embeddings):
    """Deterministic bag-of-words embedding. Loads no model, hits no network,
    but is similarity-meaningful so a real FAISS retrieval either finds the
    uploaded document or genuinely fails to."""

    dim = 32

    def _vec(self, text):
        vec = np.zeros(self.dim)
        for token in set(str(text).lower().split()):
            vec[hash(token) % self.dim] += 1.0
        norm = np.linalg.norm(vec)
        return (vec / norm if norm else vec).tolist()

    def embed_documents(self, texts):
        return [self._vec(t) for t in texts]

    def embed_query(self, text):
        return self._vec(text)


# Per-test recorders, reset by the ``stubs`` fixture.
_PROMPTS: list[str] = []
_MODEL_NAMES: list[str | None] = []
_SEARCH_QUERIES: list[str] = []


@pytest.fixture
def stubs(monkeypatch):
    """Fake exactly three boundaries: chat model, search egress, embeddings."""
    _PROMPTS.clear()
    _MODEL_NAMES.clear()
    _SEARCH_QUERIES.clear()

    register_llm(PROVIDER, _RecordingLLMFactory())

    from local_deep_research.web_search_engines.engines import (
        search_engine_wikipedia as wikipedia_engine,
    )

    def _previews(_self, query):
        _SEARCH_QUERIES.append(query)
        return [
            {
                "id": "journey-1",
                "title": SOURCE_TITLE,
                "link": SOURCE_URL,
                "snippet": SNIPPET,
            }
        ]

    def _full_content(_self, relevant_items):
        return [
            dict(
                item, full_content=f"{SNIPPET} body", content=f"{SNIPPET} body"
            )
            for item in relevant_items
        ]

    monkeypatch.setattr(
        wikipedia_engine.WikipediaSearchEngine, "_get_previews", _previews
    )
    monkeypatch.setattr(
        wikipedia_engine.WikipediaSearchEngine,
        "_get_full_content",
        _full_content,
    )

    from local_deep_research.web_search_engines.engines import (
        local_embedding_manager,
    )

    monkeypatch.setattr(
        local_embedding_manager.LocalEmbeddingManager,
        "_initialize_embeddings",
        lambda _self: _StubEmbeddings(),
    )

    yield

    unregister_llm(PROVIDER)


# ---------------------------------------------------------------------------
# HTTP helpers (same shape as tests/web/test_long_integration_flows*.py)
# ---------------------------------------------------------------------------


def _new_client(app) -> TestClient:
    client = TestClient(app, raise_server_exceptions=False)
    # Unique forwarded IP per client so per-IP rate-limit buckets from one
    # journey never leak into another.
    client.headers.update(
        {"X-Forwarded-For": f"10.{uuid.uuid4().int % 254 + 1}.9.1"}
    )
    return client


def _csrf(client: TestClient) -> str:
    client.get("/auth/login")
    resp = client.get("/auth/csrf-token")
    return resp.json().get("csrf_token", "") if resp.status_code == 200 else ""


def _drop_stale_csrf_header(client: TestClient) -> None:
    """A persistent X-CSRFToken default header shadows a fresh csrf_token
    form field once the session rotates — see
    tests/web/test_long_integration_flows.py::_drop_stale_csrf_header."""
    for name in ("X-CSRFToken", "X-CSRF-Token"):
        client.headers.pop(name, None)


def _register_and_login(client: TestClient) -> str:
    username = f"journey_{uuid.uuid4().hex[:12]}"

    _drop_stale_csrf_header(client)
    resp = client.post(
        "/auth/register",
        data={
            "username": username,
            "password": TEST_PASSWORD,
            "confirm_password": TEST_PASSWORD,
            "acknowledge": "true",
            "csrf_token": _csrf(client),
        },
        follow_redirects=False,
    )
    assert resp.status_code == 302, (
        f"register did not complete: {resp.status_code} {resp.text[:300]}"
    )

    _drop_stale_csrf_header(client)
    resp = client.post(
        "/auth/login",
        data={
            "username": username,
            "password": TEST_PASSWORD,
            "remember": "false",
            "csrf_token": _csrf(client),
        },
        follow_redirects=False,
    )
    assert resp.status_code == 302, (
        f"login did not complete: {resp.status_code} {resp.text[:300]}"
    )

    token = client.get("/auth/csrf-token").json()["csrf_token"]
    client.headers.update({"X-CSRFToken": token})
    return username


_TERMINAL_EXCLUDED = ("in_progress", "queued", "pending")


def _poll_until_terminal(client: TestClient, research_id: str) -> dict:
    """Poll the status endpoint the UI polls until the run leaves a
    non-terminal state.  Bounded; a timeout is reported as the journey
    step that broke rather than hanging the suite."""
    payload: dict = {}
    for _ in range(60):
        resp = client.get(f"/api/research/{research_id}/status")
        assert resp.status_code == 200, (
            "poll status: research the start endpoint minted is not "
            f"readable: {resp.status_code} {resp.text[:300]}"
        )
        payload = resp.json()
        if payload.get("status") not in _TERMINAL_EXCLUDED:
            return payload
        # allow: unmarked-sleep -- bounded poll of a REAL background
        # thread; there is no clock to travel, only work to wait for.
        time.sleep(0.25)  # allow: unmarked-sleep
    pytest.fail(
        "poll status: research never reached a terminal status "
        f"(last: {payload.get('status')!r})"
    )


def _configure_run_settings(client: TestClient) -> None:
    """One bulk write of the settings a background run reads when the
    caller supplies no per-request overrides (chat has no override
    channel)."""
    resp = client.post(
        "/settings/save_all_settings",
        json={
            "llm.provider": PROVIDER,
            "llm.model": "journeymodel",
            "search.tool": "wikipedia",
            "search.iterations": 1,
            "search.questions_per_iteration": 1,
            "search.search_strategy": "source-based",
        },
    )
    assert resp.status_code == 200, resp.text[:300]
    assert resp.json().get("status") == "success", resp.text[:300]


def _start_research(client: TestClient, query: str) -> str:
    resp = client.post(
        "/api/start_research",
        json={
            "query": query,
            "mode": "quick",
            "model_provider": PROVIDER,
            "model": "journeymodel",
            "search_engine": "wikipedia",
            "iterations": 1,
            "questions_per_iteration": 1,
            "strategy": "source-based",
        },
    )
    assert resp.status_code == 200, (
        f"start research: {resp.status_code} {resp.text[:400]}"
    )
    research_id = resp.json().get("research_id")
    assert research_id, f"start research returned no id: {resp.text[:300]}"
    return research_id


def _upload_document(
    client: TestClient, collection_id: str, filename: str, text: str
):
    return client.post(
        f"/library/api/collections/{collection_id}/upload",
        files=[("files", (filename, io.BytesIO(text.encode()), "text/plain"))],
    )


def _create_collection(client: TestClient, name: str) -> str:
    resp = client.post(
        "/library/api/collections", json={"name": name, "description": ""}
    )
    assert resp.status_code == 200, (
        f"create collection: {resp.status_code} {resp.text[:300]}"
    )
    collection_id = (resp.json().get("collection") or {}).get("id")
    assert collection_id, (
        "create collection reported success but returned no id the next "
        f"step could use: {resp.text[:300]}"
    )
    return collection_id


# ---------------------------------------------------------------------------
# Journey 1 — research lifecycle
# ---------------------------------------------------------------------------


class TestJourneyResearchLifecycle:
    """register -> login -> start -> poll -> report -> export -> delete."""

    def test_full_research_lifecycle_each_step_consumes_the_previous(
        self, app, stubs
    ):
        client = _new_client(app)
        _register_and_login(client)

        query = "journey lifecycle query"
        research_id = _start_research(client, query)

        status = _poll_until_terminal(client, research_id)
        assert status["status"] == "completed", (
            "poll status: the run the start endpoint accepted did not "
            f"complete: {status}"
        )

        # --- read report: must carry what the run produced, not just 200.
        report = client.get(f"/api/report/{research_id}")
        assert report.status_code == 200, report.text[:300]
        body = report.json()
        content = body["content"]
        assert ANSWER in content, (
            "read report: the report does not contain the synthesized "
            f"answer the run produced. Got: {content[:300]!r}"
        )
        assert SOURCE_URL in content, (
            "read report: the source the search step returned never "
            f"reached the report. Got: {content[:300]!r}"
        )
        assert body["metadata"]["query"] == query, (
            "read report: the report is not bound to the query the user "
            f"submitted: {body['metadata'].get('query')!r}"
        )
        assert any(s.get("url") == SOURCE_URL for s in body["sources"]), (
            f"read report: structured sources lost the URL: {body['sources']}"
        )

        # --- the history listing the UI reads must show the same run.
        history = client.get("/history/api")
        assert history.status_code == 200, history.text[:300]
        items = history.json()["items"]
        listed = [i for i in items if i.get("id") == research_id]
        assert listed, (
            "read history: the research the start endpoint minted is not "
            f"in the user's history listing: {items}"
        )
        assert listed[0].get("query") == query, (
            f"read history: listed row lost the query: {listed[0]}"
        )

        # --- export: the file must CONTAIN the report, not merely be 200.
        latex = client.post(f"/api/v1/research/{research_id}/export/latex")
        assert latex.status_code == 200, latex.text[:300]
        latex_text = latex.content.decode()
        assert ANSWER in latex_text, (
            "export latex: the downloaded file does not contain the "
            f"report body. Got: {latex_text[:400]!r}"
        )
        assert SOURCE_URL in latex_text, (
            "export latex: the report's source never reached the export."
        )
        # The filename is derived from the research the user is exporting;
        # a generic name would mean the export ignored its input.
        assert "journey_lifecycle_query" in latex.headers.get(
            "content-disposition", ""
        ), (
            "export latex: filename is not derived from the query: "
            f"{latex.headers.get('content-disposition')!r}"
        )

        quarto = client.post(f"/api/v1/research/{research_id}/export/quarto")
        assert quarto.status_code == 200, quarto.text[:300]
        archive = zipfile.ZipFile(io.BytesIO(quarto.content))
        qmd_names = [n for n in archive.namelist() if n.endswith(".qmd")]
        assert qmd_names, (
            f"export quarto: archive has no .qmd: {archive.namelist()}"
        )
        qmd = archive.read(qmd_names[0]).decode()
        assert ANSWER in qmd, (
            f"export quarto: the .qmd lacks the report body: {qmd[:400]!r}"
        )

        ris = client.post(f"/api/v1/research/{research_id}/export/ris")
        assert ris.status_code == 200, ris.text[:300]
        ris_text = ris.content.decode()
        assert SOURCE_URL in ris_text, (
            "export ris: the citation export contains none of the report's "
            f"sources: {ris_text[:300]!r}"
        )

        # --- delete: must actually delete, on every read path.
        deleted = client.request("DELETE", f"/api/delete/{research_id}")
        assert deleted.status_code == 200, deleted.text[:300]
        assert deleted.json()["status"] == "success", deleted.text[:300]

        assert client.get(f"/api/report/{research_id}").status_code == 404, (
            "delete: the report is still readable after a successful delete"
        )
        assert (
            client.get(f"/api/research/{research_id}/status").status_code == 404
        ), "delete: the status endpoint still resolves the deleted research"
        after = client.get("/history/api")
        assert after.status_code == 200, after.text[:300]
        assert not [
            i for i in after.json()["items"] if i.get("id") == research_id
        ], (
            "delete: the deleted research is still in the history listing: "
            f"{after.json()['items']}"
        )


# ---------------------------------------------------------------------------
# Journey 2 — collection retrieval
# ---------------------------------------------------------------------------


class TestJourneyCollectionRetrieval:
    """create collection -> upload -> index -> search -> find the document.

    "Search returned 200" proves nothing here: the adjacent confirmed
    defect (see module docstring) makes an EMPTY promoted index answer
    200 with zero results forever.  So the assertions are on the
    identity of what comes back and on the index surviving a query.
    """

    def _wait_for_index(self, client, collection_id) -> dict:
        payload: dict = {}
        for _ in range(60):
            resp = client.get(
                f"/library/api/collections/{collection_id}/index/status"
            )
            assert resp.status_code == 200, resp.text[:300]
            payload = resp.json()
            if payload.get("status") not in ("processing", "pending", None):
                return payload
            # allow: unmarked-sleep -- bounded poll of a REAL background
            # thread; there is no clock to travel, only work to wait for.
            time.sleep(0.25)  # allow: unmarked-sleep
        pytest.fail(
            f"index never reached a terminal status: {payload.get('status')!r}"
        )

    def test_uploaded_document_is_retrievable_after_indexing(self, app, stubs):
        client = _new_client(app)
        _register_and_login(client)

        collection_id = _create_collection(client, "Journey Collection")

        upload = _upload_document(
            client, collection_id, "journey.txt", DOCUMENT_TEXT
        )
        assert upload.status_code == 200, upload.text[:400]
        uploaded = upload.json()["uploaded"]
        assert len(uploaded) == 1, upload.text[:400]
        document_id = uploaded[0].get("id")
        assert document_id, (
            "upload reported success but returned no document id for the "
            f"next step: {upload.text[:300]}"
        )
        assert uploaded[0]["text_length"] == len(DOCUMENT_TEXT), (
            "upload: the extracted text length does not match the bytes "
            f"uploaded: {uploaded[0]}"
        )

        listing = client.get(
            f"/library/api/collections/{collection_id}/documents"
        )
        assert listing.status_code == 200, listing.text[:300]
        docs = listing.json()["documents"]
        assert [d["id"] for d in docs] == [document_id], (
            "list documents: the collection does not contain exactly the "
            f"document the upload minted: {docs}"
        )

        started = client.post(
            f"/library/api/collections/{collection_id}/index/start", json={}
        )
        assert started.status_code == 200, started.text[:400]
        assert started.json().get("task_id"), (
            f"index start returned no task id to poll: {started.text[:300]}"
        )

        index_status = self._wait_for_index(client, collection_id)
        assert index_status["status"] == "completed", (
            f"indexing did not complete: {index_status}"
        )
        result = index_status["result"]
        assert result["successful"] == 1 and result["failed"] == 0, (
            f"indexing reported success without indexing the document: {result}"
        )
        assert result["durable_indexed_chunks"] >= 1, (
            "indexing reported success but wrote no durable vectors — the "
            f"empty-index-promoted shape: {result}"
        )

        search = client.post(
            f"/library/api/collections/{collection_id}/search",
            json={"query": "photosynthesis light energy", "limit": 5},
        )
        assert search.status_code == 200, search.text[:400]
        results = search.json()["results"]
        assert results, (
            "search: retrieval returned NOTHING for a query drawn verbatim "
            "from the indexed document — the empty-promoted-index shape"
        )
        assert results[0]["document_id"] == document_id, (
            "search: the top hit is not the document the upload minted: "
            f"{results[0]}"
        )
        assert UPLOAD_MARKER in results[0]["snippet"], (
            "search: the hit's snippet does not contain the uploaded bytes: "
            f"{results[0]['snippet'][:200]!r}"
        )

        # A second query must still find it: the known defect DEMOTES the
        # real index the first time a mismatched config resolves, so one
        # successful search is not evidence the index survived.
        again = client.post(
            f"/library/api/collections/{collection_id}/search",
            json={"query": "photosynthesis light energy", "limit": 5},
        )
        assert again.status_code == 200, again.text[:300]
        assert [r["document_id"] for r in again.json()["results"]] == [
            document_id
        ], (
            "search: the second identical query no longer finds the "
            "document — the first search demoted the real index: "
            f"{again.json()['results']}"
        )

        final = client.get(
            f"/library/api/collections/{collection_id}/documents"
        )
        doc_state = final.json()["documents"][0]
        assert doc_state["indexed"] is True and doc_state["chunk_count"] >= 1, (
            f"the document is not recorded as indexed after search: {doc_state}"
        )


# ---------------------------------------------------------------------------
# Journey 3 — chat session
# ---------------------------------------------------------------------------


class TestJourneyChatSession:
    """create session -> send -> get research -> follow up -> delete an
    attempt -> send again."""

    def _send(self, client, session_id, content):
        return client.post(
            f"/api/chat/sessions/{session_id}/messages",
            json={"content": content},
        )

    def test_chat_journey_completes_around_a_newest_attempt_deletion(
        self, app, stubs
    ):
        client = _new_client(app)
        _register_and_login(client)
        _configure_run_settings(client)

        created = client.post("/api/chat/sessions", json={"title": "Journey"})
        assert created.status_code == 200, created.text[:300]
        session_id = created.json().get("session_id")
        assert session_id, (
            f"create session returned no id: {created.text[:300]}"
        )

        first_question = "JOURNEYFIRSTQUESTION about zebras"
        first = self._send(client, session_id, first_question)
        assert first.status_code == 200, (
            f"send message: {first.status_code} {first.text[:400]}"
        )
        first_research = first.json().get("research_id")
        assert first_research, (
            "send message reported success but started no research for the "
            f"next step: {first.text[:300]}"
        )

        assert (
            _poll_until_terminal(client, first_research)["status"]
            == "completed"
        )

        # --- get research: the answer must land in the session transcript
        # AND be readable through the research report endpoint.
        messages = client.get(f"/api/chat/sessions/{session_id}/messages")
        assert messages.status_code == 200, messages.text[:300]
        payload = messages.json()["messages"]
        user_turns = [m for m in payload if m["role"] == "user"]
        assert [m["content"] for m in user_turns] == [first_question], (
            f"the user's message is not in the transcript: {user_turns}"
        )
        responses = [m for m in payload if m.get("message_type") == "response"]
        assert responses, (
            "the completed research produced no assistant response message "
            f"in the session: {[m.get('message_type') for m in payload]}"
        )
        assert ANSWER in responses[-1]["content"], (
            "the assistant response does not carry the synthesized answer: "
            f"{responses[-1]['content'][:200]!r}"
        )
        assert responses[-1]["research_id"] == first_research, (
            "the response message is not bound to the research the send "
            f"started: {responses[-1]}"
        )

        report = client.get(f"/api/report/{first_research}")
        assert report.status_code == 200, report.text[:300]
        assert ANSWER in report.json()["content"], (
            "the chat-started research has no readable report"
        )

        # --- follow up: the second run must actually receive the first
        # question as context. A second run merely STARTING would pass a
        # status-code-only test while the context hand-off was broken.
        prompts_before = len(_PROMPTS)
        follow_up = self._send(
            client, session_id, "JOURNEYFOLLOWUP what about stripes"
        )
        assert follow_up.status_code == 200, follow_up.text[:400]
        second_research = follow_up.json().get("research_id")
        assert second_research and second_research != first_research, (
            f"follow-up did not start a distinct research: {follow_up.text[:300]}"
        )
        assert (
            _poll_until_terminal(client, second_research)["status"]
            == "completed"
        )
        assert any(
            "JOURNEYFIRSTQUESTION" in p for p in _PROMPTS[prompts_before:]
        ), (
            "follow up: the first question never reached the follow-up "
            "run's LLM prompt — chat context was not carried across the "
            "hand-off"
        )

        # --- delete the NEWEST attempt, then send again.
        deleted = client.request(
            "DELETE",
            f"/api/chat/sessions/{session_id}/attempts/{second_research}",
        )
        assert deleted.status_code == 200, (
            f"deleting the newest attempt: {deleted.status_code} "
            f"{deleted.text[:300]}"
        )
        assert deleted.json().get("success") is True, deleted.text[:300]

        after_delete = client.get(f"/api/chat/sessions/{session_id}/messages")
        assert after_delete.status_code == 200, after_delete.text[:300]
        remaining = after_delete.json()["messages"]
        assert not [
            m for m in remaining if m.get("research_id") == second_research
        ], (
            "deleting the attempt left its messages in the transcript: "
            f"{[m.get('research_id') for m in remaining]}"
        )
        assert [
            m for m in remaining if m.get("research_id") == first_research
        ], "deleting the newest attempt destroyed the earlier attempt too"

        resent = self._send(client, session_id, "JOURNEYRESEND after delete")
        assert resent.status_code == 200, (
            "send again: the session is unusable after deleting its NEWEST "
            f"attempt: {resent.status_code} {resent.text[:400]}"
        )
        third_research = resent.json().get("research_id")
        assert third_research, resent.text[:300]
        assert (
            _poll_until_terminal(client, third_research)["status"]
            == "completed"
        ), "send again: the re-sent message's research did not complete"

    def test_deleting_a_non_newest_attempt_leaves_the_session_usable(
        self, app, stubs
    ):
        client = _new_client(app)
        _register_and_login(client)
        _configure_run_settings(client)

        session_id = client.post(
            "/api/chat/sessions", json={"title": "Journey"}
        ).json()["session_id"]

        research_ids = []
        for index in range(2):
            resp = self._send(client, session_id, f"JOURNEYQUESTION {index}")
            assert resp.status_code == 200, resp.text[:400]
            research_id = resp.json()["research_id"]
            research_ids.append(research_id)
            assert (
                _poll_until_terminal(client, research_id)["status"]
                == "completed"
            )

        # Delete the OLDER of the two attempts.
        deleted = client.request(
            "DELETE",
            f"/api/chat/sessions/{session_id}/attempts/{research_ids[0]}",
        )
        assert deleted.status_code == 200, deleted.text[:300]

        resent = self._send(client, session_id, "JOURNEYRESEND after delete")
        assert resent.status_code == 200, (
            "the chat session is bricked after deleting a non-newest "
            f"attempt: {resent.status_code} {resent.text[:300]}"
        )
        assert (
            _poll_until_terminal(client, resent.json()["research_id"])["status"]
            == "completed"
        )


# ---------------------------------------------------------------------------
# Journey 4 — notes and annotations
# ---------------------------------------------------------------------------


class TestJourneyNotesAnnotation:
    """create note -> annotate a document -> edit -> view the annotation."""

    def test_annotation_round_trips_and_survives_an_edit(self, app, stubs):
        client = _new_client(app)
        _register_and_login(client)

        collection_id = _create_collection(client, "Journey Notes")
        upload = _upload_document(
            client, collection_id, "notes.txt", DOCUMENT_TEXT
        )
        assert upload.status_code == 200, upload.text[:400]
        document_id = upload.json()["uploaded"][0]["id"]

        # The annotation anchors into the document TEXT the library serves,
        # so the anchor is only meaningful if that text really contains it.
        text_resp = client.get(f"/library/api/document/{document_id}/text")
        assert text_resp.status_code == 200, text_resp.text[:300]
        document_text = text_resp.json()["text_content"]
        assert ANCHOR_QUOTE in document_text, (
            "the text the library serves does not contain the passage the "
            f"user is about to annotate: {document_text[:200]!r}"
        )

        note = client.post(
            "/notes/api/notes",
            json={"title": "Journey note", "content": "standalone note body"},
        )
        assert note.status_code == 201, note.text[:300]
        assert note.json().get("id"), note.text[:300]

        created = client.post(
            f"/notes/api/documents/{document_id}/annotations",
            json={
                "comment": "JOURNEYANNOTATION",
                "quote": ANCHOR_QUOTE,
                "prefix": ANCHOR_PREFIX,
                "suffix": ANCHOR_SUFFIX,
            },
        )
        assert created.status_code == 201, (
            f"annotate document: {created.status_code} {created.text[:400]}"
        )
        annotation = created.json()["annotation"]
        note_id = annotation["note_id"]
        assert note_id, f"annotation returned no note id: {created.text[:300]}"

        # --- view: the anchor must round-trip intact.
        listed = client.get(f"/notes/api/documents/{document_id}/annotations")
        assert listed.status_code == 200, listed.text[:300]
        annotations = listed.json()["annotations"]
        assert len(annotations) == 1, annotations
        stored = annotations[0]
        assert (
            stored["note_id"],
            stored["quote"],
            stored["prefix"],
            stored["suffix"],
        ) == (note_id, ANCHOR_QUOTE, ANCHOR_PREFIX, ANCHOR_SUFFIX), (
            f"the annotation anchor did not round-trip: {stored}"
        )
        assert "JOURNEYANNOTATION" in stored["comment_preview"], (
            f"the user's comment is not in the annotation view: {stored}"
        )

        # --- edit, then view again: the edit must be visible where the
        # user reads the annotation, not only on the note itself.
        current = client.get(f"/notes/api/notes/{note_id}")
        assert current.status_code == 200, current.text[:300]
        body = current.json()["note"]["content"]
        edited = client.put(
            f"/notes/api/notes/{note_id}",
            json={
                "title": "Comment: JOURNEYEDITED",
                "content": body.replace("JOURNEYANNOTATION", "JOURNEYEDITED"),
            },
        )
        assert edited.status_code == 200, (
            f"edit annotation: {edited.status_code} {edited.text[:300]}"
        )

        reread = client.get(f"/notes/api/documents/{document_id}/annotations")
        assert reread.status_code == 200, reread.text[:300]
        after = reread.json()["annotations"][0]
        assert "JOURNEYEDITED" in after["comment_preview"], (
            "the edit is not visible in the annotation view: "
            f"{after['comment_preview'][:200]!r}"
        )
        assert "JOURNEYANNOTATION" not in after["comment_preview"], (
            f"the annotation view still shows the pre-edit text: {after}"
        )
        assert (after["quote"], after["prefix"], after["suffix"]) == (
            ANCHOR_QUOTE,
            ANCHOR_PREFIX,
            ANCHOR_SUFFIX,
        ), f"editing the note destroyed its anchor into the document: {after}"

        versions = client.get(f"/notes/api/notes/{note_id}/versions")
        assert versions.status_code == 200, versions.text[:300]
        titles = [v["title"] for v in versions.json()["versions"]]
        assert "Comment: JOURNEYEDITED" in titles, (
            f"the edit produced no new version: {titles}"
        )
        assert any("JOURNEYANNOTATION" in t for t in titles), (
            f"the pre-edit version was not retained: {titles}"
        )

    def test_reuploading_an_edited_document_forks_it_and_orphans_the_annotation(
        self, app, stubs
    ):
        """There is no "edit this document" endpoint in the product: the
        library exposes no PUT/PATCH on a document's text.  Re-uploading a
        corrected file — the only way a user can act on "edit the
        document" — mints a SECOND document, and the annotation stays on
        the superseded copy.  Recorded as journey behaviour, not asserted
        as correct: the user who edits the document loses their annotation
        from the copy they will now be reading.
        """
        client = _new_client(app)
        _register_and_login(client)

        collection_id = _create_collection(client, "Journey Fork")
        original_id = _upload_document(
            client, collection_id, "fork.txt", DOCUMENT_TEXT
        ).json()["uploaded"][0]["id"]

        created = client.post(
            f"/notes/api/documents/{original_id}/annotations",
            json={
                "comment": "JOURNEYANNOTATION",
                "quote": ANCHOR_QUOTE,
                "prefix": ANCHOR_PREFIX,
                "suffix": ANCHOR_SUFFIX,
            },
        )
        assert created.status_code == 201, created.text[:300]

        revised = DOCUMENT_TEXT.replace(
            ANCHOR_QUOTE, "Photosynthesis turns sunlight"
        )
        reupload = _upload_document(client, collection_id, "fork.txt", revised)
        assert reupload.status_code == 200, reupload.text[:400]
        revised_id = reupload.json()["uploaded"][0]["id"]

        assert revised_id != original_id, (
            "re-uploading edited content updated the document in place — "
            "this test's premise (documents are immutable) no longer holds"
        )

        listing = client.get(
            f"/library/api/collections/{collection_id}/documents"
        )
        ids = {d["id"] for d in listing.json()["documents"]}
        assert {original_id, revised_id} <= ids, (
            f"expected both copies in the collection: {ids}"
        )

        on_revised = client.get(
            f"/notes/api/documents/{revised_id}/annotations"
        )
        assert on_revised.status_code == 200, on_revised.text[:300]
        assert on_revised.json()["annotations"] == [], (
            "the annotation unexpectedly followed the edited copy — "
            "re-check the fork semantics"
        )

        on_original = client.get(
            f"/notes/api/documents/{original_id}/annotations"
        )
        assert len(on_original.json()["annotations"]) == 1, (
            "the annotation is not on the original copy either — it was "
            f"lost entirely: {on_original.text[:300]}"
        )


# ---------------------------------------------------------------------------
# Journey 5 — a setting's named behaviour
# ---------------------------------------------------------------------------


class TestJourneySettingsBehaviour:
    """change a setting -> verify the behaviour -> reset -> verify revert.

    ``llm.model`` names exactly one behaviour — which model a research run
    instantiates — and that behaviour is observable end to end: the stub
    LLM is registered as a FACTORY, so it records the ``model_name`` the
    production dispatch hands it.  Reading the setting back is not enough
    and is not what this asserts.
    """

    _NO_OVERRIDES = {
        "query": "journey settings query",
        "mode": "quick",
        # Only the engine is pinned per-request, so the run is network-free
        # regardless of what the model setting is; provider/model come from
        # settings, which is the variable under test.
        "search_engine": "wikipedia",
        "strategy": "source-based",
    }

    def test_llm_model_setting_changes_the_run_and_reverts_on_reset(
        self, app, stubs
    ):
        client = _new_client(app)
        _register_and_login(client)

        shipped = client.get("/settings/api/llm.model")
        assert shipped.status_code == 200, shipped.text[:300]
        assert shipped.json()["value"] == "", (
            "premise: the shipped default for llm.model is empty; got "
            f"{shipped.json()['value']!r}"
        )

        # Behaviour under the default: a submission that relies on the
        # setting is REFUSED at the boundary.
        refused = client.post("/api/start_research", json=self._NO_OVERRIDES)
        assert refused.status_code == 400, (
            "baseline: a run with no model configured should be refused; "
            f"got {refused.status_code} {refused.text[:300]}"
        )
        assert "Model is required" in refused.json()["message"], refused.text[
            :300
        ]

        # --- change the setting.
        _configure_run_settings(client)
        assert (
            client.get("/settings/api/llm.model").json()["value"]
            == "journeymodel"
        )

        # --- the behaviour it names actually changed: the same submission
        # is now accepted, completes, and the run built its LLM with the
        # model the setting names.
        accepted = client.post("/api/start_research", json=self._NO_OVERRIDES)
        assert accepted.status_code == 200, (
            "after configuring llm.model the same submission is still "
            f"refused: {accepted.status_code} {accepted.text[:300]}"
        )
        research_id = accepted.json()["research_id"]
        status = _poll_until_terminal(client, research_id)
        assert status["status"] == "completed", status
        assert status["metadata"]["submission"]["model"] == "journeymodel", (
            "the run's recorded submission does not carry the configured "
            f"model: {status['metadata']['submission']}"
        )
        assert _MODEL_NAMES == ["journeymodel"], (
            "the configured model never reached the LLM the background run "
            f"instantiated: {_MODEL_NAMES}"
        )

        # --- reset.
        reset = client.post("/settings/reset_to_defaults", json={})
        assert reset.status_code == 200, reset.text[:300]
        assert reset.json()["status"] == "success", reset.text[:300]

        assert client.get("/settings/api/llm.model").json()["value"] == "", (
            "reset: llm.model did not return to its shipped default"
        )
        config = client.get("/research/api/settings/current-config")
        assert config.status_code == 200, config.text[:300]
        assert config.json()["config"]["model"] == "", (
            "reset: a DIFFERENT router still reports the pre-reset model: "
            f"{config.json()['config']}"
        )

        # --- the behaviour reverted too, not just the stored value.
        seen_before = len(_MODEL_NAMES)
        refused_again = client.post(
            "/api/start_research", json=self._NO_OVERRIDES
        )
        assert refused_again.status_code == 400, (
            "reset: the setting reads as default again but the run is still "
            f"accepted: {refused_again.status_code} {refused_again.text[:300]}"
        )
        assert "Model is required" in refused_again.json()["message"], (
            refused_again.text[:300]
        )
        assert _MODEL_NAMES[seen_before:] == [], (
            "reset: a run still reached the LLM factory after the model "
            f"setting was reset: {_MODEL_NAMES[seen_before:]}"
        )