"""HTML and executable assets must come from one immutable UI release.""" import hashlib import re from fastapi.testclient import TestClient from service.api import create_app from test_service import FakeEngine def make_static(tmp_path): static = tmp_path / 'static' static.mkdir() (static / 'index.html').write_text('
' '') (static / 'styles.css').write_text('body{color:green}') (static / 'app.js').write_text('window.uiRevision="first";') return static def asset_url(html, filename): return re.search(r'(/assets/[0-9a-f]{64}/' + re.escape(filename) + r')', html).group(1) def test_html_is_not_cached_and_references_fingerprinted_assets(tmp_path): static = make_static(tmp_path) app = create_app(data_root=tmp_path / 'uploads', static_root=static, engine=FakeEngine()) with TestClient(app) as client: page = client.get('/') assert page.status_code == 200 assert page.headers['cache-control'] == 'no-store, max-age=0' assert page.headers['pragma'] == 'no-cache' and page.headers['expires'] == '0' assert page.headers['x-ui-release'] in page.text assert 'src="/static/app.js"' not in page.text assert 'href="/static/styles.css' not in page.text for filename in ('app.js', 'styles.css'): response = client.get(asset_url(page.text, filename)) original = (static / filename).read_bytes() assert response.content == original assert response.headers['cache-control'] == 'public, max-age=31536000, immutable' assert hashlib.sha256(original).hexdigest() in response.headers['etag'] head = client.head('/') assert head.status_code == 200 and head.content == b'' assert head.headers['content-length'] == page.headers['content-length'] def test_release_snapshot_cannot_mix_old_html_with_new_javascript(tmp_path): static = make_static(tmp_path) first = create_app(data_root=tmp_path / 'first', static_root=static, engine=FakeEngine()) with TestClient(first) as old_client: old_page = old_client.get('/') old_url = asset_url(old_page.text, 'app.js') old_content = old_client.get(old_url).content (static / 'app.js').write_text('window.uiRevision="second";window.newPanel=true;') (static / 'index.html').write_text((static / 'index.html').read_text().replace('', '')) assert old_client.get('/').text == old_page.text assert old_client.get(old_url).content == old_content second = create_app(data_root=tmp_path / 'second', static_root=static, engine=FakeEngine()) with TestClient(second) as new_client: new_page = new_client.get('/') new_url = asset_url(new_page.text, 'app.js') assert new_url != old_url assert new_page.headers['x-ui-release'] != old_page.headers['x-ui-release'] assert 'id="new-panel"' in new_page.text assert new_client.get(new_url).content == (static / 'app.js').read_bytes() mismatch = new_client.get(old_url) assert mismatch.status_code == 404 and 'no-store' in mismatch.headers['cache-control'] assert 'immutable' not in mismatch.headers['cache-control'] def test_legacy_assets_revalidate_and_only_exact_version_can_be_immutable(tmp_path): static = make_static(tmp_path) app = create_app(data_root=tmp_path / 'uploads', static_root=static, engine=FakeEngine()) with TestClient(app) as client: legacy = client.get('/static/app.js') assert legacy.headers['cache-control'] == 'no-cache, must-revalidate' assert 'etag' in legacy.headers and 'last-modified' in legacy.headers assert client.get('/static/app.js', headers={'If-None-Match': legacy.headers['etag']}).status_code == 304 versioned_url = asset_url(client.get('/').text, 'app.js') versioned = client.get(versioned_url) not_modified = client.get(versioned_url, headers={'If-None-Match': versioned.headers['etag']}) assert not_modified.status_code == 304 and not_modified.content == b'' head = client.head(versioned_url) assert head.status_code == 200 and head.content == b'' assert head.headers['content-length'] == str(len(versioned.content)) assert client.get('/assets/' + '0' * 64 + '/app.js').status_code == 404 assert client.get('/api/status').headers['cache-control'] == 'no-store'