Add better no-frontend-dir handling (and lint, whoops)

This commit is contained in:
Jack Kingsman
2026-02-10 20:41:56 -08:00
parent a157390fb7
commit fa37ff6286
9 changed files with 180 additions and 57 deletions
+4 -1
View File
@@ -187,7 +187,10 @@ class TestMessagesEndpoint:
with (
patch("app.dependencies.radio_manager") as mock_rm,
patch("app.repository.ChannelRepository") as mock_chan_repo,
patch("app.repository.AppSettingsRepository.get", new=AsyncMock(return_value=AppSettings())),
patch(
"app.repository.AppSettingsRepository.get",
new=AsyncMock(return_value=AppSettings()),
),
patch("app.routers.messages.MessageRepository") as mock_msg_repo,
):
mock_rm.is_connected = True
+66
View File
@@ -0,0 +1,66 @@
import logging
from fastapi import FastAPI
from fastapi.testclient import TestClient
from app.frontend_static import register_frontend_static_routes
def test_missing_dist_logs_error_and_keeps_app_running(tmp_path, caplog):
app = FastAPI()
missing_dist = tmp_path / "frontend" / "dist"
with caplog.at_level(logging.ERROR):
registered = register_frontend_static_routes(app, missing_dist)
assert registered is False
assert "Frontend build directory not found" in caplog.text
with TestClient(app) as client:
# App still runs; no frontend route is registered.
assert client.get("/").status_code == 404
def test_missing_index_logs_error_and_skips_frontend_routes(tmp_path, caplog):
app = FastAPI()
dist_dir = tmp_path / "frontend" / "dist"
dist_dir.mkdir(parents=True)
with caplog.at_level(logging.ERROR):
registered = register_frontend_static_routes(app, dist_dir)
assert registered is False
assert "Frontend index file not found" in caplog.text
def test_valid_dist_serves_static_and_spa_fallback(tmp_path):
app = FastAPI()
dist_dir = tmp_path / "frontend" / "dist"
assets_dir = dist_dir / "assets"
dist_dir.mkdir(parents=True)
assets_dir.mkdir(parents=True)
index_file = dist_dir / "index.html"
index_file.write_text("<html><body>index page</body></html>")
(dist_dir / "robots.txt").write_text("User-agent: *")
(assets_dir / "app.js").write_text("console.log('ok');")
registered = register_frontend_static_routes(app, dist_dir)
assert registered is True
with TestClient(app) as client:
root_response = client.get("/")
assert root_response.status_code == 200
assert "index page" in root_response.text
file_response = client.get("/robots.txt")
assert file_response.status_code == 200
assert file_response.text == "User-agent: *"
missing_response = client.get("/channel/some-route")
assert missing_response.status_code == 200
assert "index page" in missing_response.text
asset_response = client.get("/assets/app.js")
assert asset_response.status_code == 200
assert "console.log('ok');" in asset_response.text