* feat(images): 接入 OpenAI 图片生成与编辑 * fix(images): 补齐响应容量与边界处理 * fix(images): 修正协议观测与响应上限覆盖 * fix(stream): 保证取消优先于流关闭 * fix(images): 修正空 SSE 错误对象判定 * fix(images): 避免媒体数据触发凭据误判 * fix(images): 阻止流式响应泄漏凭据 * fix(images): 校验完整流事件后再转发 * fix(images): 修正响应模型观测
129 lines
3.1 KiB
Go
129 lines
3.1 KiB
Go
package protocol
|
|
|
|
import "testing"
|
|
|
|
func TestOpenAICompletionsCanonicalProtocolIsCleanBreak(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
value Protocol
|
|
enabled bool
|
|
}{
|
|
{
|
|
name: "canonical value",
|
|
value: Protocol("openai-completions"),
|
|
enabled: true,
|
|
},
|
|
{
|
|
name: "replaced value",
|
|
value: Protocol("openai-chat-completions"),
|
|
enabled: false,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if got := tt.value.Valid(); got != tt.enabled {
|
|
t.Errorf("Protocol(%q).Valid() = %t, want %t", tt.value, got, tt.enabled)
|
|
}
|
|
if got := tt.value.DataPlaneEnabled(); got != tt.enabled {
|
|
t.Errorf(
|
|
"Protocol(%q).DataPlaneEnabled() = %t, want %t",
|
|
tt.value,
|
|
got,
|
|
tt.enabled,
|
|
)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProtocolKnownAndDataPlaneEnabled(t *testing.T) {
|
|
t.Parallel()
|
|
tests := []struct {
|
|
value Protocol
|
|
known bool
|
|
enabled bool
|
|
}{
|
|
{value: OpenAICompletions, known: true, enabled: true},
|
|
{value: OpenAIResponses, known: true, enabled: true},
|
|
{value: OpenAIImages, known: true, enabled: true},
|
|
{value: Anthropic, known: true, enabled: true},
|
|
{value: Gemini, known: true, enabled: true},
|
|
{value: Protocol("openai"), known: false, enabled: false},
|
|
{value: Protocol("openai-response"), known: false, enabled: false},
|
|
{value: Protocol("unknown"), known: false, enabled: false},
|
|
{value: "", known: false, enabled: false},
|
|
}
|
|
for _, tt := range tests {
|
|
if got := tt.value.Valid(); got != tt.known {
|
|
t.Errorf("Protocol(%q).Valid() = %t, want %t", tt.value, got, tt.known)
|
|
}
|
|
if got := tt.value.DataPlaneEnabled(); got != tt.enabled {
|
|
t.Errorf(
|
|
"Protocol(%q).DataPlaneEnabled() = %t, want %t",
|
|
tt.value,
|
|
got,
|
|
tt.enabled,
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProtocolModelOptionalRequestsAreLimitedToOpenAIResponses(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
protocol Protocol
|
|
want bool
|
|
}{
|
|
{protocol: Protocol("openai-completions"), want: false},
|
|
{protocol: Protocol("openai-responses"), want: true},
|
|
{protocol: Protocol("anthropic"), want: false},
|
|
{protocol: Protocol("gemini"), want: false},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(string(test.protocol), func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if got := test.protocol.SupportsModelOptionalRequests(); got != test.want {
|
|
t.Errorf(
|
|
"Protocol(%q).SupportsModelOptionalRequests() = %t, want %t",
|
|
test.protocol,
|
|
got,
|
|
test.want,
|
|
)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDataPlaneProtocolsReturnsCanonicalOrderAndIndependentCopies(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
first := DataPlaneProtocols()
|
|
want := []Protocol{
|
|
OpenAICompletions,
|
|
OpenAIResponses,
|
|
OpenAIImages,
|
|
Anthropic,
|
|
Gemini,
|
|
}
|
|
if len(first) != len(want) {
|
|
t.Fatalf("DataPlaneProtocols() = %#v, want %#v", first, want)
|
|
}
|
|
for index := range want {
|
|
if first[index] != want[index] {
|
|
t.Fatalf("DataPlaneProtocols()[%d] = %q, want %q", index, first[index], want[index])
|
|
}
|
|
}
|
|
|
|
first[0] = Gemini
|
|
second := DataPlaneProtocols()
|
|
if second[0] != OpenAICompletions {
|
|
t.Fatalf("DataPlaneProtocols() shared mutable storage: %#v", second)
|
|
}
|
|
}
|