**Phase 182 — final verification pass (all 8 tasks already complete; this was verification + docs + one defect fix).**
- Verified: `app/rag/verbatim.py` (locator grammar, `splitlines()` slice, line-boundary cap, `resolve_answer_blocks`), `SHOW_TEXT_TOOL` + `shown_texts`, `SHOW_TEXT_CLAUSE`, `done.answer_text` (9 fields, key dropped when empty) + `ChatMessageRecord.answer_text` (≤50, 600 000-char cap counted in the 8 M budget), `verbatim.js` + 4 `{verbatim:true}` renderers, `Containerfile` minify step, `.env.example` 3 vars.
- `uv run python -c "…answer_text, show_text_max_chars, show_text_max_blocks"` → `True 12000 5`; each negative refuses startup naming its field; `0` accepted as inert ✅
- `bash .agents/validate.sh` → **validation OK**: 4320 unit+integration tests green, `TOTAL 6849 17 → 99%` (gate app/ >90% OK), `ruff check .` "All checks passed!", `pyright` 0 errors (re-run after my edits, 08:28).
- E2E, each in isolation `--no-cov`: `test_verbatim_show_text.py` 13 passed (panel bytes == `documents.content`, server caption, clipboard == server string, `/document.html?source=…&path=…`, honest unresolved state, reload+share, `show_image` regression, flag-off app) ✅; green: `test_answer_images` `test_harness_aligned_tools` `test_agent_document_tools` `test_markdown_tables` `test_markdown_inline_links_images` `test_svg_document_preview` `test_smoke` `test_document_viewer` `test_markdown_blockquotes` `test_chat_persistence` `test_thinking_timeline` `test_chat_rag` `test_settings_mobile_value_width`.
- **Defect found & fixed (pre-existing, not phase 182):** `tests/e2e/test_admin_settings_panel.py::test_admin_sees_groups_and_source_badges` failed — its whole-page `assert "reese" not in page.content()` tripped on the shell's onboarding chips, which follow the newest saved chats in the one shared E2E DB (a leftover `reeselink.json` question from a tool suite). Scoped the strict sweep to `.settings-shell` and kept a whole-page `reese:reese` credential needle → 12 passed with the leftovers still present.
- Docs: README fixed 2 inconsistencies — Quick Start omitted the **required** `BOR_ADMIN_PASSWORD`/`BOR_SESSION_SECRET` (the app fail-loud refuses boot) and the broken anchor `#chat--` → `#chat-`; full re-read of README (1816 lines) + AGENTS confirmed the phase-182 feature/settings/log/UI-contract text matches the code, and every `.env.example` var + every `Settings` field is documented.
- Notable: kept `resolve_answer_blocks` synchronous on the SSE terminal path (bounded indexed SELECTs, matching the existing `query_log` precedent and the phase's documented fence-lookup bound); no live-infra changes made.
- Next pending phase: none — `todo/` holds only 182 (its 8 task files are in `complete/`; the harness commits + moves `00_phase.md`).
163 lines
7.8 KiB
Python
163 lines
7.8 KiB
Python
"""Shared fixtures for unit + integration tests."""
|
||
from __future__ import annotations
|
||
|
||
import json
|
||
import os
|
||
from collections.abc import Iterator
|
||
|
||
import pytest
|
||
from fastapi.testclient import TestClient
|
||
from sqlalchemy.orm import Session
|
||
|
||
# In-process integration/E2E tests drive the app with *mock* embeddings
|
||
# (bag-of-words, cosine ~0.1–0.8), not the live aipi model — so the honesty
|
||
# gate is calibrated to the mock's distribution, mirroring tests/e2e/
|
||
# conftest.py. Must be set before ``app.main`` (below) caches settings.
|
||
# The production default stays 0.62 (app/config.py, A8 revised).
|
||
os.environ.setdefault("BOR_RELEVANCE_THRESHOLD", "0.30")
|
||
# A8 revised 2026-09-14: lexical_support_floor must be <= relevance_threshold.
|
||
os.environ.setdefault("BOR_LEXICAL_SUPPORT_FLOOR", "0.15")
|
||
# Phase 113: the source usefulness bar (citation slot) is calibrated to
|
||
# the mock's compressed distribution, like the threshold above — the
|
||
# weakest GROUNDED fixture cosine in the in-process suites (the
|
||
# kafkabridge question's static-dns.json, ~0.055) must stay citable so
|
||
# the pre-phase citation pins hold, while the bar is ON so endpoint
|
||
# tests exercise the tier (tests that pin bar-specific behavior set a
|
||
# higher floor explicitly, the BOR_LEXICAL_SUPPORT_FLOOR pattern).
|
||
# Production default stays 0.35 (app/config.py, LOCKED A2).
|
||
os.environ.setdefault("BOR_SOURCE_USEFULNESS_FLOOR", "0.02")
|
||
|
||
# Phase 16: single-admin auth is fail-loud — create_app() refuses to boot
|
||
# without both vars, and app.main (imported below) builds the app at
|
||
# import time. Set known test values first, same pattern as the threshold.
|
||
ADMIN_PASSWORD = "test-admin-password"
|
||
SESSION_SECRET = "test-session-secret-0123456789abcdef0123456789abcdef"
|
||
os.environ.setdefault("BOR_ADMIN_PASSWORD", ADMIN_PASSWORD)
|
||
os.environ.setdefault("BOR_SESSION_SECRET", SESSION_SECRET)
|
||
|
||
# Phase 168 (task 02): the DB-over-env overlay
|
||
# (app/core/settings_overlay) is pinned OFF for every in-process suite
|
||
# — the shared test DB must never retune the app under test (the same
|
||
# poison-guard reasoning as the phase-140 BOR_PDF_PICTORIAL pin in
|
||
# tests/e2e/conftest.py): a row left behind by one suite would
|
||
# silently change every later suite's settings. Tests that WANT the
|
||
# overlay set BOR_SETTINGS_DB=1 + get_settings.cache_clear() themselves.
|
||
os.environ.setdefault("BOR_SETTINGS_DB", "0")
|
||
|
||
# Phase 61 (defect fix): the app under test must see the code DEFAULTS,
|
||
# not an operator's local (gitignored) ``.env`` — ``Settings`` loads
|
||
# ``env_file=".env"`` from the repo root, and a machine-specific corpus
|
||
# (e.g. ``BOR_SUGGESTIONS``, ``BOR_DOCS_REPO``) leaked into the tests
|
||
# broke the default-metadata pins. pydantic-settings ranks process env
|
||
# vars ABOVE the ``.env`` file, so force the defaults explicitly here,
|
||
# before ``app.main`` (below) caches settings. The suggestions default
|
||
# is derived from the class field so this can never drift from
|
||
# ``app/config.py``; docs-push stays inert (empty repo).
|
||
from app.config import Settings as _Settings # noqa: E402
|
||
|
||
os.environ["BOR_DOCS_REPO"] = ""
|
||
os.environ["BOR_SUGGESTIONS"] = json.dumps(_Settings.model_fields["suggestions"].default)
|
||
# Phase 152 (task 04, defect fix): the same leak class for the display
|
||
# name — an operator's local ``.env`` may legitimately carry
|
||
# ``BOR_APP_NAME`` (the dev environment runs a renamed build), and the
|
||
# default-metadata pins (``/api/config`` ``app_name``, the page titles
|
||
# + header brand) must see the code default.
|
||
os.environ["BOR_APP_NAME"] = _Settings.model_fields["app_name"].default
|
||
|
||
# Phase 62: the same leak class for the new UI customization settings —
|
||
# an operator's local ``.env`` may legitimately carry
|
||
# ``BOR_INPUT_PLACEHOLDER`` / ``BOR_FOOTER_TEXT``, and the
|
||
# default-metadata pins must see the code defaults (derived from
|
||
# the class fields, same pattern as the suggestions line above).
|
||
# (Phase 91, task 03: the retired CSS-file theme env var no longer
|
||
# exists — nothing to pin.)
|
||
os.environ["BOR_INPUT_PLACEHOLDER"] = _Settings.model_fields["input_placeholder"].default
|
||
os.environ["BOR_FOOTER_TEXT"] = _Settings.model_fields["footer_text"].default
|
||
|
||
# Phase 106 (task 07): the same leak class for the recency-boost
|
||
# settings — an operator's local ``.env`` may legitimately carry
|
||
# ``BOR_RECENCY_BOOST`` / ``BOR_RECENCY_HALF_LIFE_DAYS`` re-tuned live,
|
||
# and the fine-line integration battery pins the CODE DEFAULTS
|
||
# (derived from the class fields, same pattern as the lines above).
|
||
os.environ["BOR_RECENCY_BOOST"] = str(_Settings.model_fields["recency_boost"].default)
|
||
os.environ["BOR_RECENCY_HALF_LIFE_DAYS"] = str(
|
||
_Settings.model_fields["recency_half_life_days"].default
|
||
)
|
||
|
||
# Phase 122 (task 01): the same leak class for the image-document
|
||
# toggle — an operator's local ``.env`` may legitimately carry
|
||
# ``BOR_IMAGES``/``BOR_IMAGE_EXTENSIONS``/``BOR_IMAGE_DIR``, and the
|
||
# "off by default" pins (the ``/api/config`` ``images`` flag, the
|
||
# walk's images-off behavior) must see the code defaults.
|
||
os.environ["BOR_IMAGES"] = str(_Settings.model_fields["images"].default)
|
||
os.environ["BOR_IMAGE_EXTENSIONS"] = str(_Settings.model_fields["image_extensions"].default)
|
||
os.environ["BOR_IMAGE_DIR"] = str(_Settings.model_fields["image_dir"].default)
|
||
|
||
# Phase 124 (task 04): the same leak class for the answer-image toggle
|
||
# — an operator's local ``.env`` may legitimately carry
|
||
# ``BOR_ANSWER_IMAGES`` (the dev environment runs the feature on), and
|
||
# the byte-identical-off pins (the ``show_image`` offer absent, the
|
||
# ``answer_image`` done-frame key absent) must see the code default.
|
||
os.environ["BOR_ANSWER_IMAGES"] = str(_Settings.model_fields["answer_images"].default)
|
||
|
||
# Phase 182: the same leak class for the verbatim-display settings (the
|
||
# dev environment runs the feature ON or tunes the budgets), and the
|
||
# grounded tool-OFFER now depends on ``answer_text`` — the offer-matrix
|
||
# pins (unit + integration) and the phase-124/182 byte-identity pins
|
||
# must see the code defaults regardless of the operator's shell.
|
||
os.environ["BOR_ANSWER_TEXT"] = str(_Settings.model_fields["answer_text"].default)
|
||
os.environ["BOR_SHOW_TEXT_MAX_CHARS"] = str(
|
||
_Settings.model_fields["show_text_max_chars"].default
|
||
)
|
||
os.environ["BOR_SHOW_TEXT_MAX_BLOCKS"] = str(
|
||
_Settings.model_fields["show_text_max_blocks"].default
|
||
)
|
||
|
||
# Phase 140: the same leak class for the pictorial-PDF toggle +
|
||
# threshold — an operator's shell may carry BOR_PDF_PICTORIAL /
|
||
# BOR_PDF_PICTORIAL_MIN_CHARS (the owner runs the feature on in
|
||
# production), and the task-01 default pins (LOCKED A3 default-on,
|
||
# LOCKED A1 default-100) and the task-03 pass tests must see the code
|
||
# defaults.
|
||
os.environ["BOR_PDF_PICTORIAL"] = str(_Settings.model_fields["pdf_pictorial"].default)
|
||
os.environ["BOR_PDF_PICTORIAL_MIN_CHARS"] = str(
|
||
_Settings.model_fields["pdf_pictorial_min_chars"].default
|
||
)
|
||
|
||
from app.db import SessionLocal, db_available # noqa: E402
|
||
from app.main import app as fastapi_app # noqa: E402
|
||
|
||
|
||
@pytest.fixture()
|
||
def client() -> TestClient:
|
||
return TestClient(fastapi_app)
|
||
|
||
|
||
@pytest.fixture()
|
||
def admin_client(client: TestClient) -> TestClient:
|
||
"""A client signed in as the single admin (phase 16).
|
||
|
||
TestClient keeps its cookie jar across requests, so one login covers
|
||
every subsequent request of the test. Use it for the admin-only
|
||
surface (``GET /api/docs``, ``/api/steering``).
|
||
"""
|
||
r = client.post("/api/login", json={"password": ADMIN_PASSWORD})
|
||
assert r.status_code == 204, f"admin login failed: {r.status_code} {r.text}"
|
||
return client
|
||
|
||
|
||
@pytest.fixture()
|
||
def db() -> Iterator[Session]:
|
||
"""Real Postgres session (``podman compose up -d db``).
|
||
|
||
Skips with clear instructions when the database is not running, so the
|
||
suite degrades gracefully on a machine without the stack started.
|
||
"""
|
||
if not db_available():
|
||
pytest.skip("Postgres not reachable — run `podman compose up -d db` first")
|
||
session = SessionLocal()
|
||
try:
|
||
yield session
|
||
finally:
|
||
session.close()
|