droboticflow-new/cmd/api/main_test.go
d-robotics bccf5325eb 🐳 ship public/ assets in API image: vendor dir, diagnostics probe, immutable /vendor cache
- Dockerfile.api web-build never copied public/, so vite publicDir output
  (movi-player 0.4.1 vendor + diagnostics-probe.json) never reached the
  production image; local replica hid this because Vite dev serves the
  source tree directly
- spaHandler: /vendor/ joins the immutable-cache and miss-404 branches
  (version-pinned dirs; a failed module import must 404, not receive
  index.html as a confusing syntax error)
- application/wasm MIME comes from Go's builtin table (verified
  GOROOT/src/mime/type.go; container golang:1.24-bookworm)
- TestSPAHandlerServesVersionedVendorAssets: wasm MIME + immutable header,
  miss 404, probe not cached
- proposal-final.md D.9 records the gap and fix (release prep finding)
2026-10-11 20:43:09 +05:30

84 lines
3.5 KiB
Go

package main
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
)
func TestSPAHandlerDoesNotFallbackMissingAssetsToIndex(t *testing.T) {
distDir := t.TempDir()
if err := os.WriteFile(filepath.Join(distDir, "index.html"), []byte("<!doctype html><div id=\"root\"></div>"), 0o644); err != nil {
t.Fatalf("write index: %v", err)
}
handler := spaHandler(http.NotFoundHandler(), distDir)
assetRecorder := httptest.NewRecorder()
handler.ServeHTTP(assetRecorder, httptest.NewRequest(http.MethodGet, "/assets/missing.js", nil))
if assetRecorder.Code != http.StatusNotFound {
t.Fatalf("missing asset status = %d, want %d", assetRecorder.Code, http.StatusNotFound)
}
routeRecorder := httptest.NewRecorder()
handler.ServeHTTP(routeRecorder, httptest.NewRequest(http.MethodGet, "/monitoring", nil))
if routeRecorder.Code != http.StatusOK {
t.Fatalf("SPA route status = %d, want %d", routeRecorder.Code, http.StatusOK)
}
if routeRecorder.Header().Get("Cache-Control") != "no-store" {
t.Fatalf("SPA route cache control = %q, want no-store", routeRecorder.Header().Get("Cache-Control"))
}
rootRecorder := httptest.NewRecorder()
handler.ServeHTTP(rootRecorder, httptest.NewRequest(http.MethodGet, "/", nil))
if rootRecorder.Code != http.StatusOK {
t.Fatalf("SPA root status = %d, want %d", rootRecorder.Code, http.StatusOK)
}
if rootRecorder.Header().Get("Cache-Control") != "no-store" {
t.Fatalf("SPA root cache control = %q, want no-store", rootRecorder.Header().Get("Cache-Control"))
}
}
func TestSPAHandlerServesVersionedVendorAssets(t *testing.T) {
distDir := t.TempDir()
vendorDir := filepath.Join(distDir, "vendor", "movi-player-0.4.1")
if err := os.MkdirAll(vendorDir, 0o755); err != nil {
t.Fatalf("mkdir vendor: %v", err)
}
if err := os.WriteFile(filepath.Join(vendorDir, "movi.wasm"), []byte("\x00asm"), 0o644); err != nil {
t.Fatalf("write wasm: %v", err)
}
if err := os.WriteFile(filepath.Join(distDir, "diagnostics-probe.json"), []byte(`{"diagnosticProbe":"droboticflow-connectivity-v1"}`), 0o644); err != nil {
t.Fatalf("write probe: %v", err)
}
handler := spaHandler(http.NotFoundHandler(), distDir)
wasmRecorder := httptest.NewRecorder()
handler.ServeHTTP(wasmRecorder, httptest.NewRequest(http.MethodGet, "/vendor/movi-player-0.4.1/movi.wasm", nil))
if wasmRecorder.Code != http.StatusOK {
t.Fatalf("vendor wasm status = %d, want %d", wasmRecorder.Code, http.StatusOK)
}
if got := wasmRecorder.Header().Get("Content-Type"); got != "application/wasm" {
t.Fatalf("vendor wasm content type = %q, want application/wasm", got)
}
if got := wasmRecorder.Header().Get("Cache-Control"); got != "public, max-age=31536000, immutable" {
t.Fatalf("vendor wasm cache control = %q, want immutable", got)
}
missingRecorder := httptest.NewRecorder()
handler.ServeHTTP(missingRecorder, httptest.NewRequest(http.MethodGet, "/vendor/movi-player-9.9.9/element.slim.js", nil))
if missingRecorder.Code != http.StatusNotFound {
t.Fatalf("missing vendor status = %d, want %d (no SPA fallback for vendor paths)", missingRecorder.Code, http.StatusNotFound)
}
probeRecorder := httptest.NewRecorder()
handler.ServeHTTP(probeRecorder, httptest.NewRequest(http.MethodGet, "/diagnostics-probe.json", nil))
if probeRecorder.Code != http.StatusOK {
t.Fatalf("probe status = %d, want %d", probeRecorder.Code, http.StatusOK)
}
if got := probeRecorder.Header().Get("Cache-Control"); got == "public, max-age=31536000, immutable" {
t.Fatalf("probe cache control = %q, must not be immutable", got)
}
}