Files
archived-camoufox/python/tests/test_addons.py
T
Jake WriterandClaude Opus 5.5 6c3850f60a Remove dead and duplicated code across the launchers, scripts and CI
Values both launchers hard-coded (marker fonts, the Windows 11 fonts, the
Apple Silicon and Intel Mac core and panel tables, plausible DPR and core
counts, the macOS MacinTalk/Eloquence voices, the cache prefs) now live in
python/src/camoufox/launcher-constants.json, which the TypeScript package
ships as a data file. gen-fonts-json.py and verify-fonts.py read the same
file, so the drifted PingFang marker copy is gone (fonts.json regenerates
byte-identical) and verify-fonts no longer ast-parses fingerprints.py.

- Drop the duplicate fpgen model pin under browser/scripts/data; the TS
  test checks the fpgen package's MODEL_PIN against the Python copy.
- Move pin-fpgen-model.py and clean-fingerprint-data.py to python/scripts.
- Delete the font licence report (font-metrics/licences.py and its saved
  output, open-fonts.txt): the bundle ships as built, so the report checks
  nothing.
- Delete unreferenced files: cleanfonts.sh, python/examples,
  typescript/renovate.json.
- Delete dead code: classify_installs, remove_mmdb, write_upstream_sh,
  bump_release, unused test constants and fixture helpers, the
  _virtual_display attribute writes, package.py's pre-groups font copy (a
  missing groups.json now fails).
- Share require_bundle, zip_entries, gh_headers and parse_shard instead of
  keeping copies; fold GitHubDownloader into CamoufoxFetcher in both
  launchers.
- TS raises ValueError where Python does, and `camoufox version` prints the
  fpgen package version as Python does.
- Python tests get src/ from pytest's pythonpath instead of sys.path
  inserts; skips that hid untested paths now assert or test the behaviour.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 13:38:55 -06:00

92 lines
2.7 KiB
Python

"""
Tests for camoufox.addons default-addon download/caching.
Regression guard for #308: a partial/failed first download leaves an empty
addon directory behind. The old "already downloaded" check was a bare
os.path.exists(dir), so that empty dir was trusted forever and every later
launch raised InvalidAddonPath ("manifest.json is missing"), unrecoverable
short of manually deleting the cache.
Run with:
cd python && python -m pytest tests/test_addons.py -v
"""
import os
import pytest
from camoufox import addons as addons_mod
from camoufox.addons import DefaultAddons, maybe_download_addons
UBO = DefaultAddons.UBO.name
@pytest.fixture
def addons_dir(tmp_path, monkeypatch):
# Point the addon store at a throwaway dir so no real cache is touched.
root = tmp_path / "addons"
monkeypatch.setattr(addons_mod, "get_addon_path", lambda name: str(root / name))
return root
def _write_manifest(url, extract_path, name):
os.makedirs(extract_path, exist_ok=True)
with open(os.path.join(extract_path, "manifest.json"), "w") as f:
f.write("{}")
def test_partial_dir_is_redownloaded(addons_dir, monkeypatch):
# Leftover empty dir from a failed first download.
partial = addons_dir / UBO
partial.mkdir(parents=True)
assert not (partial / "manifest.json").exists()
calls = []
def fake(url, extract_path, name):
calls.append(name)
_write_manifest(url, extract_path, name)
monkeypatch.setattr(addons_mod, "download_and_extract", fake)
out = []
maybe_download_addons([DefaultAddons.UBO], out)
# An empty dir must trigger a re-download, not be trusted.
assert calls == [UBO]
assert (partial / "manifest.json").exists()
assert out == [str(partial)]
def test_extracted_addon_is_not_redownloaded(addons_dir, monkeypatch):
path = addons_dir / UBO
path.mkdir(parents=True)
(path / "manifest.json").write_text("{}")
def boom(*a, **k):
raise AssertionError("must not re-download an already-extracted addon")
monkeypatch.setattr(addons_mod, "download_and_extract", boom)
out = []
maybe_download_addons([DefaultAddons.UBO], out)
assert out == [str(path)]
def test_failed_download_removes_partial_dir(addons_dir, monkeypatch):
path = addons_dir / UBO
def fail(url, extract_path, name):
os.makedirs(extract_path, exist_ok=True) # partial write, then die
raise RuntimeError("network died mid-download")
monkeypatch.setattr(addons_mod, "download_and_extract", fail)
out = []
maybe_download_addons([DefaultAddons.UBO], out)
# The partial dir must be gone so the next run re-downloads instead of
# trusting an addon that has no manifest.json.
assert not path.exists()
assert out == []