mirror of
https://github.com/feder-cr/invisible_playwright.git
synced 2026-06-25 09:28:05 +02:00
Pin firefox-12: cross-OS render parity + always-present standard fonts
- BINARY_VERSION -> firefox-12 (self-calibrating font widths, per-family canvas distinctness, render-noise that preserves solid reference renders). - font_pool: the standard Windows fonts (Calibri, Franklin Gothic, Gadugi, Javanese Text, Myanmar Text) move from the per-profile optional set to core, so they are always present and the detected font set matches a real Windows install on every host. Defensive dedup in derive_font_prefs. - GPU persona applied on every platform (Linux/macOS present a coherent Windows GPU + WebGL params); pool re-rooted on a real-device GPU mix; render seeds recalibrated. - prefs: emit absolute per-family font widths that the binary self-calibrates. - geoip: always pull the latest mmdb via the releases/latest permalink, checked each launch, offline-safe (no pinned tag that can 404). - tests: per-font canvas distinctness, solid-readback purity under render-noise, always-present standard-font invariant, no duplicate families.
This commit is contained in:
parent
6dcdc42c05
commit
8f4b20a19d
15 changed files with 1885 additions and 430 deletions
|
|
@ -1,13 +1,11 @@
|
|||
"""Unit tests for the intelligent geoip mmdb auto-update in `download.py`.
|
||||
"""Unit tests for the geoip mmdb auto-update in `download.py`.
|
||||
|
||||
daijro/geoip-all-in-one rebuilds weekly; `ensure_geoip_mmdb` keeps the cache
|
||||
fresh without a download (or API call) on every launch. These tests mock the
|
||||
cache root, the latest-tag API, and the per-tag download so nothing touches the
|
||||
network.
|
||||
daijro/geoip-all-in-one rebuilds weekly and keeps only the latest ~2 releases,
|
||||
so `ensure_geoip_mmdb` never pins a tag: on every call it resolves the CURRENT
|
||||
latest tag (from the `releases/latest/download` permalink, no GitHub API) and
|
||||
downloads it only when it differs from the cache. These tests mock the cache
|
||||
root, the tag resolver, and the per-tag download so nothing touches the network.
|
||||
"""
|
||||
import os
|
||||
import time
|
||||
|
||||
import pytest
|
||||
|
||||
import invisible_playwright.download as dl
|
||||
|
|
@ -29,14 +27,6 @@ def _make_cached(root, tag, name=dl.GEOIP_MMDB_NAME):
|
|||
return f
|
||||
|
||||
|
||||
def _set_marker_age(root, days):
|
||||
m = root / "geoip" / ".last_check"
|
||||
m.parent.mkdir(parents=True, exist_ok=True)
|
||||
m.touch()
|
||||
old = time.time() - days * 86400
|
||||
os.utime(m, (old, old))
|
||||
|
||||
|
||||
# ──────────────────────────────────────────────────────────────────────
|
||||
# env override
|
||||
# ──────────────────────────────────────────────────────────────────────
|
||||
|
|
@ -56,76 +46,97 @@ def test_env_override_missing_raises(tmp_path, monkeypatch):
|
|||
|
||||
|
||||
# ──────────────────────────────────────────────────────────────────────
|
||||
# freshness window
|
||||
# every-launch latest check
|
||||
# ──────────────────────────────────────────────────────────────────────
|
||||
@pytest.mark.unit
|
||||
def test_fresh_cache_no_network(cache, monkeypatch):
|
||||
f = _make_cached(cache, "2026.06.03")
|
||||
_set_marker_age(cache, 0) # just checked
|
||||
|
||||
def boom():
|
||||
raise AssertionError("latest-tag API must NOT be called within the window")
|
||||
|
||||
monkeypatch.setattr(dl, "_latest_geoip_tag", boom)
|
||||
assert dl.ensure_geoip_mmdb(max_age_days=7) == f
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_stale_same_tag_no_download(cache, monkeypatch):
|
||||
f = _make_cached(cache, "2026.06.03")
|
||||
_set_marker_age(cache, 30) # stale → will re-check
|
||||
monkeypatch.setattr(dl, "_latest_geoip_tag", lambda: "2026.06.03")
|
||||
# real _download_geoip_tag runs but target exists, so no actual download:
|
||||
def test_cache_is_latest_no_download(cache, monkeypatch):
|
||||
f = _make_cached(cache, "2026.06.17")
|
||||
monkeypatch.setattr(dl, "_resolve_latest_geoip_tag", lambda: "2026.06.17")
|
||||
monkeypatch.setattr(dl, "_download_file", lambda *a, **k: (_ for _ in ()).throw(
|
||||
AssertionError("must not download when tag already cached")))
|
||||
assert dl.ensure_geoip_mmdb(max_age_days=7) == f
|
||||
AssertionError("must not download when cache already on the latest tag")))
|
||||
assert dl.ensure_geoip_mmdb() == f
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_stale_new_tag_downloads_and_prunes(cache, monkeypatch):
|
||||
old = _make_cached(cache, "2026.06.03")
|
||||
_set_marker_age(cache, 30)
|
||||
monkeypatch.setattr(dl, "_latest_geoip_tag", lambda: "2026.06.10")
|
||||
|
||||
def fake_download(tag):
|
||||
return _make_cached(cache, tag) # simulate fetch+extract of the new tag
|
||||
|
||||
monkeypatch.setattr(dl, "_download_geoip_tag", fake_download)
|
||||
got = dl.ensure_geoip_mmdb(max_age_days=7)
|
||||
assert got.parent.name == "2026.06.10"
|
||||
def test_new_tag_downloads_and_prunes(cache, monkeypatch):
|
||||
old = _make_cached(cache, "2026.06.10")
|
||||
monkeypatch.setattr(dl, "_resolve_latest_geoip_tag", lambda: "2026.06.17")
|
||||
monkeypatch.setattr(dl, "_download_geoip_tag", lambda tag: _make_cached(cache, tag))
|
||||
got = dl.ensure_geoip_mmdb()
|
||||
assert got.parent.name == "2026.06.17"
|
||||
assert not old.parent.exists() # old tag pruned
|
||||
assert got.exists()
|
||||
|
||||
|
||||
# ──────────────────────────────────────────────────────────────────────
|
||||
# offline resilience
|
||||
# ──────────────────────────────────────────────────────────────────────
|
||||
@pytest.mark.unit
|
||||
def test_api_down_with_cache_uses_cache(cache, monkeypatch):
|
||||
f = _make_cached(cache, "2026.06.03")
|
||||
_set_marker_age(cache, 30)
|
||||
|
||||
def boom():
|
||||
raise OSError("offline")
|
||||
|
||||
monkeypatch.setattr(dl, "_latest_geoip_tag", boom)
|
||||
assert dl.ensure_geoip_mmdb(max_age_days=7) == f # stale cache reused, no raise
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_cold_cache_api_down_falls_back_to_pinned(cache, monkeypatch):
|
||||
# no cache at all + API unreachable → pinned GEOIP_MMDB_VERSION fallback.
|
||||
def boom():
|
||||
raise OSError("offline")
|
||||
|
||||
monkeypatch.setattr(dl, "_latest_geoip_tag", boom)
|
||||
captured = {}
|
||||
|
||||
def fake_download(tag):
|
||||
captured["tag"] = tag
|
||||
return _make_cached(cache, tag)
|
||||
|
||||
monkeypatch.setattr(dl, "_download_geoip_tag", fake_download)
|
||||
got = dl.ensure_geoip_mmdb(max_age_days=7)
|
||||
assert captured["tag"] == dl.GEOIP_MMDB_VERSION
|
||||
def test_cold_cache_downloads_latest(cache, monkeypatch):
|
||||
monkeypatch.setattr(dl, "_resolve_latest_geoip_tag", lambda: "2026.06.17")
|
||||
monkeypatch.setattr(dl, "_download_geoip_tag", lambda tag: _make_cached(cache, tag))
|
||||
got = dl.ensure_geoip_mmdb()
|
||||
assert got.parent.name == "2026.06.17"
|
||||
assert got.exists()
|
||||
|
||||
|
||||
# ──────────────────────────────────────────────────────────────────────
|
||||
# offline resilience (no pinned-tag fallback — the pin rots and 404s)
|
||||
# ──────────────────────────────────────────────────────────────────────
|
||||
@pytest.mark.unit
|
||||
def test_offline_with_cache_uses_cache(cache, monkeypatch):
|
||||
f = _make_cached(cache, "2026.06.10")
|
||||
monkeypatch.setattr(dl, "_resolve_latest_geoip_tag", lambda: None) # offline
|
||||
monkeypatch.setattr(dl, "_download_file", lambda *a, **k: (_ for _ in ()).throw(
|
||||
AssertionError("offline → must not attempt a download")))
|
||||
assert dl.ensure_geoip_mmdb() == f # cache reused, no raise
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_cold_cache_offline_raises(cache, monkeypatch):
|
||||
monkeypatch.setattr(dl, "_resolve_latest_geoip_tag", lambda: None) # offline
|
||||
with pytest.raises(RuntimeError):
|
||||
dl.ensure_geoip_mmdb()
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_download_failure_with_cache_falls_back(cache, monkeypatch):
|
||||
f = _make_cached(cache, "2026.06.10")
|
||||
monkeypatch.setattr(dl, "_resolve_latest_geoip_tag", lambda: "2026.06.17")
|
||||
|
||||
def boom(tag):
|
||||
raise OSError("transient download failure")
|
||||
|
||||
monkeypatch.setattr(dl, "_download_geoip_tag", boom)
|
||||
assert dl.ensure_geoip_mmdb() == f # keeps the old cache rather than failing
|
||||
|
||||
|
||||
# ──────────────────────────────────────────────────────────────────────
|
||||
# latest-tag resolution via the permalink 302 (no GitHub API)
|
||||
# ──────────────────────────────────────────────────────────────────────
|
||||
@pytest.mark.unit
|
||||
def test_resolve_tag_from_permalink_redirect(monkeypatch):
|
||||
class _Resp:
|
||||
headers = {"Location":
|
||||
"https://github.com/daijro/geoip-all-in-one/releases/download/"
|
||||
"2026.06.17/geoip-aio-all.mmdb.zip"}
|
||||
|
||||
monkeypatch.setattr(dl.requests, "head", lambda *a, **k: _Resp())
|
||||
assert dl._resolve_latest_geoip_tag() == "2026.06.17"
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_resolve_tag_permalink_fails_falls_back_to_api(monkeypatch):
|
||||
def head_boom(*a, **k):
|
||||
raise OSError("no network for HEAD")
|
||||
|
||||
monkeypatch.setattr(dl.requests, "head", head_boom)
|
||||
monkeypatch.setattr(dl, "_latest_geoip_tag_api", lambda: "2026.06.17")
|
||||
assert dl._resolve_latest_geoip_tag() == "2026.06.17"
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_resolve_tag_all_fail_returns_none(monkeypatch):
|
||||
def boom(*a, **k):
|
||||
raise OSError("offline")
|
||||
|
||||
monkeypatch.setattr(dl.requests, "head", boom)
|
||||
monkeypatch.setattr(dl, "_latest_geoip_tag_api", boom)
|
||||
assert dl._resolve_latest_geoip_tag() is None
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue