Files
uconsole_asw_game/tests/test_web_schema_gen.py
db9979andClaude Fable 5.1 952c4a35d4 Web client: state push over WebSocket, generated schema allowlists, chart culling probe
Phase 11 of plan 1.3. /ws/v2/state pushes the active role's projection
(same bytes as GET /api/v2/state, at most 4 Hz, heartbeat 2 s, latest
state only) with the sonar stream's Origin/cookie/subprotocol rules; the
browser consumes pushed states in its poll loop, keeps polling presence,
chart and feeds on a slow cadence, falls back to full polling after two
missed heartbeats and retries every 10 s. src/commander/v2/schema.py is
the single allowlist rendered into schema.js by tools/gen_web_schema.py
(--check in CI). The browser chart culls tracks in world coordinates
before transforming and exposes a frame-time probe. OffscreenCanvas is
deferred (documented in docs/resume.md).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-27 05:28:49 +02:00

64 lines
2.4 KiB
Python

"""The browser schema's allowlists are rendered from the Python source."""
import subprocess
import sys
from pathlib import Path
import pytest
from src.commander.v2 import schema
from src.commander.v2.wire import ROLES
sys.path.insert(0, str(Path(__file__).parent))
from test_opfor_sub import _crewed # noqa: E402
ROOT = Path(__file__).resolve().parents[1]
TOOL = ROOT / "tools" / "gen_web_schema.py"
SCHEMA_JS = ROOT / "data" / "commander" / "js" / "state" / "schema.js"
def _run(*args, cwd=ROOT):
return subprocess.run([sys.executable, str(TOOL), *args], cwd=cwd,
capture_output=True, text=True)
def test_generated_block_is_deterministic_and_checked(tmp_path, monkeypatch):
sys.path.insert(0, str(ROOT / "tools"))
import gen_web_schema
first = gen_web_schema.render_block()
second = gen_web_schema.render_block()
assert first == second
current = SCHEMA_JS.read_text(encoding="utf-8")
assert first in current
assert gen_web_schema.render(current) == current
# A hand edit inside the block fails the check and is rewritten.
copy = tmp_path / "schema.js"
line = ' sonar: ["observations", "settings", "visualization"],\n'
assert current.count(line) == 1
copy.write_text(current.replace(line, line.replace('"visualization"', '"visualization", "secret"')),
encoding="utf-8")
monkeypatch.setattr(gen_web_schema, "SCHEMA_JS", copy)
assert gen_web_schema.main(["--check"]) == 1
assert gen_web_schema.main([]) == 0
assert copy.read_text(encoding="utf-8") == current
assert gen_web_schema.main(["--check"]) == 0
assert _run("--check").returncode == 0
def test_role_shapes_match_the_published_projections():
game, server, _bridge = _crewed(seed=101)
assert set(schema.ROLE_SHAPES) == set(ROLES)
for role in ROLES:
state = server.v2_states[role]
assert state["role"] == role, role
assert set(state[role]) == set(schema.ROLE_SHAPES[role]), role
assert schema.HELICOPTER_TACTICAL_FIELDS[:len(schema.TACTICAL_FIELDS)] == schema.TACTICAL_FIELDS
@pytest.mark.parametrize("name", ["TACTICAL_FIELDS", "SONAR_FIELDS", "RADIO_FIELDS"])
def test_field_tuples_are_unique_and_shared_with_the_projections(name):
from src.commander import projections
values = getattr(schema, name)
assert len(set(values)) == len(values)
assert getattr(projections, "_" + name) is values