"""Unit (grep-style): no wall-clock calls anywhere in `server/`. The phase-05 contract (task 01, work item 1): every time-dependent server object receives a `Clock`; the *only* file in `src/undercooked/server/` allowed to import `time` is `clock.py`, and even there the clock function is held as a reference — `time.monotonic()` / `time.time()` are never called in server code. This test greps for the literals (and for stray `time` imports) so a future edit cannot reintroduce a bare wall-clock call without failing the suite. """ from __future__ import annotations import re from pathlib import Path import pytest pytestmark = pytest.mark.unit REPO_ROOT = Path(__file__).resolve().parents[2] SERVER_DIR = REPO_ROOT / "src" / "undercooked" / "server" #: The wall-clock calls the contract forbids in server code. FORBIDDEN_CALLS = ("time.monotonic()", "time.time()") #: `time` may be imported by exactly one module. TIME_IMPORT = re.compile(r"^\s*(?:import time\b|from time import\b)", re.MULTILINE) def test_no_wall_clock_call_lives_in_server_code() -> None: offenders = [ f"{path.name}: {literal}" for path in sorted(SERVER_DIR.rglob("*.py")) for literal in FORBIDDEN_CALLS if literal in path.read_text(encoding="utf-8") ] assert offenders == [], "wall-clock calls leaked into server/:\n " + "\n ".join( offenders ) def test_only_the_clock_module_imports_time() -> None: offenders = [ path.name for path in sorted(SERVER_DIR.rglob("*.py")) if path.name != "clock.py" and TIME_IMPORT.search(path.read_text(encoding="utf-8")) ] assert offenders == [], ( f"modules that import `time` outside clock.py: {', '.join(offenders)}" ) def test_the_guard_is_not_vacuous() -> None: """A guard that never fires is no guard: prove both checks detect a planted violation in this very file's patterns.""" assert FORBIDDEN_CALLS[0] in "x = time.monotonic()\n" assert TIME_IMPORT.search("import time\n") is not None assert SERVER_DIR.is_dir() assert (SERVER_DIR / "clock.py").is_file()