package httpapi import ( "knowledge-governance/internal/qaopsai" "net/http" "net/http/httptest" "strings" "testing" ) func TestAIUnavailableDoesNotFalselyClaimNoModelCallOccurred(t *testing.T) { response := httptest.NewRecorder() qaAIError(response, qaopsai.ErrUnavailable) if strings.Contains(response.Body.String(), "未调用模型") { t.Fatal("a persistence failure after a model call was reported as no call") } } func TestAIEndpointsRequireAdminAndExplicitSameOriginWriteIntent(t *testing.T) { auth := newFakeAuthStore() auth.sessions["fixture-ai-admin"] = "usr-boss" auth.sessions["fixture-ai-viewer"] = "usr-eye" handler, err := NewHandler(Config{Store: &fakeJobStore{}, Auth: &AuthConfig{Store: auth}}) if err != nil { t.Fatal(err) } for _, route := range []struct{ method, path, body string }{{"GET", "/api/v1/qa-ops/ai/settings", ""}, {"GET", "/api/v1/qa-ops/ai/status", ""}, {"POST", "/api/v1/qa-ops/ai/settings", `{}`}, {"POST", "/api/v1/qa-ops/ai/settings/test", `{}`}, {"POST", "/api/v1/qa-ops/ai/previews", `{}`}, {"POST", "/api/v1/qa-ops/ai/batches", `{}`}, {"POST", "/api/v1/qa-ops/ai/automation", `{}`}, {"POST", "/api/v1/qa-ops/ai/batches/fixture/actions", `{}`}} { for _, who := range []struct { token string want int }{{"", 401}, {"fixture-ai-viewer", 403}, {"fixture-ai-admin", 503}} { request := httptest.NewRequest(route.method, route.path, strings.NewReader(route.body)) if who.token != "" { request.AddCookie(&http.Cookie{Name: sessionCookieName, Value: who.token}) } request.Header.Set("X-Governance-Action", "qa-ai") response := httptest.NewRecorder() handler.ServeHTTP(response, request) if response.Code != who.want { t.Fatalf("%s %s role=%s status=%d want=%d", route.method, route.path, who.token, response.Code, who.want) } } if route.method == "POST" { request := httptest.NewRequest(route.method, route.path, strings.NewReader(route.body)) request.AddCookie(&http.Cookie{Name: sessionCookieName, Value: "fixture-ai-admin"}) response := httptest.NewRecorder() handler.ServeHTTP(response, request) if response.Code != 403 { t.Fatal("write without explicit action was accepted") } } } }