forked from retoor/devplacepy
Happy 404: an HTML 404 (unmatched route, or an explicit not-found inside a
real route) now renders a random existing post instead of the error page,
using the exact same context builder as a real post view. Toggle is the
happy_404_enabled site setting (default on, /admin/settings); JSON/API
requests and a handful of excluded prefixes are never affected. The pool of
candidate slugs is cached in-process and resampled periodically so it stays
fast and eventually cycles the whole posts table; on any internal failure it
falls straight through to the real 404 page.
Applying this everywhere surfaced ~60 existing tests that asserted a literal
404 for a legitimate resource-not-found flow (deleted post, unknown
container, wrong project slug, etc.) - each now disables the setting for the
duration of that specific check and restores it after, so the underlying
not-found behavior stays covered independently of the new feature.
Post page also gained, all built on the same shared post_page_context() so
they render identically on both a real post and a happy-404 page:
- A left sidebar (three separate cards, matching /feed's sidebar-card
convention) for "Gists from {author}", "Projects from {author}" (private
projects filtered through the normal visibility check), and "Related
Discussions" - each cached per author and invalidated on create/edit/
delete so new content shows up immediately.
- A right column reusing /feed's exact Daily Topic widget class for up to
three "Featured" articles (the existing but previously-unused `featured`
news flag), cached as a pool with per-request random sampling.
- A "Next post -> " link beside "Back to Feed", pointing at the next older
post site-wide (blocked authors skipped). Wired through the same next_url
mechanism already used for listing pagination, so it emits a real
backend-rendered <link rel="next"> tag for SEO, not just a visible link.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BWJy6PrMMt5hwWxQwia2rd
222 lines
6.6 KiB
Python
222 lines
6.6 KiB
Python
# retoor <retoor@molodetz.nl>
|
|
|
|
import json
|
|
import time
|
|
|
|
import pytest
|
|
import requests
|
|
from tests.conftest import BASE_URL, CACHE_VERSION_PROPAGATION_SECONDS
|
|
from devplacepy.database import (
|
|
create_deepsearch_session,
|
|
get_table,
|
|
refresh_snapshot,
|
|
set_setting,
|
|
)
|
|
from devplacepy.services.jobs import queue
|
|
|
|
|
|
@pytest.fixture
|
|
def _no_happy_404(app_server):
|
|
set_setting("happy_404_enabled", "0")
|
|
time.sleep(CACHE_VERSION_PROPAGATION_SECONDS)
|
|
yield
|
|
set_setting("happy_404_enabled", "1")
|
|
time.sleep(CACHE_VERSION_PROPAGATION_SECONDS)
|
|
|
|
|
|
def _json_headers():
|
|
return {"Accept": "application/json"}
|
|
|
|
|
|
def _seed_done_session(owner_id="ds-session-owner"):
|
|
uid = queue.enqueue(
|
|
"deepsearch",
|
|
{"query": "the question", "depth": 2, "max_pages": 10},
|
|
"user",
|
|
owner_id,
|
|
"DeepSearch: the question",
|
|
)
|
|
create_deepsearch_session(
|
|
uid, "user", owner_id, "the question", 2, 10, f"ds_{uid.replace('-', '')}"
|
|
)
|
|
report = {
|
|
"query": "the question",
|
|
"summary": "A grounded summary.",
|
|
"findings": [
|
|
{"title": "Finding one", "detail": "Detail.", "confidence": 0.8, "citations": [1]}
|
|
],
|
|
"sources": [{"url": "https://example.com", "title": "Example", "source": "httpx"}],
|
|
"follow_up_questions": ["What is a follow-up question?"],
|
|
"score": 70,
|
|
"confidence": 0.7,
|
|
"source_diversity": 0.5,
|
|
"page_count": 3,
|
|
"chunk_count": 12,
|
|
}
|
|
result = {
|
|
"query": "the question",
|
|
"score": 70,
|
|
"confidence": 0.7,
|
|
"source_diversity": 0.5,
|
|
"page_count": 3,
|
|
"chunk_count": 12,
|
|
"report": report,
|
|
}
|
|
get_table("jobs").update(
|
|
{"uid": uid, "status": queue.DONE, "result": json.dumps(result)}, ["uid"]
|
|
)
|
|
get_table("deepsearch_sessions").update(
|
|
{
|
|
"uid": uid,
|
|
"status": "done",
|
|
"score": 70,
|
|
"confidence": 0.7,
|
|
"source_diversity": 0.5,
|
|
"page_count": 3,
|
|
"chunk_count": 12,
|
|
"summary": "A grounded summary.",
|
|
},
|
|
["uid"],
|
|
)
|
|
refresh_snapshot()
|
|
return uid
|
|
|
|
|
|
def _clear():
|
|
refresh_snapshot()
|
|
jobs = get_table("jobs")
|
|
for row in list(jobs.find(kind="deepsearch")):
|
|
jobs.delete(uid=row["uid"])
|
|
sessions = get_table("deepsearch_sessions")
|
|
for row in list(sessions.find()):
|
|
sessions.delete(uid=row["uid"])
|
|
|
|
|
|
def test_session_json_shape(app_server):
|
|
try:
|
|
uid = _seed_done_session()
|
|
r = requests.get(
|
|
f"{BASE_URL}/tools/deepsearch/{uid}/session", headers=_json_headers()
|
|
)
|
|
assert r.status_code == 200, r.text
|
|
body = r.json()
|
|
assert body["uid"] == uid
|
|
assert body["status"] == "done"
|
|
assert body["query"] == "the question"
|
|
assert body["score"] == 70
|
|
assert body["chat_ws_url"] == f"/tools/deepsearch/{uid}/chat"
|
|
assert body["export_md_url"] == f"/tools/deepsearch/{uid}/export.md"
|
|
assert body["findings"]
|
|
assert body["sources"]
|
|
assert body["follow_up_questions"] == ["What is a follow-up question?"]
|
|
assert "viewer_is_admin" in body
|
|
assert "viewer_owns" in body
|
|
finally:
|
|
_clear()
|
|
|
|
|
|
def test_session_html_renders_without_jinja_global_collision(app_server):
|
|
try:
|
|
uid = _seed_done_session()
|
|
r = requests.get(f"{BASE_URL}/tools/deepsearch/{uid}/session")
|
|
assert r.status_code == 200, r.text
|
|
assert "the question" in r.text
|
|
assert "dp-deepsearch-chat" in r.text
|
|
assert "What is a follow-up question?" in r.text
|
|
assert "data-followup" in r.text
|
|
finally:
|
|
_clear()
|
|
|
|
|
|
def test_session_reads_disk_report_before_result_commit(app_server):
|
|
from pathlib import Path
|
|
import shutil
|
|
|
|
from devplacepy.config import DEEPSEARCH_DIR
|
|
|
|
owner_id = "ds-race-owner"
|
|
uid = queue.enqueue(
|
|
"deepsearch",
|
|
{"query": "race question", "depth": 2, "max_pages": 10},
|
|
"user",
|
|
owner_id,
|
|
"DeepSearch: race question",
|
|
)
|
|
create_deepsearch_session(
|
|
uid, "user", owner_id, "race question", 2, 10, f"ds_{uid.replace('-', '')}"
|
|
)
|
|
report = {
|
|
"query": "race question",
|
|
"summary": "A grounded summary from disk.",
|
|
"findings": [
|
|
{"title": "Disk finding", "detail": "D.", "confidence": 0.8, "citations": [1]}
|
|
],
|
|
"sources": [{"url": "https://example.com", "title": "Example", "source": "httpx"}],
|
|
"score": 84,
|
|
"confidence": 0.85,
|
|
"source_diversity": 0.75,
|
|
"synthesis": "agents",
|
|
"page_count": 12,
|
|
"chunk_count": 61,
|
|
}
|
|
session_dir = DEEPSEARCH_DIR / uid
|
|
session_dir.mkdir(parents=True, exist_ok=True)
|
|
(session_dir / "report.json").write_text(json.dumps(report), encoding="utf-8")
|
|
get_table("deepsearch_sessions").update(
|
|
{"uid": uid, "status": "done"}, ["uid"]
|
|
)
|
|
refresh_snapshot()
|
|
try:
|
|
r = requests.get(
|
|
f"{BASE_URL}/tools/deepsearch/{uid}/session", headers=_json_headers()
|
|
)
|
|
assert r.status_code == 200, r.text
|
|
body = r.json()
|
|
assert body["status"] == "done"
|
|
assert body["score"] == 84
|
|
assert body["chunk_count"] == 61
|
|
assert body["findings"]
|
|
assert body["sources"]
|
|
assert body["chat_ws_url"] == f"/tools/deepsearch/{uid}/chat"
|
|
md = requests.get(f"{BASE_URL}/tools/deepsearch/{uid}/export.md")
|
|
assert md.status_code == 200, md.text
|
|
assert "Disk finding" in md.text
|
|
finally:
|
|
shutil.rmtree(session_dir, ignore_errors=True)
|
|
_clear()
|
|
|
|
|
|
def test_session_unknown_uid_404(app_server):
|
|
r = requests.get(
|
|
f"{BASE_URL}/tools/deepsearch/nope/session", headers=_json_headers()
|
|
)
|
|
assert r.status_code == 404
|
|
|
|
|
|
def test_export_markdown(app_server):
|
|
try:
|
|
uid = _seed_done_session()
|
|
r = requests.get(f"{BASE_URL}/tools/deepsearch/{uid}/export.md")
|
|
assert r.status_code == 200, r.text
|
|
assert "DeepSearch report" in r.text
|
|
assert "Finding one" in r.text
|
|
finally:
|
|
_clear()
|
|
|
|
|
|
def test_export_json(app_server):
|
|
try:
|
|
uid = _seed_done_session()
|
|
r = requests.get(f"{BASE_URL}/tools/deepsearch/{uid}/export.json")
|
|
assert r.status_code == 200, r.text
|
|
body = r.json()
|
|
assert body["query"] == "the question"
|
|
assert body["findings"]
|
|
finally:
|
|
_clear()
|
|
|
|
|
|
def test_export_unknown_uid_404(app_server, _no_happy_404):
|
|
r = requests.get(f"{BASE_URL}/tools/deepsearch/nope/export.md")
|
|
assert r.status_code == 404
|