Files
undercooked/tests/unit/test_session_minting.py
ducoterra f51ba919af phase: 05_anonymous_lobbies_and_room_lifecycle
All green. Final report:

**Phase 05 final verification pass — complete, 100% green.**

**Verified (all 5 tasks already in `complete/`):**
- `RoomSupervisor` import, `POST /api/rooms` → 5-char Crockford code (`VBE8Y`), `GET /api/rooms/{code}` → seat list, join/session/404 flows — all live over HTTP
- `docs/ROOMS.md` present with FSM diagram, crash-isolation and "What is lost on restart" sections
- Phase tests all pass: lifecycle 4-seat `WON`→`LOBBY` (7 tests), crash isolation, GC/idle via `FakeClock` (no sleeps), FSM, tick cadence, supervisor, sessions

**Results:**
- `uv run pytest` → **1506 passed, 0 failed**; coverage **98.89%** (>90% floor; `room.py` 99%, `rooms.py` 97%, `sessions.py` 99% — all ≥95% per-module gate)
- `uv run ruff check .` → clean; `uv run pyright` → 0 errors; `uv run undercooked-levels validate` → content OK; server boots, `/healthz` 200, client at `/` 200
- e2e US-01 suite re-run after fixes: 10 passed

**Documentation — inconsistencies found and fixed:**
- `README.md`: status now lists phases 01–05 complete, next pending `06_realtime_websocket_protocol`; dropped "(phase 05, in progress)"; 3 stale "Not implemented until phase 05." references corrected
- `app/static/js/app.js` + `tests/e2e/test_us01_landing_surface.py`: placeholder string updated in lockstep to "Not implemented until phase 07." (phase 07 task 02 wires the buttons), comments updated to reflect phase-05 completion
- `AGENTS.md`: reviewed in full — §6 already documents all phase-05 modules accurately; no changes needed

**Decisions:** no code behavior changes beyond the user-visible placeholder string; no live-infrastructure touch (only a local loopback server, stopped).

**Next pending phase:** `06_realtime_websocket_protocol`.
2026-10-08 05:17:21 -04:00

46 lines
1.3 KiB
Python

"""Unit: session token minting and `SessionRecord` (A16).
The token is the player's only identity: 24 random bytes, urlsafe
base64 — the same entropy the browser side generates with
`crypto.getRandomValues`. These tests lock the shape that
`SessionStore` (task 04) builds on.
"""
from __future__ import annotations
import string
import pytest
from undercooked.server.clock import FakeClock
from undercooked.server.sessions import mint_token, new_session
pytestmark = pytest.mark.unit
_URLSAFE = set(string.ascii_letters + string.digits + "-_")
def test_mint_token_is_24_random_bytes_of_urlsafe_base64() -> None:
token = mint_token()
assert len(token) == 32 # 24 bytes -> 32 base64url characters
assert set(token) <= _URLSAFE
assert "+" not in token and "/" not in token and "=" not in token
def test_minted_tokens_are_distinct() -> None:
tokens = {mint_token() for _ in range(100)}
assert len(tokens) == 100
def test_new_session_mints_a_token_and_stamps_the_injected_clock() -> None:
clock = FakeClock(start=42.0)
session = new_session("Ada", clock)
assert len(session.token) == 32
assert session.display_name == "Ada"
assert session.created_at == 42.0
anonymous = new_session(None, clock)
assert anonymous.display_name is None
assert anonymous.token != session.token
assert anonymous.created_at == 42.0