"""Real-browser contract for the solo console: station tabs and host controls.
Drives the packaged page in Chromium against the real Game, CommanderBridge and
a solo CommanderServer. Saves go to a temporary directory only.
"""
import json
import shutil
import subprocess
import time
import pytest
from src.commander import server as commander_transport
from src.core import config
from src.core.game import Game
from src.sonar.sonar import Contact
from src.core import manual
from test_commander_assets import ASSETS, PREFIX, Document, catalogs
CONSOLE_SCRIPT = r"""
(() => {
"use strict";
const $ = (id) => document.getElementById(id);
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
class Fail extends Error {}
const assert = (value, message) => { if (!value) throw new Fail(message); };
async function until(check, message, limit = 1500) {
for (let index = 0; index < limit; index++) {
if (check()) return;
await sleep(20);
}
throw new Fail(message);
}
const scriptErrors = [];
window.addEventListener("error", (event) => scriptErrors.push(event.message));
window.addEventListener("unhandledrejection", (event) => scriptErrors.push(String(event.reason)));
const active = () => document.querySelector(".station-tab[aria-selected='true']")?.dataset.station;
const showing = (role) => active() === role && !$(`station-${role}`).hidden && !$("operations").hidden;
const fits = (label) => assert(document.documentElement.scrollHeight <= innerHeight + 1,
`page scrolls instead of fitting the viewport (${label})`);
async function run() {
await until(() => !$("shell").hidden, "translations missing");
$("name").value = "Solo";
$("code").value = __CODE__;
$("pair-form").requestSubmit();
await until(() => $("station-tab-bridge") && showing("bridge"), "solo bridge console missing");
// Solo: one client holds every station, so there is nothing to add or release.
assert($("station-tabs").children.length === 9, "solo session must show nine station tabs");
assert([...$("station-tabs").children].every((tab, index) =>
tab.querySelector(".station-key").textContent === String(index + 1)), "tab hotkey numbers");
assert($("workstation-add-station").hidden && $("workstation-release").hidden, "add/release shown in solo");
assert(!$("host-bar").hidden && !$("role-rail").hidden === false, "host bar or role rail state");
fits("bridge");
// The operator's picture survives a station switch: returning to a station
// already visited repaints from its cache without blanking the console, and the
// map zoom and a half-typed sonar order are still there. (The very first visit
// to a station has nothing cached and may take one round trip.)
let blanked = 0;
new MutationObserver(() => { if ($("operations").hidden) blanked++; })
.observe($("operations"), {attributes: true, attributeFilter: ["hidden"]});
const before = $("role-map-scale").textContent;
$("role-map-zoom-in").click();
$("role-map-zoom-in").click();
await until(() => $("role-map-scale").textContent !== before, "map zoom did not change");
const zoomed = $("role-map-scale").textContent;
$("station-tab-sonar").click();
await until(() => showing("sonar"), "sonar tab did not activate");
await until(() => [...document.querySelectorAll("[data-sonar-plot]")].filter((panel) => !panel.hidden)
.every((panel) => panel.querySelector("canvas").width > 1), "overview plots not sized");
const shown = [...document.querySelectorAll("[data-sonar-plot]")].filter((panel) => !panel.hidden);
assert(shown.length >= 4, `sonar overview shows ${shown.length} plots`);
assert($("sonar-plots").dataset.layout.startsWith("overview"), "sonar defaults to the overview");
$("sonar-bearing").value = "77";
$("sonar-bearing").dispatchEvent(new Event("input", {bubbles: true}));
fits("sonar");
blanked = 0;
$("station-tab-bridge").click();
await until(() => showing("bridge"), "bridge tab did not activate");
await until(() => $("role-map-scale").textContent === zoomed, "map zoom was lost on station switch");
assert(blanked === 0, "the console was blanked during a station switch");
fits("bridge again");
// Hotkeys: a digit selects that station, [ and ] step through the held ones.
blanked = 0;
document.dispatchEvent(new KeyboardEvent("keydown", {key: "2", bubbles: true}));
await until(() => showing("sonar"), "hotkey 2 did not select sonar");
assert(blanked === 0, "the console was blanked returning to a visited station");
assert($("sonar-bearing").value === "77", "sonar draft was lost on station switch");
document.dispatchEvent(new KeyboardEvent("keydown", {key: "]", bubbles: true}));
await until(() => showing("weapons"), "] did not step to weapons");
blanked = 0;
document.dispatchEvent(new KeyboardEvent("keydown", {key: "[", bubbles: true}));
await until(() => showing("sonar"), "[ did not step back to sonar");
$("sonar-bearing").dispatchEvent(new KeyboardEvent("keydown", {key: "4", bubbles: true}));
await sleep(300);
assert(active() === "sonar", "a digit typed into a field switched station");
assert(blanked === 0, "the console was blanked by hotkey switching to a visited station");
// Solo host controls.
// Real time only: no pause and no time compression control.
assert(!document.getElementById("host-pause"), "pause control remains visible");
assert(!document.getElementById("host-time-scale"), "time compression control remains visible");
await until(() => !$("host-save").disabled, "save locked");
$("host-save").click();
await until(() => $("host-slot-dialog").open, "save dialog did not open");
await until(() => !document.querySelector('#host-slot-list button[data-slot="3"]').disabled &&
!$("host-save").disabled, "save slot unavailable");
document.querySelector('#host-slot-list button[data-slot="3"]').click();
assert($("host-status").dataset.status === "pending", "save command was not submitted");
await until(() => $("host-status").dataset.status === "applied" &&
!$("host-save").disabled, "save result missing");
await until(() => !$("host-new").disabled, "new game locked");
$("host-new").click();
await until(() => $("host-new-dialog").open, "new game dialog did not open");
$("host-new-scenario").value = "s1_patrouille";
$("host-new-scenario").dispatchEvent(new Event("change", {bubbles: true}));
assert(document.querySelector("#host-new-difficulty input").disabled,
"a scenario with a fixed difficulty must not offer difficulty controls");
$("host-new-world").value = "procedural";
$("host-new-seed").value = "4242";
$("host-new-form").requestSubmit();
// The world is replaced; the browser stays paired and returns to the console.
await until(() => window.__replaced === true || $("host-status").textContent.length > 0, "no new-game result");
await sleep(1500);
await until(() => $("station-tab-bridge") && !$("operations").hidden, "console lost after a new game", 3000);
assert($("station-tabs").children.length === 9, "stations missing after a new game");
assert(scriptErrors.length === 0, `script errors: ${scriptErrors.join(" | ")}`);
}
run().then(() => { document.documentElement.dataset.consoleTest = "passed"; },
(error) => {
document.documentElement.dataset.consoleTest = "failed";
document.documentElement.dataset.failure = String(error.stack || error);
});
})();
"""
def test_solo_console_tabs_keep_state_and_host_controls_drive_the_game(
tmp_path, monkeypatch):
chromium = shutil.which("chromium") or shutil.which("chromium-browser") or shutil.which("google-chrome")
if not chromium:
pytest.skip("Optional solo console browser contract: no installed Chromium")
saves = tmp_path / "saves"
monkeypatch.setattr(config, "SAVE_DIR", str(saves))
en, de = catalogs()
game = Game(seed=913, start_menu=False, audio_enabled=False, language="en")
game.sonar.contacts.clear()
for target_id, bearing in ((99001, 28.0), (99002, 52.0)):
contact = Contact(target_id - 99000, target_id, "passiv", "sub")
contact.update_passive(bearing, .8, .8, "hidden", game.sim_t)
game.sonar.contacts[target_id] = contact
console = game.commander
console.solo = True
console.port = 0
console._translations = {
"en": {key: value for key, value in en.items() if key.startswith(PREFIX)},
"de": {key: value for key, value in de.items() if key.startswith(PREFIX)},
}
console._contact_analysis_assets = {}
# Pre-rendered like the assets: resources.files is redirected below.
console._manual_pages = {}
console._manual_pages = {lang: manual.html_page(lang) for lang in manual.LANGUAGES}
html = ASSETS.joinpath("index.html").read_text().replace(
'