From dddb61c917da9fdc221ff10067de5adf4ee041eb Mon Sep 17 00:00:00 2001 From: ggapp1 <19319387+ggapp1@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:59:20 -0300 Subject: [PATCH 1/8] test: restore os.environ after every test so CLI .env loads cannot leak --- tests/conftest.py | 18 ++++++++++++++++++ tests/test_cli.py | 9 ++++----- tests/test_release_e2e.py | 3 ++- tests/test_web_api.py | 2 ++ 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index c08c6aa..0d12764 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -16,6 +16,7 @@ import contextlib import importlib +import os from datetime import datetime from typing import Any from zoneinfo import ZoneInfo @@ -26,6 +27,23 @@ from dudamel.db import Database +@pytest.fixture(autouse=True) +def _restore_environ(): + """Give every test the process environment it started with, whatever it wrote. + + `cli._load_dotenv_into_environ` writes `DUDAMEL_WEB_TOKEN` and friends into + the REAL `os.environ`. A `monkeypatch.delenv(var, raising=False)` guard does + NOT undo that: when the var is absent, pytest records nothing, so the later + write is never reverted and leaks into every following test (it silently + voided the "no token configured" precondition of web-auth tests that ran + after the CLI tests). A snapshot/restore covers writes however they happen. + """ + snapshot = dict(os.environ) + yield + os.environ.clear() + os.environ.update(snapshot) + + def _freeze_app_clock(monkeypatch: pytest.MonkeyPatch, instant: str) -> None: """Pin `App.today()`'s clock. diff --git a/tests/test_cli.py b/tests/test_cli.py index 9eb4a26..e1d067a 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -26,11 +26,10 @@ def _clean_tokens(monkeypatch: pytest.MonkeyPatch) -> None: DUDAMEL_WEB_TOKEN, and `run`/`doctor` load that .env into the REAL process environment (see `cli._load_dotenv_into_environ` -- required because web/telegram token resolution reads `os.environ` directly, not - a `Settings` field). `monkeypatch.delenv` here registers each var's - CURRENT (absent-or-not) state for restoration, so whatever the CLI - later writes into `os.environ` is undone at teardown regardless -- this - keeps that mutation from leaking into unrelated tests later in the same - pytest process.""" + a `Settings` field). The `delenv` below only CLEARS a host-set value so the + test starts without one; it does NOT undo later writes (for an absent var + pytest records nothing to restore). Undoing those is the job of the + session-wide `_restore_environ` fixture in conftest.py.""" for var in ("DUDAMEL_WEB_TOKEN", "DUDAMEL_TELEGRAM_TOKEN", "ANTHROPIC_API_KEY"): monkeypatch.delenv(var, raising=False) diff --git a/tests/test_release_e2e.py b/tests/test_release_e2e.py index f6e4e49..2ca3322 100644 --- a/tests/test_release_e2e.py +++ b/tests/test_release_e2e.py @@ -68,7 +68,8 @@ def _clean_tokens(monkeypatch: pytest.MonkeyPatch) -> None: """Mirrors test_cli.py's fixture of the same purpose: `new`/`db migrate` write/read a real .env, and `_load_dotenv_into_environ` loads it into the REAL process environment -- scrub first so a leftover host-env token - can't shadow the one this test generates and reads back.""" + can't shadow the one this test generates and reads back. (Restoring what + the CLI writes afterwards is conftest's `_restore_environ`, not this.)""" for var in ("DUDAMEL_WEB_TOKEN", "DUDAMEL_TELEGRAM_TOKEN", "ANTHROPIC_API_KEY"): monkeypatch.delenv(var, raising=False) diff --git a/tests/test_web_api.py b/tests/test_web_api.py index 2742966..66c24a5 100644 --- a/tests/test_web_api.py +++ b/tests/test_web_api.py @@ -2,6 +2,7 @@ import asyncio import logging +import os from datetime import timedelta from pathlib import Path @@ -143,6 +144,7 @@ async def test_wrong_token_returns_401(tmp_path: Path, token_env: str) -> None: async def test_missing_configured_token_rejects_any_bearer(tmp_path: Path) -> None: """No DUDAMEL_WEB_TOKEN set at all: bearer auth can never succeed.""" + assert "DUDAMEL_WEB_TOKEN" not in os.environ # the precondition, self-checked rt, transport = await build(tmp_path, []) async with client(transport) as c: resp = await c.get("/api/widgets", headers={"Authorization": "Bearer anything"}) From d00dfff2181a19942bd7c357f4e29e8374ccfc7b Mon Sep 17 00:00:00 2001 From: ggapp1 <19319387+ggapp1@users.noreply.github.com> Date: Wed, 28 Oct 2026 16:00:11 -0300 Subject: [PATCH 2/8] test(web): pin the auth negatives the happy-path tests never reached --- tests/test_web_api.py | 71 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/tests/test_web_api.py b/tests/test_web_api.py index 66c24a5..a0049ab 100644 --- a/tests/test_web_api.py +++ b/tests/test_web_api.py @@ -16,8 +16,9 @@ from dudamel.llm.testing import Completion, FakeProvider, fake_text, fake_tool_call from dudamel.models_core import Activity, PendingConfirmation from dudamel.router import _utcnow +from dudamel.web import auth as web_auth from dudamel.web.api import create_api -from dudamel.web.auth import CSRF_HEADER +from dudamel.web.auth import CSRF_HEADER, SESSION_COOKIE, SESSION_TTL from dudamel.web.throttle import MAX_FAILURES TOKEN = "s3cr3t-token" # noqa: S105 — test fixture, not a real credential @@ -316,6 +317,74 @@ async def test_cookie_get_does_not_require_csrf(tmp_path: Path, token_env: str) await rt.stop() +async def test_cookie_post_with_a_wrong_csrf_token_is_403(tmp_path: Path, token_env: str) -> None: + """A PRESENT but wrong `X-CSRF-Token` must be refused, not merely an absent + one. The wrong value shares its first 16 characters with the real token, so + a prefix-only comparison cannot pass this either.""" + rt, transport = await build(tmp_path, [fake_text("hello!")]) + async with client(transport) as c: + login = await c.post("/login", json={"token": token_env}) + csrf_token = login.json()["csrf_token"] + near_miss = csrf_token[:16] + ("A" if csrf_token[16:17] != "A" else "B") + csrf_token[17:] + assert near_miss != csrf_token + + for wrong in ("not-the-token", near_miss, csrf_token + "x"): + resp = await c.post("/api/chat", json={"text": "hi"}, headers={CSRF_HEADER: wrong}) + assert resp.status_code == 403, wrong + + ok = await c.post("/api/chat", json={"text": "hi"}, headers={CSRF_HEADER: csrf_token}) + assert ok.status_code == 200 + await rt.stop() + + +async def test_a_bogus_session_cookie_does_not_authenticate_a_get( + tmp_path: Path, token_env: str +) -> None: + """GET is exempt from CSRF, but only for a session that EXISTS: an unknown + session id must still be a 401, not a free pass because no CSRF check ran.""" + rt, transport = await build(tmp_path, []) + async with client(transport) as c: + c.cookies.set(SESSION_COOKIE, "no-such-session") + resp = await c.get("/api/widgets") + assert resp.status_code == 401 + await rt.stop() + + +async def test_a_session_expires_after_the_ttl_and_is_forgotten( + tmp_path: Path, token_env: str, monkeypatch: pytest.MonkeyPatch +) -> None: + """The session lives for exactly SESSION_TTL (24h): still valid just inside + it, rejected just past it, and the expired entry is removed from the table + rather than left to accumulate.""" + rt, transport = await build(tmp_path, []) + app = transport.app + start = web_auth._now() + clock = {"now": start} + monkeypatch.setattr(web_auth, "_now", lambda: clock["now"]) + async with client(transport) as c: + await c.post("/login", json={"token": token_env}) + assert len(app.state.sessions._sessions) == 1 + + clock["now"] = start + SESSION_TTL - timedelta(seconds=1) + assert (await c.get("/api/widgets")).status_code == 200 + + clock["now"] = start + SESSION_TTL + timedelta(seconds=1) + assert (await c.get("/api/widgets")).status_code == 401 + assert app.state.sessions._sessions == {} + await rt.stop() + + +async def test_a_bearer_token_with_extra_trailing_characters_is_rejected( + tmp_path: Path, token_env: str +) -> None: + """The bearer must EQUAL the configured token, not merely start with it.""" + rt, transport = await build(tmp_path, []) + async with client(transport) as c: + resp = await c.get("/api/widgets", headers={"Authorization": f"Bearer {token_env}x"}) + assert resp.status_code == 401 + await rt.stop() + + # --- cookie Secure / __Host- / Max-Age ---------------------------------------- From 5aa3dc0fbcd5f09e11b23d1d41b1a9a221f0082a Mon Sep 17 00:00:00 2001 From: ggapp1 <19319387+ggapp1@users.noreply.github.com> Date: Wed, 28 Oct 2026 16:01:31 -0300 Subject: [PATCH 3/8] test(router): pin the confirmation lifecycle's statuses, TTL and resume validation --- tests/test_router_confirm.py | 164 ++++++++++++++++++++++++++++++++++- 1 file changed, 160 insertions(+), 4 deletions(-) diff --git a/tests/test_router_confirm.py b/tests/test_router_confirm.py index b6a1225..d6bfd5e 100644 --- a/tests/test_router_confirm.py +++ b/tests/test_router_confirm.py @@ -1,4 +1,5 @@ -from datetime import timedelta +from datetime import date, timedelta +from enum import Enum import pytest from sqlalchemy import select @@ -52,7 +53,28 @@ async def import_feed(url: str) -> str: return app -def build(tmp_path, script, taint_mode: str = "turn"): +class Level(Enum): + LOW = "low" + HIGH = "high" + + +TYPED: list[tuple[object, object]] = [] + + +def make_typed_app() -> App: + app = App("planner", description="d") + + @app.tool(confirm=True) + async def schedule(day: date, level: Level) -> str: + """Schedule something: typed arguments that only survive a JSON + round-trip as strings.""" + TYPED.append((day, level)) + return "scheduled" + + return app + + +def build(tmp_path, script, taint_mode: str = "turn", *, extra_apps=(), **router_kwargs): url = f"sqlite+aiosqlite:///{tmp_path}/cf.db" upgrade_core(url) db = Database(url) @@ -62,14 +84,14 @@ def build(tmp_path, script, taint_mode: str = "turn"): db=db, budget=BudgetConfig(), ) - registry = Registry([make_app()]) + registry = Registry([make_app(), *extra_apps]) convo = ConversationStore(db) router = Router( llm=llm, registry=registry, convo=convo, db=db, - config=RouterConfig(taint_mode=taint_mode), + config=RouterConfig(taint_mode=taint_mode, **router_kwargs), ) return router, fp, db, convo, registry @@ -79,6 +101,7 @@ def _clear(): DELETED.clear() MUTATED.clear() READS.clear() + TYPED.clear() async def test_suspend_then_approve_executes_and_resumes(tmp_path) -> None: @@ -563,3 +586,136 @@ async def test_a_model_chosen_argument_name_cannot_forge_a_line_in_the_confirm(t assert "\n" not in reply.text, f"argument name forged a line break: {reply.text!r}" assert reply.text.count("Confirm: run") == 1 await db.dispose() + + +# --- confirmation lifecycle: what each resolution leaves behind ---------------- + + +async def _only_row(db) -> PendingConfirmation: + async with db.session() as s: + return (await s.execute(select(PendingConfirmation))).scalar_one() + + +async def test_approving_the_same_confirmation_twice_runs_the_tool_once(tmp_path) -> None: + """A double-tap (or a retried webhook) on Approve must not re-run a + destructive tool: the second resolution sees a row that is no longer + pending and says so, without executing or calling the model again.""" + script = [fake_tool_call("wipe_log", {"reason": "once"}), fake_text("All wiped!")] + router, fp, db, convo, _ = build(tmp_path, script) + r1 = await router.handle(channel="t:1", text="wipe", user_id="u1") + first = await router.resolve_confirmation( + r1.pending_confirmation_id, approved=True, user_id="u1" + ) + assert first.text == "All wiped!" + + second = await router.resolve_confirmation( + r1.pending_confirmation_id, approved=True, user_id="u1" + ) + assert second.text == "Unknown confirmation — already confirmed." + assert DELETED == ["once"] + assert len(fp.calls) == 2 # suspend turn + the one resume + await db.dispose() + + +async def test_a_resolved_confirmation_is_left_alone_by_later_messages(tmp_path) -> None: + """The next user message sweeps PENDING confirmations only. A confirmation + already resolved must keep its status and must not have a second + "declined" tool result appended to the history on every later message.""" + script = [ + fake_tool_call("wipe_log", {"reason": "x"}), + fake_text("All wiped!"), + fake_text("one"), + fake_text("two"), + ] + router, fp, db, convo, _ = build(tmp_path, script) + r1 = await router.handle(channel="t:1", text="wipe", user_id="u1") + await router.resolve_confirmation(r1.pending_confirmation_id, approved=True, user_id="u1") + cid = await convo.get_or_create("t:1") + before = [m.role for m in await convo.recent(cid)] + assert before == ["user", "assistant", "tool", "assistant"] + + await router.handle(channel="t:1", text="and now?", user_id="u1") + await router.handle(channel="t:1", text="anything else?", user_id="u1") + + assert (await _only_row(db)).status == "confirmed" + after = [m.role for m in await convo.recent(cid)] + assert after == [*before, "user", "assistant", "user", "assistant"] + await db.dispose() + + +async def test_a_new_confirmation_expires_after_the_configured_ttl(tmp_path) -> None: + """`confirm_ttl_seconds` is the lifetime actually written to the row (to + within the milliseconds between the two clock reads), not just a config + field nobody consults.""" + router, fp, db, convo, _ = build( + tmp_path, [fake_tool_call("wipe_log", {"reason": "x"})], confirm_ttl_seconds=100 + ) + await router.handle(channel="t:1", text="wipe", user_id="u1") + row = await _only_row(db) + lifetime = (row.expires_at - row.created_at).total_seconds() + assert lifetime == pytest.approx(100, abs=5) + await db.dispose() + + +async def test_declining_records_the_declined_status(tmp_path) -> None: + script = [fake_tool_call("wipe_log", {"reason": "x"}), fake_text("Okay.")] + router, fp, db, convo, _ = build(tmp_path, script) + r1 = await router.handle(channel="t:1", text="wipe", user_id="u1") + await router.resolve_confirmation(r1.pending_confirmation_id, approved=False, user_id="u1") + assert (await _only_row(db)).status == "declined" + assert DELETED == [] + await db.dispose() + + +async def test_a_confirmation_that_timed_out_is_swept_as_expired(tmp_path) -> None: + """The sweep run by the next message distinguishes a confirmation that + simply timed out (`expired`) from one the user superseded (`declined`).""" + script = [fake_tool_call("wipe_log", {"reason": "x"}), fake_text("hello!")] + router, fp, db, convo, _ = build(tmp_path, script) + await router.handle(channel="t:1", text="wipe", user_id="u1") + async with db.session() as s: + row = (await s.execute(select(PendingConfirmation))).scalar_one() + row.expires_at = _utcnow() - timedelta(seconds=1) + + await router.handle(channel="t:1", text="hi", user_id="u1") + assert (await _only_row(db)).status == "expired" + await db.dispose() + + +async def test_an_approved_call_is_revalidated_into_its_typed_arguments(tmp_path) -> None: + """loop_state stores the call's args as JSON, so a date and an enum come + back as plain strings. The tool must still receive a `date` and a `Level` + -- the resumed call goes through the schema again, not straight to `fn`.""" + script = [ + fake_tool_call("schedule", {"day": "2026-03-04", "level": "high"}), + fake_text("Scheduled."), + ] + router, fp, db, convo, _ = build(tmp_path, script, extra_apps=[make_typed_app()]) + r1 = await router.handle(channel="t:1", text="book it", user_id="u1") + assert r1.pending_confirmation_id + await router.resolve_confirmation(r1.pending_confirmation_id, approved=True, user_id="u1") + assert TYPED == [(date(2026, 3, 4), Level.HIGH)] + await db.dispose() + + +async def test_a_hallucinated_tool_in_a_batch_does_not_gate_a_native_write(tmp_path) -> None: + """A name the live registry does not know produces only the router's own + "unknown tool" error: nothing untrusted was fetched, so it must not make + the batch count as untrusted and drag a sibling native write into a + confirmation.""" + both = Completion( + message=Message( + role="assistant", + tool_calls=[ + ToolCall(id="g", name="no_such_tool", args={}), + ToolCall(id="w", name="set_pref", args={"value": "a"}), + ], + ), + usage=Usage(1, 1), + stop_reason="tool_calls", + ) + router, fp, db, convo, _ = build(tmp_path, [both, fake_text("done")]) + reply = await router.handle(channel="t:1", text="go", user_id="u1") + assert reply.pending_confirmation_id is None + assert MUTATED == ["a"] + await db.dispose() From e09a833eae423411ba39e0dd96ed29e6d2f22c12 Mon Sep 17 00:00:00 2001 From: ggapp1 <19319387+ggapp1@users.noreply.github.com> Date: Wed, 28 Oct 2026 16:02:30 -0300 Subject: [PATCH 4/8] test(router): prove a summary that swallowed an untrusted call is written tainted --- tests/test_router_loop.py | 56 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/tests/test_router_loop.py b/tests/test_router_loop.py index ce80fc6..754b23a 100644 --- a/tests/test_router_loop.py +++ b/tests/test_router_loop.py @@ -863,6 +863,62 @@ async def test_compaction_prepends_summary_once_per_turn_within_budget(tmp_path) await db.dispose() +async def test_a_summary_that_swallowed_an_untrusted_call_is_written_tainted(tmp_path) -> None: + """The router -> compactor seam: the router decides, from the registry, + whether the span it is about to drop contains an untrusted (MCP) call and + hands that to the compactor as `dropped_tainted`. The compactor tests pass + that flag themselves, so only a router-level run proves the row comes out + tainted -- and that the next turn, whose window no longer shows the call, + still gates a native mutation.""" + config = RouterConfig(window_tokens=60) + router, fp_standard, fp_compact, db, convo = make_router_with_compaction( + tmp_path, + [ + fake_text("noted"), + fake_tool_call("log_workout", {"exercise": "bench", "reps": 5}), + ], + [fake_text("SUMMARY: one."), fake_text("SUMMARY: two."), fake_text("SUMMARY: three.")], + config=config, + ) + router._registry.tools["serialized_probe"].origin = "mcp" + conv_id = await convo.get_or_create("t:1") + filler = "filler " * 20 + # The untrusted call is the OLDEST thing in the history, so the window drops it. + await convo.append(conv_id, Message(role="user", text="look something up")) + await convo.append( + conv_id, + Message( + role="assistant", + tool_calls=[ToolCall(id="old", name="serialized_probe", args={})], + ), + ) + await convo.append(conv_id, Message(role="tool", text="ok", tool_call_id="old")) + for i in range(5): + await convo.append(conv_id, Message(role="user", text=f"prior user {i} {filler}")) + await convo.append(conv_id, Message(role="assistant", text=f"prior assistant {i} {filler}")) + + await router.handle(channel="t:1", text="hi", user_id="u1") + async with db.session() as s: + newest = ( + ( + await s.execute( + select(Summary).where(Summary.conversation_id == conv_id).order_by(Summary.id) + ) + ) + .scalars() + .all() + )[-1] + assert newest.tainted is True + window_roles = [m.role for m in fp_standard.calls[0]["messages"]] + assert "tool" not in window_roles # the call really is out of the window + + reply = await router.handle(channel="t:1", text="log a set", user_id="u1") + assert reply.pending_confirmation_id is not None + assert "Confirm: run log_workout" in reply.text + assert CALLS == [] + await db.dispose() + + async def test_initial_taint_seeded_from_newest_summary_in_default_turn_mode(tmp_path) -> None: """`_loop` seeds turn_tainted from the newest summary's flag under the default taint_mode="turn" -- not just "window" -- even when nothing From 117048cf51ac5f9b54ac0b1e8e0404c9172ed4d0 Mon Sep 17 00:00:00 2001 From: ggapp1 <19319387+ggapp1@users.noreply.github.com> Date: Wed, 28 Oct 2026 16:03:24 -0300 Subject: [PATCH 5/8] test(migrate): pin pending-migration reporting, lane order and the upgrade backup --- tests/test_suite_migrations.py | 72 ++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/tests/test_suite_migrations.py b/tests/test_suite_migrations.py index af95ad8..8b6cccd 100644 --- a/tests/test_suite_migrations.py +++ b/tests/test_suite_migrations.py @@ -1,12 +1,16 @@ +from importlib.resources import files from pathlib import Path import pytest +from alembic import command +from alembic.config import Config from sqlalchemy import create_engine, inspect, text from dudamel.exceptions import DudamelError from dudamel.migrate import ( pending_migrations, suite_version_table, + sync_url, upgrade_all, upgrade_core, upgrade_suite_app, @@ -228,3 +232,71 @@ def test_pending_migrations_names_each_lane(tmp_path) -> None: assert any("notes" in p for p in pending) upgrade_all(url, tmp_path, [("notes", notes)]) assert pending_migrations(url, tmp_path, [("notes", notes)]) == [] + + +def test_pending_migrations_reports_a_lane_that_is_not_the_first_by_name(tmp_path) -> None: + """Every lane is checked, not just the alphabetically first: with `aaa` + current and `zzz` behind, `zzz` is what must be named.""" + url = db_url_for(tmp_path) + upgrade_core(url) + aaa = make_lane(tmp_path, "aaa", body=CREATE_NOTES.replace("notes_note", "aaa_t"), rev="a1") + zzz = make_lane(tmp_path, "zzz", body=CREATE_TASKS.replace("tasks_task", "zzz_t"), rev="z1") + upgrade_suite_app(url, "aaa", aaa) + + pending = pending_migrations(url, tmp_path, [("zzz", zzz), ("aaa", aaa)]) + + assert pending == ["app 'zzz' schema is behind head"] + + +def test_pending_migrations_reports_a_core_that_is_behind_not_just_unapplied(tmp_path) -> None: + """Core with SOME revisions applied but not the newest is behind. (An + unmigrated database is the other case, covered in test_migrate.py.)""" + url = db_url_for(tmp_path) + cfg = Config() + cfg.set_main_option("script_location", str(files("dudamel") / "migrations")) + cfg.set_main_option("sqlalchemy.url", sync_url(url)) + cfg.set_main_option("version_table", "alembic_version_core") + command.upgrade(cfg, "0001") + assert pending_migrations(url, tmp_path) == ["core schema is behind head"] + + upgrade_core(url) + assert pending_migrations(url, tmp_path) == [] + + +def test_upgrade_all_applies_lanes_in_name_order_whatever_order_they_are_passed(tmp_path) -> None: + """Name order is the documented apply order, so the result cannot depend on + discovery order. Each revision appends its lane's name to a log as it runs.""" + url = db_url_for(tmp_path) + upgrade_core(url) + log = tmp_path / "order.log" + lanes = [ + ( + name, + make_lane( + tmp_path, + name, + body=f"open({str(log)!r}, 'a').write({name!r} + '\\n')", + rev=f"{name}1", + ), + ) + for name in ("bbb", "aaa", "ccc") + ] + + upgrade_all(url, tmp_path, list(reversed(sorted(lanes)))) + + assert log.read_text().split() == ["aaa", "bbb", "ccc"] + + +def test_upgrade_all_takes_a_backup_before_touching_a_lane(tmp_path) -> None: + """The single backup is the real undo for a lane that fails half-way (see + upgrade_all), so it must exist once upgrade_all has run.""" + url = db_url_for(tmp_path) + upgrade_core(url) + backup = tmp_path / "x.db.bak" + assert not backup.exists() # upgrade_core on a fresh file had nothing to back up + + upgrade_all( + url, tmp_path, [("notes", make_lane(tmp_path, "notes", body=CREATE_NOTES, rev="n1"))] + ) + + assert backup.exists() From 1801eca9b69ec9d8b6aff363a0cc4b4272150757 Mon Sep 17 00:00:00 2001 From: ggapp1 <19319387+ggapp1@users.noreply.github.com> Date: Wed, 28 Oct 2026 16:05:54 -0300 Subject: [PATCH 6/8] test(contract): pin every edge of the unsafe-character class, trim order and url whitespace --- tests/test_display_text.py | 70 +++++++++++++++++++++++++++++++++++++- tests/test_widgets.py | 15 ++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/tests/test_display_text.py b/tests/test_display_text.py index f665e23..afe4bfb 100644 --- a/tests/test_display_text.py +++ b/tests/test_display_text.py @@ -6,7 +6,14 @@ which is the same surface disagreement already fixed one field to the left. """ -from dudamel.contract.renderers import validate_widget_payload +import pytest + +from dudamel.contract.renderers import ( + DISPLAY_TEXT_MAX, + clean_action_label, + clean_display_text, + validate_widget_payload, +) def _one(title: str = "t", subtitle: str | None = None): @@ -114,3 +121,64 @@ def test_a_tables_headers_and_string_cells_are_cleaned(): assert "‮" not in payload.columns[0] assert "‮" not in payload.rows[0][0] assert payload.rows[0][1:] == [5, 3.5, None] + + +# Every range in UNSAFE_DISPLAY_CHARS, as (first, last): the codepoints at and +# just inside each edge must go, the ones just outside each edge must stay. +_STRIPPED = [ + 0x00, # NUL, first of C0 + 0x09, # TAB: inside the class even though str.strip() would also take it at an edge + 0x1F, # US, last of C0 + 0x7F, # DEL + 0x80, # first of C1 + 0x9F, # APC, last of C1 + 0x2028, # LINE SEPARATOR + 0x2029, # PARAGRAPH SEPARATOR + 0x202A, # LRE, first of the embedding/override run + 0x202D, + 0x202E, # RLO, last of the run + 0x2066, # LRI, first of the isolates + 0x2069, # PDI, last of the isolates +] +_SURVIVING = [ + 0x20, # SPACE: one past the end of C0 + 0x7E, # TILDE: one before DEL + 0xA0, # NBSP: one past the end of C1 + 0x2027, # one before LINE SEPARATOR + 0x202F, # NARROW NBSP: one past RLO + 0x2065, # one before LRI + 0x206A, # one past PDI + 0x200E, # LRM -- deliberately NOT in the class (see the comment on it) + 0x200F, # RLM -- likewise +] + + +@pytest.mark.parametrize("cp", _STRIPPED, ids=lambda cp: f"U+{cp:04X}") +@pytest.mark.parametrize("clean", [clean_action_label, clean_display_text]) +def test_every_edge_of_the_unsafe_class_is_stripped(cp: int, clean) -> None: + assert clean(f"Ar{chr(cp)}chive") == "Archive" + + +@pytest.mark.parametrize("cp", _SURVIVING, ids=lambda cp: f"U+{cp:04X}") +@pytest.mark.parametrize("clean", [clean_action_label, clean_display_text]) +def test_characters_just_outside_the_unsafe_class_survive(cp: int, clean) -> None: + """The other half of a boundary: an over-wide class would delete legitimate + text (a space between words, a non-breaking space in a price).""" + text = f"Ar{chr(cp)}chive" + assert clean(text) == text + + +def test_overrides_are_removed_before_the_outer_whitespace_is_trimmed() -> None: + """Order matters: stripping first would leave the space that only becomes + outer once the override in front of it is gone.""" + assert clean_action_label("\u202e Archive") == "Archive" + assert clean_display_text("\u202e Archive \u202e") == "Archive" + + +def test_display_text_is_truncated_only_past_the_maximum() -> None: + exact = "x" * DISPLAY_TEXT_MAX + assert clean_display_text(exact) == exact + + over = clean_display_text("x" * (DISPLAY_TEXT_MAX + 1)) + assert over == "x" * (DISPLAY_TEXT_MAX - 1) + "\u2026" + assert len(over) == DISPLAY_TEXT_MAX diff --git a/tests/test_widgets.py b/tests/test_widgets.py index 67965d1..dcb6cee 100644 --- a/tests/test_widgets.py +++ b/tests/test_widgets.py @@ -154,6 +154,21 @@ def test_list_item_rejects_unsafe_url_schemes(url: str) -> None: validate_widget_payload("list", [{"title": "t", "url": url}]) +@pytest.mark.parametrize("char", ["\x00", "\x1f", " ", "\x7f"]) +def test_list_item_rejects_a_url_with_a_control_character_or_space_anywhere(char: str) -> None: + """The class runs from NUL through the SPACE (U+0020) to DEL: a space is + whitespace a surface may split on, and an otherwise-valid prefix must not + launder one. The characters are placed mid-string, where the prefix check + cannot see them, and each end of the range is covered.""" + with pytest.raises(ValueError, match="control characters or whitespace"): + validate_widget_payload("list", [{"title": "t", "url": f"https://example.com/a{char}b"}]) + + +def test_list_item_accepts_a_url_just_outside_the_control_range() -> None: + url = "https://example.com/!~" # U+0021 and U+007E sit either side of the class + assert validate_widget_payload("list", [{"title": "t", "url": url}])[0].url == url + + @pytest.mark.parametrize( "url", [ From 9818a39c8e359db3a8bb0cb694e83ea9a73fca2f Mon Sep 17 00:00:00 2001 From: ggapp1 <19319387+ggapp1@users.noreply.github.com> Date: Wed, 28 Oct 2026 16:06:44 -0300 Subject: [PATCH 7/8] test(llm): tell the framework-zone budget day apart from the UTC and wall-clock ones --- tests/test_llm_client.py | 54 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/tests/test_llm_client.py b/tests/test_llm_client.py index 9bc84c2..3a18119 100644 --- a/tests/test_llm_client.py +++ b/tests/test_llm_client.py @@ -158,12 +158,58 @@ async def test_the_budget_day_rolls_over_in_the_framework_zone(db: Database, mon assert (await client.complete([Message(role="user", text="x")])).message.text == "ok" +async def test_the_budget_day_rolls_over_ahead_of_utc_too(db: Database, monkeypatch) -> None: + """The mirror image for a zone AHEAD of UTC, at an instant where the local + date (Jan 17, Tokyo) is later than the UTC one (Jan 16). + + The Tokyo day began at 15:00Z on Jan 16, so spend at 14:59Z belongs to + yesterday and must not count; a day taken from the UTC date would start a + day too early and count it. (The New York test above cannot catch that: at + 14:00Z both zones share a date.) + """ + async with db.session() as s: + s.add( + LlmCall( + tier="standard", + provider="fake", + model="m", + tokens_in=900, + tokens_out=200, + created_at=datetime(2026, 1, 16, 14, 59), + ) + ) + _freeze_client_clock(monkeypatch, datetime(2026, 1, 16, 20, 0, tzinfo=UTC)) + client = make_client( + db, + [fake_text("ok")], + budget=BudgetConfig(daily_tokens=1000), + timezone=ZoneInfo("Asia/Tokyo"), + ) + assert (await client.complete([Message(role="user", text="x")])).message.text == "ok" + + +@pytest.mark.parametrize( + "now", + [ + datetime(2026, 1, 16, 14, 0, tzinfo=UTC), + # 21:00 on Jan 16 in New York but already Jan 17 in UTC. Here the local + # date and the UTC (or naive wall-clock) date DISAGREE, which is the only + # kind of instant at which "which date is today" can be told apart: at + # 14:00Z both zones say Jan 16 and any date source gives the same window. + datetime(2026, 1, 17, 2, 0, tzinfo=UTC), + ], + ids=["same-date-in-both-zones", "utc-is-already-tomorrow"], +) async def test_spend_inside_the_local_day_still_exhausts_the_budget( - db: Database, monkeypatch + db: Database, monkeypatch, now: datetime ) -> None: - """The other half of the boundary. 06:00Z on 2026-01-16 IS 2026-01-16 in + """The other half of the boundary. 10:00Z on 2026-01-16 IS 2026-01-16 in New York, so it must count -- without this row, an implementation that simply counts nothing satisfies the test above. + + Run at an instant where UTC has already rolled to Jan 17: the day the cap + measures must be the LOCAL one (Jan 16, which began 05:00Z), not the UTC or + wall-clock one, whose window would start at 00:00Z Jan 17 and miss the row. """ async with db.session() as s: s.add( @@ -173,10 +219,10 @@ async def test_spend_inside_the_local_day_still_exhausts_the_budget( model="m", tokens_in=900, tokens_out=200, - created_at=datetime(2026, 1, 16, 6, 0), + created_at=datetime(2026, 1, 16, 10, 0), ) ) - _freeze_client_clock(monkeypatch, datetime(2026, 1, 16, 14, 0, tzinfo=UTC)) + _freeze_client_clock(monkeypatch, now) client = make_client( db, [fake_text("should never run")], From b3738c43a0d5c01d5a6aef9e25ba4cafd4cb70f0 Mon Sep 17 00:00:00 2001 From: ggapp1 <19319387+ggapp1@users.noreply.github.com> Date: Wed, 28 Oct 2026 16:07:08 -0300 Subject: [PATCH 8/8] test(telegram): pin the homescreen token lifetime to an absolute hour --- tests/test_telegram.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/test_telegram.py b/tests/test_telegram.py index 9d03b88..7acddb1 100644 --- a/tests/test_telegram.py +++ b/tests/test_telegram.py @@ -852,6 +852,24 @@ def test_an_expired_token_does_not_resolve() -> None: assert tokens.consume(token, user_id=7) is None +@pytest.mark.parametrize( + ("age", "resolves"), + [(timedelta(minutes=59), True), (timedelta(minutes=61), False)], + ids=["59-minutes", "61-minutes"], +) +def test_a_token_lives_for_about_an_hour(age: timedelta, resolves: bool) -> None: + """An absolute check on the lifetime. The test above is written relative to + `_ACTION_TOKEN_TTL`, so it passes for any value of the constant -- including + a TTL of seconds (every button dead before anyone taps it) or of days (a + homescreen from last week still executing). The intended window is an hour.""" + tokens = _ActionTokens() + token = tokens.issue("complete", {"id": 4}, user_id=7) + entry = tokens._entries[token] + tokens._entries[token] = (*entry[:3], entry[3] - age) + expected = ("complete", {"id": 4}) if resolves else None + assert tokens.consume(token, user_id=7) == expected + + def test_the_map_is_bounded_by_lru_eviction() -> None: """Age alone cannot bound the map: every entry issued inside the TTL window is fresh, so an age filter removes nothing. LRU eviction caps it