Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/setup/development-workflows.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ All work lands on `main` through pull requests -- never commit directly to `main
- `plugins/<name>/tests/` -- Each plugin's own test suite (see "Plugin tests" below)
- `tests/migration_support.py` -- The `deployment` fixture behind the `tests/test_*_migration.py` end-to-end Alembic tests: a prod-mode deployment in a temp data dir (env, `config.paths`, encryption key cache, ORM store bindings) that `alembic.command` runs against in the test process; the module docstring has the mechanics
- `tests/authed_get_support.py` -- `mock_httpx_client()`, the recording stand-in for `httpx.AsyncClient` shared by the core and plugin suites that drive `_make_authed_request`
- `tests/route_table_support.py` -- The session-scoped `quest_route_table` fixture: the real `quest.app` route table (methods, paths in registration order, API-docs URLs), read once in a subprocess because importing `quest` loads every plugin into the process-global registries
- `conftest.py` (repo root) -- Pytest hook that also collects out-of-tree plugin test suites from `QUEST_PLUGIN_PATH` roots
- `pyproject.toml` -- Dev dependency group includes `pytest>=8.0.0`

Expand Down
1 change: 1 addition & 0 deletions plugins/telegram/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
from pathlib import Path

from tests.plugin_support import plugin_fixture
from tests.route_table_support import quest_route_table # noqa: F401 (fixture, re-exported)

telegram_plugin = plugin_fixture(Path(__file__).resolve().parent.parent)
28 changes: 5 additions & 23 deletions plugins/telegram/tests/test_telegram_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,30 +140,12 @@ def test_curl_proxy_get_on_telegram_read_returns_blocked_error(self):
assert "dedicated tool" in payload["error"]
assert notices == []

def test_main_app_has_no_telegram_api_routes(self):
def test_main_app_has_no_telegram_api_routes(self, quest_route_table):
"""quest.py mounts no /api/telegram/*; only the plugin's /auth/telegram
login routes exist. Subprocess: importing quest loads plugins into
the process-global registries."""
import subprocess
import sys
from pathlib import Path

script = (
"import json, quest\n"
"from chat.route_table import iter_app_routes\n"
"paths = sorted({r.path for r in iter_app_routes(quest.app) if r.path})\n"
"print(json.dumps({'api': [p for p in paths if p.startswith('/api/telegram')],"
" 'auth': [p for p in paths if p.startswith('/auth/telegram')]}))\n"
)
proc = subprocess.run(
[sys.executable, "-c", script],
capture_output=True, text=True,
cwd=Path(__file__).resolve().parents[3],
)
assert proc.returncode == 0, proc.stderr
routes = json.loads(proc.stdout.strip().splitlines()[-1])
assert routes["api"] == []
assert routes["auth"] == [
login routes exist."""
paths = {p for p in quest_route_table["paths"] if p}
assert [p for p in paths if p.startswith("/api/telegram")] == []
assert sorted(p for p in paths if p.startswith("/auth/telegram")) == [
"/auth/telegram", "/auth/telegram/2fa", "/auth/telegram/disconnect",
"/auth/telegram/send-code", "/auth/telegram/verify",
]
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from tests.db_support import isolated_db # noqa: F401 (fixture, re-exported)
from tests.plugin_support import plugin_fixture
from tests.route_table_support import quest_route_table # noqa: F401 (fixture, re-exported)

# The _example smoke plugin (never discovered in production -- the loader
# skips _-prefixed directories, but plugin_fixture imports it directly) is
Expand Down
63 changes: 63 additions & 0 deletions tests/route_table_support.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""The real app's route table, for tests that pin what ``quest.app`` mounts.

Importing ``quest`` loads every plugin into the process-global registries,
which would pollute other tests, so the table is read in a subprocess and
handed back as plain data. The app is booted once per test process however
many tests and suites ask (the result is kept at module level, so the copies
of the fixture that different conftests register share it).
``tests/conftest.py`` exposes it to the core suite; a plugin suite imports
it into its own conftest, as with ``tests.db_support``.
"""

import json
import subprocess
import sys
from pathlib import Path

import pytest

_REPO_ROOT = Path(__file__).resolve().parent.parent

_SCRIPT = (
"import json, quest\n"
"from chat.route_table import iter_app_routes\n"
"app = quest.app\n"
"routes = sorted({(m, r.path) for r in iter_app_routes(app)"
" if r.methods for m in r.methods})\n"
"paths = [r.path for r in iter_app_routes(app)]\n"
"print(json.dumps({\n"
" 'routes': routes,\n"
" 'paths': paths,\n"
" 'docs_url': app.docs_url,\n"
" 'redoc_url': app.redoc_url,\n"
" 'openapi_url': app.openapi_url,\n"
" 'oauth2_redirect_url': app.swagger_ui_oauth2_redirect_url,\n"
"}))\n"
)


_cached: dict | None = None


@pytest.fixture(scope="session")
def quest_route_table() -> dict:
"""``quest.app``'s routes as data.

``routes`` is the sorted list of ``[method, path]`` pairs, ``paths``
every route path in registration order (duplicates and method-less
routes included), plus the app's ``docs_url``, ``redoc_url``,
``openapi_url`` and ``oauth2_redirect_url``.
"""
global _cached
if _cached is not None:
return _cached
proc = subprocess.run(
[sys.executable, "-c", _SCRIPT],
capture_output=True,
text=True,
cwd=_REPO_ROOT,
)
assert proc.returncode == 0, proc.stderr
# The JSON document is the last stdout line (imports may log above it).
_cached = json.loads(proc.stdout.strip().splitlines()[-1])
return _cached
36 changes: 5 additions & 31 deletions tests/test_autoproject_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
from __future__ import annotations

import json
import subprocess
import sys
from pathlib import Path

import pytest

Expand Down Expand Up @@ -328,39 +325,16 @@ def test_account_delete_removes_autoprojects_runs_and_directories(env):
# quest.py: SPA catch-all and the router mount
# ---------------------------------------------------------------------------

_SCRIPT = (
"import json, quest\n"
"from chat.route_table import iter_app_routes\n"
"app = quest.app\n"
"routes = sorted({(m, r.path) for r in iter_app_routes(app)"
" if r.methods for m in r.methods})\n"
"paths = [r.path for r in iter_app_routes(app)]\n"
"print(json.dumps({'routes': routes, 'paths': paths}))\n"
)


@pytest.fixture(scope="module")
def route_table() -> dict:
# A subprocess: importing quest loads the plugins into the
# process-global registries (tests/test_spa_tables_routes.py pattern).
proc = subprocess.run(
[sys.executable, "-c", _SCRIPT],
capture_output=True, text=True, cwd=Path(__file__).resolve().parent.parent,
)
assert proc.returncode == 0, proc.stderr
return json.loads(proc.stdout.strip().splitlines()[-1])


def test_spa_autoprojects_routes_precede_static_fallback(route_table):
routes = {tuple(r) for r in route_table["routes"]}
paths = route_table["paths"]
def test_spa_autoprojects_routes_precede_static_fallback(quest_route_table):
routes = {tuple(r) for r in quest_route_table["routes"]}
paths = quest_route_table["paths"]
for path in ("/autoprojects", "/autoprojects/{rest:path}"):
assert ("GET", path) in routes
assert paths.index(path) < paths.index("/{filename}")


def test_autoprojects_api_mounted(route_table):
routes = {tuple(r) for r in route_table["routes"]}
def test_autoprojects_api_mounted(quest_route_table):
routes = {tuple(r) for r in quest_route_table["routes"]}
for method, path in [
("GET", "/app/api/autoprojects"),
("POST", "/app/api/autoprojects"),
Expand Down
24 changes: 3 additions & 21 deletions tests/test_connections_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,27 +493,9 @@ def test_real_auth_dependency_401_without_credentials(env, multiconn_plugin):
assert _run(env.store.list_connections(env.users["alice"]["id"], SERVICE)) == []


def test_quest_app_mounts_connection_routes():
"""quest.app serves the routes (subprocess: importing quest loads every
plugin into the process-global registries)."""
import json
import subprocess
import sys
from pathlib import Path

script = (
"import json, quest\n"
"from chat.route_table import iter_app_routes\n"
"routes = sorted({(m, r.path) for r in iter_app_routes(quest.app)"
" if r.methods for m in r.methods})\n"
"print(json.dumps(routes))\n"
)
proc = subprocess.run(
[sys.executable, "-c", script], capture_output=True, text=True,
cwd=Path(__file__).resolve().parent.parent,
)
assert proc.returncode == 0, proc.stderr
routes = {tuple(r) for r in json.loads(proc.stdout.strip().splitlines()[-1])}
def test_quest_app_mounts_connection_routes(quest_route_table):
"""quest.app serves the routes."""
routes = {tuple(r) for r in quest_route_table["routes"]}
assert ("POST", "/auth/connections/{service}") in routes
assert ("PUT", "/auth/connections/{service}/{connection_id}") in routes
assert ("DELETE", "/auth/connections/{service}/{connection_id}") in routes
Expand Down
2 changes: 1 addition & 1 deletion tests/test_impersonation_guard.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
impersonation cookie, the route's normal behaviour without one), and the
real app's route table in a subprocess (importing quest loads every plugin
into the process-global registries; same pattern as
tests/test_spa_docs_routes.py).
tests/route_table_support.py).
"""

from __future__ import annotations
Expand Down
25 changes: 2 additions & 23 deletions tests/test_sandbox_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,32 +63,11 @@ def test_no_docs_endpoints(self):
assert app.docs_url is None
assert app.openapi_url is None

def test_main_app_serves_the_same_roster(self):
def test_main_app_serves_the_same_roster(self, quest_route_table):
"""The main app must keep the shared routes: the LLM's in-process
curl_proxy_* dispatch resolves against quest.app's route table.

Runs in a subprocess because importing quest loads the plugins into
the process-global registries, which would pollute other tests.
"""
import json
import subprocess
import sys
from pathlib import Path

script = (
"import json, quest\n"
"routes = sorted({(m, r.path) for r in quest.app.routes"
" if hasattr(r, 'methods') for m in r.methods})\n"
"print(json.dumps(routes))\n"
)
proc = subprocess.run(
[sys.executable, "-c", script],
capture_output=True,
text=True,
cwd=Path(__file__).resolve().parent.parent,
)
assert proc.returncode == 0, proc.stderr
main_routes = {tuple(r) for r in json.loads(proc.stdout)}
main_routes = {tuple(r) for r in quest_route_table["routes"]}
for method_path in EXPECTED_SANDBOX_ROUTES - {("GET", "/health")}:
assert method_path in main_routes, method_path

Expand Down
2 changes: 1 addition & 1 deletion tests/test_security_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_streaming_body_and_websocket_pass_through():

def test_quest_app_sends_the_headers():
# Subprocess: importing quest loads the plugins into the process-global
# registries (same pattern as tests/test_spa_docs_routes.py).
# registries (same pattern as tests/route_table_support.py).
script = (
"import json, quest\n"
"from starlette.testclient import TestClient\n"
Expand Down
75 changes: 18 additions & 57 deletions tests/test_spa_docs_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,82 +4,43 @@
interactive API docs moved from its defaults (/docs, /redoc) to /api-docs and
/api-redoc. These tests pin both halves of that swap on the real route table.

Runs in a subprocess because importing quest loads the plugins into the
process-global registries, which would pollute other tests (same pattern as
tests/test_sandbox_api.py).
The route table comes from the shared ``quest_route_table`` fixture
(tests/route_table_support.py).
"""

from __future__ import annotations

import json
import subprocess
import sys
from pathlib import Path

import pytest


_SCRIPT = (
"import json, quest\n"
"from chat.route_table import iter_app_routes\n"
"app = quest.app\n"
"routes = sorted({(m, r.path) for r in iter_app_routes(app)"
" if r.methods for m in r.methods})\n"
"paths = [r.path for r in iter_app_routes(app)]\n"
"print(json.dumps({\n"
" 'routes': routes,\n"
" 'paths': paths,\n"
" 'docs_url': app.docs_url,\n"
" 'redoc_url': app.redoc_url,\n"
" 'openapi_url': app.openapi_url,\n"
" 'oauth2_redirect_url': app.swagger_ui_oauth2_redirect_url,\n"
"}))\n"
)


@pytest.fixture(scope="module")
def route_table() -> dict:
proc = subprocess.run(
[sys.executable, "-c", _SCRIPT],
capture_output=True,
text=True,
cwd=Path(__file__).resolve().parent.parent,
)
assert proc.returncode == 0, proc.stderr
# The JSON document is the last stdout line (imports may log above it).
return json.loads(proc.stdout.strip().splitlines()[-1])


def test_spa_docs_routes_exist(route_table):
routes = {tuple(r) for r in route_table["routes"]}
def test_spa_docs_routes_exist(quest_route_table):
routes = {tuple(r) for r in quest_route_table["routes"]}
assert ("GET", "/docs") in routes
assert ("GET", "/docs/{rest:path}") in routes


def test_spa_docs_routes_precede_static_fallback(route_table):
def test_spa_docs_routes_precede_static_fallback(quest_route_table):
"""/docs must be registered before the /{filename} static fallback,
which would otherwise 404 it (no dist file named "docs")."""
paths = route_table["paths"]
paths = quest_route_table["paths"]
assert paths.index("/docs") < paths.index("/{filename}")
assert paths.index("/docs/{rest:path}") < paths.index("/{filename}")


def test_swagger_and_redoc_moved(route_table):
assert route_table["docs_url"] == "/api-docs"
assert route_table["redoc_url"] == "/api-redoc"
assert route_table["openapi_url"] == "/openapi.json"
def test_swagger_and_redoc_moved(quest_route_table):
assert quest_route_table["docs_url"] == "/api-docs"
assert quest_route_table["redoc_url"] == "/api-redoc"
assert quest_route_table["openapi_url"] == "/openapi.json"


def test_default_redoc_path_gone(route_table):
assert "/redoc" not in route_table["paths"]
def test_default_redoc_path_gone(quest_route_table):
assert "/redoc" not in quest_route_table["paths"]


def test_docs_path_is_only_the_spa_route(route_table):
def test_docs_path_is_only_the_spa_route(quest_route_table):
"""Exactly one route owns /docs: the SPA one, not Swagger UI."""
assert route_table["paths"].count("/docs") == 1
assert quest_route_table["paths"].count("/docs") == 1
# Swagger's OAuth2 redirect page follows docs_url too, so no non-SPA
# route is left anywhere under /docs/.
assert route_table["oauth2_redirect_url"] == "/api-docs/oauth2-redirect"
assert not [p for p in route_table["paths"] if p and p.startswith("/docs/") and p != "/docs/{rest:path}"]
assert "/api-docs" in route_table["paths"]
assert "/api-redoc" in route_table["paths"]
assert quest_route_table["oauth2_redirect_url"] == "/api-docs/oauth2-redirect"
assert not [p for p in quest_route_table["paths"] if p and p.startswith("/docs/") and p != "/docs/{rest:path}"]
assert "/api-docs" in quest_route_table["paths"]
assert "/api-redoc" in quest_route_table["paths"]
Loading
Loading