from __future__ import annotations from pathlib import Path from fastapi import FastAPI from fastapi.testclient import TestClient from app.main import _FrontendStaticFiles def _client(tmp_path: Path) -> TestClient: static = tmp_path / "static" (static / "assets").mkdir(parents=True) (static / "index.html").write_text("
", encoding="utf-8") (static / "assets" / "index-abc123.js").write_text("console.log(1)", encoding="utf-8") (static / "favicon.svg").write_text("", encoding="utf-8") app = FastAPI() @app.get("/api/v1/ping") def ping() -> dict[str, str]: return {"status": "ok"} app.mount("/", _FrontendStaticFiles(directory=static, html=True), name="frontend") return TestClient(app) def test_client_routes_fall_back_to_the_spa_shell(tmp_path: Path) -> None: client = _client(tmp_path) for path in ("/repositories", "/activity", "/queue", "/settings", "/repositories/gitea%3A64/findings"): response = client.get(path) assert response.status_code == 200, path assert "id=root" in response.text assert response.headers["cache-control"] == "no-cache" def test_api_assets_and_missing_files_keep_an_honest_404(tmp_path: Path) -> None: client = _client(tmp_path) assert client.get("/api/v1/ping").json() == {"status": "ok"} assert client.get("/api/v1/unknown").status_code == 404 assert client.get("/assets/missing-chunk.js").status_code == 404 assert client.get("/robots.txt").status_code == 404 assert client.post("/repositories").status_code in {404, 405} def test_hashed_assets_stay_immutable_and_files_are_served_directly(tmp_path: Path) -> None: client = _client(tmp_path) asset = client.get("/assets/index-abc123.js") assert asset.status_code == 200 assert asset.headers["cache-control"] == "public, max-age=31536000, immutable" assert client.get("/favicon.svg").headers["cache-control"] == "no-cache" assert client.get("/").headers["cache-control"] == "no-cache"