Files
ai_academy/internal/handler/initialization_chat_check_test.go
T
runny 9058149576
Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
refactor: 项目更名 WeKnora → Academy
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
2026-11-13 20:52:51 +08:00

93 lines
3.4 KiB
Go

package handler
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"academy/internal/types"
"academy/internal/utils"
"github.com/stretchr/testify/require"
)
// fakeChatGateway answers every chat completion with the given status and body.
func fakeChatGateway(t *testing.T, status int, body string) *httptest.Server {
t.Helper()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_, _ = w.Write([]byte(body))
}))
t.Cleanup(srv.Close)
// The test server is a bare loopback IP; let the SSRF guard through.
utils.SetSSRFWhitelistFromRaw("127.0.0.1")
t.Cleanup(func() { utils.SetSSRFWhitelistFromRaw("") })
return srv
}
func remoteChatModel(baseURL string) *types.Model {
return &types.Model{
Name: "gpt-5.6-luna",
Type: types.ModelTypeKnowledgeQA,
Source: types.ModelSourceRemote,
Parameters: types.ModelParameters{
BaseURL: baseURL,
APIKey: "sk-test",
},
}
}
// A reasoning model behind LiteLLM cannot finish within the 1-token probe and
// the gateway answers 400. The endpoint is reachable and the key accepted, so
// the check must pass, as it did before the HTTP client changed its wording.
func TestCheckChatModelConnectionTreats400AsReachable(t *testing.T) {
srv := fakeChatGateway(t, http.StatusBadRequest,
`{"error":{"message":"litellm.BadRequestError: OpenAIException - Could not finish the message `+
`because max_tokens or model output limit was reached.","type":"invalid_request_error","code":"400"}}`)
h := &InitializationHandler{}
available, message := h.checkChatModelConnection(context.Background(), remoteChatModel(srv.URL), "", "")
require.True(t, available, "message: %s", message)
}
func TestCheckChatModelConnectionReportsAuthFailure(t *testing.T) {
srv := fakeChatGateway(t, http.StatusUnauthorized,
`{"error":{"message":"Invalid API key","type":"invalid_request_error","code":"401"}}`)
h := &InitializationHandler{}
available, message := h.checkChatModelConnection(context.Background(), remoteChatModel(srv.URL), "", "")
require.False(t, available)
require.Contains(t, message, "认证失败")
}
// Gemini's native API reports an invalid key as 400 INVALID_ARGUMENT /
// API_KEY_INVALID. That 400 is an auth failure, not "reachable".
func TestCheckChatModelConnectionReportsGeminiInvalidKey(t *testing.T) {
var gotPath string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{"error":{"code":400,"message":"API key not valid. Please pass a valid API key.",` +
`"status":"INVALID_ARGUMENT","details":[{"@type":"type.googleapis.com/google.rpc.ErrorInfo",` +
`"reason":"API_KEY_INVALID","domain":"googleapis.com"}]}}`))
}))
t.Cleanup(srv.Close)
utils.SetSSRFWhitelistFromRaw("127.0.0.1")
t.Cleanup(func() { utils.SetSSRFWhitelistFromRaw("") })
model := remoteChatModel(srv.URL)
model.Name = "gemini-2.5-flash"
model.Parameters.Provider = "gemini"
h := &InitializationHandler{}
available, message := h.checkChatModelConnection(context.Background(), model, "", "")
require.Contains(t, gotPath, ":generateContent", "probe should use the native Gemini API")
require.False(t, available, "message: %s", message)
require.Contains(t, message, "认证失败")
}