DecisionLab / tests /test_assets.py
RealFalconsAI's picture
Upload 39 files
66ee87e verified
Raw History Blame Contribute Delete
1.61 kB
"""Every release gets its own asset URLs, so a browser can never run a stale app.js or app.css from an older release."""
import unittest
from pathlib import Path
from app.assets import stamp_assets
INDEX = Path(__file__).resolve().parent.parent / "app" / "static" / "index.html"
class StampAssetsTest(unittest.TestCase):
def test_script_and_stylesheet_get_the_version(self):
html = '<link rel="stylesheet" href="/static/app.css"><script src="/static/app.js"></script>'
self.assertEqual(stamp_assets(html, "1.9.2"),
'<link rel="stylesheet" href="/static/app.css?v=1.9.2"><script src="/static/app.js?v=1.9.2"></script>')
def test_images_and_other_urls_get_it_too(self):
self.assertEqual(stamp_assets('<img src="/static/logo.png">', "2"), '<img src="/static/logo.png?v=2">')
def test_links_to_other_sites_are_untouched(self):
html = '<a href="https://huggingface.co/x">x</a><a href="#setup">s</a>'
self.assertEqual(stamp_assets(html, "2"), html)
def test_version_is_url_safe(self):
self.assertEqual(stamp_assets('<script src="/static/app.js"></script>', "1.9.2 beta/1"),
'<script src="/static/app.js?v=1.9.2-beta-1"></script>')
def test_the_real_page_references_only_stamped_assets(self):
stamped = stamp_assets(INDEX.read_text(encoding="utf-8"), "9.9.9")
self.assertIn('/static/app.js?v=9.9.9"', stamped)
self.assertIn('/static/app.css?v=9.9.9"', stamped)
self.assertNotRegex(stamped, r'/static/[^"?]+"')
if __name__ == "__main__":
unittest.main()