diff --git a/scripts/m0/m0lib/video.py b/scripts/m0/m0lib/video.py index e6f8d02..8c6ead6 100644 --- a/scripts/m0/m0lib/video.py +++ b/scripts/m0/m0lib/video.py @@ -48,6 +48,7 @@ ROUNDS = ("busy", "busy_cap95", "busy_cap95_2", "busy_2") # this many times in all; one spoilt by a lower quality or frame rate is not (a busy GPU can cause that). ATTEMPTS = 3 REPLAY_S = 30 # pressing play on a stopped player, before the page is loaded again +RELOAD_S = 120 # the page loaded again, until its video plays # -------------------------------------------------------------------------- @@ -268,11 +269,15 @@ class VideoPage: out.update(quality=snap.get("quality"), width=snap.get("width"), height=snap.get("height")) return out - def reload(self) -> dict: - """Load the watch page again (its counters go with it) and get the video going as at the start.""" - self.cdp.call("Page.navigate", {"url": self.url}) + def reload(self) -> bool: + """Load the watch page again (its counters go with it) and get the video going as at the start. + False when it did not play again; the window is then measured as it is and not counted.""" + try: + self.cdp.call("Page.navigate", {"url": self.url}) + except RuntimeError: + return False time.sleep(SETTLE_S) - return self.start(self.quality) + return bool(self.start(self.quality, timeout=RELOAD_S)["started"]) def keep_room(self, min_left: float = MIN_LEFT_S) -> list[str]: """Before a window: get a stopped, broken or reloaded player going again (pressing play first, @@ -282,18 +287,21 @@ class VideoPage: snap = self.snapshot() if snap.get("error"): did.append(f"the player showed an error: {_short(snap.get('error_text'))}") - if snap.get("lost") or snap.get("error"): - did.append("loaded the page again") - self.reload() - elif snap.get("paused") or snap.get("ended") or snap.get("ad_now") or not snap.get("height"): + reload = bool(snap.get("lost") or snap.get("error")) + if not reload and (snap.get("paused") or snap.get("ended") or snap.get("ad_now") or not snap.get("height")): did.append("pressed play") - if not self.start(self.quality, timeout=REPLAY_S)["started"]: - did.append("loaded the page again") - self.reload() + reload = not self.start(self.quality, timeout=REPLAY_S)["started"] + if reload: + did.append("loaded the page again") + if not self.reload(): + did.append("the video did not play again") if did: snap = self.snapshot() - if (snap.get("duration") or 0) - (snap.get("time") or 0) < min_left: - self.cdp.evaluate("(() => { document.getElementById('movie_player').seekTo(5, true); return true; })()") + # Only a playing video is moved back; a missing player is left to the window, which then does + # not count and is tried again. + if not snap.get("lost") and snap.get("duration") and snap["duration"] - (snap.get("time") or 0) < min_left: + self.cdp.evaluate("(() => { const p = document.getElementById('movie_player'); " + "if (p && p.seekTo) p.seekTo(5, true); return true; })()") time.sleep(SETTLE_S) self.cdp.evaluate(_INSTALL) return did diff --git a/tests/test_m0_kit.py b/tests/test_m0_kit.py index 807ec04..5bd25a7 100644 --- a/tests/test_m0_kit.py +++ b/tests/test_m0_kit.py @@ -1562,3 +1562,53 @@ def test_a_page_script_that_fails_while_the_page_loads_reads_as_a_lost_page(): raise RuntimeError("page script failed: Execution context was destroyed.") assert video.VideoPage(Broken()).snapshot()["lost"] is True + + +class _DyingCdp: + """A watch page that plays, then loses its player for good (loading it again does not help); + like Chrome, a page script that reaches for the missing player throws.""" + + def __init__(self, die_at: int): + from m0lib import video + self.v, self.die_at, self.snaps, self.total, self.navigated = video, die_at, 0, 0, 0 + + def evaluate(self, expr): + dead = self.snaps >= self.die_at + if expr == self.v._SNAPSHOT: + self.snaps += 1 + if dead: + return {"lost": True, "error": False, "error_text": None} + self.total += 3600 + return {**_snap(self.total, 0), "paused": False, "ad_now": False, "time": 30.0, "duration": 634.0} + if expr == self.v._STEP: + return {"state": "loading"} if dead else {"state": "playing", "levels": ["hd1080"]} + if expr in (self.v._INSTALL, self.v._RESET): + return not dead + if dead and "movie_player" in expr: + raise RuntimeError("page script failed: TypeError: Cannot read properties of null (reading 'seekTo')") + return True + + def call(self, method, params=None): + self.navigated += method == "Page.navigate" + return {} + + +def test_a_player_that_never_comes_back_is_tried_again_per_window_and_the_stage_goes_on( + tmp_path, monkeypatch, fake_server): + from m0lib import video + for name, value in (("SETTLE_S", 0), ("REPLAY_S", 0.01), ("RELOAD_S", 0.01)): + monkeypatch.setattr(video, name, value) + cdp = _DyingCdp(die_at=4) # two snapshots while starting, then it dies during the first window + page = video.VideoPage(cdp) + closed = [] + monkeypatch.setattr(page, "close", lambda: closed.append(True)) + r, _, running, stt = _run_video_stage(tmp_path, monkeypatch, fake_server, page) + rows = r["video_rows"] + assert [x["key"] for x in rows] == ["video_only", "jarvis_idle", *video.ROUNDS, "video_only_again"] + assert all(not x["video"]["valid"] for x in rows) + # Every window was tried the full number of times, each time loading the page again. + assert all(len(x["video"]["earlier_attempts"]) == video.ATTEMPTS - 1 for x in rows) + assert all("the video did not play again" in x["video"]["player_fixes"] for x in rows) + assert cdp.navigated >= len(rows) * (video.ATTEMPTS - 1) + assert not any("seekTo" in e["error"] for e in r["errors"]) + assert closed and running["now"] is None and stt.loaded is None and not running["held"]