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.

feat(tools): web_check --persistence reloads an autosave in the browser (Fixes #910) (#922)

Eyal Golan3d2bc06891b8
diff --git a/tools/tests/test_web_check.py b/tools/tests/test_web_check.py
index 8a2a45f9e..57345e6ac 100644
--- a/tools/tests/test_web_check.py
+++ b/tools/tests/test_web_check.py
@@ -8,6 +8,8 @@ import threading
import time
import urllib.request
+import pytest
+
import web_check as wc
@@ -179,3 +181,72 @@ def test_find_chrome_without_chrome_path_picks_the_newest_revision(tmp_path, mon
exe.write_bytes(b"")
found.append(exe)
assert wc.find_chrome() == found[1]
+
+
+def test_persistence_check_reloads_after_a_settled_and_an_immediate_autosave(capsys):
+ actions, sleeps = [], []
+ check = wc.PersistenceCheck(actions.append, sleeps.append)
+ check.feed("GAME OK 1948-05-15 turn 0 hash aa00")
+ check.feed("TURN 1 1948-06-01 turn 1 hash one") # unrelated malformed diagnostic is ignored
+ check.feed("TURN 1 1948-06-01 hash aaa1")
+ check.feed("GAME OK 1948-06-01 turn 1 hash aaa1")
+ check.feed("TURN 2 1948-07-01 hash aaa2")
+ check.feed("GAME OK 1948-07-01 turn 2 hash aaa2")
+ new_game, cont, end_turn = (f"click:{x},{y}" for x, y in (wc.NEW_GAME, wc.CONTINUE, wc.END_TURN))
+ assert actions == [new_game, end_turn, "reload", cont, end_turn, "reload"]
+ assert sleeps == [3, 2, 2, 3, 2] # no wait between the second TURN and its reload
+ assert check.done
+ assert "settled reload OK" in capsys.readouterr().out
+
+
+def test_persistence_check_fails_when_the_immediate_reload_did_not_load_the_saved_turn():
+ check = wc.PersistenceCheck(lambda action: None, lambda seconds: None)
+ for text in ("GAME OK 1948-05-15 turn 0 hash aa", "TURN 1 1948-06-01 hash bb",
+ "GAME OK 1948-06-01 turn 1 hash bb", "TURN 2 1948-07-01 hash cc"):
+ check.feed(text)
+ try:
+ check.feed("GAME OK 1948-06-01 turn 1 hash bb")
+ except ValueError as e:
+ assert "PERSISTENCE FAIL immediate reload" in str(e) and "file a follow-up" in str(e)
+ else:
+ raise AssertionError("a lost immediate autosave must fail")
+
+
+def _played_to_the_settled_reload(check):
+ for text in ("GAME OK 1948-05-15 turn 0 hash aa", "TURN 1 1948-06-15 hash bb"):
+ check.feed(text)
+
+
+def test_persistence_check_fails_when_the_settled_reload_did_not_load_the_saved_turn():
+ check = wc.PersistenceCheck(lambda action: None, 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") # the reload came up with no save: a fresh game
+ assert not check.done
+
+
+def test_persistence_check_fails_when_continue_started_a_new_game():
+ """A click that misses Continue and starts a new game must not pass: the turn after Continue is the reloaded
+ game's next turn (2), not a new game's turn 1."""
+ check = wc.PersistenceCheck(lambda action: None, lambda seconds: None)
+ _played_to_the_settled_reload(check)
+ check.feed("GAME OK 1948-06-15 turn 1 hash bb")
+ with pytest.raises(ValueError, match="PERSISTENCE FAIL End turn after Continue: turn 1 .*expected turn 2"):
+ check.feed("TURN 1 1948-06-15 hash cc")
+ assert not check.done
+
+
+def test_persistence_check_fails_when_the_first_boot_is_not_a_fresh_game():
+ check = wc.PersistenceCheck(lambda action: None, lambda seconds: None)
+ with pytest.raises(ValueError, match="PERSISTENCE FAIL first boot"):
+ 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
diff --git a/tools/web_check.py b/tools/web_check.py
index e00ae757b..575e61ed7 100644
--- a/tools/web_check.py
+++ b/tools/web_check.py
@@ -12,7 +12,9 @@ Usage (from the repo root):
Options: --query Q (page URL query, without '?'), --expect TEXT (repeatable), --fail REGEX (default: GOLDEN FAIL|SMOKE FAIL|BOOT FAIL),
--timeout SECONDS (default 300), --stall SECONDS (fail when no console message arrives for that long; 0 = off),
--swiftshader (software WebGL when there is no GPU), --dpr N (default 1), --shots DIR (reassemble the player's
-"SHOT" console lines, from ?shot, into DIR/<pose>.png; a "SHOT done" with an unfinished shot fails the run).
+"SHOT" console lines, from ?shot, into DIR/<pose>.png; a "SHOT done" with an unfinished shot fails the run),
+--persistence (play one normal turn, reload and Continue after a settling delay, then play another turn and reload
+immediately; both reloads must report the saved date, turn and hash).
The page is opened over the DevTools protocol with its viewport fixed to 1920x1080 at the DPR before it loads
(Emulation.setDeviceMetricsOverride: --window-size alone gives a 1920x937 viewport in --headless=new), and the run
fails on any "res=" line, or any screenshot, of another size. Headed runs go through tools/headed.py.
@@ -234,6 +236,7 @@ class Page:
def __init__(self, profile: str, url: str, width: int, height: int, dpr: float):
self.profile, self.url, self.metrics = profile, url, viewport_override(width, height, dpr)
self.ready, self.stop = threading.Event(), threading.Event()
+ self.commands: queue.Queue = queue.Queue()
self.error: BaseException | None = None
self.thread = threading.Thread(target=lambda: asyncio.run(self._run()), daemon=True)
@@ -248,6 +251,16 @@ class Page:
self.stop.set()
self.thread.join(5)
+ def command(self, name: str) -> None:
+ """Runs one action on the page's CDP thread and waits for it: "reload", or "click:X,Y" (a left click at CSS
+ pixels X, Y of the viewport)."""
+ done, result = threading.Event(), []
+ self.commands.put((name, done, result))
+ if not done.wait(10):
+ raise ValueError(f"browser action {name} did not finish")
+ if result:
+ raise ValueError(f"browser action {name} failed: {result[0]}")
+
async def _run(self) -> None:
try:
import websockets # lazy: the unit tests do not install it
@@ -277,7 +290,27 @@ class Page:
self.ready.set()
while not self.stop.is_set():
try:
- await asyncio.wait_for(ws.recv(), 0.5)
+ name, done, result = self.commands.get_nowait()
+ except queue.Empty:
+ name = None
+ if name is not None:
+ try:
+ if name.startswith("click:"):
+ x, y = (float(v) for v in name[len("click:"):].split(","))
+ await call("Input.dispatchMouseEvent", {"type": "mouseMoved", "x": x, "y": y}, sid)
+ for kind in ("mousePressed", "mouseReleased"):
+ await call("Input.dispatchMouseEvent", {"type": kind, "x": x, "y": y, "button": "left",
+ "clickCount": 1}, sid)
+ elif name == "reload":
+ await call("Page.reload", {"ignoreCache": True}, sid)
+ else:
+ raise ValueError(f"unknown command {name}")
+ except Exception as e:
+ result.append(e)
+ finally:
+ done.set()
+ try:
+ await asyncio.wait_for(ws.recv(), 0.1)
except asyncio.TimeoutError:
pass
except Exception as e: # reported by open(); after it, the browser closing ends the thread
@@ -325,13 +358,91 @@ def drain(inbox: queue.Queue) -> list:
return batch
-def watch(inbox, poll, join_reader, expect, fail, timeout, now=time.time, sleep=time.sleep, on_text=None, stall=0) -> int:
+# Button centres in CSS pixels of the fixed 1920x1080 viewport, measured on headless captures of this branch's WebGL
+# build: the start screen's New game (alone, a fresh profile) and Continue (beside New game, a saved game), and the top
+# bar's End turn (clickable while the first event card is open). Keys do not work here: Enter sent over CDP before any
+# click reaches no Unity button, and after New game the first event card, not End turn, takes the focus.
+NEW_GAME, CONTINUE, END_TURN = (960, 1002), (840, 1002), (1840, 45)
+GAME_STATE = re.compile(r"^GAME OK (\d{4}-\d{2}-\d{2}) turn (\d+) hash ([0-9a-f]+)$")
+TURN_STATE = re.compile(r"^TURN (\d+) (\d{4}-\d{2}-\d{2}) hash ([0-9a-f]+)$")
+
+
+class PersistenceCheck:
+ """Drives and checks the two reloads of issue #910. `action` is Page.command (injected, so the unit tests need no
+ browser); a reload keeps the page's profile and origin, so its IndexedDB autosave."""
+
+ def __init__(self, action, settle=time.sleep):
+ self.action, self.settle = action, settle
+ self.stage, self.saved, self.done = "first_boot", None, False
+
+ def feed(self, text: str) -> None:
+ game, turn = GAME_STATE.match(text), TURN_STATE.match(text)
+ if game:
+ state, kind = (game.group(1), int(game.group(2)), game.group(3)), "GAME"
+ elif turn:
+ state, kind = (turn.group(2), int(turn.group(1)), turn.group(3)), "TURN"
+ else:
+ return
+ if self.stage == "first_boot" and kind == "GAME":
+ self._turn(state, 0, "first boot (a fresh profile has no save)")
+ self.settle(3) # the start screen lays out and takes input
+ self.click(NEW_GAME) # normal mode: no ?smoke and no ?nostart
+ self.settle(2)
+ self.click(END_TURN)
+ self.stage = "first_turn"
+ elif self.stage == "first_turn" and kind == "TURN":
+ self._turn(state, 1, "first End turn after New game")
+ self.saved = state
+ self.settle(2) # ordinary reload: give IDBFS autosync time to settle
+ self.action("reload")
+ self.stage = "settled_reload"
+ elif self.stage == "settled_reload" and kind == "GAME":
+ self._same(state, "settled")
+ print(f"PERSISTENCE settled reload OK {state[0]} turn {state[1]} hash {state[2]}")
+ self.settle(3)
+ self.click(CONTINUE)
+ self.settle(2)
+ self.click(END_TURN)
+ self.stage = "second_turn"
+ elif self.stage == "second_turn" and kind == "TURN":
+ # Continue must have resumed the reloaded game: its next turn, not a new game's turn 1 (a click on the wrong
+ # button would otherwise pass).
+ self._turn(state, self.saved[1] + 1, "End turn after Continue")
+ self.saved = state
+ self.action("reload") # deliberately no settling delay after the TURN acknowledgement
+ self.stage = "immediate_reload"
+ elif self.stage == "immediate_reload" and kind == "GAME":
+ self._same(state, "immediate")
+ print(f"PERSISTENCE immediate reload OK {state[0]} turn {state[1]} hash {state[2]}")
+ self.done = True
+
+ def click(self, point: tuple[int, int]) -> None:
+ self.action(f"click:{point[0]},{point[1]}")
+
+ @staticmethod
+ def _turn(state, expected: int, what: str) -> None:
+ if state[1] != expected:
+ raise ValueError(f"PERSISTENCE FAIL {what}: turn {state[1]} ({state[0]}), expected turn {expected}")
+
+ def _same(self, actual, pass_name: str) -> None:
+ if actual != self.saved:
+ date, turn, digest = self.saved
+ raise ValueError(f"PERSISTENCE FAIL {pass_name} reload got {actual[0]} turn {actual[1]} hash {actual[2]}; "
+ f"saved {date} turn {turn} hash {digest}; file a follow-up save-persistence bug")
+
+
+def watch(inbox, poll, join_reader, expect, fail, timeout, now=time.time, sleep=time.sleep, on_text=None, stall=0,
+ complete=None) -> int:
"""The check loop: 0 on PASS, 1 on a failure line, on BROWSER EXITED, on TIMEOUT, or (stall > 0) when no console
message arrives for stall seconds (STALLED). poll() is the browser's returncode (None while running); join_reader()
waits briefly for the stderr reader thread to hit EOF, so output written just before the exit is still scanned.
on_text(text) -> True consumes a message (it is not printed)."""
pending, deadline = list(expect), now() + timeout
last = now()
+ end = ["the end of the --persistence run"] if complete is not None else []
+
+ def missing() -> str:
+ return ", ".join(pending + [e for e in end if not complete()]) or "nothing"
def look():
nonlocal pending, last
@@ -345,8 +456,8 @@ def watch(inbox, poll, join_reader, expect, fail, timeout, now=time.time, sleep=
if status == "fail":
print(f"FAIL: {texts[-1]}")
return 1
- if status == "pass":
- print("PASS: saw " + ", ".join(expect))
+ if status == "pass" and (complete is None or complete()):
+ print("PASS: saw " + ", ".join(list(expect) + end))
return 0
return None
@@ -360,13 +471,13 @@ def watch(inbox, poll, join_reader, expect, fail, timeout, now=time.time, sleep=
result = look()
if result is not None:
return result
- print(f"BROWSER EXITED code {code}; never saw " + ", ".join(pending))
+ print(f"BROWSER EXITED code {code}; never saw " + missing())
return 1
if stall and now() - last > stall:
- print(f"STALLED: no console message for {stall:.0f} s; never saw " + ", ".join(pending))
+ print(f"STALLED: no console message for {stall:.0f} s; never saw " + missing())
return 1
sleep(0.2)
- print("TIMEOUT: never saw " + ", ".join(pending))
+ print("TIMEOUT: never saw " + missing())
return 1
@@ -381,6 +492,7 @@ def main() -> int:
ap.add_argument("--shots", type=Path)
ap.add_argument("--dpr", type=float, default=1.0)
ap.add_argument("--stall", type=float, default=0)
+ ap.add_argument("--persistence", action="store_true")
a = ap.parse_args()
if not (a.build / "index.html").exists():
sys.exit(f"{a.build}/index.html not found")
@@ -415,8 +527,16 @@ def main() -> int:
page = Page(profile, url, w, h, a.dpr)
page.open()
print(f"viewport {w}x{h} at DPR {a.dpr:g} (expect res={res})")
+ persistence = PersistenceCheck(page.command) if a.persistence else None
+
+ def handle_text(text: str) -> bool:
+ consumed = on_text(text)
+ if persistence is not None:
+ persistence.feed(text)
+ return consumed
return watch(inbox, proc.poll, lambda: reader.join(2), list(a.expect), re.compile(a.fail), a.timeout,
- on_text=on_text, stall=a.stall)
+ on_text=handle_text, stall=a.stall,
+ complete=(lambda: persistence.done) if persistence is not None else None)
except ValueError as e:
print(f"FAIL: {e}")
return 1