Files
uconsole_asw_game/tests/test_commander_assets.py
T
Claude c1946eb75f Add crew watches, fatigue, action stations and morale
Frigate and crewed boat stand three watches relieved every hour of game
time. Action stations make the crew more alert but tire everybody;
morale moves with kills, damage and HQ tasks. The crew's performance
raises the sonar recognition threshold, needs more lookout/periscope
contrast and slows damage-control work. A fresh crew is exactly the
calibrated crew (77/77 metrics unchanged). Damage page 3, Bridge G,
boat keys B/M, Remote Crew cards and orders, save v21 "watch" and
crew.watch.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019eZphdvoJiBgWqq2zSH4aJ
2026-09-27 17:20:08 +00:00

1442 lines
91 KiB
Python

"""Resource/catalog contracts; optional real Chromium tests need no driver or Node."""
import json
import re
import shutil
import subprocess
import threading
from html.parser import HTMLParser
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from importlib import resources
from pathlib import Path
from string import Formatter
import pytest
from commander_web import client_css, client_js, function_source, index_html, inject_probe, WEB_ROUTES
from src.core.version import APP_VERSION
from src.data.contact_analysis import project_contact_catalog
ROOT = Path(__file__).resolve().parents[1]
ASSETS = resources.files("data.commander")
PREFIX = "commander.web."
class Document(HTMLParser):
def __init__(self, text):
super().__init__()
self.elements = []
self.feed(text)
def handle_starttag(self, tag, attrs):
self.elements.append((tag, dict(attrs)))
def catalogs():
return [json.loads((ROOT / "data" / "i18n" / f"{lang}.json").read_text())
for lang in ("en", "de")]
def test_commander_resources_are_self_contained_and_csp_safe():
assert all(ASSETS.joinpath(name).is_file() for name in ("__init__.py", "index.html", "js/main.js", "css/tokens.css", "voice.js", "voice-worklet.js", "sonar-audio-worklet.js"))
html = ASSETS.joinpath("index.html").read_text()
js = client_js()
css = client_css()
document = Document(html)
ids = [attrs["id"] for _, attrs in document.elements if "id" in attrs]
assert len(ids) == len(set(ids))
assert set(re.findall(r'\$\("([\w-]+)"\)', js)) <= set(ids)
code = next(attrs for _, attrs in document.elements if attrs.get("id") == "code")
name = next(attrs for _, attrs in document.elements if attrs.get("id") == "name")
assert code["maxlength"] == "6" and code["pattern"] == "[0-9]{3}[A-Za-z]{3}"
assert code["autocapitalize"] == "characters" and code["autocomplete"] == "off"
assert code["placeholder"] == "482KMT" and code["aria-describedby"] in ids
assert name["minlength"] == "1" and name["maxlength"] == "32"
assert "required" in name and name["data-i18n-placeholder"] == "pair_name_default"
tabs = [attrs for _, attrs in document.elements
if attrs.get("role") == "tab" and attrs.get("id", "").startswith("tab-")]
panels = [attrs for _, attrs in document.elements
if attrs.get("role") == "tabpanel" and attrs.get("id", "").startswith("panel-")]
assert len(tabs) == len(panels) == 4
assert sum(tab.get("aria-selected") == "true" and tab.get("tabindex") == "0"
for tab in tabs) == 1
assert all(tab.get("type") == "button" and tab["aria-controls"] in ids for tab in tabs)
assert all(panel["aria-labelledby"] in ids for panel in panels)
assert {tab["aria-controls"] for tab in tabs} == {panel["id"] for panel in panels}
assert sum("hidden" not in panel for panel in panels) == 1
lookout = next(attrs for _, attrs in document.elements
if attrs.get("id") == "panel-lookout")
assert "pending-panel" not in lookout.get("class", "")
assert {"lookout", "lookout-zoom-in", "lookout-zoom-out", "lookout-reset",
"lookout-range", "lookout-sea", "lookout-light", "lookout-note"} <= set(ids)
for tag, attrs in document.elements:
assert not any(key.startswith("on") or key == "style" for key in attrs)
assert tag not in {"iframe", "img", "object", "embed", "style"}
for key in ("src", "href"):
if key in attrs:
assert (attrs[key] in {"./js/main.js", "./voice.js"}
or key == "href" and re.fullmatch(r"\./css/[a-z]+\.css", attrs[key])
or key == "href" and attrs[key].startswith("#guide-")
or key == "href" and attrs[key] == "/manual-en"
and attrs.get("target") == "_blank"
and attrs.get("rel") == "noopener"
or key == "href" and attrs.get("id") == "web-admin-link"
and attrs[key] == "/admin" and "hidden" in attrs)
csp = next(attrs["content"] for _, attrs in document.elements if attrs.get("http-equiv") == "Content-Security-Policy")
assert "default-src 'none'" in csp
assert "connect-src 'self'" in csp
assert "img-src 'self'" in csp
assert "media-src 'none'" in csp
assert "unsafe-inline" not in csp and "unsafe-eval" not in csp
for forbidden in ("innerHTML", "outerHTML", "insertAdjacentHTML", "localStorage", "sessionStorage", "indexedDB", "document.cookie", "Math.random", "getUserMedia", "eval(", "new Function", "https://", "http://"):
assert forbidden not in js
assert 'new window.WebSocket(`${scheme}//${location.host}/ws/v2/sonar`, "u-jagd-sonar-v2")' in js
assert "socket.binaryType = \"arraybuffer\"" in js
assert "@import" not in css
# The only external references are the bundled fonts.
assert set(re.findall(r"url\(([^)]*)\)", css)) == {
'"../fonts/inter-variable.woff2"', '"../fonts/jetbrains-mono-regular.woff2"',
'"../fonts/jetbrains-mono-bold.woff2"'}
assert "AbortController" in js and "Authorization" not in js
assert 'credentials: "same-origin"' in js and 'request("/session", { auth: false })' in js
assert "protocolMode" not in js and "adaptV2State" not in js
assert "protocol: 1" not in js and "/api/v1" not in js
assert "pairingAvailable = error.status === 401" in js
assert 'request("/pair", { method: "POST", body: {code, name}, auth: false })' in js
assert 'headers["X-U-Jagd-CSRF"] = csrf' in js
assert 'mutateStation("/stations/request", { station })' in js
assert 'mutateStation("/stations/activate"' in js
assert 'mutateStation("/stations/release"' in js
assert "active_generation: S.session.active_generation" in js
assert 'started - S.lastSessionFetch >= 1000' in js
assert 'window.addEventListener("pagehide"' not in js
assert "crypto.randomUUID" in js and "crypto.getRandomValues" in js
assert 'const stateRoute = sonarStream.connected' in js and 'request(stateRoute)' in js and 'request("/chart")' in js
assert 'request("/commands"' in js and 'expected: 202' in js
assert 'request("/results"' in js and "next_command_seq" in js
assert "S.nextCommandSeq = Math.max(S.nextCommandSeq, command.body.seq + 1)" in js
assert "S.eventContext !== key || S.eventBaselinePending || document.hidden || !navigator.onLine" in js
assert 'if (document.hidden) S.eventBaselinePending = true' in js
assert 'window.addEventListener("offline", () => { S.eventBaselinePending = true;' in js
assert "`bridge_set_${kind}`" in js
bridge_sender = js.split("async function sendBridgeOrder", 1)[1].split(
"async function pollV2Result", 1)[0]
assert "setTimeout" not in bridge_sender and "transmitCommand" not in bridge_sender
assert "secureId()" in bridge_sender and "station_generation" in bridge_sender
assert "result.reasoncode" in js and not re.search(r"result\.reason\b", js)
lookout_renderer = js.split("function drawLookout()", 1)[1].split(
"function changeLookoutRange", 1)[0]
assert "chart" not in lookout_renderer and "sendCommand" not in lookout_renderer
assert "snapshot.tracks" in lookout_renderer and "snapshot.ownship" in lookout_renderer
analyzer_renderer = function_source(js, "renderContactAnalysis")
assert "sendCommand" not in analyzer_renderer and 'request("/commands"' not in analyzer_renderer
assert "analysisSelected" in analyzer_renderer and "textContent" in analyzer_renderer
assert 'request("/contacts", { auth: false })' in js
assert 'fetch(`/api/v2${path}`' in js
assert "innerHTML" not in analyzer_renderer
assert 'image.alt = descriptions.join(" ")' in analyzer_renderer
sonar_toggle = next(attrs for _, attrs in document.elements
if attrs.get("id") == "sonar-live-toggle")
assert sonar_toggle["type"] == "button" and sonar_toggle["aria-pressed"] == "false"
assert '"/api/v2/sonar/audio"' in js
assert '"/api/v2/helicopter/audio"' in js
assert "if (!S.sonarAudioEnabled || S.sonarAudioController || S.sonarAudioSocket?.readyState" in js
assert 'new AudioWorkletNode(S.audio, "sonar-audio-v2"' in js
assert '"u-jagd-audio-v2"' in js
assert "sonarAudioSources.length >= sonarAudioMaxSources" in js
assert "queuedAhead >= sonarAudioTargetAhead" in js
assert "start > S.audio.currentTime + 7.0" in js
assert "raster = {canvas: off, context, image, pixels:" in js
assert "const image = raster.context.createImageData" not in js
assert "offset + value.byteLength > output.byteLength" in js
assert "offset !== output.byteLength" in js
assert 'response.headers.get("x-u-jagd-pcm") !== "s16le"' in js
assert "sonarAudioController?.abort()" in js
assert "flushSonarAudioQueue()" not in js
toggle_handler = js.split('$("sonar-live-toggle").addEventListener("click"', 1)[1]
assert "new Audio()" in toggle_handler and "scheduleSonarAudioPoll()" in toggle_handler
assert "machine.cruise_lines" in analyzer_renderer
assert "machine.cruise_broadband" in analyzer_renderer
assert "analyzer_spectrum_legend" in html
assert "analyzer_hypothesis_legend" in html
def test_v2_proposal_event_and_role_simlog_contracts_are_strict_and_role_scoped():
html = ASSETS.joinpath("index.html").read_text()
js = client_js()
assert "future-v2" not in html
assert {"target-proposal-controls", "target-proposal", "navigation-proposal"} <= {
attrs["id"] for _, attrs in Document(html).elements if "id" in attrs
}
assert 'request("/proposals"' in js and 'request("/events"' in js
assert 'request("/simlog")' in js
assert '["protocol", "session", "epoch", "role", "target", "navigation"]' in js
assert '["protocol", "session", "epoch", "role", "latest_seq", "events"]' in js
assert '["protocol", "session", "epoch", "role", "entries"]' in js
assert 'sendStationAction("propose_target", {ref: track.ref})' in js
assert 'sendStationAction("clear_target_proposal", {})' in js
assert 'sendStationAction("propose_navigation", params)' in js
assert 'event.severity === "warning"' in js
assert "localStorage" not in js and "sessionStorage" not in js
assert 'location.hash === "#simlog"' in js and 'session?.simlog !== true' in js
def test_v2_roles_have_dedicated_payload_renderers():
html = ASSETS.joinpath("index.html").read_text()
js = client_js()
document = Document(html)
# The submarine sonar room shares the sonar section; its commander has one.
roles = ("bridge", "sonar", "weapons", "damage", "opz", "radio",
"engine", "helicopter", "eloka", "uboot")
sections = {attrs.get("data-station-role"): attrs for _, attrs in document.elements
if attrs.get("data-station-role")}
renderers = js[js.index("function tacticalEntries"):js.index("function visualContext")]
labels = set(re.findall(r'\["([a-z][a-z_]+)",', renderers))
en, de = catalogs()
assert not {key for key in labels if PREFIX + key not in en or PREFIX + key not in de}
assert set(sections) == set(roles)
assert all(sections[role]["id"] == f"station-{role}" and
sections[role]["aria-labelledby"] == f"station-{role}-title"
for role in roles)
distinctive = {
"Bridge": ("navigation", "orders", "tactical_summary", "rudder_angle", "yaw_rate"),
"Sonar": ("observations", "settings", "harmonic_hz", "audio_enabled", "quiet_mode"),
"Uboot": ("navigation", "status", "weapons", "alarms", "contacts", "feed"),
"Weapons": ("inventory", "readiness", "designated_target", "own_weapons", "chaff_ready"),
"Damage": ("compartments", "teams", "total", "sunk"),
"Opz": ("observations", "fusions", "radar", "source_classifications", "own_assets"),
"Radio": ("observations", "logged_fixes", "logged_bearings", "messages", "observer_x"),
"Engine": ("propulsion", "machinery", "environment_effects", "telegraph", "cavitating"),
"Helicopter": ("asset", "waypoint", "buoys", "battery_s", "airborne"),
"Eloka": ("intercepts", "frequency_hz", "prf_hz", "modulation", "annotation", "candidates", "correlations"),
}
for index, (name, fields) in enumerate(distinctive.items()):
start = js.index(f"function render{name}Station(payload)")
next_names = list(distinctive)[index + 1:index + 2]
end = js.index(f"function render{next_names[0]}Station(payload)", start) if next_names else js.index("function renderStationView()", start)
renderer = js[start:end]
assert "innerHTML" not in renderer
assert all(field in renderer for field in fields), (name, fields, renderer)
station_dispatch = js.split("function renderStationView()", 1)[1].split("function clearRoleState()", 1)[0]
assert "v2State[active]" in station_dispatch
assert 'section.querySelectorAll("dl, .station-list")' in station_dispatch
assert 'dataset.tracks = String(!active || trackRoles.has(active))' in station_dispatch
assert ('const trackRoles = new Set(["bridge", "sonar", "weapons", "opz", '
'"helicopter", "eloka", "uboot", "uboot_sonar",\n "uboot_weapons", "uboot_nav"])') in js
assert "innerHTML" not in station_dispatch
def test_v2_enriched_visualizations_use_canvases_and_accessible_equivalents():
html = ASSETS.joinpath("index.html").read_text()
js = client_js()
document = Document(html)
ids = {attrs["id"] for _, attrs in document.elements if "id" in attrs}
canvases = {attrs["id"] for tag, attrs in document.elements
if tag == "canvas" and "id" in attrs}
expected = {"role-map", "sonar-broadband", "sonar-lofar", "sonar-demon",
"sonar-tma-plot", "sonar-environment", "sonar-active",
"damage-schematic", "engine-instruments", "eloka-scope",
"weapons-system"}
assert expected <= canvases
assert {f"{name}-text" for name in (
"role-map", "sonar-broadband", "sonar-lofar", "sonar-demon",
"sonar-tma", "sonar-environment", "sonar-active",
"damage-schematic", "engine-instruments", "eloka-scope",
"weapons-system")} <= ids
for function, fields in {
"drawSonarVisuals": ("broadband", "lofar", "demon", "tma", "bt",
"active_echoes", "receiver"),
"drawRoleMap": ("landmasses", "range_uncertainty_nm", "bearingLogs",
"fixes", "assets", "members", "drawOpzSweepOverlay"),
"drawOpzSweepOverlay": ("sweep_bearing", "surface_effective_range_nm",
"air_effective_range_nm",
"roleMapSweepCtx"),
"drawDamageVisual": ("compartments", "flood", "fire", "trend", "teams"),
"drawEngineVisual": ("telegraph", "rpm", "speed", "noise",
"effective_speed_cap", "roll", "pitch"),
"drawElokaVisual": ("bearing", "frequency_hz", "prf_hz",
"modulation", "candidates", "correlations"),
"drawWeaponsVisual": ("readiness", "interlock", "tubes", "nixies",
"active_assets"),
}.items():
body = js.split(f"function {function}", 1)[1].split("\nfunction ", 1)[0]
assert "getContext" in js
assert all(field in body for field in fields), (function, fields)
assert all(field in js.split("function mapPayload", 1)[1].split(
"function drawRoleMap", 1)[0] for field in
("logged_bearings", "logged_fixes", "waypoint", "buoys", "active_assets"))
validator = js.split("function validateV2State", 1)[1].split(
"function buildDisplayModel", 1)[0]
for field in ("visualization", "threat", "active_assets", "trend",
"asm_observations", "covariance_nm2", "tas_performance",
"rtb_margin_s", "position_available"):
assert field in validator
assert 'const forbidden = new Set(["target_id", "track_id", "kind", "signature", "emitter_key", "seed", "rng"])' in validator
clearer = js.split("function clearRoleState", 1)[1].split(
"function renderLobby", 1)[0]
assert "clearVisuals()" in clearer and "releaseCanvas" in js.split(
"function clearVisuals", 1)[1].split("function tacticalEntries", 1)[0]
assert "roleMapViews[role]" in js and '("role-map").addEventListener("wheel"' in js
assert '("role-map").addEventListener("pointerdown"' in js
assert '("role-map").addEventListener("keydown"' in js
def test_simlog_map_dialog_toolbar_is_structured_and_wired():
"""Regression: .simlog-map-shell/-header/-toolbar/-layout/-plot/-sidebar
in style.css had no matching markup (the dialog was unstyled default
block flow), and its three toolbar buttons (close/fit-to-world/
fit-to-units) had no click handlers at all - silently dead controls."""
html = ASSETS.joinpath("index.html").read_text()
js = client_js()
document = Document(html)
dialog = next(attrs for tag, attrs in document.elements if attrs.get("id") == "simlog-map-dialog")
assert "hidden" in dialog
classes = {attrs.get("class", "") for _, attrs in document.elements}
for expected in ("simlog-map-shell", "simlog-map-header", "simlog-map-toolbar",
"simlog-map-layout", "simlog-map-plot", "simlog-map-sidebar"):
assert any(expected in value.split() for value in classes), expected
for control in ("simlog-map-close", "simlog-map-world", "simlog-map-units-fit"):
assert any(attrs.get("id") == control and attrs.get("data-i18n")
for _, attrs in document.elements), control
body = js.split("function closeSimlogMap", 1)[1].split("function openSimlogMap", 1)[0]
assert "dialog.close()" in body
wiring = js.split('$("simlog-map-close").addEventListener("click"', 1)[1][:900]
assert "closeSimlogMap" in wiring
assert '$("simlog-map-dialog").addEventListener("close"' in wiring
assert "simlogMapData = null" in wiring and "releaseCanvas(simlogMapCanvas)" in wiring
assert '$("simlog-map-world").addEventListener("click"' in wiring
assert '$("simlog-map-units-fit").addEventListener("click"' in wiring
assert 'simlogMapFit = "world"' in wiring and 'simlogMapFit = "units"' in wiring
world_button = next(attrs for _, attrs in document.elements if attrs.get("id") == "simlog-map-world")
units_button = next(attrs for _, attrs in document.elements if attrs.get("id") == "simlog-map-units-fit")
assert world_button["aria-pressed"] == "true" and units_button["aria-pressed"] == "false"
def test_v2_browser_presentation_interactions_are_bounded_and_guarded():
html = ASSETS.joinpath("index.html").read_text()
js = client_js()
ids = {attrs["id"] for _, attrs in Document(html).elements if "id" in attrs}
assert {"damage-team", "analysis-sensors", "analysis-emitters"} <= ids
assert "const maxRoleMapHits = 512" in js
assert "roleMapHits.length < maxRoleMapHits" in js
role_pointer = js.split('$("role-map").addEventListener("pointerdown"', 1)[1].split(
'$("role-map").addEventListener("keydown"', 1)[0]
assert "Math.hypot(dx, dy) > 5" in role_pointer
assert "selectTrack(contact.ref)" in role_pointer
assert 'sendStationAction("helicopter_set_waypoint", {x: worldX, y: worldY})' in role_pointer
assert "stationActionAvailable()" in role_pointer
damage_pointer = js.split('$("damage-schematic").addEventListener("click"', 1)[1].split(
'$("follow").addEventListener', 1)[0]
assert '"damage_unassign_team" : "damage_assign_team"' in damage_pointer
assert "stationActionAvailable()" in damage_pointer
animation = js.split("function currentOpzSweepBearing", 1)[1].split(
"function roleMapGeometry", 1)[0]
assert "requestAnimationFrame(animate)" in animation
assert "cancelAnimationFrame(S.opzSweepFrame)" in animation
assert "document.hidden" in animation and 'v2State?.phase === "live"' in animation
assert "radar.surface || radar.air" in animation
assert "radar.sweep_rate_deg_s" in animation and ": 90" in animation
validator = js.split("function validateV2State", 1)[1].split("function buildDisplayModel", 1)[0]
assert '"sweep_rate_deg_s"' in validator
assert '"speed_kn", "altitude_m", "observer_x", "observer_y"' in validator
assert '["speed", unit(row.speed_kn, "kn")]' in js
role_map = js.split("function drawRoleMap", 1)[1].split("function gauge", 1)[0]
assert 'plot.context.textAlign = "left"' in role_map
assert 'plot.context.strokeText(text, 4, baseline)' in role_map
assert 'plot.context.save(); plot.context.textAlign = "right"' in role_map
assert 'const followTarget = role === "helicopter"' in role_map
assert 'payload.asset : data.own' in role_map
assert '"follow_helicopter" : "follow"' in role_map
assert 'item.display || item.ref || t("helicopter")' in role_map
lobby = js.split("function renderLobby", 1)[1].split("function acceptSession", 1)[0]
assert 'if (select.dataset.options !== signature)' in lobby
assert "document.activeElement !== select || S.stationMutation" in lobby
helicopter = js.split("function renderHelicopterStation", 1)[1].split(
"function renderElokaStation", 1)[0]
assert '["reference", row.label]' in helicopter
assert '["reference", row.ref]' not in helicopter
def test_browser_game_audio_uses_one_shot_events_without_ambient_loops():
js = client_js()
audio = js.split("function playGameEffect", 1)[1].split(
"function renderSonarAudio", 1)[0]
assert "gameEffectKinds.has(kind)" in audio
assert "oscillator.stop(now + duration + .02)" in audio
assert "source.loop = true" not in js
assert "machineryOscillator" not in js
assert "helicopterOscillator" not in js
assert "bridgeCavitationSource" not in js
def test_disabled_web_controls_publish_specific_localized_reasons():
js = client_js()
resolver = js.split("const unavailable =", 1)[1].split(
"function renderOpzControls", 1)[0]
assert 'document.querySelectorAll("button, input, select")' in resolver
assert 'control.title = text' in resolver
assert 'control.dataset.disabledReason = text' in resolver
assert 'control.setAttribute("aria-disabled", "true")' in resolver
assert '$("disabled-control-explain").hidden = visibleReasons.length === 0' in resolver
assert '$("disabled-control-help").textContent = reasons.join(" ")' in js
for key in (
"reason_tas_too_fast", "reason_tas_too_slow", "reason_tas_not_streamed",
"reason_cooldown", "reason_no_focus", "reason_no_target",
"reason_invalid_depth", "reason_no_inventory", "reason_no_ready_tube",
"reason_not_airborne", "reason_position_unavailable",
"reason_direct_fire_grant", "reason_sonar_audio_grant",
"reason_station_request_pending", "reason_station_change_pending",
"reason_station_occupied", "reason_station_already_leased",
):
assert f'"{key}"' in resolver
def test_commander_capture_keeps_pairing_code_out_of_public_assets():
source = (ROOT / "tools" / "capture_commander.py").read_text()
capture = source.split("def _capture_one", 1)[1].split("def capture_all", 1)[0]
assert 'server._http.assets["/app.js"] =' not in capture
assert 'script_path = f"/.capture-{nonce}.js"' in capture
assert 'row["name"] == capture_name' in capture
assert "server._rotate_code_locked()" in capture
def test_reference_catalog_renders_existing_sensor_and_emitter_details():
js = client_js()
css = client_css()
renderer = js.split("function renderContactAnalysis", 1)[1].split(
"function request", 1)[0]
for field in ("modes", "emits", "synthetic_range_nm", "sensitivity_db",
"cadence_s", "bearing_uncertainty_deg", "range_uncertainty_nm",
"depth_uncertainty_m", "frequency_band_hz", "prf_band_hz",
"modulation_codes"):
assert field in renderer
assert '$("analysis-sensors")' in renderer
assert '$("analysis-emitters")' in renderer
assert "innerHTML" not in renderer
assert re.search(r"\.analyzer-detail \{[^}]*overflow-y: auto", css)
assert not re.search(r"\.analysis-component-list \{[^}]*overflow:", css)
def test_v2_nonlethal_station_controls_are_native_and_exactly_wired():
html = ASSETS.joinpath("index.html").read_text()
js = client_js()
document = Document(html)
ids = {attrs["id"] for _, attrs in document.elements if "id" in attrs}
assert {
"sonar-control-page", "sonar-bearing", "sonar-clear-focus",
"sonar-array-mode", "sonar-tas", "sonar-depth", "sonar-bt",
"sonar-ping", "sonar-tma", "sonar-gain", "sonar-band",
"sonar-notch", "sonar-peak", "sonar-harmonic-input",
"sonar-audition-mode", "sonar-listen-band", "sonar-listen-notch",
"opz-designate", "engine-telegraph", "engine-speed", "engine-quiet",
"helicopter-launch", "helicopter-return", "helicopter-x",
"helicopter-y", "helicopter-buoy", "station-command-status",
} <= ids
actions = {
"engine_set_telegraph", "engine_set_course", "engine_set_speed",
"engine_set_quiet_mode",
"damage_assign_team", "damage_unassign_team", "radio_capture_hfdf",
"radio_task_accept", "radio_task_decline", "crew_action_stations",
"crew_watch_change",
"eloka_annotate", "eloka_clear_annotation",
"sonar_set_listen_bearing", "sonar_set_focus", "sonar_clear_focus",
"sonar_set_array_mode", "sonar_set_tas", "sonar_set_tow_depth",
"sonar_measure_bt", "sonar_active_ping", "sonar_set_tma_enabled",
"sonar_set_gain", "sonar_set_audition_mode", "sonar_set_band_preset", "sonar_set_notch",
"sonar_set_peak_hold", "sonar_set_harmonic",
"sonar_designate_target", "opz_designate_target",
"helicopter_launch", "helicopter_return", "helicopter_set_waypoint",
"helicopter_deploy_buoy",
}
assert all(f'"{action}"' in js for action in actions)
assert "sendStationAction(action, params)" in js
assert "data-station-action" not in html # Row actions are built from opaque projected refs.
assert "sonar audio" not in html.lower()
assert 'control.id === "sonar-control-page"' in js
assert 'stationDrafts.clear()' in js and 'sessionChanged || epochChanged' in js
assert 'control.closest("[data-station-role]")' in js
def test_v2_direct_fire_controls_use_opaque_projections_and_exact_actions():
html = ASSETS.joinpath("index.html").read_text()
js = client_js()
document = Document(html)
ids = {attrs["id"] for _, attrs in document.elements if "id" in attrs}
assert {
"weapons-fire-target", "weapons-fire-depth", "weapons-fire-torpedo",
"weapons-fire-helicopter", "weapons-fire-nixie", "weapons-fire-status",
"opz-fire-target", "opz-fire-essm", "opz-fire-chaff", "opz-fire-status",
"helicopter-fire-target", "helicopter-fire-depth",
"helicopter-fire-torpedo", "helicopter-fire-status",
} <= ids
actions = {"weapons_launch_torpedo", "helicopter_launch_torpedo",
"weapons_deploy_nixie", "opz_launch_essm", "opz_launch_chaff"}
assert all(f'data-fire-action="{action}"' in html for action in actions)
direct = js.split("function directFireSpec", 1)[1].split(
"function renderStationControls", 1)[0]
assert all(f'"{action}"' in direct for action in actions)
assert "target_choices" in direct and "asm_observations" in direct
assert "selectedTrack" not in direct and "selected =" not in direct
assert "target_id" not in direct and "track_id" not in direct
assert "session?.grants.direct_fire === true" in direct
assert "chartMatches(S.snapshot)" in direct and "!S.pending" in js.split(
"function stationActionAvailable", 1)[1].split("function renderOpzControls", 1)[0]
assert "performance.now() + 5000" in direct
assert "station_generation" in direct and "v2State?.epoch" in direct
assert "sendStationAction(state.action, spec.params)" in direct
assert "transmitCommand" not in direct
assert all(term not in html for term in ("target_id", "track_id", "profile_key"))
def test_guide_is_complete_static_content_with_panel_local_navigation():
html = ASSETS.joinpath("index.html").read_text()
js = client_js()
css = client_css()
document = Document(html)
guide = next(attrs for _, attrs in document.elements
if attrs.get("id") == "panel-guide")
assert "pending-panel" not in guide["class"]
links = [attrs for tag, attrs in document.elements
if tag == "a" and attrs.get("href", "").startswith("#guide-")]
expected = {"guide-access", "guide-operations", "guide-observations",
"guide-proposals", "guide-eloka", "guide-analyzer",
"guide-connection", "guide-authority"}
ids = {attrs["id"] for _, attrs in document.elements if "id" in attrs}
assert {link["href"][1:] for link in links} == expected <= ids
assert all("data-i18n" in link for link in links)
guide_handler = js.split(
'for (const link of document.querySelectorAll("#guide-nav a"))', 1
)[1].split('$("track-list").addEventListener', 1)[0]
assert "event.preventDefault()" in guide_handler
assert 'const panel = $("panel-guide")' in guide_handler
assert "panel.scrollTop" in guide_handler and "window.scroll" not in guide_handler
assert "sendCommand" not in guide_handler and 'request("/commands"' not in guide_handler
assert re.search(r"\.guide-panel:not\(\[hidden\]\).*display: block", css)
assert re.search(r"\.tab-panel \{[^}]*overflow-y: auto", css)
def test_contacts_panel_owns_bounded_browser_and_detail_scrolling():
html = ASSETS.joinpath("index.html").read_text()
css = client_css()
document = Document(html)
contacts = next(attrs for _, attrs in document.elements
if attrs.get("id") == "panel-contacts")
assert "pending-panel" not in contacts["class"]
ids = {attrs["id"] for _, attrs in document.elements if "id" in attrs}
assert "contact-filter" in ids
assert {"analysis-filter", "analysis-category", "analysis-list",
"analysis-profile", "analysis-images"} <= ids
assert 'maxlength="96"' in html
assert re.search(r"\.analyzer-panel:not\(\[hidden\]\).*overflow: hidden", css)
assert re.search(r"\.analysis-list \{[^}]*overflow-y: auto", css)
assert re.search(r"\.analyzer-detail \{[^}]*overflow-y: auto", css)
assert "silhouette" not in client_js().lower()
for catalog in catalogs():
spectrum = catalog[PREFIX + "analyzer_spectrum_legend"]
hypothesis = catalog[PREFIX + "analyzer_hypothesis_legend"]
assert "LOFAR" in spectrum and "0-300 Hz" in spectrum
assert "0-50 Hz" in hypothesis
assert "DEMON" in hypothesis
assert any(word in hypothesis.lower() for word in ("measurement", "messung"))
def test_commander_catalogs_cover_markup_and_script():
en, de = catalogs()
english = {key: value for key, value in en.items() if key.startswith(PREFIX)}
german = {key: value for key, value in de.items() if key.startswith(PREFIX)}
assert english.keys() == german.keys()
assert len(english) >= 100
for key, value in english.items():
assert isinstance(value, str) and value and isinstance(german[key], str) and german[key]
fields = lambda text: {field for _, field, _, _ in Formatter().parse(text) if field is not None}
assert fields(value) == fields(german[key])
assert all(field.isidentifier() for field in fields(value))
html = ASSETS.joinpath("index.html").read_text()
js = client_js()
markup_keys = set(re.findall(r'data-i18n(?:-aria)?="([\w]+)"', html))
literal_keys = set(re.findall(r'\bt\("([\w]+)"', js))
metric_keys = set(re.findall(r'\["([a-z_]+)", (?:unit\(|number\(|t\(|`|track\.|item\.|helo\.|typeof |finite\()', js))
metric_keys -= {"synthetic_range_nm", "sensitivity_db", "cadence_s", "depth_uncertainty_m",
"bearing_uncertainty_deg", "range_uncertainty_nm"}
dynamic_keys = set(re.findall(r'"((?:aff_|class_|domain_|command_|proposal_|connection_|sound_|phase_|damage_|helo_|reason_)[a-z_]+)"', js))
dynamic_keys -= {"damage_assign_team", "damage_unassign_team", "damage_control"}
dynamic_keys |= {"connection_syncing", "connection_connected", "connection_stale", "connection_unpaired"}
dynamic_keys |= {"connection_lobby", "lobby_pending", "lobby_waiting", "lobby_request_cleared",
"station_mutation_failed", "station_request", "station_requested",
"occupancy_available", "occupancy_occupied", "occupancy_mine",
"role_assigned", "role_release", "role_commands", "role_read_only", "role_revoked"}
dynamic_keys |= {f"station_{station}" for station in
("bridge", "sonar", "weapons", "damage", "opz", "radio",
"engine", "helicopter", "eloka")}
required = {PREFIX + key for key in markup_keys | literal_keys | metric_keys | dynamic_keys}
assert required <= english.keys(), sorted(required - english.keys())
for reason in ("invalid_schema", "unauthorized", "stale_session", "stale_epoch", "commands_blocked",
"duplicate_id", "revision_conflict", "unknown_track", "ineligible_track", "ok"):
assert f'{reason}: "reason_{reason}"' in js
key = PREFIX + "reason_" + reason
assert english[key] != reason and german[key] != reason and english[key] != german[key]
assert not any(item.name.endswith(".json") for item in ASSETS.iterdir())
BROWSER_CONTRACT = r"""
"use strict";
const $test = (id) => document.getElementById(id);
const failures = [];
window.addEventListener("error", (event) => failures.push(event.message));
window.addEventListener("unhandledrejection", (event) => failures.push(String(event.reason)));
const assert = (condition, message) => { if (!condition) throw new Error(message); };
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
// Chromium's dump-dom virtual clock can starve compositor frames. Drive the
// same on-demand canvas callbacks with that clock, not a wall-time compositor.
window.requestAnimationFrame = (callback) => setTimeout(() => callback(performance.now()), 16);
window.cancelAnimationFrame = clearTimeout;
async function until(predicate, message) {
for (let i = 0; i < 900; i++) { if (predicate()) return; await sleep(20); }
throw new Error(message);
}
const nativeFetch = window.fetch.bind(window);
const issued = [];
const commands = [];
let active = 0, maxActive = 0, charts = 0, stateCalls = 0, tones = 0;
let offline = false, mismatch = false, loseAck = false, holdState = false, stalledSeq = false;
let reconcileActor = false, publishActorResults = false, rejectRetry = false, actorEffects = 0;
const actorResults = new Map();
let lastSignal = null;
const liveFixture = __STATE__;
const statusFixture = __STATUS_STATE__;
const fixture = structuredClone(statusFixture);
const simlogFixture = [{seq: 1, t: 10, stamp: "00:10", cat: "state", text: "", data: {
mission_t: 10, result: null,
ship: {x: 250, y: 250, course: 15, speed: 12, damage: 0, sunk: false, stations: {}},
world: {hour: 12, sea_state: 3, night: false}, weapons: {},
subs: [{id: 1, x: 100, y: 100}], surfaces: [{id: 2, x: 150, y: 150}],
animals: [{id: 3, x: 200, y: 200}], torpedoes: [{id: 4, x: 220, y: 220}],
enemy_torpedoes: [{id: 5, x: 230, y: 230}], decoys: [{id: 6, x: 240, y: 240}],
asms: [{seq: 7, x: 260, y: 260}], essms: [{seq: 8, x: 270, y: 270}],
asrocs: [{seq: 9, x: 280, y: 280}], nixies: [{seq: 10, x: 290, y: 290}],
buoys: [{seq: 11, x: 300, y: 300}], flights: [{seq: 12, x: 310, y: 310}],
raiders: [{seq: 13, x: 320, y: 320}], helo: {x: 330, y: 330, airborne: true}
}}];
const drawnFixes = new Map();
const drawnSonarFixes = new Map();
let canvasFrame = {texts: [], translations: [], fills: 0};
let lookoutFrame = {texts: [], translations: [], rotations: [], arcs: [], fills: 0};
let simlogMapTexts = [];
const nativeFillRect = CanvasRenderingContext2D.prototype.fillRect;
CanvasRenderingContext2D.prototype.fillRect = function (...args) {
if (this.canvas.id === "lookout") lookoutFrame = {texts: [], translations: [], rotations: [], arcs: [], fills: 0};
else if (this.canvas.id === "simlog-map") simlogMapTexts = [];
else canvasFrame = {texts: [], translations: [], fills: 0};
return nativeFillRect.apply(this, args);
};
const nativeTranslate = CanvasRenderingContext2D.prototype.translate;
CanvasRenderingContext2D.prototype.translate = function (x, y) {
(this.canvas.id === "lookout" ? lookoutFrame : canvasFrame).translations.push({x, y});
return nativeTranslate.call(this, x, y);
};
const nativeRotate = CanvasRenderingContext2D.prototype.rotate;
CanvasRenderingContext2D.prototype.rotate = function (angle) {
if (this.canvas.id === "lookout") lookoutFrame.rotations.push(angle);
return nativeRotate.call(this, angle);
};
const nativeFill = CanvasRenderingContext2D.prototype.fill;
CanvasRenderingContext2D.prototype.fill = function (...args) {
(this.canvas.id === "lookout" ? lookoutFrame : canvasFrame).fills++;
return nativeFill.apply(this, args);
};
const nativeArc = CanvasRenderingContext2D.prototype.arc;
CanvasRenderingContext2D.prototype.arc = function (x, y, radius, start, end, ...rest) {
if (this.canvas.id === "lookout") lookoutFrame.arcs.push({x, y, radius, start, end});
return nativeArc.call(this, x, y, radius, start, end, ...rest);
};
const nativeFillText = CanvasRenderingContext2D.prototype.fillText;
CanvasRenderingContext2D.prototype.fillText = function (text, x, y, ...rest) {
if (/^[-\d.,]+$/.test(text)) {
assert(text !== "-0", "chart grid does not label the origin as negative zero");
assert(x >= 0 && x + this.measureText(text).width <= this.canvas.clientWidth &&
y >= parseFloat(this.font) && y <= this.canvas.clientHeight, "numeric chart labels remain inside the canvas");
}
if (this.canvas.id === "simlog-map") {
const height = parseFloat(this.font);
simlogMapTexts.push({text, x, y: y - height, right: x + this.measureText(text).width, bottom: y + 3});
}
(this.canvas.id === "lookout" ? lookoutFrame : canvasFrame).texts.push(text);
if (fixture.tracks.some((track) => track.label === text)) drawnFixes.set(text, {x, y});
if (/ (PING|TMA|SONOBUOY)$/.test(text)) drawnSonarFixes.set(text, {x, y});
return nativeFillText.call(this, text, x, y, ...rest);
};
const chartFixture = {revision: "chart-A", size_nm: 500, landmasses: [{points: [[20, 20], [160, 40], [90, 160], [20, 20]]}], disclaimer: "Synthetic test geography <script>not markup</script>"};
class TestAudio {
constructor() { this.state = "suspended"; this.currentTime = 0; this.destination = {}; }
async resume() { this.state = "running"; }
async suspend() { this.state = "suspended"; }
createOscillator() {
tones++;
return {frequency: {setValueAtTime() {}}, connect() {}, disconnect() {}, start() {}, stop() { setTimeout(() => this.onended?.(), 300); }};
}
createGain() { return {gain: {setValueAtTime() {}, linearRampToValueAtTime() {}}, connect() {}, disconnect() {}}; }
}
window.AudioContext = TestAudio;
window.fetch = async (url, options = {}) => {
issued.push({url: String(url), options});
active++; maxActive = Math.max(maxActive, active);
try {
assert(options.signal instanceof AbortSignal, "every request has an abort deadline");
assert(!String(url).includes("test-secret"), "token absent from URL");
const path = new URL(url, location.href).pathname;
if (path === "/api/v2/session") return new Response("{}", {status: 404});
if (path === "/api/v2/ui" || path === "/api/v2/contacts") {
assert(!options.headers.Authorization, "public static data has no bearer token");
return await nativeFetch(url, options);
}
if (path === "/api/v2/pair") return await nativeFetch(url, options);
if (path === "/api/v2/commands") {
const command = JSON.parse(options.body);
commands.push(command);
if (loseAck) throw new TypeError("lost acknowledgement");
if (rejectRetry) return new Response("", {status: 429});
if (reconcileActor) {
// A small crew actor fixture: ID replay precedes revision checks. It
// lets the browser prove that reconciliation does not create new actions.
let previous = actorResults.get(command.id);
if (previous) {
assert(previous.envelope === options.body, "replay has the identical serialized envelope");
} else {
const applied = command.revision === fixture.revision;
const result = {id: command.id, status: applied ? "applied" : "rejected", reasoncode: applied ? "ok" : "revision_conflict"};
previous = {envelope: options.body, result};
actorResults.set(command.id, previous);
if (applied) {
actorEffects++;
fixture.revision++;
fixture.proposal = {ref: command.track, label: "Reconciled proposal", status: "pending"};
}
}
if (publishActorResults) fixture.results.push(structuredClone(previous.result));
}
return await nativeFetch(url, options);
}
if (path === "/api/v2/state") {
stateCalls++;
lastSignal = options.signal;
if (holdState) await new Promise((resolve, reject) => options.signal.addEventListener("abort", () => reject(new DOMException("timeout", "AbortError")), {once: true}));
if (offline) throw new TypeError("offline");
if (!stalledSeq) fixture.seq++;
await sleep(15);
return new Response(JSON.stringify(fixture), {status: 200});
}
if (path === "/api/v2/chart") {
charts++;
await sleep(15);
const picture = fixture.ownship.x === null ? {size_nm: 500, landmasses: [], disclaimer: ""} : chartFixture;
return new Response(JSON.stringify({...picture, revision: mismatch ? "wrong-chart" : fixture.chart_revision}), {status: 200});
}
if (path === "/api/v2/simlog") return new Response(JSON.stringify(simlogFixture), {status: 200});
throw new Error("unexpected request " + path);
} finally { active--; }
};
async function runContract() {
await until(() => !$test("shell").hidden, "translations bootstrap");
assert($test("operations").hidden, "no data before pairing");
assert(issued[0].url.includes("/api/v2/session"), "v2 session resume attempted first");
assert($test("sound").getAttribute("aria-pressed") === "false" && tones === 0, "muted by default");
assert($test("code").maxLength === 6 && $test("code").autocapitalize === "characters", "six-character code input with capitalization hint");
for (const invalid of ["123456", "ABC123", "12ABCD", "123AB", "123ABCD", "123A!C"]) {
$test("code").value = invalid;
assert(!$test("code").checkValidity(), "native validation rejects malformed code " + invalid);
$test("pair-form").requestSubmit();
}
await sleep(50);
assert(!issued.some((entry) => entry.url === "/api/v2/pair"), "invalid codes never reach the network");
$test("code").value = "123ABC";
assert($test("code").checkValidity(), "three digits followed by three uppercase letters accepted");
$test("pair-form").requestSubmit();
await until(() => $test("pair-error").textContent && !$test("pair-submit").disabled, "rate-limited pairing returns an editable form");
assert($test("pair-error").textContent.includes("Wait") && $test("pair-error").textContent.includes("new code"), "pairing failure advises waiting or a new code");
assert($test("code").value === "" && $test("operations").hidden, "failed pairing clears code and reveals no session");
await sleep(650);
assert(issued.filter((entry) => entry.url === "/api/v2/pair").length === 1, "rate-limited pairing never retries automatically");
$test("code").value = "123abc";
assert($test("code").checkValidity(), "lowercase letters pass native validation before normalization");
$test("pair-form").requestSubmit();
await until(() => !$test("operations").hidden, "first matched chart and session");
await sleep(50);
assert(charts === 1 && stateCalls >= 2, "chart sandwiched by snapshots");
assert($test("connection").dataset.state === "connected" && $test("phase").textContent === "Menu", "status-only menu is a healthy connection");
assert($test("mission-name").textContent === "" && $test("objective").textContent === "", "status-only mission is empty");
assert([...$test("own-metrics").querySelectorAll("dd")].every((value) => /^[\s/-]+$/.test(value.textContent)), "null ownship metrics are unavailable, not zero");
assert([...$test("inventory").querySelectorAll("dd"), ...$test("mission-metrics").querySelectorAll("dd")].every((value) => value.textContent === "--"), "null inventory and clock stay unavailable");
assert(!$test("track-list").querySelector("button") && !$test("damage-list").querySelector(".damage-row"), "status-only snapshot has no contacts or damage");
assert($test("follow").disabled && $test("follow").getAttribute("aria-pressed") === "false", "no follow without an ownship position");
$test("follow").dispatchEvent(new Event("click"));
$test("zoom-in").click();
await sleep(50);
assert($test("follow").getAttribute("aria-pressed") === "false", "follow handler cannot invent a position");
assert(canvasFrame.texts.length > 0 && canvasFrame.translations.length === 0 && canvasFrame.fills === 0, "menu draws chart grid but no ownship, helicopter, land or bearing wedges");
assert(!canvasFrame.texts.includes("Own ship") && !canvasFrame.texts.includes("Own helicopter"), "no imaginary asset labels");
assert($test("propose").disabled && $test("apply-classification").disabled && $test("apply-affiliation").disabled && $test("clear-proposal").disabled, "status-only commands are blocked");
await sleep(650);
assert($test("connection").dataset.state === "connected" && commands.length === 0, "status-only polling stays healthy without commands");
const menuSession = fixture.session, menuRevision = fixture.chart_revision;
Object.assign(fixture, structuredClone(liveFixture), {epoch: fixture.epoch + 1, seq: fixture.seq});
await until(() => !$test("operations").hidden && $test("mission-name").textContent === liveFixture.mission.name, "live mission after menu");
await sleep(50);
assert(fixture.session === menuSession && fixture.chart_revision === menuRevision && charts === 2, "epoch change refetches chart even with unchanged session and chart revision");
assert($test("chart-disclaimer").textContent === chartFixture.disclaimer && canvasFrame.fills > 0, "live chart replaces the redacted chart");
assert(!$test("follow").disabled && canvasFrame.translations.length === 1 && canvasFrame.texts.includes("Own ship"), "live ownship geometry returns");
assert(!document.body.textContent.includes("test-secret"), "token never rendered");
assert(localStorage.length === 0 && sessionStorage.length === 0, "no persistent token");
assert($test("code").value === "", "code cleared");
assert($test("mission-name").textContent === fixture.mission.name && !$test("mission-name").children.length, "authored mission is inert text");
assert($test("chart-disclaimer").children.length === 0, "disclaimer is inert text");
const bounds = $test("chart").getBoundingClientRect();
assert(innerWidth === __WIDTH__, "requested CSS viewport width: " + innerWidth);
assert(bounds.width > 200 && bounds.height > 200, "responsive chart is visible");
assert(document.documentElement.scrollWidth <= innerWidth + 1, "no horizontal page overflow");
assert(document.documentElement.scrollHeight <= innerHeight + 1, "authenticated shell fits the viewport");
assert($test("chart").width >= bounds.width, "high DPI backing canvas");
assert(!drawnFixes.has(fixture.tracks[0].label), "bearing-only contact has no invented position marker");
const fix = drawnFixes.get(fixture.tracks[1].label), air = drawnFixes.get(fixture.tracks[2].label);
assert(fix && air && Math.abs((air.x - fix.x) / 50 - (air.y - fix.y) / -30) < .001, "equal X/Y scale, north up");
const teamCounts = [...$test("damage-list").querySelectorAll(".metrics")].map((list) => list.lastElementChild.textContent);
assert(teamCounts[0] === "Assigned teams (count)2" && teamCounts[1] === "Assigned teams (count)0", "team ID arrays display explicit counts, including empty assignments");
const heloLabels = {AUF: "Airborne", HANGAR: "In hangar", ZURUECK: "Returning", VERLOREN: "Lost"};
for (const state of ["AUF", "HANGAR", "ZURUECK", "VERLOREN"]) {
Object.assign(fixture.ownship.helo, {state, x: 310, y: 300, course: 10});
await until(() => $test("helo-metrics").textContent.includes(heloLabels[state]), "helicopter state snapshot " + state);
await sleep(50);
assert(canvasFrame.texts.includes("Own helicopter") === ["AUF", "ZURUECK"].includes(state), "only airborne helicopter states plot, even if stale coordinates exist: " + state);
}
fixture.ownship.helo = structuredClone(liveFixture.ownship.helo);
$test("contact-filter").value = "radar";
$test("contact-filter").dispatchEvent(new Event("input", {bubbles: true}));
assert($test("track-list").querySelectorAll("button").length === 2 &&
$test("track-count").textContent.includes("2") && $test("track-count").textContent.includes("3"),
"contact register filters by source and reports filtered/total count");
$test("contact-filter").value = "no-such-contact";
$test("contact-filter").dispatchEvent(new Event("input", {bubbles: true}));
assert(!$test("track-list").querySelector("button") && $test("track-list").textContent.includes("No contacts"),
"contact register has a localized empty filter state");
$test("contact-filter").value = "";
$test("contact-filter").dispatchEvent(new Event("input", {bubbles: true}));
const first = $test("track-list").querySelector("button");
first.focus(); first.click();
assert($test("detail-label").textContent === fixture.tracks[0].label, "local selection details");
const sonarLabel = `${fixture.tracks[1].label} PING`;
const sonarFix = drawnSonarFixes.get(sonarLabel);
assert(sonarFix, "nested sonar fix is rendered under its opaque parent");
fixture.tracks[1].fixes = [];
await until(() => !canvasFrame.texts.includes(sonarLabel), "expired nested fix leaves draw geometry");
const staleX = bounds.left + sonarFix.x - 9, staleY = bounds.top + sonarFix.y + 8;
const oldSetCapture = $test("chart").setPointerCapture, oldReleaseCapture = $test("chart").releasePointerCapture;
$test("chart").setPointerCapture = () => {};
$test("chart").releasePointerCapture = () => {};
for (const type of ["pointerdown", "pointerup"]) $test("chart").dispatchEvent(new PointerEvent(type, {
bubbles: true, pointerId: 77, isPrimary: true, button: 0, clientX: staleX, clientY: staleY,
}));
$test("chart").setPointerCapture = oldSetCapture;
$test("chart").releasePointerCapture = oldReleaseCapture;
assert($test("detail-label").textContent === fixture.tracks[0].label,
"removed sonar fix is no longer clickable");
first.focus();
if (innerWidth > 1250) {
const supportGrid = document.querySelector(".support-grid");
const support = supportGrid.getBoundingClientRect();
const panel = document.querySelector(".chart-panel").getBoundingClientRect();
const plot = $test("chart").getBoundingClientRect();
assert(panel.bottom <= support.top, "support panels follow the workspace without overlap");
const operationsPanel = $test("panel-operations");
assert(support.bottom <= operationsPanel.scrollHeight + operationsPanel.getBoundingClientRect().top + 1, "support panels remain in the Operations scroller");
assert(plot.height > 200 && plot.bottom <= panel.bottom, "bounded desktop chart retains a usable, contained plot");
const details = document.querySelector(".details-panel");
assert(getComputedStyle(details).overflowY === "auto", "long contact assessments scroll inside their panel");
$test("propose-navigation").scrollIntoView({block: "nearest"});
const action = $test("propose-navigation").getBoundingClientRect();
const detailBounds = details.getBoundingClientRect();
assert(action.top >= detailBounds.top && action.bottom <= detailBounds.bottom, "navigation action is reachable inside contact details");
supportGrid.scrollIntoView({block: "start"});
const reached = supportGrid.getBoundingClientRect();
const operationBounds = operationsPanel.getBoundingClientRect();
assert(reached.top >= operationBounds.top - 1 && reached.top < operationBounds.bottom, "Operations scrolling reaches support panels");
operationsPanel.scrollTop = 0;
details.scrollTop = 0;
}
$test("affiliation").value = "HOSTILE";
$test("affiliation").dispatchEvent(new Event("change", {bubbles: true}));
await sleep(650);
assert(commands.length === 0 && fixture.crew_target === "fix-2", "selection and drafts never send commands");
assert(document.activeElement === first, "poll preserves keyboard focus");
assert($test("affiliation").value === "HOSTILE", "poll preserves operator draft");
$test("navigation-course").value = "87.5";
const commandsBeforeTabs = commands.length;
const operationScale = $test("chart-scale").textContent;
const tab = (name) => $test(`tab-${name}`);
tab("lookout").click();
await new Promise((resolve) => requestAnimationFrame(resolve));
assert(tab("lookout").getAttribute("aria-selected") === "true" && !$test("panel-lookout").hidden && $test("panel-operations").hidden, "click activates the Lookout tab");
const lookoutBounds = $test("lookout").getBoundingClientRect();
const requestedDpr = Math.min(devicePixelRatio || 1, 3);
const lookoutDpr = Math.min(requestedDpr, Math.sqrt(8000000 /
Math.max(1, $test("lookout").clientWidth * $test("lookout").clientHeight)));
assert(lookoutBounds.width > 100 && lookoutBounds.height > 50 &&
$test("lookout").width === Math.round($test("lookout").clientWidth * lookoutDpr) &&
$test("lookout").height === Math.round($test("lookout").clientHeight * lookoutDpr), "Lookout canvas has exact responsive backing dimensions");
assert($test("lookout-sea").textContent === "3" && $test("lookout-light").textContent === "Day" &&
$test("lookout-scope").dataset.light === "day", "snapshot environment drives Lookout status and palette");
assert(lookoutFrame.arcs.filter((arc) => Math.abs(arc.radius - 4) < .01).length === 1 &&
lookoutFrame.texts.includes(fixture.tracks[1].label) && !lookoutFrame.texts.includes(fixture.tracks[0].label),
"positioned observations are points while bearing-only reports have no invented point or label position");
const center = {x: lookoutBounds.width / 2, y: lookoutBounds.height / 2};
const rings = lookoutFrame.arcs.filter((arc) => Math.abs((arc.end - arc.start) - Math.PI * 2) < .001 &&
Math.abs(arc.x - center.x) < 1 && Math.abs(arc.y - center.y) < 1);
const lookoutRadius = Math.max(...rings.map((ring) => ring.radius));
const point = lookoutFrame.arcs.find((arc) => Math.abs(arc.radius - 4) < .01);
const expectedScale = lookoutRadius / 100;
assert(Math.abs(point.x - (center.x + 20 * expectedScale)) < 1 &&
Math.abs(point.y - (center.y - 50 * expectedScale)) < 1, "positioned Lookout point uses equal north-up snapshot scale");
const indicator = lookoutFrame.translations.find((entry) => Math.hypot(entry.x - center.x, entry.y - center.y) > 20);
assert(indicator && Math.abs(indicator.x - (center.x + Math.sin(35 * Math.PI / 180) * lookoutRadius)) < 1 &&
Math.abs(indicator.y - (center.y - Math.cos(35 * Math.PI / 180) * lookoutRadius)) < 1,
"bearing-only report is marked at the correct nautical display edge");
assert(lookoutFrame.rotations.some((angle) => Math.abs(angle - 15 * Math.PI / 180) < .001),
"own course vector uses the published nautical heading");
assert($test("lookout-observations").textContent.includes(fixture.tracks[0].label) &&
$test("lookout-observations").textContent.includes("bearing-only observation") &&
$test("lookout-own").textContent.includes("15"), "Lookout canvas has a current accessible text equivalent");
fixture.environment.is_night = true;
await until(() => $test("lookout-scope").dataset.light === "night" && $test("lookout-light").textContent === "Night", "night snapshot updates Lookout presentation");
fixture.environment.is_night = false;
await until(() => $test("lookout-scope").dataset.light === "day", "day snapshot restores Lookout presentation");
const lookoutRange = $test("lookout-range").textContent;
$test("lookout-zoom-in").click();
await new Promise((resolve) => requestAnimationFrame(resolve));
assert($test("lookout-range").textContent !== lookoutRange && $test("chart-scale").textContent === operationScale,
"Lookout range is independent from Operations chart scale");
$test("lookout-reset").click();
$test("lookout").dispatchEvent(new WheelEvent("wheel", {deltaY: 1, bubbles: true, cancelable: true}));
$test("lookout").dispatchEvent(new KeyboardEvent("keydown", {key: "Home", bubbles: true, cancelable: true}));
await new Promise((resolve) => requestAnimationFrame(resolve));
assert($test("lookout-range").textContent === lookoutRange && commands.length === commandsBeforeTabs,
"Lookout controls remain browser-local and Home restores its default range");
$test("lookout").style.width = "300px";
$test("lookout").style.height = "180px";
const resizedDpr = Math.min(requestedDpr, Math.sqrt(8000000 / (300 * 180)));
await until(() => $test("lookout").clientWidth === 300 && $test("lookout").clientHeight === 180 &&
$test("lookout").width === Math.round(300 * resizedDpr) && $test("lookout").height === Math.round(180 * resizedDpr),
"active Lookout resize updates both backing dimensions");
await new Promise((resolve) => requestAnimationFrame(resolve));
const resizedRings = lookoutFrame.arcs.filter((arc) => Math.abs((arc.end - arc.start) - Math.PI * 2) < .001 &&
Math.abs(arc.x - 150) < 1 && Math.abs(arc.y - 90) < 1);
assert(resizedRings.length === 4 && Math.max(...resizedRings.map((ring) => ring.radius)) < 90,
"Lookout geometry recenters after active resize");
$test("lookout").style.removeProperty("width");
$test("lookout").style.removeProperty("height");
await until(() => $test("lookout").clientWidth > 300 && $test("lookout").height ===
Math.round($test("lookout").clientHeight * Math.min(requestedDpr, Math.sqrt(8000000 /
Math.max(1, $test("lookout").clientWidth * $test("lookout").clientHeight)))),
"Lookout restores responsive backing dimensions after resize");
await new Promise((resolve) => requestAnimationFrame(resolve));
const restoredRings = lookoutFrame.arcs.filter((arc) => Math.abs((arc.end - arc.start) - Math.PI * 2) < .001 &&
Math.abs(arc.x - $test("lookout").clientWidth / 2) < 1 && Math.abs(arc.y - $test("lookout").clientHeight / 2) < 1);
assert(Math.abs(Math.max(...restoredRings.map((ring) => ring.radius)) - lookoutRadius) < 1,
"Lookout geometry restores after responsive resize");
assert($test("chart").width === 1 && $test("chart").height === 1, "inactive Operations canvas releases its backing store");
tab("lookout").dispatchEvent(new KeyboardEvent("keydown", {key: "End", bubbles: true}));
assert(document.activeElement === tab("contacts") && tab("contacts").getAttribute("aria-selected") === "true", "End activates the final tab");
assert($test("lookout").width === 1 && $test("lookout").height === 1, "inactive Lookout canvas releases its backing store");
await until(() => $test("analysis-list").querySelector("button"), "static contact analysis loads once");
const operationSelection = $test("detail-label").textContent;
const commandsBeforeAnalysis = commands.length;
$test("analysis-list").querySelector("button").click();
assert($test("analysis-name").textContent === "Reference Unit <inert>" &&
$test("detail-label").textContent === operationSelection && first.getAttribute("aria-pressed") === "true",
"analyzer selection is independent and uses inert text");
assert($test("analysis-sensors").textContent.includes("active") &&
$test("analysis-sensors").textContent.includes("2.0 s") &&
$test("analysis-sensors").textContent.includes("Sensor 1") &&
$test("analysis-emitters").textContent.includes("8,000,000,000") &&
$test("analysis-emitters").textContent.includes("1,200") &&
$test("analysis-emitters").textContent.includes("Emitter 1"),
"reference catalog renders sensor modes, cadence, uncertainty, radar frequency and PRF");
$test("analysis-filter").value = "no-match";
$test("analysis-filter").dispatchEvent(new Event("input"));
assert(!$test("analysis-list").querySelector("button") && commands.length === commandsBeforeAnalysis,
"analyzer filtering is bounded and never calls a command path");
$test("analysis-filter").value = "";
$test("analysis-filter").dispatchEvent(new Event("input"));
tab("contacts").dispatchEvent(new KeyboardEvent("keydown", {key: "ArrowRight", bubbles: true}));
assert(document.activeElement === tab("operations") && tab("operations").getAttribute("aria-selected") === "true", "Right arrow wraps to Operations");
tab("operations").dispatchEvent(new KeyboardEvent("keydown", {key: "ArrowLeft", bubbles: true}));
assert(document.activeElement === tab("contacts"), "Left arrow wraps to Contacts");
tab("contacts").dispatchEvent(new KeyboardEvent("keydown", {key: "Home", bubbles: true}));
await new Promise((resolve) => requestAnimationFrame(resolve));
assert(document.activeElement === tab("operations") && !$test("panel-operations").hidden, "Home restores Operations");
assert(first.getAttribute("aria-pressed") === "true" && $test("detail-label").textContent === fixture.tracks[0].label && $test("affiliation").value === "HOSTILE" && $test("navigation-course").value === "87.5", "tab round trip preserves selection and drafts");
assert(commands.length === commandsBeforeTabs && $test("chart").width > 0 && $test("chart").height > 0, "tab switching sends no command and redraws Operations");
const shellBeforeEpoch = $test("shell"), chartsBeforeVisualEpoch = charts;
fixture.epoch += 1;
await until(() => charts > chartsBeforeVisualEpoch && $test("detail-label").textContent === fixture.tracks[0].label &&
$test("affiliation").value === "UNKNOWN" && $test("navigation-course").value === "",
"same-session epoch resynchronizes without losing selection");
assert($test("shell") === shellBeforeEpoch && first.getAttribute("aria-pressed") === "true",
"uConsole context change does not reload or replace the browser view");
assert($test("affiliation").value === "UNKNOWN" && $test("navigation-course").value === "",
"uConsole context change invalidates command drafts");
$test("affiliation").value = "HOSTILE";
$test("navigation-course").value = "87.5";
tab("guide").click();
await sleep(650);
assert(tab("guide").getAttribute("aria-selected") === "true", "poll preserves active tab");
const guidePanel = $test("panel-guide");
guidePanel.scrollTop = 0;
const commandsBeforeGuide = commands.length;
$test("guide-nav").querySelector('a[href="#guide-authority"]').click();
assert(guidePanel.scrollTop > 0 && document.activeElement === $test("guide-authority") &&
window.scrollY === 0 && location.hash === "", "guide navigation scrolls and focuses only inside the Guide panel");
assert(commands.length === commandsBeforeGuide, "guide navigation has no command coupling");
$test("language").value = "de";
$test("language").dispatchEvent(new Event("change"));
await until(() => document.documentElement.lang === "de", "tab language switch");
assert(tab("guide").getAttribute("aria-selected") === "true" &&
$test("guide-authority").textContent.includes("Einsatzregeln"),
"language switch preserves and retranslates the active guide");
$test("language").value = "en";
$test("language").dispatchEvent(new Event("change"));
await until(() => document.documentElement.lang === "en", "restore language after tab check");
tab("operations").click();
$test("apply-affiliation").click();
await until(() => commands.length === 1, "explicit affiliation submit");
await sleep(650);
assert($test("command-status").dataset.status === "pending" && $test("propose").disabled, "202 is pending, one action at a time");
assert(commands[0].action === "affiliate" && commands[0].value === "HOSTILE" && commands[0].track === "bearing-1", "exact annotation envelope");
assert(commands[0].session === fixture.session && commands[0].epoch === fixture.epoch && Number.isInteger(commands[0].revision), "optimistic context envelope");
assert(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(commands[0].id), "secure UUID");
fixture.results = [{id: commands[0].id, status: "applied", reasoncode: "ok"}];
await until(() => $test("command-status").dataset.status === "applied", "authoritative result settles pending");
// Exercise the secure random fallback used on a plain HTTP LAN origin.
Object.defineProperty(crypto, "randomUUID", {value: undefined, configurable: true});
$test("classification").value = "";
$test("apply-classification").click();
await until(() => commands.length === 2, "explicit clear classification");
assert(commands[1].action === "classify" && commands[1].value === null, "null classification is explicit");
assert(commands[1].id !== commands[0].id && commands[1].id.length === 36, "secure fallback UUID");
fixture.results.push({id: commands[1].id, status: "rejected", reasoncode: "revision_conflict"});
await until(() => $test("command-status").dataset.status === "rejected", "rejection result");
assert($test("command-status").textContent.includes("The assessment has changed."), "known reason code is meaningful English");
$test("language").value = "de";
$test("language").dispatchEvent(new Event("change"));
await until(() => document.documentElement.lang === "de", "rejection language switch");
assert($test("command-status").textContent.includes("Die Bewertung hat sich geändert."), "stored reason code retranslates into German");
$test("language").value = "en";
$test("language").dispatchEvent(new Event("change"));
await until(() => document.documentElement.lang === "en", "restore English after rejection check");
$test("propose").click();
await until(() => commands.length === 3, "proposal sent");
assert(commands[2].action === "propose" && !("value" in commands[2]), "proposal has no invented value");
assert(fixture.crew_target === "fix-2", "proposal never assigns crew target");
await until(() => !$test("retry-command").disabled, "acknowledged command with no result becomes manually reconcilable");
assert(commands.length === 3 && $test("propose").disabled && $test("command-status").dataset.status === "pending", "missing result never triggers resend or automatic unlock");
fixture.proposal = {ref: "bearing-1", label: "Sierra 01", status: "pending"};
fixture.results.push({id: commands[2].id, status: "applied", reasoncode: "ok"});
await until(() => !$test("clear-proposal").disabled, "proposal can be cleared after result");
$test("clear-proposal").click();
await until(() => commands.length === 4, "clear proposal sent");
assert(commands[3].action === "clear_proposal" && !("track" in commands[3]) && !("value" in commands[3]), "clear omits track and value");
fixture.results.push({id: commands[3].id, status: "rejected", reasoncode: "queue_expired"});
await until(() => !$test("propose").disabled, "authoritative expiry result releases pending action");
assert($test("command-status").dataset.status === "rejected" && $test("command-status").textContent.includes("queue_expired"), "unknown expiry reason remains safe technical text");
$test("sound").click();
await until(() => $test("sound").getAttribute("aria-pressed") === "true", "sound enabled by gesture");
assert(tones === 1, "enabling sound starts only the machinery ambience");
fixture.events.push({seq: 2, kind: "sensor", severity: "warning", message: "New alarm"});
await until(() => tones === 2, "new alert gets one tone");
await sleep(650);
assert(tones === 2, "event sequences deduplicated");
offline = true;
await until(() => $test("connection").dataset.state === "stale", "disconnect is stale");
assert($test("propose").disabled, "stale actions disabled");
await new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(resolve)));
const frozen = $test("chart").toDataURL();
const frozenSize = [$test("chart").width, $test("chart").height];
await sleep(1100);
assert($test("chart").toDataURL() === frozen, "no extrapolated motion while disconnected: " + frozenSize + " -> " + [$test("chart").width, $test("chart").height]);
fixture.events.push({seq: 3, kind: "sensor", severity: "critical", message: "Reconnect backlog"});
offline = false;
window.dispatchEvent(new Event("online"));
await until(() => $test("connection").dataset.state === "connected", "reconnect");
assert(tones === 2, "no reconnect alert backlog");
loseAck = true;
$test("propose").click();
await until(() => commands.length === 5 && $test("command-status").textContent.includes("uncertain"), "lost ack remains uncertain");
loseAck = false;
await sleep(1000);
assert(commands.length === 5 && $test("propose").disabled, "uncertain command never auto retries or unlocks");
await until(() => !$test("retry-command").disabled, "manual reconciliation available after cooldown");
assert($test("command-retry-note").textContent.includes("first time") && $test("command-retry-note").textContent.includes("5 seconds"), "explicit first-application warning and retry rate");
const originalEnvelope = issued.filter((entry) => entry.url === "/api/v2/commands").at(-1).options.body;
$test("track-list").querySelectorAll("button")[1].click();
$test("affiliation").value = "NEUTRAL";
reconcileActor = true;
$test("retry-command").click();
for (let i = 0; i < 10; i++) $test("retry-command").dispatchEvent(new Event("click"));
await until(() => commands.length === 6, "explicit retry reaches actor for the first time");
assert(JSON.stringify(commands[5]) === originalEnvelope && actorEffects === 1, "retry preserves original ID/envelope despite changed selection and may first apply");
assert(fixture.proposal.ref === "bearing-1" && fixture.crew_target === "fix-2", "actor changes only the original proposal, not crew target or new selection");
await sleep(1000);
assert(commands.length === 6 && $test("retry-command").disabled && $test("apply-affiliation").disabled, "in-flight and cooldown clicks cannot duplicate a request or unlock other actions");
await until(() => !$test("retry-command").disabled, "next manual attempt after cooldown");
rejectRetry = true;
$test("retry-command").click();
await until(() => commands.length === 7, "retry can be refused by HTTP transport");
await sleep(100);
assert($test("command-status").dataset.status === "pending" && $test("apply-affiliation").disabled && actorEffects === 1, "HTTP rejection of retry never erases an unresolved original action");
rejectRetry = false;
await until(() => !$test("retry-command").disabled, "reconcile again only after another cooldown");
publishActorResults = true;
$test("retry-command").click();
await until(() => $test("command-status").dataset.status === "applied", "identical replay recovers authoritative result");
assert(commands.length === 8 && actorEffects === 1 && actorResults.size === 1, "actor deduplicates repeat ID before current revision check");
assert(commands.slice(4).every((command) => JSON.stringify(command) === originalEnvelope), "every manual attempt preserves all envelope fields and the original ID");
assert($test("command-reconcile").hidden && !$test("apply-affiliation").disabled, "only authoritative result clears pending and retry UI");
assert(localStorage.length === 0 && sessionStorage.length === 0 && !document.body.textContent.includes(commands[4].id), "cached reconciliation envelope stays in JS memory only");
first.click();
reconcileActor = false;
stalledSeq = true;
await until(() => $test("connection").dataset.state === "stale", "unchanging producer sequence is stale even with HTTP 200");
assert($test("propose").disabled, "stalled producer locks actions");
stalledSeq = false;
await until(() => $test("connection").dataset.state === "connected", "producer recovery");
holdState = true;
const callsBeforeTimeout = stateCalls;
await until(() => stateCalls > callsBeforeTimeout && lastSignal?.aborted, "stalled fetch aborted");
assert($test("connection").dataset.state === "stale", "deadline marks stale");
holdState = false;
window.dispatchEvent(new Event("online"));
await until(() => $test("connection").dataset.state === "connected", "timeout recovery");
fixture.session = "session-B"; fixture.chart_revision = "chart-B";
fixture.mission.name = "Second session"; mismatch = true;
await until(() => $test("operations").hidden, "old session hidden before new chart");
await sleep(1200);
assert($test("operations").hidden, "mismatched chart cannot reveal data");
mismatch = false;
window.dispatchEvent(new Event("online"));
await until(() => !$test("operations").hidden && $test("mission-name").textContent === "Second session", "new matching session committed");
assert($test("track-detail").hidden, "selection resets across session");
$test("language").value = "de";
$test("language").dispatchEvent(new Event("change"));
await until(() => document.documentElement.lang === "de", "German catalog switch");
assert($test("pair-submit").textContent === "Station verbinden", "German labels from root catalog");
assert(document.documentElement.scrollWidth <= innerWidth + 1, "German layout does not overflow");
assert($test("damage-list").textContent.includes("Zugewiesene Teams (Anzahl)"), "team count label is localized in German");
fixture.commands_allowed = false;
await sleep(600);
$test("track-list").querySelector("button").click();
assert($test("propose").disabled && $test("apply-affiliation").disabled, "read only phase blocks commands");
$test("follow").click();
assert($test("follow").getAttribute("aria-pressed") === "true", "live ownship can be followed before redaction");
const chartsBeforeBlocked = charts;
const blockedSession = fixture.session, blockedRevision = fixture.chart_revision;
Object.assign(fixture, structuredClone(statusFixture), {
phase: "blocked", session: fixture.session, chart_revision: fixture.chart_revision,
epoch: fixture.epoch + 1, seq: fixture.seq,
});
await until(() => !$test("operations").hidden && $test("mission-name").textContent === "" && $test("connection").dataset.state === "connected", "live to blocked status-only snapshot remains healthy");
await sleep(50);
assert(charts === chartsBeforeBlocked + 1 && fixture.session === blockedSession && fixture.chart_revision === blockedRevision, "blocked epoch replaces live chart at the same revision");
assert($test("chart-disclaimer").textContent === "" && !$test("track-list").querySelector("button") && $test("track-detail").hidden, "blocked snapshot clears live chart and contact display");
assert($test("follow").disabled && $test("follow").getAttribute("aria-pressed") === "false", "redaction cancels active follow");
$test("follow").dispatchEvent(new Event("click"));
$test("zoom-in").click();
await sleep(50);
assert(canvasFrame.texts.length > 0 && canvasFrame.translations.length === 0 && canvasFrame.fills === 0, "blocked geometry contains no previous or imaginary ownship");
assert(!canvasFrame.texts.includes("Eigenes Schiff") && !canvasFrame.texts.includes("Eigener Hubschrauber"), "blocked geometry has no asset labels in German");
tab("lookout").click();
await new Promise((resolve) => requestAnimationFrame(resolve));
assert($test("lookout-sea").textContent === "--" && $test("lookout-light").textContent === "--" &&
lookoutFrame.arcs.length === 0 && lookoutFrame.translations.length === 0,
"status-only redaction clears prior Lookout environment and geometry");
assert($test("propose").disabled && $test("apply-classification").disabled && $test("apply-affiliation").disabled && $test("clear-proposal").disabled, "blocked status-only commands stay disabled");
await sleep(650);
assert($test("connection").dataset.state === "connected" && commands.length === 8, "blocked polling is healthy and sends no commands");
assert(maxActive === 1, "at most one outstanding request");
assert(!failures.length, "browser errors: " + failures.join("; "));
Object.assign(fixture, structuredClone(liveFixture), {
session: fixture.session, chart_revision: fixture.chart_revision,
epoch: fixture.epoch + 1, seq: fixture.seq,
});
await until(() => $test("lookout-sea").textContent === "3" && $test("lookout-observations").children.length === 3,
"live Lookout data returns before direct disconnect");
location.hash = "#simlog";
await until(() => !$test("simlog-view").hidden && !$test("simlog-current-map").disabled,
"hidden simlog view loads its current state");
const simlogTables = [...$test("simlog-current").querySelectorAll(".simlog-table-wrap")];
const surfaceBounds = simlogTables[1].getBoundingClientRect();
const biologicalBounds = simlogTables[2].getBoundingClientRect();
assert(surfaceBounds.bottom <= biologicalBounds.top + 1 || biologicalBounds.bottom <= surfaceBounds.top + 1,
"wide surface table does not overlap the biological section");
$test("simlog-current-map").click();
await until(() => $test("simlog-map-dialog").open && $test("simlog-map").width > 1,
"current snapshot opens the responsive map dialog");
assert($test("simlog-map-units").children.length === 15,
"map lists own ship and every finite-position snapshot category");
assert($test("simlog-map-units").firstElementChild.textContent.includes("15") &&
$test("simlog-map-units").firstElementChild.textContent.includes("12"),
"map unit listing includes available course and speed");
const mapLabels = simlogMapTexts.filter((entry) => entry.text !== "N");
assert(mapLabels.every((entry, index) => mapLabels.slice(index + 1).every((other) =>
entry.right < other.x || entry.x > other.right || entry.bottom < other.y || entry.y > other.bottom)),
"map labels never overlap each other");
const mapBounds = $test("simlog-map-dialog").getBoundingClientRect();
assert(mapBounds.left >= -1 && mapBounds.top >= -1 && mapBounds.right <= innerWidth + 1 && mapBounds.bottom <= innerHeight + 1,
"simlog map dialog remains inside the viewport");
$test("simlog-map-units-fit").click();
assert($test("simlog-map-units-fit").getAttribute("aria-pressed") === "true" &&
$test("simlog-map-world").getAttribute("aria-pressed") === "false",
"fit-to-units toggle is reflected on both toolbar buttons");
$test("simlog-map-close").click();
assert(!$test("simlog-map-dialog").open, "close button actually closes the map dialog");
location.hash = "";
await until(() => !$test("operations").hidden, "operations return without a page reload");
$test("disconnect").click();
assert($test("operations").hidden && !$test("pairing").hidden, "disconnect hides private data");
assert($test("track-list").children.length === 0 && !$test("lookout-sea").textContent &&
!$test("lookout-light").textContent && !$test("lookout-observations").textContent &&
$test("lookout-scope").dataset.light === "unknown", "disconnect clears private DOM");
document.documentElement.dataset.contract = "passed";
if (parent !== window) parent.postMessage({contract: "passed"}, location.origin);
}
window.addEventListener("DOMContentLoaded", () => runContract().catch((error) => {
document.documentElement.dataset.contract = "failed";
document.documentElement.dataset.failure = String(error.stack || error);
if (parent !== window) parent.postMessage({contract: "failed", failure: String(error.stack || error)}, location.origin);
}));
"""
def browser_state():
tracks = []
for ref, domain, x, y in [("bearing-1", "SUBSURFACE", None, None), ("fix-2", "SURFACE", 270, 200), ("air-3", "AIR", 320, 170)]:
tracks.append(dict(ref=ref, label=f"{ref} <b>observed</b>", domain=domain,
source="PASSIVE" if x is None else "RADAR", affiliation="UNKNOWN",
classification=None, bearing=35, range_nm=None if x is None else 50,
x=x, y=y, depth_m=None, course=None, speed_kn=None,
quality=.65, age_s=2, fix_age_s=None if x is None else 2,
bearing_uncertainty_deg=5, range_uncertainty_nm=None,
fixes=[],
can_classify=domain == "SUBSURFACE", can_propose=domain == "SUBSURFACE"))
tracks[1]["fixes"] = [dict(source="PING", x=290, y=220,
measured_at=80, fixed_at=82, measurement_age_s=10, fix_age_s=8,
uncertainty_nm=1.5, depth_m=70, depth_uncertainty_m=2, quality=.9)]
return dict(version=APP_VERSION, session="session-A", epoch=1, revision=12,
seq=1, phase="live", commands_allowed=True, language="en",
clock=dict(sim=90, mission=90, world=12.5),
environment=dict(sea_state=3, is_night=False),
mission=dict(name="Northern watch <img src=x onerror=alert(1)>", objective="Maintain the observation picture", remaining_s=900),
ownship=dict(x=250, y=250, course=15, speed=12, target_course=20, target_speed=15,
damage=[dict(key="bridge", name="Bridge", state="OK", flood=0, fire=0, teams=[1, 3]),
dict(key="engine", name="Engine", state="OK", flood=0, fire=0, teams=[])],
inventory=dict(torpedoes=8, vls=16, ciws=800, chaff_ready=True),
helo=dict(state="HANGAR", x=None, y=None, course=None, fuel_s=1800, torpedoes=2, buoys=6)),
tracks=tracks, crew_target="fix-2", proposal=None,
events=[dict(seq=1, kind="mission", severity="warning", message="Initial backlog <b>inert</b>")],
results=[], chart_revision="chart-A")
def browser_status_state():
state = browser_state()
state.update(phase="menu", commands_allowed=False,
clock={key: None for key in state["clock"]},
environment=dict(sea_state=None, is_night=None),
mission=dict(name="", objective="", remaining_s=None),
ownship=dict(x=None, y=None, course=None, speed=None,
target_course=None, target_speed=None, damage=[],
inventory={key: None for key in state["ownship"]["inventory"]},
helo={key: None for key in state["ownship"]["helo"]}),
tracks=[], crew_target=None, proposal=None, events=[], results=[])
return state
def browser_contact_analysis():
return dict(version=1, profiles=[dict(
key="reference_unit", name="Reference Unit <inert>", resource="subs.json",
reference=dict(hull_type="single", length_m=80),
machine=dict(cruise_speed_kn=8, maximum_speed_kn=18,
quiet_speed_kn=5, propulsion_codes=["DE"], motor_rpm=None,
shaft_rpm=None, propulsor_type="propeller", blade_count=None,
cruise_lines=[], high_speed_lines=[], cruise_broadband=None,
high_speed_broadband=None),
components=dict(
sensors=[dict(domain="radar", modes=["active", "track"], emits=True,
synthetic_range_nm=80, sensitivity_db=-80, cadence_s=2,
bearing_uncertainty_deg=1, range_uncertainty_nm=.2,
depth_uncertainty_m=None)],
emitters=[dict(domain="radar", frequency_band_hz=[8e9, 12e9],
prf_band_hz=[800, 1200], modulation_codes=["pulse"])],
weapons=[], launchers=[], magazines=[], countermeasures=[]),
assets={},
)])
def _superseded_commander_browser_contract(tmp_path, width, height):
chromium = shutil.which("chromium") or shutil.which("chromium-browser") or shutil.which("google-chrome")
if not chromium:
pytest.skip("Optional browser contract: no installed Chromium")
html = inject_probe(index_html(), "contract.js")
script = (BROWSER_CONTRACT.replace("__STATE__", json.dumps(browser_state()))
.replace("__STATUS_STATE__", json.dumps(browser_status_state()))
.replace("__WIDTH__", str(width)))
requests = []
problems = []
en, de = catalogs()
class Handler(BaseHTTPRequestHandler):
def log_message(self, *_args):
pass
def reply(self, status, content, mime):
content = content if isinstance(content, bytes) else content.encode()
self.send_response(status)
self.send_header("Content-Type", mime)
self.send_header("Content-Length", str(len(content)))
self.end_headers()
self.wfile.write(content)
def do_GET(self):
if self.path == "/viewport":
# Desktop Chromium has a 500px minimum window. A same-origin
# fixture frame gives the app a real 390px CSS viewport instead.
self.reply(200, f'<!doctype html><html><head><script src="/viewport.js" defer></script></head>'
f'<body><iframe src="/" width="{width}" height="{height}" frameborder="0"></iframe></body></html>', "text/html")
elif self.path == "/viewport.js":
self.reply(200, 'window.addEventListener("message", (event) => {'
'if (event.origin === location.origin && event.data?.contract) {'
'document.documentElement.dataset.contract = event.data.contract;'
'document.documentElement.dataset.failure = event.data.failure || "";'
'}});', "text/javascript")
elif self.path in ("/", "/index.html"):
self.reply(200, html, "text/html")
elif self.path == "/contract.js":
self.reply(200, script, "text/javascript")
elif self.path in WEB_ROUTES:
self.reply(200, WEB_ROUTES[self.path][1], WEB_ROUTES[self.path][0])
elif self.path in ("/api/v2/ui?lang=en", "/api/v2/ui?lang=de"):
source = de if self.path.endswith("de") else en
self.reply(200, json.dumps({key: value for key, value in source.items() if key.startswith(PREFIX)}), "application/json")
elif self.path == "/api/v2/contacts":
self.reply(200, json.dumps(browser_contact_analysis()), "application/json")
else:
self.reply(404, "", "text/plain")
def do_POST(self):
body = json.loads(self.rfile.read(int(self.headers["Content-Length"])))
requests.append((self.path, body))
if self.headers.get("Origin") != f"http://127.0.0.1:{self.server.server_port}":
problems.append("Browser did not supply same-origin Origin on POST")
if self.path == "/api/v2/pair":
if body != {"code": "123ABC"}:
problems.append("Incorrect pairing envelope")
if len([path for path, _ in requests if path == "/api/v2/pair"]) == 1:
self.reply(429, json.dumps({"error": "rate_limited"}), "application/json")
else:
self.reply(200, json.dumps({"token": "test-secret"}), "application/json")
elif self.path == "/api/v2/commands":
self.reply(202, "", "application/json")
else:
self.reply(404, "", "text/plain")
server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
try:
result = subprocess.run(
[chromium, "--headless", "--no-sandbox", "--disable-gpu", "--disable-background-networking",
"--no-first-run", "--no-default-browser-check", "--disable-dev-shm-usage",
f"--user-data-dir={tmp_path / 'browser'}", f"--window-size={width},{height}",
"--force-device-scale-factor=1.5", "--virtual-time-budget=90000", "--dump-dom",
f"http://127.0.0.1:{server.server_port}/{'viewport' if width < 500 else ''}"],
capture_output=True, text=True, timeout=60,
)
finally:
server.shutdown()
server.server_close()
thread.join(timeout=5)
assert result.returncode == 0, result.stderr
document = Document(result.stdout)
root = next((attrs for tag, attrs in document.elements if tag == "html"), {})
assert root.get("data-contract") == "passed", root.get("data-failure", result.stdout[-5000:] + result.stderr[-2000:])
assert not problems
assert len([path for path, _ in requests if path == "/api/v2/pair"]) == 2
assert len([path for path, _ in requests if path == "/api/v2/commands"]) == 6
def test_contact_analyzer_rejects_schema_error_when_chromium_available(tmp_path):
chromium = shutil.which("chromium") or shutil.which("chromium-browser") or shutil.which("google-chrome")
if not chromium:
pytest.skip("Optional browser contract: no installed Chromium")
html = inject_probe(index_html(), "schema-test.js")
en = catalogs()[0]
requests = []
malformed_analysis = project_contact_catalog()
malformed_analysis["profiles"][0]["reference"] = None
probe = r"""
window.addEventListener("DOMContentLoaded", async () => {
for (let i = 0; !document.getElementById("analysis-status").textContent.includes("unavailable or invalid") && i < 300; i++)
await new Promise((resolve) => setTimeout(resolve, 20));
const status = document.getElementById("analysis-status");
document.documentElement.dataset.schema = status.textContent.includes("unavailable or invalid") &&
!document.getElementById("analysis-list").children.length ? "rejected" : "failed";
});
"""
class Handler(BaseHTTPRequestHandler):
def log_message(self, *_args):
pass
def reply(self, content, mime="application/json"):
body = content if isinstance(content, bytes) else content.encode()
self.send_response(200)
self.send_header("Content-Type", mime)
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def do_GET(self):
requests.append(self.path)
if self.path == "/":
self.reply(html, "text/html")
elif self.path == "/schema-test.js":
self.reply(probe, "text/javascript")
elif self.path in WEB_ROUTES:
self.reply(WEB_ROUTES[self.path][1], WEB_ROUTES[self.path][0])
elif self.path == "/api/v2/ui?lang=en":
self.reply(json.dumps({key: value for key, value in en.items()
if key.startswith(PREFIX)}))
elif self.path == "/api/v2/contacts":
self.reply(json.dumps(malformed_analysis))
else:
self.send_error(404)
server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
try:
result = subprocess.run(
[chromium, "--headless", "--no-sandbox", "--disable-gpu",
"--disable-background-networking", "--no-first-run",
"--no-default-browser-check", "--disable-dev-shm-usage",
f"--user-data-dir={tmp_path / 'browser'}", "--virtual-time-budget=10000",
"--dump-dom", f"http://127.0.0.1:{server.server_port}/"],
capture_output=True, text=True, timeout=30,
)
finally:
server.shutdown()
server.server_close()
thread.join(timeout=5)
assert result.returncode == 0, result.stderr
root = next(attrs for tag, attrs in Document(result.stdout).elements if tag == "html")
assert root.get("data-schema") == "rejected"
assert requests.count("/api/v2/contacts") == 1
assert not any(path == "/api/v2/commands" for path in requests)