tests: unit coverage for the fossil-screenshot fix
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
(follow-up to previous commit — test appends initially landed in the wrong clone; content identical)
This commit is contained in:
@@ -184,3 +184,78 @@ def test_screenshot_reachable_through_real_load_path(tmp_path):
|
||||
hook = S._load_screenshot_hook(meta)
|
||||
assert callable(hook), "SCREENSHOT hook did not survive the orchestrator load path (R2)"
|
||||
assert S._load_screenshot_hook(meta_mod.load("no-such", tests_dir=str(tmp_path))) is None
|
||||
|
||||
|
||||
# ---- capture() fossil guard (2026-10-05) -----------------------------------------------------
|
||||
# Drone build numbers restarted when its DB was re-created while /var/lib/cc-ci-runs still holds
|
||||
# old-era dirs numbered into the thousands; run 35 (!testme gitea #10) collided with a May-31 run
|
||||
# 35 and embedded custom-html-tiny's fossil screenshot.png because the hook branch SKIPPED the
|
||||
# snap when out_path already existed. capture() must always snap fresh.
|
||||
|
||||
|
||||
class _FakeLaunchable:
|
||||
def __init__(self, page):
|
||||
self._page = page
|
||||
self.chromium = self
|
||||
|
||||
def launch(self, args=None):
|
||||
return self
|
||||
|
||||
def new_context(self, **kw):
|
||||
return self
|
||||
|
||||
def new_page(self):
|
||||
return self._page
|
||||
|
||||
def close(self):
|
||||
pass
|
||||
|
||||
|
||||
class _FakeSyncPlaywright:
|
||||
"""sync_playwright() replacement: context-manager factory handing out one fake browser."""
|
||||
|
||||
def __init__(self, page):
|
||||
self._page = page
|
||||
|
||||
def __call__(self):
|
||||
return self
|
||||
|
||||
def __enter__(self):
|
||||
return _FakeLaunchable(self._page)
|
||||
|
||||
def __exit__(self, *exc):
|
||||
return False
|
||||
|
||||
|
||||
def _install_fake_playwright(monkeypatch, page):
|
||||
import types
|
||||
|
||||
fake_api = types.ModuleType("playwright.sync_api")
|
||||
fake_api.sync_playwright = _FakeSyncPlaywright(page)
|
||||
fake = types.ModuleType("playwright")
|
||||
fake.sync_api = fake_api
|
||||
monkeypatch.setitem(sys.modules, "playwright", fake)
|
||||
monkeypatch.setitem(sys.modules, "playwright.sync_api", fake_api)
|
||||
|
||||
|
||||
def test_capture_overwrites_fossil_screenshot(tmp_path, monkeypatch):
|
||||
"""A pre-existing out_path in a recycled run artifact dir must never be embedded: the hook
|
||||
branch snaps fresh regardless (the old exists-guard returned the fossil as 'captured')."""
|
||||
out_path = str(tmp_path / "screenshot.png")
|
||||
with open(out_path, "wb") as f:
|
||||
f.write(b"STALE-FOSSIL-FROM-ANOTHER-RECIPE" * 400)
|
||||
|
||||
fired = {}
|
||||
page = _FakePage([35707]) # one real-sized frame, like a painted page
|
||||
|
||||
def hook(p, ctx):
|
||||
fired["yes"] = True # navigation only — hooks never save the PNG themselves
|
||||
|
||||
_install_fake_playwright(monkeypatch, page)
|
||||
got = S.capture("gitea.example.org", out_path, recipe_meta={"SCREENSHOT": hook})
|
||||
|
||||
assert got == out_path, "capture must report success after snapping fresh"
|
||||
assert fired.get("yes"), "SCREENSHOT hook must run"
|
||||
assert page.shots == 1, "capture must snap exactly once — the fossil is not a captured frame"
|
||||
with open(out_path, "rb") as f:
|
||||
assert f.read(4) == b"\x89PNG", "out_path still holds the fossil — snap did not overwrite"
|
||||
|
||||
Reference in New Issue
Block a user