import re from fastapi.testclient import TestClient import main def test_assets_are_versioned_and_the_page_is_not_cached(): # Not a context manager: that would run the lifespan, which seeds months of # history from Yahoo before serving anything. response = TestClient(main.app).get("/") assert response.headers["cache-control"] == "no-store" refs = re.findall(r'(?:src|href)="(/static/[^"]+)"', response.text) assert refs, "no static assets referenced" # Without this a tab left open keeps running the JavaScript it first loaded, # which made a fixed bug look like it was still broken. assert all("?v=" in ref for ref in refs), refs def test_version_tracks_content_not_timestamps(tmp_path, monkeypatch): asset = tmp_path / "app.js" asset.write_text("//", encoding="utf-8") monkeypatch.setattr(main, "STATIC_DIR", tmp_path) first = main.asset_version() asset.touch() # A deploy checks every file out fresh; unchanged assets must stay cached. assert main.asset_version() == first asset.write_text("// changed", encoding="utf-8") assert main.asset_version() != first