|
| 1 | +# ABOUTME: Tests for AssetResolver — manifest caching, mtime invalidation, fallbacks. |
| 2 | +# ABOUTME: Verifies templates aren't re-parsing manifest.json on every asset() call. |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import json |
| 7 | +import os |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +from gitcabin.web.assets import AssetResolver |
| 11 | + |
| 12 | + |
| 13 | +def test_resolves_logical_name_via_manifest(tmp_path: Path) -> None: |
| 14 | + dist = tmp_path / "dist" |
| 15 | + dist.mkdir() |
| 16 | + (dist / "manifest.json").write_text(json.dumps({"main.css": "main.aaa.css"})) |
| 17 | + |
| 18 | + resolver = AssetResolver(dist_dir=dist) |
| 19 | + assert resolver("main.css") == "/static/dist/main.aaa.css" |
| 20 | + |
| 21 | + |
| 22 | +def test_falls_back_to_bare_name_when_manifest_missing(tmp_path: Path) -> None: |
| 23 | + # No manifest file written — resolver should still emit a usable URL so |
| 24 | + # unhashed legacy assets keep working. |
| 25 | + dist = tmp_path / "dist" |
| 26 | + dist.mkdir() |
| 27 | + |
| 28 | + resolver = AssetResolver(dist_dir=dist) |
| 29 | + assert resolver("legacy.css") == "/static/dist/legacy.css" |
| 30 | + |
| 31 | + |
| 32 | +def test_falls_back_to_bare_name_when_key_absent(tmp_path: Path) -> None: |
| 33 | + dist = tmp_path / "dist" |
| 34 | + dist.mkdir() |
| 35 | + (dist / "manifest.json").write_text(json.dumps({"main.css": "main.aaa.css"})) |
| 36 | + |
| 37 | + resolver = AssetResolver(dist_dir=dist) |
| 38 | + assert resolver("unknown.js") == "/static/dist/unknown.js" |
| 39 | + |
| 40 | + |
| 41 | +def test_caches_manifest_until_mtime_changes(tmp_path: Path) -> None: |
| 42 | + """A rebuilt manifest should be picked up; an unchanged one stays cached.""" |
| 43 | + dist = tmp_path / "dist" |
| 44 | + dist.mkdir() |
| 45 | + manifest = dist / "manifest.json" |
| 46 | + manifest.write_text(json.dumps({"main.css": "main.aaa.css"})) |
| 47 | + |
| 48 | + resolver = AssetResolver(dist_dir=dist) |
| 49 | + assert resolver("main.css") == "/static/dist/main.aaa.css" |
| 50 | + |
| 51 | + # Rewrite with new content and bump mtime — touch alone may collide with |
| 52 | + # the previous mtime on filesystems with coarse timestamp resolution. |
| 53 | + manifest.write_text(json.dumps({"main.css": "main.bbb.css"})) |
| 54 | + new_mtime = manifest.stat().st_mtime + 1 |
| 55 | + os.utime(manifest, (new_mtime, new_mtime)) |
| 56 | + |
| 57 | + assert resolver("main.css") == "/static/dist/main.bbb.css" |
| 58 | + |
| 59 | + |
| 60 | +def test_cache_recovers_after_manifest_disappears(tmp_path: Path) -> None: |
| 61 | + # If the bundler wipes dist/ mid-run, the resolver should fall back to |
| 62 | + # bare names rather than serving a stale manifest forever. |
| 63 | + dist = tmp_path / "dist" |
| 64 | + dist.mkdir() |
| 65 | + manifest = dist / "manifest.json" |
| 66 | + manifest.write_text(json.dumps({"main.css": "main.aaa.css"})) |
| 67 | + |
| 68 | + resolver = AssetResolver(dist_dir=dist) |
| 69 | + assert resolver("main.css") == "/static/dist/main.aaa.css" |
| 70 | + |
| 71 | + manifest.unlink() |
| 72 | + assert resolver("main.css") == "/static/dist/main.css" |
| 73 | + |
| 74 | + # Restoring the manifest should rebuild the cache. |
| 75 | + manifest.write_text(json.dumps({"main.css": "main.ccc.css"})) |
| 76 | + assert resolver("main.css") == "/static/dist/main.ccc.css" |
0 commit comments