"""The app shell must request scripts and stylesheets under a version that follows their content.""" import os import tempfile import time import unittest from pathlib import Path os.environ.setdefault("CONFIG_DIR", tempfile.mkdtemp(prefix="ttvc-assetv-")) class AssetBuildIdTests(unittest.TestCase): def test_the_index_rewrites_every_static_asset_version(self): import asyncio from routes.admin import asset_build_id, index response = asyncio.run(index()) html = response.body.decode("utf-8") build = asset_build_id() self.assertIn(f"/static/style.css?v={build}", html) self.assertIn(f"/static/loader.js?v={build}", html) self.assertNotIn("?v=1.53.4", html) self.assertEqual(response.headers["cache-control"], "no-store, max-age=0") def test_the_build_id_changes_when_a_script_changes_and_not_otherwise(self): from core.constants import STATIC_DIR from routes.admin import asset_build_id first = asset_build_id() self.assertEqual(first, asset_build_id()) probe = Path(STATIC_DIR) / "js" / "magazine.js" original = probe.stat().st_mtime try: os.utime(probe, (original + 1_000_000, original + 1_000_000)) self.assertNotEqual(first, asset_build_id()) finally: os.utime(probe, (original, original)) self.assertEqual(first, asset_build_id()) if __name__ == "__main__": unittest.main()