Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

53.8 KB
Raw Blame
"""tools/web_check.py: the console-line parser and the static server's headers (no browser needed)."""
import gzip
import http.client
import queue
import re
import shutil
import socket
import sys
import threading
import time
import urllib.request
from pathlib import Path

import pytest

import web_check as wc

# A client connects to the address serve() bound (127.0.0.1), not "localhost": Windows resolves localhost to ::1 first,
# where nothing listens, and with no timeout the connect waits with no end. The timeout bounds every request.
TIMEOUT = 10


def base_url(server) -> str:
    host, port = server.server_address[:2]
    return f"http://{host}:{port}"


@pytest.mark.parametrize("error", [BrokenPipeError, ConnectionResetError])
def test_server_client_disconnects_do_not_print_a_false_failure(error, capsys):
    server = wc.Server(("127.0.0.1", 0), wc.Handler)
    try:
        try:
            raise error("browser closed an outstanding response")
        except error:
            server.handle_error(None, server.server_address)
    finally:
        server.server_close()
    assert capsys.readouterr().err == ""


@pytest.mark.parametrize("error", [ValueError, OSError])
def test_server_unexpected_request_errors_are_still_reported(error, capsys):
    server = wc.Server(("127.0.0.1", 0), wc.Handler)
    try:
        try:
            raise error("unexpected request failure")
        except error:
            server.handle_error(None, server.server_address)
    finally:
        server.server_close()
    report = capsys.readouterr().err
    assert "Traceback" in report
    assert f"{error.__name__}: unexpected request failure" in report


def test_console_lines_are_read_from_their_first_line():
    one = '[1010/025003.375619:INFO:CONSOLE:2] "GOLDEN OK 1950 abc", source: http://localhost:1/index.html (2)'
    assert wc.CONSOLE.search(one).group(1) == "GOLDEN OK 1950 abc"
    # Unity's Debug.Log adds a stack trace, so the message continues on the next stderr lines.
    first = '[1010/030000.1:INFO:CONSOLE:9] "SMOKE OK 5 turns 1949-01-01'
    assert wc.CONSOLE.search(first).group(1) == "SMOKE OK 5 turns 1949-01-01"


def test_precompressed_files_get_content_encoding_and_wasm_its_type(tmp_path):
    (tmp_path / "Build").mkdir()
    (tmp_path / "Build" / "game.wasm.br").write_bytes(b"\x00")
    (tmp_path / "Build" / "game.data.gz").write_bytes(gzip.compress(b"x"))
    (tmp_path / "index.html").write_text("<html></html>")
    server = wc.serve(tmp_path)
    try:
        base = base_url(server)
        r = urllib.request.urlopen(base + "/Build/game.wasm.br", timeout=TIMEOUT)
        assert (r.headers["Content-Encoding"], r.headers["Content-Type"]) == ("br", "application/wasm")
        r = urllib.request.Request(base + "/Build/game.data.gz", headers={"Accept-Encoding": "identity"})
        assert urllib.request.urlopen(r, timeout=TIMEOUT).headers["Content-Encoding"] == "gzip"
        assert urllib.request.urlopen(base + "/index.html", timeout=TIMEOUT).headers["Content-Encoding"] is None
    finally:
        server.shutdown()
        server.server_close()


def test_a_burst_of_connections_while_the_accept_thread_is_starved_is_served(tmp_path):
    """The boot fetches every StreamingAssets file at once over HTTP/1.0 (a new connection each), up to 6 at a time per
    host in Chrome. On a loaded machine the accept thread can lag; macOS answers a connection that overflows the listen
    backlog with a reset, which the player sees as a failed fetch (BOOT FAIL <file>: Unknown Error). Holding the accept
    thread while a burst arrives must not lose any connection."""
    (tmp_path / "a.yaml").write_text("a: 1\n")
    server = wc.serve(tmp_path)
    gate, accept = threading.Event(), server.get_request
    server.get_request = lambda: (gate.wait(10), accept())[1]    # the starved accept thread
    socks, errors = [], {}
    try:
        for i in range(16):
            s = socket.socket()
            socks.append(s)
            s.settimeout(10)
            try:
                s.connect(("127.0.0.1", server.server_address[1]))
                s.sendall(b"GET /a.yaml HTTP/1.0\r\nHost: localhost\r\n\r\n")
            except OSError as e:
                errors[i] = type(e).__name__
        time.sleep(0.2)
        gate.set()
        statuses = []
        for i, s in enumerate(socks):
            try:
                statuses.append(errors.get(i) or s.recv(64).split(b"\r\n")[0].decode())
            except OSError as e:
                statuses.append(type(e).__name__)
        assert statuses == ["HTTP/1.0 200 OK"] * 16
    finally:
        gate.set()
        for s in socks:
            s.close()
        server.shutdown()
        server.server_close()


FAIL = re.compile(r"GOLDEN FAIL|BOOT FAIL")


def line(text):
    return f'[1010/025003.375619:INFO:CONSOLE:2] "{text}", source: http://localhost:1/index.html (2)'


def test_scan_passes_when_every_expected_text_was_seen():
    status, pending, texts = wc.scan([line("GOLDEN OK 1950 a"), line("GOLDEN OK 1952 b")], ["GOLDEN OK 1950", "GOLDEN OK 1952"], FAIL)
    assert (status, pending) == ("pass", []) and texts == ["GOLDEN OK 1950 a", "GOLDEN OK 1952 b"]


def test_scan_fails_on_a_failure_line_and_waits_for_missing_texts():
    status, pending, texts = wc.scan([line("GOLDEN FAIL 1950 x expected y")], ["GOLDEN OK 1950"], FAIL)
    assert status == "fail" and texts == ["GOLDEN FAIL 1950 x expected y"]
    status, pending, _ = wc.scan([line("GOLDEN OK 1950 a")], ["GOLDEN OK 1950", "GOLDEN OK 1952"], FAIL)
    assert (status, pending) == ("pending", ["GOLDEN OK 1952"])


def test_scan_ignores_lines_that_are_not_console_messages():
    status, pending, texts = wc.scan(["[0101/000000:ERROR:gpu] something", "  at Foo.Bar()"], ["X"], FAIL)
    assert (status, pending, texts) == ("pending", ["X"], [])


def test_compressed_scripts_and_data_keep_their_types_and_traversal_is_404(tmp_path):
    (tmp_path / "site").mkdir()
    (tmp_path / "site" / "a.framework.js.gz").write_bytes(gzip.compress(b"x"))
    (tmp_path / "site" / "a.data.br").write_bytes(b"\x00")
    (tmp_path / "secret.txt").write_text("secret")
    server = wc.serve(tmp_path / "site")
    try:
        base = base_url(server)
        js = urllib.request.urlopen(base + "/a.framework.js.gz", timeout=TIMEOUT)
        assert js.headers["Content-Type"] == "application/javascript"
        data = urllib.request.urlopen(base + "/a.data.br", timeout=TIMEOUT)
        assert data.headers["Content-Type"] == "application/octet-stream"
        conn = http.client.HTTPConnection(*server.server_address[:2], timeout=TIMEOUT)
        for path in ("/../secret.txt", "/%2e%2e/secret.txt"):
            conn.request("GET", path)
            r = conn.getresponse(); r.read()
            assert r.status == 404, path
    finally:
        server.shutdown()
        server.server_close()


def run_watch(lines, exit_code=None, expect=("GOLDEN OK",), timeout=5.0, late=()):
    """Drives wc.watch with a fake browser: exit_code is what poll() returns (None = still running); late lines
    arrive only after the reader thread is joined (output written just before the exit)."""
    inbox = queue.Queue()
    for l in lines:
        inbox.put(l)
    clock = [0.0]

    def join_reader():
        for l in late:
            inbox.put(l)

    return wc.watch(inbox, lambda: exit_code, join_reader, list(expect), FAIL, timeout,
                    now=lambda: clock[0], sleep=lambda d: clock.__setitem__(0, clock[0] + d))


def test_a_pass_line_written_just_before_the_exit_is_a_pass():
    assert run_watch([], exit_code=0, late=[line("GOLDEN OK 1950 a")]) == 0
    assert run_watch([line("GOLDEN OK 1950 a")], exit_code=0) == 0


def test_an_exit_without_the_expected_lines_is_browser_exited(capsys):
    assert run_watch([line("something else")], exit_code=3) == 1
    assert "BROWSER EXITED code 3" in capsys.readouterr().out


def test_a_failure_line_fails_and_a_silent_running_browser_times_out(capsys):
    assert run_watch([line("GOLDEN FAIL 1950 a expected b")]) == 1
    assert "FAIL: GOLDEN FAIL" in capsys.readouterr().out
    assert run_watch([], exit_code=None) == 1
    assert "TIMEOUT" in capsys.readouterr().out


def test_find_chrome_takes_chrome_path_when_set(tmp_path, monkeypatch):
    exe = tmp_path / "chrome.exe"
    exe.write_bytes(b"")
    monkeypatch.setenv("CHROME_PATH", str(exe))
    assert wc.find_chrome() == exe


def test_find_chrome_refuses_a_chrome_path_that_is_not_a_file(tmp_path, monkeypatch):
    monkeypatch.setenv("CHROME_PATH", str(tmp_path / "missing"))
    try:
        wc.find_chrome()
    except SystemExit as e:
        assert "CHROME_PATH is not a file" in str(e)
    else:
        raise AssertionError("expected SystemExit")


def test_find_chrome_without_chrome_path_picks_the_newest_revision(tmp_path, monkeypatch):
    # PR #289 review: the override must not change the default lookup, which returns the highest chromium-<rev>.
    monkeypatch.delenv("CHROME_PATH", raising=False)
    monkeypatch.setattr(wc.Path, "home", classmethod(lambda cls: tmp_path))
    found = []
    for rev in ("999", "1223", "1100"):
        exe = tmp_path / "Library" / "Caches" / wc.CHROME_GLOB.replace("chromium-*", f"chromium-{rev}").replace("*", "x")
        exe.parent.mkdir(parents=True, exist_ok=True)
        exe.write_bytes(b"")
        found.append(exe)
    assert wc.find_chrome() == found[1]


# A start buttons line as StartScreenPresenter logs it under ?navlog (a save: Continue shows), and the CSS pixels each
# centre maps to in the 1920x1080 viewport.
LAID_OUT = ("NAV f=88 t=9625 start buttons start-continue=0.3750,0.9278 start-new-game=0.4479,0.9278 "
            "start-star=0.5521,0.9278 start-listen=0.4229,0.8704")
AT = {"start-continue": (720, 1002), "start-new-game": (860, 1002), "start-star": (1060, 1002), "start-listen": (812, 940)}


# The map's tokens as UnitLayer logs them under ?navlog (#2056): the turn, the Israeli and the enemy totals, then each enemy
# side's count.
def units_line(turn, israel, enemy):
    return f"NAV f=412 t=20311 units drawn turn={turn} israel={israel} enemy={enemy}" + (f" egypt={enemy}" if enemy else "")


def _buttons(text=LAID_OUT, wait=0.0):
    buttons = wc.StartButtons((1920, 1080), wait=wait, sleep=lambda s: None)
    buttons.feed(text)
    return buttons


def test_start_buttons_maps_the_logged_centres_onto_the_viewport():
    buttons = _buttons()
    assert {name: buttons.point(name) for name in AT} == AT
    phone = wc.StartButtons((390, 844))
    phone.feed("NAV f=1 t=2 start buttons start-new-game=0.5,0.75")
    assert phone.point("start-new-game") == (195, 633)


def test_start_buttons_keeps_only_the_latest_layout_and_forgets_it_on_clear():
    buttons = _buttons()
    buttons.feed("NAV f=90 t=9700 start buttons start-new-game=0.5000,0.9000")    # the row moved: Continue is gone
    assert buttons.point("start-new-game") == (960, 972)
    buttons.feed("console: start buttons start-new-game=0.1,0.1")                  # not a NAV line: ignored
    buttons.feed("RECORDING playing audio/declaration.wav (9.29 s, at 0.20 s)")
    assert buttons.point("start-new-game") == (960, 972)
    with pytest.raises(ValueError, match="START SCREEN FAIL no centre for start-continue .*logged: start-new-game"):
        buttons.point("start-continue")
    buttons.clear()
    with pytest.raises(ValueError, match="no start buttons line; the page needs [?]navlog"):
        buttons.point("start-new-game")


def test_start_buttons_waits_for_a_line_the_reader_thread_brings():
    t = [0.0]
    buttons = wc.StartButtons((1920, 1080), wait=10, clock=lambda: t[0])

    def sleep(seconds):
        t[0] += seconds
        if t[0] >= 3:
            buttons.feed(LAID_OUT)
    buttons.sleep = sleep
    assert buttons.point("start-new-game") == AT["start-new-game"] and 3 <= t[0] < 3.2


def test_start_buttons_fails_a_centre_outside_the_window():
    with pytest.raises(ValueError, match="start-star is outside the window"):
        _buttons("NAV f=1 t=2 start buttons start-star=1.0200,0.9000").point("start-star")


def test_page_query_adds_navlog_only_to_the_start_screen_runs():
    assert wc.page_query(_args("--startscreen", "new")) == "navlog"
    assert wc.page_query(_args("--startscreen", "new", "--query", "lang=he")) == "lang=he&navlog"
    assert wc.page_query(_args("--persistence")) == "navlog"
    assert wc.page_query(_args("--startscreen", "recording", "--query", "navlog&lang=he")) == "navlog&lang=he"
    assert wc.page_query(_args("--query", "golden")) == "golden"


class FakeGame:
    """Answers a driver's clicks as ?navlog's game does, through a real ConsoleTail: End turn (either layout's) selects a
    unit while `units` still need orders, else ends the turn: a TURN line in the console EndTurn reads (a driver's own
    TURN lines are fed by the test). The unit card is EN_CARD, its rows mirrored in Hebrew: a probe names the element under
    it (a "map" one is taken by the map), and a click on Skip skips a unit. The map's tokens (#2056) are `drawn`, fed as
    UnitLayer logs them: `tokens[turn]` (Israeli, enemy) for the new game's turn 0 at once, and for each later turn just
    before its TURN line (SimBridge raises Changed before TurnEnded); a turn missing from `tokens` logs no line."""

    def __init__(self, units=0, card=None, hebrew=False, answers=True, opening=None, tokens=None, raid=None, flight=True):
        self.tail, self.units, self.answers = wc.ConsoleTail(), units, answers
        self.card, self.hebrew, self.actions, self.turn = card or EN_CARD, hebrew, [], 1
        self.opening = opening   # #530: the opening's layout while it covers the window (top bar included), until Skip
        # #3003: the raid card's layout once the opening is gone, until its OK; with `flight`, the raid flight's clear layer
        # (#3000) covers the window first, and any press on it ends the flight and shows the card (RaidFlightPresenter).
        self.raid, self.flight = raid, flight and raid is not None
        self.tokens = {0: (10, 2), 1: (10, 3)} if tokens is None else tokens
        self.drawn = wc.UnitsOnMap(wait=0.05)
        self.draw(0)

    def draw(self, turn):
        if turn in self.tokens:
            israel, enemy = self.tokens[turn]
            self.drawn.feed(units_line(turn, israel, enemy))

    def nav(self, what):
        self.tail.feed(f"NAV f=1 t=1 {what}")

    def at(self, x, y):
        if self.opening is not None:
            return next((name for (x0, x1, y0, y1), name in self.opening if x0 <= x <= x1 and y0 <= y <= y1),
                        "opening-card" if 440 <= x <= 1480 else "opening-layer")
        if self.flight:
            return "raid-flight-layer"
        if self.raid is not None:
            return next((name for (x0, x1, y0, y1), name in self.raid if x0 <= x <= x1 and y0 <= y <= y1), "raid-layer")
        if self.hebrew:
            x = wc.INFO_PANEL_LEFT + wc.INFO_PANEL_RIGHT - x
        inside = wc.INFO_PANEL_LEFT <= x <= wc.INFO_PANEL_RIGHT and y >= 1080 - wc.INFO_PANEL_BOTTOM - wc.INFO_PANEL_MAX_HEIGHT
        return next((name for (x0, x1, y0, y1), name in self.card if x0 <= x <= x1 and y0 <= y <= y1),
                    "info-region" if inside else "top-bar")

    def __call__(self, action):
        self.actions.append(action)
        kind, _, at = action.partition(":")
        if not self.answers or not at:
            return
        x, y = (int(v) for v in at.split(","))
        if kind == "click" and self.opening is not None:        # the opening takes every click while it shows
            target = self.at(x, y)
            self.nav(f"ui click count=1 target={target}")
            if target == "opening-skip":
                self.opening = None
        elif self.flight and self.opening is None:   # a press ends the flight and shows the card, which takes the release
            self.flight = False
            self.nav(f"ui pointerdown id=0 at ({x}, {y}) target=raid-flight-layer path=raid-flight-layer.raid-flight-layer "
                     f"< root.event-open.raid-flight-open.root modals=-")
            if kind == "click":                  # the 0.4.1 nightly's trace: the click goes to their common ancestor
                self.nav(f"ui pointerup id=0 at ({x}, {y}) target=raid-layer")
                self.nav("ui click count=1 target=root")
        elif kind == "click" and self.raid is not None:          # the card takes every click while it shows
            target = self.at(x, y)
            self.nav(f"ui click count=1 target={target}")
            if target == "raid-ok":
                self.raid = None
        elif kind == "click" and (x, y) in (wc.END_TURN, wc.END_TURN_HE):
            self.nav("ui click count=1 target=end-turn")
            if self.units == 0:
                self.draw(self.turn)
                self.tail.feed(f"TURN {self.turn} 1948-06-15 hash bb")
            self.turn += self.units == 0
        elif kind == "probe":
            target = self.at(x, y)
            if target == "map":                      # a press the UI does not take: the map's hex click
                self.nav(f"map click at ({x}, {1080 - y}) overUi=False")
            else:
                self.nav(f"ui pointerdown id=0 at ({x}, {y}) target={target} path={target} < root.root modals=-")   # NavEventLog.cs:81's format
        elif kind == "click":
            target = self.at(x, y)
            self.nav(f"ui click count=1 target={target}")
            self.units -= target == "info-skip"


# The unit card of the RC build at 1920x1080 (English): ((x0, x1, y0, y1), the element there); elsewhere the description.
EN_CARD = [((84, 536, 1010, 1064), "info-reason"), ((96, 200, 976, 1002), "info-skip"), ((206, 290, 976, 1002), "info-attack"),
           ((96, 300, 930, 956), "info-defence"), ((306, 380, 930, 956), "info-move"), ((84, 536, 920, 1009), "VisualElement")]
# Skip second in its line: found along the line, not at its start.
SECOND_CARD = [((84, 536, 1010, 1064), "info-reason"), ((96, 180, 976, 1002), "info-move"), ((186, 300, 976, 1002), "info-skip"),
               ((84, 536, 960, 1009), "VisualElement")]
# The opening's first panel (#530) at 1920x1080, as middle-button probes of the 0.3.3 nightly build found it: its Back, Next
# and Skip row is centred under the prose. English: Skip starts at the centre. Hebrew: the row is mirrored and the centre
# falls in a gap between buttons (the unnamed row, VisualElement). FAR: Skip is off both scan columns, found along the row.
EN_OPENING = [((811, 873, 552, 598), "opening-back"), ((885, 947, 552, 598), "opening-next"),
              ((960, 1108, 552, 598), "opening-skip"), ((805, 1114, 548, 602), "VisualElement")]
HE_OPENING = [((800, 948, 582, 628), "opening-skip"), ((966, 1028, 582, 628), "opening-next"),
              ((1040, 1102, 582, 628), "opening-back"), ((794, 1108, 578, 632), "VisualElement")]
FAR_OPENING = [((900, 1000, 560, 600), "opening-next"), ((1100, 1200, 560, 600), "opening-skip")]
NO_SKIP_OPENING = [((900, 1000, 560, 600), "opening-next")]
# The raid card (#3003) at 1920x1080: the opening's card frame centred over a dim backdrop (Statecraft.uss .raid-layer), its
# title, clip and scrolling text over one centred row with only OK (Game.uxml .raid-actions, min-width 160 px).
EN_RAID = [((880, 1040, 900, 944), "raid-ok"), ((700, 1220, 560, 890), "raid-text"), ((700, 1220, 150, 550), "raid-media"),
           ((690, 1230, 120, 960), "raid-card")]
NO_OK_RAID = [((700, 1220, 560, 890), "raid-text"), ((690, 1230, 120, 960), "raid-card")]
NO_SKIP_CARD = [((84, 536, 1010, 1064), "info-reason"), ((96, 180, 976, 1002), "info-move"), ((84, 536, 960, 1009), "VisualElement")]


def _quiet():
    """A PersistenceCheck's game, buttons and console."""
    game = FakeGame()
    return game, _buttons(), game.tail


def _quiet_start():
    """An English New game StartScreenCheck's game, recording, hebrew, buttons, console and tokens."""
    game = FakeGame()
    return game, False, False, _buttons(), game.tail, game.drawn


def _end_turn(game, **kw):
    return wc.EndTurn(game, game.tail, game.hebrew, settle=lambda s: None, answer=0.05, turn_wait=0.05, **kw)


def test_end_turn_skips_each_unit_it_selects_then_ends_the_turn(capsys):
    game = FakeGame(units=3)
    assert _end_turn(game)() == 3 and game.units == 0
    clicks = [a for a in game.actions if a.startswith("click:")]
    assert clicks == _clicks(wc.END_TURN, (110, 998)) * 3 + _clicks(wc.END_TURN), "only End turn and Skip are left-clicked"
    probes = [a for a in game.actions if a.startswith("probe:")]
    assert probes.count("probe:110,998") == 3 and len(probes) == 7, "Skip is looked for once, then probed where it was"
    assert "END TURN the turn ended after 3 Skip(s)" in capsys.readouterr().out


def test_end_turn_with_no_unit_needing_orders_is_one_click():
    game = FakeGame()
    assert _end_turn(game)() == 0 and game.actions == _clicks(wc.END_TURN)


@pytest.mark.parametrize("hebrew", [False, True])
def test_end_turn_finds_skip_along_its_line_in_either_layout(hebrew):
    game = FakeGame(units=1, card=SECOND_CARD, hebrew=hebrew)
    assert _end_turn(game)() == 1
    x = int([a for a in game.actions if a.startswith("click:")][1].split(":")[1].split(",")[0])
    assert (wc.INFO_PANEL_LEFT + wc.INFO_PANEL_RIGHT - x if hebrew else x) in range(186, 301)
    assert game.actions[-1] == "click:" + ",".join(map(str, wc.END_TURN_HE if hebrew else wc.END_TURN))


def test_end_turn_fails_when_the_turn_never_ends():
    game = FakeGame(units=100)
    with pytest.raises(ValueError, match="END TURN FAIL End turn still selected a unit needing orders after 2 Skips"):
        _end_turn(game, max_skips=2)()
    assert game.units == 98


def test_end_turn_fails_on_a_card_without_skip():
    with pytest.raises(ValueError, match="card shows no info-skip .*order rows at y \\[998\\]"):
        _end_turn(FakeGame(units=1, card=NO_SKIP_CARD))()


def test_end_turn_fails_when_its_click_misses_end_turn():
    with pytest.raises(ValueError, match="END TURN FAIL the click at \\(1840, 45\\) did not press end-turn"):
        _end_turn(FakeGame(answers=False))()


@pytest.mark.parametrize("hebrew,layout", [(False, EN_OPENING), (True, HE_OPENING), (False, FAR_OPENING)])
def test_end_turn_skips_the_opening_that_covers_it_as_a_player_does(hebrew, layout, capsys):
    """#530: after New game the opening covers the top bar, so the first End turn click reaches the opening. EndTurn presses
    the opening's own Skip, found with middle-button probes, then End turn again."""
    game = FakeGame(hebrew=hebrew, opening=layout)
    end_turn = wc.END_TURN_HE if hebrew else wc.END_TURN
    assert _end_turn(game)() == 0 and game.opening is None
    clicks = [a for a in game.actions if a.startswith("click:")]
    assert clicks[0] == clicks[2] == _clicks(end_turn)[0] and len(clicks) == 3, "End turn, Skip, End turn"
    x, y = (int(v) for v in clicks[1].split(":")[1].split(","))
    assert next(name for (x0, x1, y0, y1), name in layout if x0 <= x <= x1 and y0 <= y <= y1) == "opening-skip"
    assert game.tail.wait(wc.TURN_STATE, 0, 0) is not None
    out = capsys.readouterr().out
    assert "the click reached opening-layer" in out and f"click opening-skip at ({x}, {y})" in out


def test_end_turn_fails_on_an_opening_without_skip():
    with pytest.raises(ValueError, match="OPENING FAIL the opening covers End turn, but no opening-skip"):
        _end_turn(FakeGame(opening=NO_SKIP_OPENING))()


def test_end_turn_fails_when_the_opening_is_still_there_after_skip():
    game = FakeGame(opening=EN_OPENING)

    def stuck(action):           # Skip is pressed, but the opening stays over End turn
        layout = game.opening
        game(action)
        game.opening = layout
    with pytest.raises(ValueError, match="END TURN FAIL the click at \\(1840, 45\\) did not press end-turn .*reached opening-layer"):
        wc.EndTurn(stuck, game.tail, False, settle=lambda s: None, answer=0.05, turn_wait=0.05)()


def test_start_screen_check_skips_the_opening_after_new_game():
    game = FakeGame(opening=EN_OPENING)
    check = wc.StartScreenCheck(game, recording=False, hebrew=False, buttons=_buttons(), tail=game.tail, units=game.drawn,
                                settle=lambda s: None)
    check.feed("GAME OK 1948-05-15 turn 0 hash aa")
    clicks = [a for a in game.actions if a.startswith("click:")]
    assert clicks[:2] == _clicks(AT["start-new-game"], wc.END_TURN) and clicks[-1] == _clicks(wc.END_TURN)[0]
    assert game.opening is None
    check.feed("TURN 1 1948-06-15 hash bb")
    assert check.done


@pytest.mark.parametrize("hebrew,layout", [(False, EN_OPENING), (True, HE_OPENING)])
def test_end_turn_answers_the_raid_flight_and_card_after_the_opening_as_a_player_does(hebrew, layout, capsys):
    """#3003: after the opening's Skip the raid flight's clear layer covers End turn. End turn's press ends the flight and
    shows the raid card, which takes the release, so the click reaches root (the 0.4.1 nightly's pass 3). EndTurn presses
    the card's OK, found with middle-button probes, then End turn again."""
    game = FakeGame(hebrew=hebrew, opening=layout, raid=EN_RAID)
    end_turn = wc.END_TURN_HE if hebrew else wc.END_TURN
    assert _end_turn(game)() == 0 and game.opening is None and game.raid is None and not game.flight
    clicks = [a for a in game.actions if a.startswith("click:")]
    assert len(clicks) == 5 and clicks[0] == clicks[2] == clicks[4] == _clicks(end_turn)[0], \
        "End turn, the opening's Skip, End turn (the flight takes it), OK, End turn"
    x, y = (int(v) for v in clicks[3].split(":")[1].split(","))
    assert next(name for (x0, x1, y0, y1), name in EN_RAID if x0 <= x <= x1 and y0 <= y <= y1) == "raid-ok"
    assert game.tail.wait(wc.TURN_STATE, 0, 0) is not None
    out = capsys.readouterr().out
    assert "the click reached root (pressed raid-flight-layer)" in out and f"click raid-ok at ({x}, {y})" in out


def test_end_turn_presses_ok_on_a_raid_card_shown_without_the_flight():
    """With no flight (no Dakota to fly, or the flight already over) the card itself takes End turn's click."""
    game = FakeGame(raid=EN_RAID, flight=False)
    assert _end_turn(game)() == 0 and game.raid is None
    clicks = [a for a in game.actions if a.startswith("click:")]
    assert len(clicks) == 3 and clicks[0] == clicks[2] == _clicks(wc.END_TURN)[0], "End turn, OK, End turn"


def test_end_turn_fails_on_a_raid_card_without_ok():
    with pytest.raises(ValueError, match="RAID FAIL the raid card covers End turn, but no raid-ok"):
        _end_turn(FakeGame(opening=EN_OPENING, raid=NO_OK_RAID))()


def test_end_turn_fails_when_the_raid_card_is_still_there_after_ok():
    game = FakeGame(raid=EN_RAID)

    def stuck(action):           # OK is pressed, but the card stays over End turn
        layout = game.raid
        game(action)
        game.raid = layout
    with pytest.raises(ValueError, match="END TURN FAIL the click at \\(1840, 45\\) did not press end-turn .*reached raid-layer"):
        wc.EndTurn(stuck, game.tail, False, settle=lambda s: None, answer=0.05, turn_wait=0.05)()


def test_end_turn_still_fails_a_click_that_reaches_root_off_the_raid_flight():
    """Only a press the raid flight took explains a click that reached root; any other still fails."""
    game = FakeGame()

    def elsewhere(action):
        game.actions.append(action)
        game.nav("ui pointerdown id=0 at (1840, 45) target=top-bar path=top-bar < root.root modals=-")
        game.nav("ui click count=1 target=root")
    with pytest.raises(ValueError, match="END TURN FAIL the click at \\(1840, 45\\) did not press end-turn .*reached root\\)"):
        wc.EndTurn(elsewhere, game.tail, False, settle=lambda s: None, answer=0.05, turn_wait=0.05)()


def test_click_end_turn_told_no_raid_fails_on_the_raid_flight():
    with pytest.raises(ValueError, match="END TURN FAIL the click at \\(1840, 45\\) did not press end-turn .*reached root"):
        _end_turn(FakeGame(raid=EN_RAID)).click_end_turn(opening=False)


@pytest.mark.parametrize("flight,reached", [(True, "root"), (False, "raid-layer")])
def test_end_turn_answers_the_raid_only_on_its_first_turn(flight, reached):
    """The game plays the raid only on New game (GameRoot.PlayNewGameIntro: "Never on Continue or a load"). One EndTurn
    answers it on its first End turn; a raid on any later call of the same EndTurn (PersistenceCheck's End turn after
    Continue) still fails."""
    game = FakeGame(raid=EN_RAID, flight=flight)
    end_turn = _end_turn(game)
    assert end_turn() == 0 and game.raid is None
    game.raid, game.flight = EN_RAID, flight                     # the raid shows again before the next End turn
    missed = f"END TURN FAIL the click at \\(1840, 45\\) did not press end-turn .*reached {reached}\\)"
    with pytest.raises(ValueError, match=missed):
        end_turn()
    assert game.raid is not None, "the second raid's OK was not pressed"


def test_persistence_check_answers_the_raid_after_new_game_but_fails_one_after_continue():
    """#3003 on the --persistence run: the raid after New game is answered; one replayed after Continue must fail."""
    game = FakeGame()
    new_game, cont = _clicks(AT["start-new-game"], AT["start-continue"])

    def player(action):
        game(action)
        if action in (new_game, cont):                           # the raid flight covers the window once the map shows
            game.raid, game.flight = EN_RAID, True
    check = wc.PersistenceCheck(player, _buttons(), game.tail, lambda seconds: None)
    _played_to_the_settled_reload(check)
    assert game.raid is None, "the raid after New game was answered"
    check.buttons.feed(LAID_OUT)  # the reloaded page reports its own layout
    missed = "END TURN FAIL the click at \\(1840, 45\\) did not press end-turn .*reached root\\)"
    with pytest.raises(ValueError, match=missed):
        check.feed("GAME OK 1948-06-15 turn 1 hash bb")


def test_a_click_that_does_not_press_skip_fails():
    game = FakeGame(units=1)
    probe_at = game.at
    game.at = lambda x, y: probe_at(x, y) if game.actions[-1].startswith("probe:") else "info-hold"   # the card moved
    with pytest.raises(ValueError, match="END TURN FAIL the click at \\(110, 998\\) did not press info-skip"):
        _end_turn(game)()


def test_a_probe_that_reaches_the_map_fails_at_once():
    game = FakeGame(units=1, card=[((0, 2000, 0, 2000), "map")])
    with pytest.raises(ValueError, match="END TURN FAIL a press at \\(110, 1038\\) reached the map"):
        _end_turn(game)()
    assert sum(a.startswith("probe:") for a in game.actions) == 1


def test_console_tail_waits_for_a_match_after_the_mark_from_another_thread():
    tail = wc.ConsoleTail()
    tail.feed("NAV f=1 t=1 ui click count=1 target=end-turn")
    since = tail.mark()
    assert tail.wait(wc.NAV_CLICK, since, 0, "end-turn") is None, "lines before the mark do not count"
    threading.Timer(0.05, tail.feed, ["NAV f=2 t=2 ui click count=1 target=info-skip"]).start()
    assert tail.wait(wc.NAV_CLICK, since, 5, "info-skip").group(1) == "info-skip"
    assert tail.wait(wc.NAV_DOWN, since, 0) is None


def test_watch_does_not_stall_while_a_driver_waits_on_lines_that_keep_coming(capsys):
    inbox, t = queue.Queue(), [0.0]
    inbox.put(line("GAME OK 1948-05-15 turn 0 hash aa"))

    def on_text(text):
        if text.startswith("GAME OK"):
            t[0] += 30                      # EndTurn's Skips took 30 s, the page logging all along
            inbox.put(line("TURN 1 1948-06-15 hash bb"))
        return False
    rc = wc.watch(inbox, lambda: None, lambda: None, ["TURN 1"], re.compile("BOOT FAIL"), 300, now=lambda: t[0],
                  sleep=lambda s: t.__setitem__(0, t[0] + s), on_text=on_text, stall=20)
    assert rc == 0, capsys.readouterr().out


def _committed_turn(check, generation, date, turn, digest):
    receipt = f"{generation} {date} turn {turn} hash {digest}"
    check.feed("AUTOSAVE PENDING " + receipt)
    check.feed(f"TURN {turn} {date} hash {digest}")
    check.feed("AUTOSAVE OK " + receipt)


def test_persistence_check_reloads_after_a_settled_and_an_immediate_autosave(capsys):
    sleeps, game = [], FakeGame()
    actions = game.actions
    check = wc.PersistenceCheck(game, _buttons(), game.tail, sleeps.append)
    check.feed("GAME OK 1948-05-15 turn 0 hash aa00")
    check.feed("TURN 1 1948-06-01 turn 1 hash one")       # malformed diagnostic is ignored
    _committed_turn(check, 1, "1948-06-01", 1, "aaa1")
    assert check.buttons.centres == {}, "a reload forgets the old page's layout"
    check.buttons.feed(LAID_OUT)  # the reloaded page reports its own layout
    check.feed("GAME OK 1948-06-01 turn 1 hash aaa1")
    _committed_turn(check, 1, "1948-07-01", 2, "aaa2")    # a reload resets the page's counter
    check.buttons.feed(LAID_OUT)  # the reloaded page reports its own layout
    check.feed("GAME OK 1948-07-01 turn 2 hash aaa2")
    new_game, cont, end_turn = _clicks(AT["start-new-game"], AT["start-continue"], wc.END_TURN)
    assert actions == [new_game, end_turn, "reload", cont, end_turn, "reload"]
    assert sleeps == [3, 2, 3, 2]     # UI layout only; neither commit uses a settling delay
    assert check.done
    assert "settled reload OK" in capsys.readouterr().out


def _played_to_the_settled_reload(check):
    check.feed("GAME OK 1948-05-15 turn 0 hash aa")
    _committed_turn(check, 1, "1948-06-15", 1, "bb")


def test_persistence_check_fails_when_the_immediate_reload_did_not_load_the_saved_turn():
    check = wc.PersistenceCheck(*_quiet(), lambda seconds: None)
    _played_to_the_settled_reload(check)
    check.buttons.feed(LAID_OUT)  # the reloaded page reports its own layout
    check.feed("GAME OK 1948-06-15 turn 1 hash bb")
    _committed_turn(check, 1, "1948-07-15", 2, "cc")
    with pytest.raises(ValueError, match="PERSISTENCE FAIL immediate reload"):
        check.feed("GAME OK 1948-06-15 turn 1 hash bb")


def test_persistence_check_fails_when_the_settled_reload_did_not_load_the_saved_turn():
    check = wc.PersistenceCheck(*_quiet(), lambda seconds: None)
    _played_to_the_settled_reload(check)
    with pytest.raises(ValueError, match="PERSISTENCE FAIL settled reload got 1948-05-15 turn 0"):
        check.feed("GAME OK 1948-05-15 turn 0 hash aa")


def test_persistence_check_fails_when_continue_started_a_new_game():
    check = wc.PersistenceCheck(*_quiet(), lambda seconds: None)
    _played_to_the_settled_reload(check)
    check.buttons.feed(LAID_OUT)  # the reloaded page reports its own layout
    check.feed("GAME OK 1948-06-15 turn 1 hash bb")
    with pytest.raises(ValueError, match="expected turn 2"):
        check.feed("TURN 1 1948-06-15 hash bb")


def test_persistence_check_fails_when_the_first_boot_is_not_a_fresh_game():
    check = wc.PersistenceCheck(*_quiet(), lambda seconds: None)
    with pytest.raises(ValueError, match="expected turn 0"):
        check.feed("GAME OK 1948-06-15 turn 1 hash bb")


def test_watch_names_the_unfinished_persistence_run(capsys):
    inbox = queue.Queue()
    inbox.put("GAME OK 1948-05-15 turn 0 hash aa")
    t = [0.0]
    rc = wc.watch(inbox, lambda: None, lambda: None, [], re.compile("BOOT FAIL"), 5, now=lambda: t[0],
                  sleep=lambda s: t.__setitem__(0, t[0] + 1), complete=lambda: False)
    assert rc == 1
    assert "TIMEOUT: never saw the end of the --persistence run" in capsys.readouterr().out


def _clicks(*points):
    return [f"click:{x},{y}" for x, y in points]


def test_start_screen_check_presses_new_game_then_end_turn_and_needs_turn_1(capsys):
    game = FakeGame()
    actions = game.actions
    check = wc.StartScreenCheck(game, recording=False, hebrew=False, buttons=_buttons(), tail=game.tail, units=game.drawn,
                                settle=lambda s: None)
    check.feed("GAME OK 1948-05-15 turn 0 hash aa")
    assert actions == _clicks(AT["start-new-game"], wc.END_TURN)
    assert not check.done
    check.feed("TURN 1 1948-06-15 hash bb")
    assert check.done
    out = capsys.readouterr().out
    assert "click start-new-game at (860, 1002)" in out and "START SCREEN OK New game reached the map: TURN 1" in out


def test_start_screen_check_fails_at_once_when_the_game_never_says_where_new_game_is():
    check = wc.StartScreenCheck(lambda a: None, False, False, buttons=_buttons(""), tail=wc.ConsoleTail(),
                                units=wc.UnitsOnMap(), settle=lambda s: None)
    with pytest.raises(ValueError, match="START SCREEN FAIL no centre for start-new-game"):
        check.feed("GAME OK 1948-05-15 turn 0 hash aa")


def test_start_screen_check_plays_the_recording_first_and_waits_for_it_hebrew_layout():
    game = FakeGame()
    actions, buttons = game.actions, _buttons()
    check = wc.StartScreenCheck(game, recording=True, hebrew=True, buttons=buttons, tail=game.tail, units=game.drawn,
                                settle=lambda s: None)
    check.feed("GAME OK 1948-05-15 turn 0 hash aa")
    assert actions == _clicks(AT["start-listen"])
    check.feed("TURN 1 1948-06-15 hash bb")            # no New game before the recording plays
    assert actions == _clicks(AT["start-listen"]) and not check.done
    buttons.feed("NAV f=300 t=19000 start buttons start-new-game=0.5521,0.8000")   # the caption moved the row up
    check.feed("RECORDING playing audio/declaration.wav (9.29 s, at 0.20 s)")
    assert actions == _clicks(AT["start-listen"], (1060, 864), wc.END_TURN_HE)
    check.feed("TURN 1 1948-06-15 hash bb")
    assert check.done


def test_start_screen_check_fails_on_a_saved_game_or_a_wrong_turn():
    with pytest.raises(ValueError, match="START SCREEN FAIL the first boot is turn 3"):
        wc.StartScreenCheck(*_quiet_start(), lambda s: None).feed("GAME OK 1948-08-15 turn 3 hash aa")
    check = wc.StartScreenCheck(*_quiet_start(), lambda s: None)
    check.feed("GAME OK 1948-05-15 turn 0 hash aa")
    with pytest.raises(ValueError, match="START SCREEN FAIL End turn after New game logged turn 2"):
        check.feed("TURN 2 1948-07-15 hash cc")
    assert not check.done


def _start(game):
    return wc.StartScreenCheck(game, recording=False, hebrew=False, buttons=_buttons(), tail=game.tail, units=game.drawn,
                               settle=lambda s: None)


def test_start_screen_check_needs_israeli_and_enemy_tokens_on_turns_1_and_2(capsys):
    """#2056 (owner, 1 November 2026): the player's turn 1 is the new game (sim turn 0, GDD: "the units of turn 1 of a new
    historical game"), turn 2 the state after the first End turn (TURN 1)."""
    check = _start(FakeGame())
    check.feed("GAME OK 1948-05-15 turn 0 hash aa")
    check.feed("TURN 1 1948-06-15 hash bb")
    assert check.done
    out = capsys.readouterr().out
    assert "UNITS OK turn 1 (New game): israel=10 enemy=2" in out and "UNITS OK turn 2 (after End turn): israel=10 enemy=3" in out


def test_the_31_october_build_with_no_enemy_tokens_fails_before_end_turn():
    """v0.3.1-nightly.20261028: the enemy tokens waited for a side figure that was never requested, so UnitLayer drew
    Israel's tokens alone."""
    game = FakeGame(tokens={0: (10, 0), 1: (10, 0)})
    with pytest.raises(ValueError, match=r"UNITS FAIL turn 1 \(New game\): no enemy unit tokens drawn within .*enemy=0"):
        _start(game).feed("GAME OK 1948-05-15 turn 0 hash aa")
    assert game.actions == _clicks(AT["start-new-game"]), "End turn is not pressed"


def test_enemy_tokens_gone_on_turn_2_fail():
    game = FakeGame(tokens={0: (10, 2), 1: (10, 0)})
    check = _start(game)
    check.feed("GAME OK 1948-05-15 turn 0 hash aa")
    with pytest.raises(ValueError, match=r"UNITS FAIL turn 2 \(after End turn\): no enemy unit tokens"):
        check.feed("TURN 1 1948-06-15 hash bb")
    assert not check.done


def test_no_israeli_tokens_fail():
    with pytest.raises(ValueError, match="UNITS FAIL turn 1 .*no Israeli unit tokens"):
        _start(FakeGame(tokens={0: (0, 2)})).feed("GAME OK 1948-05-15 turn 0 hash aa")


def test_no_units_line_at_all_fails_and_never_passes():
    """Fail closed: a build that logs no units line (one from before #2056, or a page without ?navlog) fails."""
    game = FakeGame(tokens={})
    with pytest.raises(ValueError, match="UNITS FAIL turn 1 .*no 'units drawn' line .*navlog"):
        _start(game).feed("GAME OK 1948-05-15 turn 0 hash aa")


def test_turn_2_needs_a_line_for_turn_1_not_the_new_games():
    """The new game's line says nothing of turn 2: a game that logs no line once the turn ends fails."""
    check = _start(FakeGame(tokens={0: (10, 2)}))
    check.feed("GAME OK 1948-05-15 turn 0 hash aa")
    with pytest.raises(ValueError, match="UNITS FAIL turn 2 .*no 'units drawn' line for turn 1 .*the last was .*turn=0"):
        check.feed("TURN 1 1948-06-15 hash bb")


def test_units_on_map_waits_for_a_token_that_arrives_later_and_keeps_the_latest_line():
    """An enemy token is drawn once its figure streams in (#665): the check waits for it, and reads the latest line."""
    drawn = wc.UnitsOnMap(wait=5)
    drawn.feed(units_line(0, 10, 0))
    threading.Timer(0.05, drawn.feed, [units_line(0, 10, 2)]).start()
    assert drawn.require(0, "turn 1") == (10, 2)
    drawn.feed(units_line(0, 10, 0))
    drawn.wait = 0
    with pytest.raises(ValueError, match="no enemy unit tokens"):
        drawn.require(0, "turn 1")


@pytest.mark.parametrize("text", ["NAV f=1 t=1 units drawn turn=0 israel=10", "units drawn turn=0 israel=10 enemy=2",
                                  "NAV f=1 t=1 units drawn turn=x israel=10 enemy=2", "NAV f=1 t=1 ui click count=1 target=units"])
def test_a_line_not_in_the_units_format_is_no_signal(text):
    drawn = wc.UnitsOnMap(wait=0)
    drawn.feed(text)
    with pytest.raises(ValueError, match="no 'units drawn' line"):
        drawn.require(0, "turn 1")


@pytest.mark.parametrize("mode", ["new", "recording"])
def test_every_start_screen_run_checks_the_tokens(mode):
    """run_plan gives the nightly's four start screen passes (both modes, both languages) the units check."""
    lines = BOOTED_AND_TURNED[:1] + ["RECORDING playing audio/declaration.wav (9.29 s, at 0.20 s)"] * (mode == "recording")
    with pytest.raises(ValueError, match=r"UNITS FAIL turn 1 \(New game\): no enemy unit tokens"):
        _run(("--startscreen", mode), lines, game=FakeGame(tokens={0: (10, 0)}))


def test_query_value():
    assert wc.query_value("lang=he&smoke", "lang") == "he"
    assert wc.query_value("smoke", "lang") is None
    assert wc.query_value("", "lang") is None


def test_start_screen_check_english_recording_uses_the_english_layout():
    game = FakeGame()
    actions = game.actions
    check = wc.StartScreenCheck(game, recording=True, hebrew=False, buttons=_buttons(), tail=game.tail, units=game.drawn,
                                settle=lambda s: None)
    check.feed("GAME OK 1948-05-15 turn 0 hash aa")
    check.feed("RECORDING playing audio/declaration.wav (9.29 s, at 0.20 s)")
    assert actions == _clicks(AT["start-listen"], AT["start-new-game"], wc.END_TURN)


def _args(*argv):
    return wc.parser().parse_args(["build", *argv])


def _plan(*argv, game=None):
    game = game or FakeGame()
    driver, fail, plan = wc.run_plan(_args(*argv), game, _buttons(), game.tail, game.drawn)
    if driver is not None:
        driver.settle = driver.end_turn.settle = lambda seconds: None
    return driver, fail, plan


def test_every_run_fails_on_boot_fail_by_default():
    _, fail, plan = _plan()
    assert fail.search("BOOT FAIL manifest.json: timeout") and plan == {}


@pytest.mark.parametrize("mode", ["new", "recording"])
def test_a_start_screen_run_fails_on_boot_fail_exceptions_and_the_recording_whatever_fail_says(mode):
    for argv in ((), ("--fail", "SOMETHING ELSE")):
        _, fail, _ = _plan("--startscreen", mode, *argv)
        for text in ("BOOT FAIL manifest.json: timeout", "NullReferenceException: Object reference not set",
                     "RECORDING failed audio/declaration.wav: no audio"):
            assert fail.search(text), (argv, text)
    assert _plan("--startscreen", mode, "--fail", "SOMETHING ELSE")[1].search("SOMETHING ELSE")


def test_the_start_screen_run_takes_the_hebrew_layout_from_the_query():
    he, _, _ = _plan("--startscreen", "recording", "--query", "lang=he&smoke=0")
    en, _, _ = _plan("--startscreen", "recording")
    assert (he.end_turn.point, en.end_turn.point) == (wc.END_TURN_HE, wc.END_TURN)


def test_persistence_and_start_screen_are_separate_runs():
    with pytest.raises(ValueError, match="separate runs"):
        _plan("--persistence", "--startscreen", "new")


def _run(plan_args, lines, late=(), game=None):
    """Feeds `lines` to watch through run_plan's driver; `late` arrive at the first sleep, after them."""
    driver, fail, plan = _plan(*plan_args, game=game)
    inbox = queue.Queue()
    for text in lines:
        inbox.put(line(text))
    t, late = [0.0], list(late)

    def sleep(seconds):
        while late:
            inbox.put(line(late.pop(0)))
        t[0] += seconds

    return wc.watch(inbox, lambda: None, lambda: None, [], fail, 60, now=lambda: t[0], sleep=sleep,
                    on_text=lambda text: driver.feed(text) and False, **plan)


BOOTED_AND_TURNED = ["GAME OK 1948-05-15 turn 0 hash aa", "TURN 1 1948-06-15 hash bb"]


def test_a_start_screen_run_passes_after_turn_1_and_names_itself(capsys):
    assert _run(("--startscreen", "new"), BOOTED_AND_TURNED) == 0
    assert "PASS: saw the end of the --startscreen new run" in capsys.readouterr().out


def test_an_exception_logged_just_after_turn_1_still_fails_the_run(capsys):
    assert _run(("--startscreen", "new"), BOOTED_AND_TURNED, late=["NullReferenceException: late presenter"]) == 1
    assert "FAIL: NullReferenceException: late presenter" in capsys.readouterr().out


def test_a_start_screen_run_that_never_reaches_turn_1_fails_naming_itself(capsys):
    assert _run(("--startscreen", "recording"), ["GAME OK 1948-05-15 turn 0 hash aa"]) == 1
    assert "TIMEOUT: never saw the end of the --startscreen recording run" in capsys.readouterr().out


@pytest.mark.parametrize("bad", ["BOOT FAIL manifest.json: timeout", "RECORDING failed audio/declaration.wav: no audio",
                                 "InvalidOperationException: presenter"])
def test_a_failure_line_during_a_start_screen_run_fails_it(bad, capsys):
    assert _run(("--startscreen", "recording"), ["GAME OK 1948-05-15 turn 0 hash aa", bad]) == 1
    assert f"FAIL: {bad}" in capsys.readouterr().out


def test_viewport_parses_width_x_height_css_pixels():
    assert wc.parse_viewport("390x844") == (390, 844)   # the phone the HUD's three-row bar is tuned for
    assert wc.parse_viewport("1920x1080") == (1920, 1080)
    assert wc.parse_viewport(" 390X844 ") == (390, 844)
    for bad in ("", "abc", "39x84", "390x", "x844", "390-844"):
        with pytest.raises(ValueError):
            wc.parse_viewport(bad)


def test_mobile_only_flips_the_emulation_flag():
    assert wc.viewport_override(1920, 1080, 1.0)["mobile"] is False          # the desktop default (review ruling 1)
    phone = wc.viewport_override(390, 844, 3.0, True)
    assert phone == {"width": 390, "height": 844, "deviceScaleFactor": 3.0, "mobile": True}


def test_persistence_check_does_not_reload_turn_two_before_its_commit_ack():
    game = FakeGame()
    actions = game.actions
    check = wc.PersistenceCheck(game, _buttons(), game.tail, lambda seconds: None)
    check.feed("GAME OK 1948-05-15 turn 0 hash aa")
    check.feed("AUTOSAVE PENDING 1 1948-06-15 turn 1 hash bb")
    check.feed("TURN 1 1948-06-15 hash bb")
    check.feed("AUTOSAVE OK 1 1948-06-15 turn 1 hash bb")
    check.buttons.feed(LAID_OUT)  # the reloaded page reports its own layout
    check.feed("GAME OK 1948-06-15 turn 1 hash bb")
    assert actions.count("reload") == 1
    check.feed("AUTOSAVE PENDING 2 1948-07-15 turn 2 hash cc")
    check.feed("TURN 2 1948-07-15 hash cc")
    assert actions.count("reload") == 1, "TURN is not an IndexedDB commit acknowledgement"
    check.feed("AUTOSAVE OK 2 1948-07-15 turn 2 hash cc")
    assert actions.count("reload") == 2
    check.buttons.feed(LAID_OUT)  # the reloaded page reports its own layout
    check.feed("GAME OK 1948-07-15 turn 2 hash cc")
    assert check.done


@pytest.mark.parametrize("ack", [
    "AUTOSAVE OK 1 1948-07-15 turn 2 hash cc",  # old generation, identical state
    "AUTOSAVE OK 2 1948-07-15 turn 2 hash dd",  # wrong hash
    "AUTOSAVE OK 2 1948-06-15 turn 1 hash bb",  # old turn/date
])
def test_a_stale_or_mismatched_save_ack_cannot_authorize_reload(ack):
    game = FakeGame()
    actions = game.actions
    check = wc.PersistenceCheck(game, _buttons(), game.tail, lambda seconds: None)
    _played_to_the_settled_reload(check)
    check.buttons.feed(LAID_OUT)  # the reloaded page reports its own layout
    check.feed("GAME OK 1948-06-15 turn 1 hash bb")
    check.feed("AUTOSAVE PENDING 2 1948-07-15 turn 2 hash cc")
    check.feed("TURN 2 1948-07-15 hash cc")
    check.feed(ack)
    assert actions.count("reload") == 1
    assert not check.done
    check.feed("AUTOSAVE OK 2 1948-07-15 turn 2 hash cc")
    assert actions.count("reload") == 2


def test_a_commit_timeout_fails_even_without_any_further_console_line():
    clock = [0.0]
    check = wc.PersistenceCheck(*_quiet(), lambda seconds: None, now=lambda: clock[0], save_timeout=5)
    check.feed("GAME OK 1948-05-15 turn 0 hash aa")
    check.feed("TURN 1 1948-06-15 hash bb")
    clock[0] = 5
    with pytest.raises(ValueError, match="no matching IndexedDB commit within 5s"):
        _ = check.done


def test_an_indexeddb_failure_without_the_player_message_fails_after_the_toast_deadline():
    clock = [0.0]
    check = wc.PersistenceCheck(*_quiet(), lambda seconds: None, now=lambda: clock[0], save_timeout=5)
    check.feed("AUTOSAVE FAIL 1 1948-06-15 turn 1 hash bb")
    clock[0] = 5
    with pytest.raises(ValueError, match="save.write_failed was not shown to the player"):
        _ = check.done


def test_an_indexeddb_failure_with_the_player_message_still_fails_but_records_coverage():
    check = wc.PersistenceCheck(*_quiet(), lambda seconds: None)
    check.feed("AUTOSAVE FAIL 1 1948-06-15 turn 1 hash bb")
    with pytest.raises(ValueError, match="save.write_failed was shown to the player"):
        check.feed("SAVE FAILURE SHOWN save.write_failed")


def test_commit_ack_before_turn_diagnostic_is_not_lost():
    game = FakeGame()
    actions = game.actions
    check = wc.PersistenceCheck(game, _buttons(), game.tail, lambda seconds: None)
    check.feed("GAME OK 1948-05-15 turn 0 hash aa")
    check.feed("AUTOSAVE PENDING 1 1948-06-15 turn 1 hash bb")
    check.feed("AUTOSAVE OK 1 1948-06-15 turn 1 hash bb")
    assert "reload" not in actions
    check.feed("TURN 1 1948-06-15 hash bb")
    assert actions.count("reload") == 1


def test_commit_timeout_is_polled_while_an_unrelated_expectation_is_still_missing():
    clock = [0.0]
    check = wc.PersistenceCheck(*_quiet(), lambda seconds: None, now=lambda: clock[0], save_timeout=5)
    check.feed("GAME OK 1948-05-15 turn 0 hash aa")
    check.feed("TURN 1 1948-06-15 hash bb")
    with pytest.raises(ValueError, match="no matching IndexedDB commit within 5s"):
        wc.watch(queue.Queue(), lambda: None, lambda: None, ["UNRELATED"], re.compile("BOOT FAIL"), 20,
                 now=lambda: clock[0], sleep=lambda seconds: clock.__setitem__(0, clock[0] + 1),
                 complete=lambda: check.done)


# --- the itch.io save test: --url, --profile, --first-save ----------------------------------------------------------------

def test_first_save_stops_after_the_first_durable_commit(capsys):
    game = FakeGame()
    check = wc.PersistenceCheck(game, _buttons(), game.tail, lambda seconds: None, first_save_only=True)
    check.feed("GAME OK 1948-05-15 turn 0 hash aa")
    assert not check.done
    check.feed("TURN 1 1948-05-16 hash ab")
    check.feed("AUTOSAVE PENDING 1 1948-05-16 turn 1 hash ab")
    assert not check.done, "a commit that is not acknowledged is not durable yet"
    check.feed("AUTOSAVE OK 1 1948-05-16 turn 1 hash ab")
    assert check.done and "reload" not in game.actions
    assert "PERSISTENCE saved 1948-05-16 turn 1 hash ab" in capsys.readouterr().out


def test_first_save_still_fails_on_a_wrong_first_turn():
    game = FakeGame()
    check = wc.PersistenceCheck(game, _buttons(), game.tail, lambda seconds: None, first_save_only=True)
    check.feed("GAME OK 1948-05-15 turn 0 hash aa")
    with pytest.raises(ValueError, match="expected turn 1"):
        check.feed("TURN 2 1948-05-16 hash ab")


def test_first_save_uses_the_persistence_driver_and_navlog():
    driver, fail, plan = _plan("--first-save")
    assert isinstance(driver, wc.PersistenceCheck) and driver.first_save_only and plan["complete_what"].endswith("--first-save run")
    assert wc.page_query(_args("--first-save")) == "navlog"
    assert not _plan("--persistence")[0].first_save_only
    with pytest.raises(ValueError, match="separate runs"):
        _plan("--first-save", "--startscreen", "new")


def test_url_and_build_are_exclusive_and_one_is_required(capsys):
    ap = wc.parser()
    for argv in ([], ["build", "--url", "https://x.invalid/index.html"]):
        a = ap.parse_args(argv)
        with pytest.raises(SystemExit):
            wc.check_source(ap, a)
        assert "exactly one of build and --url" in capsys.readouterr().err
    wc.check_source(ap, ap.parse_args(["build"]))
    wc.check_source(ap, ap.parse_args(["--url", "https://x.invalid/index.html"]))


@pytest.mark.parametrize("expect", [(), ("--expect", ""), ("--expect", "   ")],
                         ids=["absent", "empty", "whitespace"])
def test_main_refuses_without_a_positive_check(expect, tmp_path, monkeypatch, capsys):
    build = tmp_path / "build"
    build.mkdir()
    (build / "index.html").write_text("<!doctype html>")
    monkeypatch.setattr(sys, "argv", ["web_check.py", str(build), *expect])
    monkeypatch.setattr(wc, "find_chrome", lambda: (_ for _ in ()).throw(AssertionError("Chrome was reached")))
    with pytest.raises(SystemExit) as e:
        wc.main()
    assert e.value.code == 2
    assert "require at least one positive check" in capsys.readouterr().err


def test_a_url_takes_the_query_after_a_question_mark_or_an_ampersand():
    assert wc.with_query("https://h/index.html", "navlog") == "https://h/index.html?navlog"
    assert wc.with_query("https://h/index.html?v=1", "navlog") == "https://h/index.html?v=1&navlog"
    assert wc.with_query("https://h/index.html?v=1", "") == "https://h/index.html?v=1"


def test_a_given_profile_is_made_and_kept_and_a_default_one_is_owned(tmp_path):
    given = tmp_path / "kept" / "profile"
    assert wc.pick_profile(_args("--profile", str(given))) == (str(given), False) and given.is_dir()
    own, owned = wc.pick_profile(_args())
    try:
        assert owned and Path(own).is_dir()
    finally:
        shutil.rmtree(own)