mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-18 23:01:23 +00:00
背景:gpt-5.3-codex-spark 使用独立于 codex 全局(5h/7d)的配额窗口(数据源是 /wham/usage 响应体的 codex_bengalfox,而非 codex 全局用的 x-codex-* 响应头),且 只能挂在已完成 OAuth 授权的 OpenAI 账号下复用其登录态,不能作为独立账号单独接入。 为此新增“链接型影子账号”(spark shadow account):影子账号本身不持有任何凭据, 通过 parent_account_id 指向母账号,凭据/token/代理透传自母账号并共享母账号的刷新 周期,仅在配额维度(quota_dimension=spark)和用量窗口上与母账号完全独立调度、互不 连坐。 实现: - 数据模型:migration 154(+154a)给 accounts 表加 parent_account_id / quota_dimension 列 + 4 条约束(维度合法 / parent⟺非 global 维度一致 / 禁自指 / FK)+ 2 个 CONCURRENTLY 索引(母账号索引 + 每母账号至多一个影子的唯一索引)。 - 创建:POST /api/v1/admin/accounts/:id/shadow(CreateShadow)—— 一母一影(唯一 索引兜底并发竞态),继承母账号 proxy/分组/并发/优先级(显式传参可覆盖),默认 model_mapping 恒等映射到 spark(拒绝非 spark 模型),母账号必须是真实的 OpenAI OAuth 账号(非影子)。 - 凭据透传:resolveCredentialAccount 把影子解析回母账号,GetAccessToken / 请求头 / WS 三条路径统一走此函数;影子自身 Credentials 恒为空(仅允许写 model_mapping), 凭据写入的汇聚点 persistAccountCredentials 对影子早返 no-op,防止误写。 - 调度:parentHealthyForShadow 只看母账号是否仍是 OpenAI OAuth + 凭据/传输是否 可用(active、token 未过期、未处于 401/刷新失败/传输故障导致的临时不可调度冷却), 刻意不看母账号的 global 限流窗口——两条 429 道互不连坐。 - 用量:影子的 codex_5h/7d 走 OpenAIQuotaService.QueryUsage(/wham/usage 的 codex_bengalfox),与母账号走的 WSv2 探测(/responses 头)完全独立的数据源、 刷新节流与 staleness 判定。 - 备份:ExportData 显式排除影子账号(影子不持凭据,通用凭据型导入强制 credentials 非空、无法表达父子链接),按 skipped_shadows 计数提示前端。 - 前端:账号操作菜单新增“创建 Spark 影子”入口,影子行展示回填的母账号信息 (邮箱 / plan / 隐私模式 / 订阅到期 / chatgpt_account_id),批量操作自动跳过 影子账号。 说明:migrations 目录用完整文件名(而非纯数字前缀)标识迁移,故本次新增的 154_account_spark_shadow.sql / 154a_..._notx.sql 与已有的 154_add_ops_system_logs_api_key_id.sql 按序号共存,与目录里 145/151 已有的 先例一致。 测试:新增约 20 个测试文件,覆盖 handler(CreateShadow 校验 / 母账号信息回填)、 repository(影子 round-trip / 一母一影唯一索引 / 迁移 schema)、service(凭据 透传三路径 / 调度母健康门 / 用量窗口来源与刷新节流 / CRS 母账号不变量 / 各类 早返与 fail-closed 场景)及前端组件(账号列表 / 操作菜单 / 用量重置)。 验证(镜像 CI;golangci-lint 首次全量分析耗时过长被跳过,其余全部实测): - gofmt -l:干净 - go build ./... / go vet ./...:通过 - go test ./... -count=1:全绿(全部包 ok,含 internal/service、 internal/repository、migrations) - go test -tags integration ./internal/repository/... ./internal/service/... (真实 Postgres,testcontainers):全绿,含迁移幂等性 (TestMigrationsRunner_IsIdempotent_AndSchemaIsUpToDate)与影子相关全部用例 - pnpm lint:check / pnpm typecheck / pnpm build(真实 vite 构建)/ pnpm vitest run:全绿(124 文件 760 用例) Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
541 lines
19 KiB
Go
541 lines
19 KiB
Go
//go:build unit
|
|
|
|
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/config"
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/openai_compat"
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/tlsfingerprint"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/require"
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
// --- shared test helpers ---
|
|
|
|
type queuedHTTPUpstream struct {
|
|
responses []*http.Response
|
|
requests []*http.Request
|
|
tlsFlags []bool
|
|
}
|
|
|
|
func (u *queuedHTTPUpstream) Do(_ *http.Request, _ string, _ int64, _ int) (*http.Response, error) {
|
|
return nil, fmt.Errorf("unexpected Do call")
|
|
}
|
|
|
|
func (u *queuedHTTPUpstream) DoWithTLS(req *http.Request, _ string, _ int64, _ int, profile *tlsfingerprint.Profile) (*http.Response, error) {
|
|
u.requests = append(u.requests, req)
|
|
u.tlsFlags = append(u.tlsFlags, profile != nil)
|
|
if len(u.responses) == 0 {
|
|
return nil, fmt.Errorf("no mocked response")
|
|
}
|
|
resp := u.responses[0]
|
|
u.responses = u.responses[1:]
|
|
return resp, nil
|
|
}
|
|
|
|
func newJSONResponse(status int, body string) *http.Response {
|
|
return &http.Response{
|
|
StatusCode: status,
|
|
Header: make(http.Header),
|
|
Body: io.NopCloser(strings.NewReader(body)),
|
|
}
|
|
}
|
|
|
|
// --- test functions ---
|
|
|
|
func newTestContext() (*gin.Context, *httptest.ResponseRecorder) {
|
|
gin.SetMode(gin.TestMode)
|
|
rec := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(rec)
|
|
c.Request = httptest.NewRequest(http.MethodPost, "/api/v1/admin/accounts/1/test", nil)
|
|
return c, rec
|
|
}
|
|
|
|
type openAIAccountTestRepo struct {
|
|
mockAccountRepoForGemini
|
|
updatedExtra map[string]any
|
|
bulkUpdatedIDs []int64
|
|
bulkUpdatedPayload AccountBulkUpdate
|
|
rateLimitedID int64
|
|
rateLimitedAt *time.Time
|
|
clearedErrorID int64
|
|
setErrorID int64
|
|
setErrorMsg string
|
|
}
|
|
|
|
func (r *openAIAccountTestRepo) UpdateExtra(_ context.Context, _ int64, updates map[string]any) error {
|
|
r.updatedExtra = updates
|
|
return nil
|
|
}
|
|
|
|
func (r *openAIAccountTestRepo) BulkUpdate(_ context.Context, ids []int64, updates AccountBulkUpdate) (int64, error) {
|
|
r.bulkUpdatedIDs = append([]int64(nil), ids...)
|
|
r.bulkUpdatedPayload = updates
|
|
return int64(len(ids)), nil
|
|
}
|
|
|
|
func (r *openAIAccountTestRepo) SetRateLimited(_ context.Context, id int64, resetAt time.Time) error {
|
|
r.rateLimitedID = id
|
|
r.rateLimitedAt = &resetAt
|
|
return nil
|
|
}
|
|
|
|
func (r *openAIAccountTestRepo) ClearError(_ context.Context, id int64) error {
|
|
r.clearedErrorID = id
|
|
return nil
|
|
}
|
|
|
|
func (r *openAIAccountTestRepo) SetError(_ context.Context, id int64, errorMsg string) error {
|
|
r.setErrorID = id
|
|
r.setErrorMsg = errorMsg
|
|
return nil
|
|
}
|
|
|
|
func TestAccountTestService_OpenAISuccessPersistsSnapshotFromHeaders(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
ctx, recorder := newTestContext()
|
|
|
|
resp := newJSONResponse(http.StatusOK, "")
|
|
resp.Body = io.NopCloser(strings.NewReader(`data: {"type":"response.completed"}
|
|
|
|
`))
|
|
resp.Header.Set("x-codex-primary-used-percent", "88")
|
|
resp.Header.Set("x-codex-primary-reset-after-seconds", "604800")
|
|
resp.Header.Set("x-codex-primary-window-minutes", "10080")
|
|
resp.Header.Set("x-codex-secondary-used-percent", "42")
|
|
resp.Header.Set("x-codex-secondary-reset-after-seconds", "18000")
|
|
resp.Header.Set("x-codex-secondary-window-minutes", "300")
|
|
|
|
repo := &openAIAccountTestRepo{}
|
|
upstream := &queuedHTTPUpstream{responses: []*http.Response{resp}}
|
|
svc := &AccountTestService{accountRepo: repo, httpUpstream: upstream}
|
|
account := &Account{
|
|
ID: 89,
|
|
Platform: PlatformOpenAI,
|
|
Type: AccountTypeOAuth,
|
|
Concurrency: 1,
|
|
Credentials: map[string]any{"access_token": "test-token"},
|
|
}
|
|
|
|
err := svc.testOpenAIAccountConnection(ctx, account, "gpt-5.4", "", "")
|
|
require.NoError(t, err)
|
|
require.Len(t, upstream.requests, 1)
|
|
require.Equal(t, HTTPUpstreamProfileOpenAI, HTTPUpstreamProfileFromContext(upstream.requests[0].Context()))
|
|
require.NotEmpty(t, repo.updatedExtra)
|
|
require.Equal(t, 42.0, repo.updatedExtra["codex_5h_used_percent"])
|
|
require.Equal(t, 88.0, repo.updatedExtra["codex_7d_used_percent"])
|
|
require.Contains(t, recorder.Body.String(), "test_complete")
|
|
}
|
|
|
|
func TestAccountTestService_OpenAIShadowUsesParentCredentialsAndShadowModel(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
ctx, recorder := newTestContext()
|
|
|
|
resp := newJSONResponse(http.StatusOK, "")
|
|
resp.Body = io.NopCloser(strings.NewReader(`data: {"type":"response.completed"}
|
|
|
|
`))
|
|
|
|
parentID := int64(100)
|
|
parent := &Account{
|
|
ID: parentID,
|
|
Platform: PlatformOpenAI,
|
|
Type: AccountTypeOAuth,
|
|
Status: StatusActive,
|
|
Credentials: map[string]any{
|
|
"access_token": "parent-token",
|
|
"chatgpt_account_id": "org-parent",
|
|
},
|
|
}
|
|
shadow := &Account{
|
|
ID: 200,
|
|
Platform: PlatformOpenAI,
|
|
Type: AccountTypeOAuth,
|
|
Status: StatusActive,
|
|
ParentAccountID: &parentID,
|
|
QuotaDimension: QuotaDimensionSpark,
|
|
Concurrency: 2,
|
|
Credentials: map[string]any{
|
|
"model_mapping": map[string]any{
|
|
"gpt-5.3-codex-spark": "gpt-5.3-codex-spark",
|
|
},
|
|
},
|
|
}
|
|
|
|
repo := &openAIAccountTestRepo{
|
|
mockAccountRepoForGemini: mockAccountRepoForGemini{
|
|
accountsByID: map[int64]*Account{
|
|
parentID: parent,
|
|
200: shadow,
|
|
},
|
|
},
|
|
}
|
|
upstream := &queuedHTTPUpstream{responses: []*http.Response{resp}}
|
|
svc := &AccountTestService{accountRepo: repo, httpUpstream: upstream}
|
|
|
|
err := svc.TestAccountConnection(ctx, shadow.ID, "gpt-5.3-codex-spark", "", "")
|
|
require.NoError(t, err)
|
|
require.Len(t, upstream.requests, 1)
|
|
req := upstream.requests[0]
|
|
require.Equal(t, "Bearer parent-token", req.Header.Get("Authorization"))
|
|
require.Equal(t, "org-parent", req.Header.Get("chatgpt-account-id"))
|
|
body, err := io.ReadAll(req.Body)
|
|
require.NoError(t, err)
|
|
require.Equal(t, "gpt-5.3-codex-spark", gjson.GetBytes(body, "model").String())
|
|
require.Contains(t, recorder.Body.String(), `"success":true`)
|
|
}
|
|
|
|
func TestAccountTestService_OpenAIStreamEOFBeforeCompletedFails(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
ctx, recorder := newTestContext()
|
|
|
|
resp := newJSONResponse(http.StatusOK, "")
|
|
resp.Body = io.NopCloser(strings.NewReader(`data: {"type":"response.output_text.delta","delta":"hi"}
|
|
|
|
`))
|
|
|
|
upstream := &queuedHTTPUpstream{responses: []*http.Response{resp}}
|
|
svc := &AccountTestService{httpUpstream: upstream}
|
|
account := &Account{
|
|
ID: 90,
|
|
Platform: PlatformOpenAI,
|
|
Type: AccountTypeOAuth,
|
|
Concurrency: 1,
|
|
Credentials: map[string]any{"access_token": "test-token"},
|
|
}
|
|
|
|
err := svc.testOpenAIAccountConnection(ctx, account, "gpt-5.4", "", "")
|
|
require.Error(t, err)
|
|
require.Contains(t, recorder.Body.String(), "response.completed")
|
|
require.NotContains(t, recorder.Body.String(), `"success":true`)
|
|
}
|
|
|
|
func TestAccountTestService_OpenAI429PersistsSnapshotAndRateLimitState(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
ctx, _ := newTestContext()
|
|
|
|
resp := newJSONResponse(http.StatusTooManyRequests, `{"error":{"type":"usage_limit_reached","message":"limit reached","resets_at":1777283883}}`)
|
|
resp.Header.Set("x-codex-primary-used-percent", "100")
|
|
resp.Header.Set("x-codex-primary-reset-after-seconds", "604800")
|
|
resp.Header.Set("x-codex-primary-window-minutes", "10080")
|
|
resp.Header.Set("x-codex-secondary-used-percent", "100")
|
|
resp.Header.Set("x-codex-secondary-reset-after-seconds", "18000")
|
|
resp.Header.Set("x-codex-secondary-window-minutes", "300")
|
|
|
|
repo := &openAIAccountTestRepo{}
|
|
upstream := &queuedHTTPUpstream{responses: []*http.Response{resp}}
|
|
svc := &AccountTestService{accountRepo: repo, httpUpstream: upstream}
|
|
account := &Account{
|
|
ID: 88,
|
|
Platform: PlatformOpenAI,
|
|
Type: AccountTypeOAuth,
|
|
Status: StatusError,
|
|
Concurrency: 1,
|
|
Credentials: map[string]any{"access_token": "test-token"},
|
|
}
|
|
|
|
err := svc.testOpenAIAccountConnection(ctx, account, "gpt-5.4", "", "")
|
|
require.Error(t, err)
|
|
require.NotEmpty(t, repo.updatedExtra)
|
|
require.Equal(t, 100.0, repo.updatedExtra["codex_5h_used_percent"])
|
|
require.Equal(t, account.ID, repo.rateLimitedID)
|
|
require.NotNil(t, repo.rateLimitedAt)
|
|
require.Equal(t, account.ID, repo.clearedErrorID)
|
|
require.Equal(t, StatusActive, account.Status)
|
|
require.Empty(t, account.ErrorMessage)
|
|
require.NotNil(t, account.RateLimitResetAt)
|
|
}
|
|
|
|
func TestAccountTestService_OpenAI429BodyOnlyPersistsRateLimitAndClearsStaleError(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
ctx, _ := newTestContext()
|
|
|
|
resp := newJSONResponse(http.StatusTooManyRequests, `{"error":{"type":"usage_limit_reached","message":"limit reached","resets_at":"1777283883"}}`)
|
|
|
|
repo := &openAIAccountTestRepo{}
|
|
upstream := &queuedHTTPUpstream{responses: []*http.Response{resp}}
|
|
svc := &AccountTestService{accountRepo: repo, httpUpstream: upstream}
|
|
account := &Account{
|
|
ID: 77,
|
|
Platform: PlatformOpenAI,
|
|
Type: AccountTypeOAuth,
|
|
Status: StatusError,
|
|
ErrorMessage: "Access forbidden (403): account may be suspended or lack permissions",
|
|
Concurrency: 1,
|
|
Credentials: map[string]any{"access_token": "test-token"},
|
|
}
|
|
|
|
err := svc.testOpenAIAccountConnection(ctx, account, "gpt-5.4", "", "")
|
|
require.Error(t, err)
|
|
require.Equal(t, account.ID, repo.rateLimitedID)
|
|
require.NotNil(t, repo.rateLimitedAt)
|
|
require.Equal(t, account.ID, repo.clearedErrorID)
|
|
require.Equal(t, StatusActive, account.Status)
|
|
require.Empty(t, account.ErrorMessage)
|
|
require.NotNil(t, account.RateLimitResetAt)
|
|
require.Empty(t, repo.updatedExtra)
|
|
}
|
|
|
|
func TestAccountTestService_OpenAI429SyncsObservedPlanType(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
ctx, _ := newTestContext()
|
|
|
|
resp := newJSONResponse(http.StatusTooManyRequests, `{"error":{"type":"usage_limit_reached","message":"limit reached","plan_type":"free","resets_at":1777283883}}`)
|
|
|
|
repo := &openAIAccountTestRepo{}
|
|
upstream := &queuedHTTPUpstream{responses: []*http.Response{resp}}
|
|
svc := &AccountTestService{accountRepo: repo, httpUpstream: upstream}
|
|
account := &Account{
|
|
ID: 81,
|
|
Platform: PlatformOpenAI,
|
|
Type: AccountTypeOAuth,
|
|
Status: StatusActive,
|
|
Concurrency: 1,
|
|
Credentials: map[string]any{"access_token": "test-token", "plan_type": "plus"},
|
|
}
|
|
|
|
err := svc.testOpenAIAccountConnection(ctx, account, "gpt-5.4", "", "")
|
|
require.Error(t, err)
|
|
require.Equal(t, []int64{account.ID}, repo.bulkUpdatedIDs)
|
|
require.Equal(t, "free", repo.bulkUpdatedPayload.Credentials["plan_type"])
|
|
require.Equal(t, "free", account.Credentials["plan_type"])
|
|
require.Equal(t, account.ID, repo.rateLimitedID)
|
|
require.NotNil(t, account.RateLimitResetAt)
|
|
}
|
|
|
|
func TestAccountTestService_OpenAI429ActiveAccountDoesNotClearError(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
ctx, _ := newTestContext()
|
|
|
|
resp := newJSONResponse(http.StatusTooManyRequests, `{"error":{"type":"usage_limit_reached","message":"limit reached","resets_in_seconds":3600}}`)
|
|
|
|
repo := &openAIAccountTestRepo{}
|
|
upstream := &queuedHTTPUpstream{responses: []*http.Response{resp}}
|
|
svc := &AccountTestService{accountRepo: repo, httpUpstream: upstream}
|
|
account := &Account{
|
|
ID: 78,
|
|
Platform: PlatformOpenAI,
|
|
Type: AccountTypeOAuth,
|
|
Status: StatusActive,
|
|
Concurrency: 1,
|
|
Credentials: map[string]any{"access_token": "test-token"},
|
|
}
|
|
|
|
err := svc.testOpenAIAccountConnection(ctx, account, "gpt-5.4", "", "")
|
|
require.Error(t, err)
|
|
require.Equal(t, account.ID, repo.rateLimitedID)
|
|
require.NotNil(t, repo.rateLimitedAt)
|
|
require.Zero(t, repo.clearedErrorID)
|
|
require.Equal(t, StatusActive, account.Status)
|
|
require.NotNil(t, account.RateLimitResetAt)
|
|
}
|
|
|
|
func TestAccountTestService_OpenAI429WithoutResetSignalDoesNotMutateRuntimeState(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
ctx, _ := newTestContext()
|
|
|
|
resp := newJSONResponse(http.StatusTooManyRequests, `{"error":{"type":"usage_limit_reached","message":"limit reached"}}`)
|
|
|
|
repo := &openAIAccountTestRepo{}
|
|
upstream := &queuedHTTPUpstream{responses: []*http.Response{resp}}
|
|
svc := &AccountTestService{accountRepo: repo, httpUpstream: upstream}
|
|
account := &Account{
|
|
ID: 79,
|
|
Platform: PlatformOpenAI,
|
|
Type: AccountTypeOAuth,
|
|
Status: StatusError,
|
|
ErrorMessage: "stale 403",
|
|
Concurrency: 1,
|
|
Credentials: map[string]any{"access_token": "test-token"},
|
|
}
|
|
|
|
err := svc.testOpenAIAccountConnection(ctx, account, "gpt-5.4", "", "")
|
|
require.Error(t, err)
|
|
require.Zero(t, repo.rateLimitedID)
|
|
require.Nil(t, repo.rateLimitedAt)
|
|
require.Zero(t, repo.clearedErrorID)
|
|
require.Equal(t, StatusError, account.Status)
|
|
require.Equal(t, "stale 403", account.ErrorMessage)
|
|
require.Nil(t, account.RateLimitResetAt)
|
|
}
|
|
|
|
func TestAccountTestService_OpenAI401SetsPermanentErrorOnly(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
ctx, _ := newTestContext()
|
|
|
|
resp := newJSONResponse(http.StatusUnauthorized, `{"error":"bad token"}`)
|
|
|
|
repo := &openAIAccountTestRepo{}
|
|
upstream := &queuedHTTPUpstream{responses: []*http.Response{resp}}
|
|
svc := &AccountTestService{accountRepo: repo, httpUpstream: upstream}
|
|
account := &Account{
|
|
ID: 80,
|
|
Platform: PlatformOpenAI,
|
|
Type: AccountTypeOAuth,
|
|
Status: StatusActive,
|
|
Concurrency: 1,
|
|
Credentials: map[string]any{"access_token": "test-token"},
|
|
}
|
|
|
|
err := svc.testOpenAIAccountConnection(ctx, account, "gpt-5.4", "", "")
|
|
require.Error(t, err)
|
|
require.Equal(t, account.ID, repo.setErrorID)
|
|
require.Contains(t, repo.setErrorMsg, "Authentication failed (401)")
|
|
require.Zero(t, repo.rateLimitedID)
|
|
require.Zero(t, repo.clearedErrorID)
|
|
require.Nil(t, account.RateLimitResetAt)
|
|
}
|
|
|
|
func TestAccountTestService_OpenAIAPIKeyResponsesUnsupportedUsesChatCompletionsPath(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
ctx, recorder := newTestContext()
|
|
|
|
upstreamBody := strings.Join([]string{
|
|
`data: {"id":"chatcmpl_test","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"pong"},"finish_reason":null}]}`,
|
|
"",
|
|
`data: {"id":"chatcmpl_test","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}`,
|
|
"",
|
|
"data: [DONE]",
|
|
"",
|
|
}, "\n")
|
|
upstream := &httpUpstreamRecorder{resp: &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Header: http.Header{"Content-Type": []string{"text/event-stream"}},
|
|
Body: io.NopCloser(strings.NewReader(upstreamBody)),
|
|
}}
|
|
svc := &AccountTestService{
|
|
httpUpstream: upstream,
|
|
cfg: &config.Config{Security: config.SecurityConfig{URLAllowlist: config.URLAllowlistConfig{Enabled: false}}},
|
|
}
|
|
account := &Account{
|
|
ID: 91,
|
|
Platform: PlatformOpenAI,
|
|
Type: AccountTypeAPIKey,
|
|
Concurrency: 1,
|
|
Credentials: map[string]any{
|
|
"api_key": "sk-test",
|
|
"base_url": "https://compat-upstream.example/v1",
|
|
},
|
|
Extra: map[string]any{openai_compat.ExtraKeyResponsesSupported: false},
|
|
}
|
|
|
|
err := svc.testOpenAIAccountConnection(ctx, account, "gpt-5.4", "hello", "")
|
|
require.NoError(t, err)
|
|
require.NotNil(t, upstream.lastReq)
|
|
require.Equal(t, HTTPUpstreamProfileOpenAI, HTTPUpstreamProfileFromContext(upstream.lastReq.Context()))
|
|
require.Equal(t, "https://compat-upstream.example/v1/chat/completions", upstream.lastReq.URL.String())
|
|
require.Equal(t, "Bearer sk-test", upstream.lastReq.Header.Get("Authorization"))
|
|
require.Equal(t, "text/event-stream", upstream.lastReq.Header.Get("Accept"))
|
|
require.Equal(t, "gpt-5.4", gjson.GetBytes(upstream.lastBody, "model").String())
|
|
require.True(t, gjson.GetBytes(upstream.lastBody, "stream").Bool())
|
|
require.Equal(t, "hello", gjson.GetBytes(upstream.lastBody, "messages.0.content").String())
|
|
require.False(t, gjson.GetBytes(upstream.lastBody, "input").Exists())
|
|
body := recorder.Body.String()
|
|
require.Contains(t, body, "pong")
|
|
require.Contains(t, body, "已通过 /v1/chat/completions 验证")
|
|
require.Contains(t, body, `"success":true`)
|
|
require.NotContains(t, body, "当前测试接口仅支持 Responses API 路径")
|
|
}
|
|
|
|
func TestAccountTestService_OpenAIChatCompletionsPathReturns4xx(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
ctx, recorder := newTestContext()
|
|
|
|
upstream := &httpUpstreamRecorder{resp: newJSONResponse(http.StatusBadRequest, `{"error":{"message":"bad request"}}`)}
|
|
svc := &AccountTestService{
|
|
httpUpstream: upstream,
|
|
cfg: &config.Config{Security: config.SecurityConfig{URLAllowlist: config.URLAllowlistConfig{Enabled: false}}},
|
|
}
|
|
account := &Account{
|
|
ID: 92,
|
|
Platform: PlatformOpenAI,
|
|
Type: AccountTypeAPIKey,
|
|
Concurrency: 1,
|
|
Credentials: map[string]any{
|
|
"api_key": "sk-test",
|
|
"base_url": "https://compat-upstream.example",
|
|
},
|
|
Extra: map[string]any{openai_compat.ExtraKeyResponsesSupported: false},
|
|
}
|
|
|
|
err := svc.testOpenAIAccountConnection(ctx, account, "gpt-5.4", "", "")
|
|
require.Error(t, err)
|
|
require.Equal(t, "https://compat-upstream.example/v1/chat/completions", upstream.lastReq.URL.String())
|
|
require.Contains(t, err.Error(), "Chat Completions API (/v1/chat/completions) returned 400")
|
|
require.Contains(t, recorder.Body.String(), "/v1/chat/completions")
|
|
require.NotContains(t, recorder.Body.String(), `"success":true`)
|
|
}
|
|
|
|
func TestAccountTestService_OpenAIChatCompletionsPathTimeout(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
ctx, recorder := newTestContext()
|
|
|
|
upstream := &httpUpstreamRecorder{err: context.DeadlineExceeded}
|
|
svc := &AccountTestService{
|
|
httpUpstream: upstream,
|
|
cfg: &config.Config{Security: config.SecurityConfig{URLAllowlist: config.URLAllowlistConfig{Enabled: false}}},
|
|
}
|
|
account := &Account{
|
|
ID: 93,
|
|
Platform: PlatformOpenAI,
|
|
Type: AccountTypeAPIKey,
|
|
Concurrency: 1,
|
|
Credentials: map[string]any{
|
|
"api_key": "sk-test",
|
|
"base_url": "https://compat-upstream.example",
|
|
},
|
|
Extra: map[string]any{openai_compat.ExtraKeyResponsesSupported: false},
|
|
}
|
|
|
|
err := svc.testOpenAIAccountConnection(ctx, account, "gpt-5.4", "", "")
|
|
require.Error(t, err)
|
|
require.Equal(t, "https://compat-upstream.example/v1/chat/completions", upstream.lastReq.URL.String())
|
|
require.Contains(t, err.Error(), "Chat Completions API (/v1/chat/completions) request failed")
|
|
require.Contains(t, err.Error(), context.DeadlineExceeded.Error())
|
|
require.Contains(t, recorder.Body.String(), "/v1/chat/completions")
|
|
require.NotContains(t, recorder.Body.String(), `"success":true`)
|
|
}
|
|
|
|
func TestAccountTestService_OpenAIChatCompletionsPathRejectsNonJSONStream(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
ctx, recorder := newTestContext()
|
|
|
|
upstream := &httpUpstreamRecorder{resp: &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Header: http.Header{"Content-Type": []string{"text/event-stream"}},
|
|
Body: io.NopCloser(strings.NewReader("data: not-json\n\n")),
|
|
}}
|
|
svc := &AccountTestService{
|
|
httpUpstream: upstream,
|
|
cfg: &config.Config{Security: config.SecurityConfig{URLAllowlist: config.URLAllowlistConfig{Enabled: false}}},
|
|
}
|
|
account := &Account{
|
|
ID: 94,
|
|
Platform: PlatformOpenAI,
|
|
Type: AccountTypeAPIKey,
|
|
Concurrency: 1,
|
|
Credentials: map[string]any{
|
|
"api_key": "sk-test",
|
|
"base_url": "https://compat-upstream.example",
|
|
},
|
|
Extra: map[string]any{openai_compat.ExtraKeyResponsesSupported: false},
|
|
}
|
|
|
|
err := svc.testOpenAIAccountConnection(ctx, account, "gpt-5.4", "", "")
|
|
require.Error(t, err)
|
|
require.Equal(t, "https://compat-upstream.example/v1/chat/completions", upstream.lastReq.URL.String())
|
|
require.Contains(t, err.Error(), "Invalid Chat Completions response from /v1/chat/completions")
|
|
require.Contains(t, recorder.Body.String(), "/v1/chat/completions")
|
|
require.NotContains(t, recorder.Body.String(), `"success":true`)
|
|
}
|