Files
sub2api/backend/internal/service/composite_platform_test.go
T
SavitarCandClaude Opus 5.5 874871a25a feat(platform): 接入 Cline 平台
Cline API(https://api.cline.bot/api/v1)是 OpenAI 兼容的多模型聚合端点,
只提供 Chat Completions(Cline 自己的客户端同样只走该端点),模型名带厂商
前缀。同一个 API Key 下有两个互不影响的额度来源,由模型名决定扣哪一边:
cline-pass/* 走 ClinePass 订阅(5 小时 / 7 天 / 30 天滚动上限),其余付费
模型扣账户积分;cline-free/* 免费模型只有逐模型的每日额度。

转发:
- 平台清单与 provider profile 新增 cline:按入站协议分流,只登记 Chat
  Completions 端点,Responses / Anthropic 入站由现有转换链转为 Chat
  Completions,无需新增转发代码。
- 只有一个接入模式,不需要选择账号类型。
- 连接测试未指定模型时使用 profile 默认模型(固定 Chat Completions 的账号
  此前落到不带厂商前缀的通用默认模型);订阅了 ClinePass 的账号改用订阅
  模型,不消耗积分,ClinePass 冷却中时仍用按量模型。

用量与余额:API Key 调 /api/v1/users/me/plan/usage-limits(三档窗口的已用
百分比与重置时间,未订阅时 404)与 /api/v1/users/me、/users/{id}/balance
(余额单位为百万分之一积分,1 积分 = 1 美元),兼容 {success, data} 信封,
只查官方主机。复用额度探测与周期检测:账号列表显示 ClinePass 窗口与积分
余额;窗口用满时 ClinePass 模型冷却到重置时间,未订阅时不调度 ClinePass
模型,积分低于阈值时冷却付费模型;数据显示恢复后自动解除,但只解除探测能
证明已恢复的原因(窗口用满、未订阅、积分不足),组织账户、所需订阅档位、推理
额度与花费上限的冷却到期后由请求重新确认。周期检测中走账号接口探测的平台
改为登记表(平台、支持判定、检测函数一一对应),按账号所属平台分派。

上游错误(依据 docs.cline.bot/api/errors 与 Cline 客户端的错误分类,按文案
识别):积分不足(含其他 402)、组织花费上限、推理额度上限、ClinePass 超限、
未订阅 ClinePass / 组织账户、免费模型每日额度。两边的超限只冷却请求模型所属
的一边(模型级冷却键 cline:pass / cline:credits,调度按请求模型检查),不停
整个账号;免费模型只限该模型,冷却到文案中的 "try again in …"。ClinePass
超限冷却到快照中对应窗口的重置时间,没有快照时 5 小时窗口 1 小时、7 天
6 小时、30 天 24 小时。普通频率限制走默认 429 逻辑。402 的冷却写入失败时
也不落入默认分支永久置 error(探测只覆盖激活账号,置 error 后无法自动恢复)。
余额类冷却时长抽为 cnBalanceCheckCooldown 共用。

上游计费探测的官方域名加入 cline.bot。

前端:官方标志(机器人头像)、violet 配色(品牌紫)、复用额度与余额单元格
(同样只对官方主机上的 API Key 账号显示);接入模式展示名抽为共用函数,编辑
弹窗不再显示原始模式值。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 10:11:06 +08:00

232 lines
8.6 KiB
Go

package service
import (
"context"
"testing"
"github.com/Wei-Shaw/sub2api/internal/pkg/ctxkey"
"github.com/stretchr/testify/require"
)
type compositeOwnershipAccountRepo struct {
AccountRepository
accounts []Account
}
func (r *compositeOwnershipAccountRepo) ListSchedulableByGroupID(context.Context, int64) ([]Account, error) {
return r.accounts, nil
}
// Scenario: 唯一平台的精确别名可路由
func TestResolveCompositeModelOwnershipKeepsProviderAccountsIsolated(t *testing.T) {
groupID := int64(7)
repo := &compositeOwnershipAccountRepo{
accounts: []Account{
{
ID: 1,
Platform: PlatformOpenAI,
Credentials: map[string]any{
"model_mapping": map[string]any{"gpt-public": "gpt-5"},
},
},
{
ID: 2,
Platform: PlatformDeepseek,
Credentials: map[string]any{
"model_mapping": map[string]any{"reasoning-alias": "deepseek-v4-pro"},
},
},
},
}
svc := &GatewayService{accountRepo: repo}
deepSeekOwnership, err := svc.resolveCompositeModelOwnership(context.Background(), groupID, "reasoning-alias")
require.NoError(t, err)
require.Equal(t, CompositeModelOwnership{TargetPlatform: PlatformDeepseek, Matched: true}, deepSeekOwnership)
openAIOwnership, err := svc.resolveCompositeModelOwnership(context.Background(), groupID, "gpt-public")
require.NoError(t, err)
require.Equal(t, CompositeModelOwnership{TargetPlatform: PlatformOpenAI, Matched: true}, openAIOwnership)
}
// Scenario: 通配符和空映射不声明所有权
func TestResolveCompositeModelOwnershipRequiresNonEmptyExactMappings(t *testing.T) {
groupID := int64(7)
repo := &compositeOwnershipAccountRepo{
accounts: []Account{
{
ID: 1,
Platform: PlatformOpenAI,
Credentials: map[string]any{
"model_mapping": map[string]any{"*": "gpt-5", "gpt-*": "gpt-5", "empty-alias": ""},
},
},
{
ID: 2,
Platform: PlatformGrok,
Credentials: map[string]any{
"model_mapping": map[string]any{"grok-public": "grok-4"},
},
},
},
}
svc := &GatewayService{accountRepo: repo}
for _, model := range []string{"gpt-5", "empty-alias", "unknown-alias"} {
ownership, err := svc.resolveCompositeModelOwnership(context.Background(), groupID, model)
require.NoError(t, err)
require.Equal(t, CompositeModelOwnership{}, ownership, "model=%s", model)
}
ownership, err := svc.resolveCompositeModelOwnership(context.Background(), groupID, "grok-public")
require.NoError(t, err)
require.Equal(t, CompositeModelOwnership{TargetPlatform: PlatformGrok, Matched: true}, ownership)
}
func TestResolveCompositeModelOwnershipAllowsSamePlatformAndRejectsCrossPlatformAliases(t *testing.T) {
groupID := int64(7)
repo := &compositeOwnershipAccountRepo{
accounts: []Account{
{ID: 1, Platform: PlatformOpenAI, Credentials: map[string]any{"model_mapping": map[string]any{"shared-openai": "gpt-5", "ambiguous": "gpt-5"}}},
{ID: 2, Platform: PlatformOpenAI, Credentials: map[string]any{"model_mapping": map[string]any{"shared-openai": "gpt-5.1"}}},
{ID: 3, Platform: PlatformDeepseek, Credentials: map[string]any{"model_mapping": map[string]any{"ambiguous": "deepseek-v4-pro"}}},
},
}
svc := &GatewayService{accountRepo: repo}
samePlatform, err := svc.resolveCompositeModelOwnership(context.Background(), groupID, "shared-openai")
require.NoError(t, err)
require.Equal(t, CompositeModelOwnership{TargetPlatform: PlatformOpenAI, Matched: true}, samePlatform)
ambiguous, err := svc.resolveCompositeModelOwnership(context.Background(), groupID, "ambiguous")
require.NoError(t, err)
require.Equal(t, CompositeModelOwnership{Ambiguous: true}, ambiguous)
}
func TestNewGatewayServiceWiresCompositeModelOwnershipResolver(t *testing.T) {
groupID := int64(7)
repo := &compositeOwnershipAccountRepo{
accounts: []Account{{
ID: 1,
Platform: PlatformDeepseek,
Credentials: map[string]any{"model_mapping": map[string]any{"reasoning-alias": "deepseek-v4-pro"}},
}},
}
resolver := NewCompositeRouteResolver(nil)
svc := NewGatewayService(
repo,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
resolver,
nil,
nil,
)
require.Same(t, resolver, svc.compositeResolver)
decision, err := resolver.Resolve(context.Background(), groupID, "reasoning-alias", CompositeRouteEndpointResponses)
require.NoError(t, err)
require.True(t, decision.Matched)
require.Equal(t, CompositeRouteSourceAccount, decision.Source)
require.Equal(t, PlatformDeepseek, decision.TargetPlatform)
}
func TestDetectModelPlatform(t *testing.T) {
tests := []struct {
name string
model string
platform string
ok bool
}{
{name: "claude", model: "claude-sonnet-4-5", platform: PlatformAnthropic, ok: true},
{name: "anthropic prefix", model: "anthropic/claude-opus-4-5", platform: PlatformAnthropic, ok: true},
{name: "gpt", model: "gpt-5.1", platform: PlatformOpenAI, ok: true},
{name: "o series", model: "o3-mini", platform: PlatformOpenAI, ok: true},
{name: "embedding", model: "text-embedding-3-large", platform: PlatformOpenAI, ok: true},
{name: "gemini", model: "gemini-3-pro", platform: PlatformGemini, ok: true},
{name: "gemini models prefix", model: "models/gemini-2.5-flash", platform: PlatformGemini, ok: true},
{name: "learnlm", model: "learnlm-2.0-flash-experimental", platform: PlatformGemini, ok: true},
{name: "grok", model: "grok-4", platform: PlatformGrok, ok: true},
{name: "xai prefix", model: "xai/grok-4", platform: PlatformGrok, ok: true},
{name: "kimi", model: "kimi-k2-thinking", platform: PlatformKimi, ok: true},
{name: "kimi code bare k3", model: "K3", platform: PlatformKimi, ok: true},
{name: "kimi code bare k3 256k", model: "k3-256k", platform: PlatformKimi, ok: true},
{name: "kimi code provider prefix", model: "kimi-code/k3", platform: PlatformKimi, ok: true},
{name: "moonshot prefix", model: "moonshot/moonshot-v1-32k", platform: PlatformKimi, ok: true},
{name: "zhipu", model: "glm-5.2", platform: PlatformZhipu, ok: true},
{name: "deepseek", model: "deepseek-v4-pro", platform: PlatformDeepseek, ok: true},
{name: "minimax", model: "MiniMax-M3", platform: PlatformMiniMax, ok: true},
{name: "minimax prefix", model: "minimax/MiniMax-M2.5", platform: PlatformMiniMax, ok: true},
{name: "abab legacy", model: "abab6.5-chat", platform: PlatformMiniMax, ok: true},
{name: "abab7 legacy", model: "abab7-chat-preview", platform: PlatformMiniMax, ok: true},
{name: "jev", model: "jev-latest", platform: PlatformTypeSafe, ok: true},
{name: "typesafe prefix", model: "typesafe/jev-latest", platform: PlatformTypeSafe, ok: true},
{name: "abab unrelated namespace", model: "abab-other", ok: false},
{name: "unknown k3 alias", model: "k3-preview", ok: false},
{name: "unknown", model: "llama-4-maverick", ok: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
platform, ok := DetectModelPlatform(tt.model)
require.Equal(t, tt.ok, ok)
require.Equal(t, tt.platform, platform)
})
}
}
func TestQuotaPlatformCompositeUsesResolvedOrForceOnly(t *testing.T) {
apiKey := &APIKey{Group: &Group{Platform: PlatformComposite}}
require.Equal(t, "", QuotaPlatform(context.Background(), apiKey))
require.Equal(t, PlatformGemini, QuotaPlatform(WithResolvedTargetPlatform(context.Background(), PlatformGemini), apiKey))
require.Equal(t, PlatformAntigravity, QuotaPlatform(context.WithValue(context.Background(), ctxkey.ForcePlatform, PlatformAntigravity), apiKey))
ctx := WithResolvedTargetPlatform(context.Background(), PlatformAnthropic)
ctx = context.WithValue(ctx, ctxkey.ForcePlatform, PlatformAntigravity)
require.Equal(t, PlatformAntigravity, QuotaPlatform(ctx, apiKey))
}
func TestCompositeGroupSchedulerHasAllCanonicalPlatformBuckets(t *testing.T) {
seen := make(map[string]struct{})
for _, bucket := range schedulerCanonicalBuckets(99) {
seen[bucket.Platform] = struct{}{}
}
platforms := make([]string, 0, len(seen))
for platform := range seen {
platforms = append(platforms, platform)
}
require.ElementsMatch(t,
[]string{PlatformAnthropic, PlatformGemini, PlatformOpenAI, PlatformAntigravity, PlatformGrok, PlatformKimi, PlatformZhipu, PlatformDeepseek, PlatformMiniMax, PlatformOpenCodeGo, PlatformTypeSafe, PlatformCommandCode, PlatformCline},
platforms,
)
}
func TestCompositeConcretePlatformsIncludeCNProviders(t *testing.T) {
for _, platform := range []string{PlatformKimi, PlatformZhipu, PlatformDeepseek, PlatformMiniMax, PlatformOpenCodeGo, PlatformTypeSafe, PlatformCommandCode, PlatformCline} {
require.True(t, isConcreteRequestPlatform(platform))
require.True(t, canCopyAccountsFromGroupPlatform(PlatformComposite, platform))
}
}