From f03db2888f86f7ce30ac8cc459481b745deed13f Mon Sep 17 00:00:00 2001 From: mkelvers Date: Mon, 6 Jul 2026 22:10:29 +0200 Subject: [PATCH] test: add test for simulcast deferred content loading --- internal/anime/catalog_handler_test.go | 64 ++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 internal/anime/catalog_handler_test.go diff --git a/internal/anime/catalog_handler_test.go b/internal/anime/catalog_handler_test.go new file mode 100644 index 00000000..9efa6121 --- /dev/null +++ b/internal/anime/catalog_handler_test.go @@ -0,0 +1,64 @@ +package anime + +import ( + "context" + "mal/integrations/playback/allanime" + "mal/templates" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/gin-gonic/gin" +) + +type countingSeasonalProvider struct { + calls int +} + +func (p *countingSeasonalProvider) SeasonalShows(context.Context, string, int) ([]allanime.ProviderShow, error) { + p.calls++ + return nil, nil +} + +func TestSimulcastPageDefersProviderFetch(t *testing.T) { + gin.SetMode(gin.TestMode) + provider := &countingSeasonalProvider{} + handler := NewAnimeHandler(nil, nil, nil, newSeasonDiscoveryService(provider)) + renderer, err := templates.ProvideRenderer() + if err != nil { + t.Fatalf("ProvideRenderer: %v", err) + } + + router := gin.New() + router.HTMLRender = renderer + router.GET("/simulcast", handler.HandleSimulcast) + router.GET("/api/simulcast", handler.HandleSimulcastContent) + + page := httptest.NewRecorder() + router.ServeHTTP(page, httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/simulcast?season=winter&year=2024", nil)) + if page.Code != http.StatusOK { + t.Fatalf("page status = %d, want %d", page.Code, http.StatusOK) + } + if provider.calls != 0 { + t.Fatalf("provider calls during page render = %d, want 0", provider.calls) + } + if !strings.Contains(page.Body.String(), `hx-get="/api/simulcast?season=winter&year=2024"`) { + t.Fatalf("page did not preserve the selected season in the deferred request:\n%s", page.Body.String()) + } + + fragment := httptest.NewRecorder() + router.ServeHTTP(fragment, httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/api/simulcast?season=winter&year=2024", nil)) + if fragment.Code != http.StatusOK { + t.Fatalf("fragment status = %d, want %d", fragment.Code, http.StatusOK) + } + if provider.calls != 2 { + t.Fatalf("provider calls after fragment render = %d, want 2", provider.calls) + } + if strings.Contains(fragment.Body.String(), "") { + t.Fatal("fragment response unexpectedly rendered the full page") + } + if !strings.Contains(fragment.Body.String(), `id="simulcast-content"`) { + t.Fatalf("fragment response is missing its replacement root:\n%s", fragment.Body.String()) + } +}