Files
shaw f08f9111e5 refactor(platform-seam): gatewayplatform 接缝——Provider/Registry + 两处 Forward 分发替换
经双视角评审裁决的收窄版 Phase-3 接缝(零行为变更):
- internal/gatewayplatform:Provider{Platform,Forward} 两方法 v1 接口 +
  ForwardRequest{Parsed,Body,IsStickySession,SessionGroupID,SessionKey} +
  构造期注册 Registry(重复 panic,运行期并发只读)
- 3 个单语句直返 adapter(anthropic/antigravity/gemini,错误零包裹——
  BetaBlockedError/PromptTooLong 的 errors.As 链由 T4 特征化守护)
- Messages 端点两处平台分发改 registry 查找;Type!=APIKey 条件保留调用点
- OpenAI(独立 handler)、v1beta 端点、内核 internal/plugin 零触碰

门禁:T1-T5 特征化全绿、安全网 45 包、bench allocs 全部持平(266/329/93/134)。
2026-06-12 00:27:10 +08:00

113 lines
3.5 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gatewayplatform
import (
"context"
"sync"
"testing"
"github.com/Wei-Shaw/sub2api/internal/service"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
)
// stubProvider 是注册表测试用的最小 Provider 实现。
type stubProvider struct {
platform string
}
func (s *stubProvider) Platform() string { return s.platform }
func (s *stubProvider) Forward(_ context.Context, _ *gin.Context, _ *service.Account, _ *ForwardRequest) (*service.ForwardResult, error) {
return nil, nil
}
func TestRegistry_GetReturnsRegisteredProvider(t *testing.T) {
anthropic := &stubProvider{platform: service.PlatformAnthropic}
antigravity := &stubProvider{platform: service.PlatformAntigravity}
gemini := &stubProvider{platform: service.PlatformGemini}
registry := NewRegistry(anthropic, antigravity, gemini)
require.Same(t, Provider(anthropic), registry.Get(service.PlatformAnthropic))
require.Same(t, Provider(antigravity), registry.Get(service.PlatformAntigravity))
require.Same(t, Provider(gemini), registry.Get(service.PlatformGemini))
}
func TestRegistry_GetUnregisteredPlatformReturnsNil(t *testing.T) {
registry := NewRegistry(&stubProvider{platform: service.PlatformAnthropic})
require.Nil(t, registry.Get(service.PlatformOpenAI), "未注册平台必须返回 nil")
require.Nil(t, registry.Get(""), "空平台必须返回 nil")
var nilRegistry *Registry
require.Nil(t, nilRegistry.Get(service.PlatformAnthropic), "nil Registry 必须返回 nil 而非 panic")
}
func TestNewRegistry_DuplicatePlatformPanics(t *testing.T) {
require.PanicsWithValue(t,
`gatewayplatform: duplicate provider for platform "anthropic"`,
func() {
NewRegistry(
&stubProvider{platform: service.PlatformAnthropic},
&stubProvider{platform: service.PlatformAnthropic},
)
})
}
func TestNewRegistry_NilProviderPanics(t *testing.T) {
require.PanicsWithValue(t, "gatewayplatform: nil provider", func() {
NewRegistry(&stubProvider{platform: service.PlatformAnthropic}, nil)
})
}
func TestNewRegistry_EmptyPlatformPanics(t *testing.T) {
require.PanicsWithValue(t, "gatewayplatform: provider with empty platform", func() {
NewRegistry(&stubProvider{platform: ""})
})
}
// TestRegistry_ConcurrentGet 验证构造完成后的注册表可被并发只读访问
// (配合 -race 检测数据竞争)。
func TestRegistry_ConcurrentGet(t *testing.T) {
registry := NewRegistry(
&stubProvider{platform: service.PlatformAnthropic},
&stubProvider{platform: service.PlatformAntigravity},
&stubProvider{platform: service.PlatformGemini},
)
platforms := []string{
service.PlatformAnthropic,
service.PlatformAntigravity,
service.PlatformGemini,
service.PlatformOpenAI, // miss 路径同样并发安全
}
var wg sync.WaitGroup
for i := 0; i < 32; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
for j := 0; j < 200; j++ {
platform := platforms[(i+j)%len(platforms)]
got := registry.Get(platform)
if platform == service.PlatformOpenAI {
require.Nil(t, got)
} else {
require.NotNil(t, got)
require.Equal(t, platform, got.Platform())
}
}
}(i)
}
wg.Wait()
}
// TestAdapters_PlatformIdentity 锁定三个 adapter 的注册键 = service.Platform* 常量
// (Registry 查找键与 :444/:794 调用点的查找表达式一致)。
func TestAdapters_PlatformIdentity(t *testing.T) {
require.Equal(t, service.PlatformAnthropic, NewAnthropicProvider(nil).Platform())
require.Equal(t, service.PlatformAntigravity, NewAntigravityProvider(nil).Platform())
require.Equal(t, service.PlatformGemini, NewGeminiProvider(nil).Platform())
}