package handler import ( "encoding/json" "net/http" "net/http/httptest" "testing" ) func TestHealth(t *testing.T) { request := httptest.NewRequest(http.MethodGet, "/health", nil) response := httptest.NewRecorder() NewRouter().ServeHTTP(response, request) if response.Code != http.StatusOK { t.Fatalf("status = %d, want %d", response.Code, http.StatusOK) } if got := response.Header().Get("Content-Type"); got != "application/json; charset=utf-8" { t.Fatalf("Content-Type = %q, want application/json; charset=utf-8", got) } var body healthResponse if err := json.NewDecoder(response.Body).Decode(&body); err != nil { t.Fatalf("decode response: %v", err) } if body.Status != "ok" { t.Fatalf("status body = %q, want ok", body.Status) } } func TestHealthRejectsOtherMethods(t *testing.T) { request := httptest.NewRequest(http.MethodPost, "/health", nil) response := httptest.NewRecorder() NewRouter().ServeHTTP(response, request) if response.Code != http.StatusMethodNotAllowed { t.Fatalf("status = %d, want %d", response.Code, http.StatusMethodNotAllowed) } } func TestRouterDoesNotExposeMCPWithoutConfiguredHandler(t *testing.T) { request := httptest.NewRequest(http.MethodPost, "/mcp", nil) response := httptest.NewRecorder() NewRouter().ServeHTTP(response, request) if response.Code != http.StatusNotFound { t.Fatalf("status = %d, want %d", response.Code, http.StatusNotFound) } } func TestRouterRegistersConfiguredMCPHandler(t *testing.T) { request := httptest.NewRequest(http.MethodPost, "/mcp", nil) response := httptest.NewRecorder() mcp := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusAccepted) }) NewRouter(RouterOptions{MCP: mcp}).ServeHTTP(response, request) if response.Code != http.StatusAccepted { t.Fatalf("status = %d, want %d", response.Code, http.StatusAccepted) } } func TestRouterRegistersConfiguredDashScopeChatHandler(t *testing.T) { request := httptest.NewRequest(http.MethodPost, "/api/v1/apps/test-app/completion", nil) response := httptest.NewRecorder() chat := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.PathValue("app_id") != "test-app" { t.Fatalf("app_id = %q", r.PathValue("app_id")) } w.WriteHeader(http.StatusAccepted) }) NewRouter(RouterOptions{DashScopeChat: chat}).ServeHTTP(response, request) if response.Code != http.StatusAccepted { t.Fatalf("status = %d, want %d", response.Code, http.StatusAccepted) } } func TestRouterDoesNotExposeChatPageWithoutConfiguredHandler(t *testing.T) { for _, path := range []string{"/chat", "/chat/", "/chat/app.js", "/chat/app.css"} { request := httptest.NewRequest(http.MethodGet, path, nil) response := httptest.NewRecorder() NewRouter().ServeHTTP(response, request) if response.Code != http.StatusNotFound { t.Fatalf("%s status = %d, want %d", path, response.Code, http.StatusNotFound) } } } func TestRouterRegistersConfiguredChatPageHandler(t *testing.T) { page, err := NewChatPageHandler(ChatPageOptions{AppID: "fire-safety-app"}) if err != nil { t.Fatalf("NewChatPageHandler() error = %v", err) } request := httptest.NewRequest(http.MethodGet, "/chat/", nil) response := httptest.NewRecorder() NewRouter(RouterOptions{ChatPage: page}).ServeHTTP(response, request) if response.Code != http.StatusOK { t.Fatalf("/chat/ status = %d, want %d", response.Code, http.StatusOK) } request = httptest.NewRequest(http.MethodGet, "/chat", nil) response = httptest.NewRecorder() NewRouter(RouterOptions{ChatPage: page}).ServeHTTP(response, request) if response.Code != http.StatusPermanentRedirect || response.Header().Get("Location") != "/chat/" { t.Fatalf("/chat status = %d location = %q, want redirect to /chat/", response.Code, response.Header().Get("Location")) } request = httptest.NewRequest(http.MethodGet, "/chat/app.js", nil) response = httptest.NewRecorder() NewRouter(RouterOptions{ChatPage: page}).ServeHTTP(response, request) if response.Code != http.StatusOK || response.Header().Get("Content-Type") != "text/javascript; charset=utf-8" { t.Fatalf("/chat/app.js status = %d content-type = %q, want JavaScript 200", response.Code, response.Header().Get("Content-Type")) } }