Option C Tools
Loading...
Searching...
No Matches
test_index_html_offline.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# tests/tests_assets/test_index_html_offline.py
4
5"""
6Purpose
7-------
8OI-532 regression — ``assets/index.html`` must render without reaching
9out to ``fonts.googleapis.com`` or ``fonts.gstatic.com``. Web fonts are
10self-hosted under ``assets/fonts/`` and referenced via inline
11``@font-face`` declarations.
12
13Responsibilities
14----------------
15- No ``fonts.googleapis.com`` / ``fonts.gstatic.com`` hosts appear.
16- Every ``@font-face`` ``src: url(...)`` points at a relative path
17 rooted in ``fonts/``, and the target ``.woff2`` file exists on disk.
18
19Diagnostics
20-----------
21Domain: ASSETS-TESTS
22Levels:
23 L2 — test lifecycle
24 L3 — assertion details
25 L4 — deep tracing
26
27Contracts
28---------
29- Tests read ``assets/index.html`` from the repository root; no temp
30 files are created.
31- All assertions are read-only checks against existing project assets.
32"""
33
34from __future__ import annotations
35
36import re
37from pathlib import Path
38
39
40REPO_ROOT = Path(__file__).resolve().parents[3]
41INDEX_HTML = REPO_ROOT / "assets" / "index.html"
42FONTS_DIR = REPO_ROOT / "assets" / "fonts"
43
44
45def _index_text() -> str:
46 return INDEX_HTML.read_text(encoding="utf-8")
47
48
50 text = _index_text()
51 assert "fonts.googleapis.com" not in text, (
52 "assets/index.html must not reference fonts.googleapis.com "
53 "(OI-532: fonts are self-hosted under assets/fonts/)"
54 )
55 assert "fonts.gstatic.com" not in text, (
56 "assets/index.html must not reference fonts.gstatic.com "
57 "(OI-532: fonts are self-hosted under assets/fonts/)"
58 )
59
60
62 text = _index_text()
63 srcs = re.findall(r"src:\s*url\‍(['\"]?([^'\")]+)['\"]?\‍)", text)
64 assert srcs, "expected at least one @font-face src: url(...) declaration"
65 for src in srcs:
66 assert src.startswith("fonts/"), (
67 f"@font-face src {src!r} must be rooted in fonts/ (relative)"
68 )
69 assert src.endswith(".woff2"), (
70 f"@font-face src {src!r} must be a .woff2 file"
71 )
72 target = FONTS_DIR / Path(src).name
73 assert target.is_file(), f"missing vendored font file: {target}"