diff --git a/fastapi_startkit/tests/vite/__init__.py b/fastapi_startkit/tests/vite/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/fastapi_startkit/tests/vite/test_vite.py b/fastapi_startkit/tests/vite/test_vite.py new file mode 100644 index 00000000..b7cb6a11 --- /dev/null +++ b/fastapi_startkit/tests/vite/test_vite.py @@ -0,0 +1,300 @@ +import json + +import pytest + +from fastapi_startkit.vite.exceptions import ViteException, ViteManifestNotFoundException +from fastapi_startkit.vite.vite import Vite + + +# --------------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------------- + + +def make_vite(tmp_path, build_dir="build", manifest=None, hot_content=None, asset_url=""): + """Create a Vite instance with an optional manifest.json and/or hot file.""" + public = tmp_path / "public" + public.mkdir() + + if manifest is not None: + build = public / build_dir + build.mkdir(parents=True) + (build / "manifest.json").write_text(json.dumps(manifest)) + + if hot_content is not None: + (public / "hot").write_text(hot_content) + + # Clear class-level manifest cache between tests + Vite._manifests.clear() + + return Vite(public_path=str(public), build_directory=build_dir, asset_url=asset_url) + + +SIMPLE_MANIFEST = { + "resources/js/app.js": { + "file": "assets/app-abc123.js", + "src": "resources/js/app.js", + "isEntry": True, + } +} + +MANIFEST_WITH_CSS = { + "resources/js/app.js": { + "file": "assets/app-abc123.js", + "src": "resources/js/app.js", + "isEntry": True, + "css": ["assets/app-def456.css"], + } +} + + +# --------------------------------------------------------------------------- +# Production mode (no hot file) +# --------------------------------------------------------------------------- + + +class TestProductionMode: + def test_is_not_running_hot_without_hot_file(self, tmp_path): + vite = make_vite(tmp_path, manifest=SIMPLE_MANIFEST) + assert vite.is_running_hot() is False + + def test_asset_url_contains_hashed_filename(self, tmp_path): + vite = make_vite(tmp_path, manifest=SIMPLE_MANIFEST) + result = vite("resources/js/app.js") + assert "app-abc123.js" in result + + def test_script_tag_generated(self, tmp_path): + vite = make_vite(tmp_path, manifest=SIMPLE_MANIFEST) + result = vite("resources/js/app.js") + assert "