Major rewrite of the Go backend to support:
- Three API format imports: openai, anthropic, compatible
- Three protocol conversions: Chat Completions, Responses, Messages
- Hub-and-spoke architecture with Chat as intermediate format
Deleted:
- opencat.go (old entry)
- store/, team/, pkg/team/, pkg/store/ (old data layer)
- internal/model/, internal/consts/ (old types)
- internal/service/team/, internal/controller/team/ (old handlers)
- llm/ (removed LLM client library, pure proxy mode)
- dist/, assets/ (old build artifacts)
Added:
- internal/store/ — 9 GORM models + multi-DB support
- internal/pkg/ — crypto (AES-GCM), apikey, jwt, ratelimit, resp, tokenizer
- internal/channel/ — channel selection, weighted LB, health checks
- internal/proxy/convert/ — 6 protocol conversion functions + SSE streaming
- internal/proxy/ — gateway with request dispatch and upstream selection
- internal/usage/ — async usage recorder with batch writes
- internal/api/ — management API (auth, users, keys, channels, models)
- Makefile for build/test/deploy
Fixed API to match frontend expectations:
- Login response wraps token in { data: { token } }
- GET /api/profile route added
- Profile response wraps user in { code, data }
- Role returned as number (10=admin, 1=user)
126 lines
2.8 KiB
Go
126 lines
2.8 KiB
Go
package channel
|
|
|
|
import (
|
|
"opencatd-open/internal/store"
|
|
"testing"
|
|
)
|
|
|
|
func TestChannelFormatsEffective(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
channel store.Channel
|
|
expected []string
|
|
}{
|
|
{
|
|
name: "anthropic default",
|
|
channel: store.Channel{
|
|
Provider: store.ChannelProviderAnthropic,
|
|
},
|
|
expected: []string{store.FormatMessages},
|
|
},
|
|
{
|
|
name: "openai default",
|
|
channel: store.Channel{
|
|
Provider: store.ChannelProviderOpenAI,
|
|
},
|
|
expected: []string{store.FormatChat, store.FormatResponses},
|
|
},
|
|
{
|
|
name: "compatible default",
|
|
channel: store.Channel{
|
|
Provider: store.ChannelProviderCompatible,
|
|
},
|
|
expected: []string{store.FormatChat},
|
|
},
|
|
{
|
|
name: "custom formats override",
|
|
channel: store.Channel{
|
|
Provider: store.ChannelProviderOpenAI,
|
|
Formats: []string{store.FormatChat},
|
|
},
|
|
expected: []string{store.FormatChat},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := tt.channel.FormatsEffective()
|
|
if len(result) != len(tt.expected) {
|
|
t.Errorf("FormatsEffective() returned %d formats, want %d", len(result), len(tt.expected))
|
|
return
|
|
}
|
|
for i, f := range result {
|
|
if f != tt.expected[i] {
|
|
t.Errorf("FormatsEffective()[%d] = %q, want %q", i, f, tt.expected[i])
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestChannelUpstreamURL(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
channel store.Channel
|
|
proto string
|
|
path string
|
|
expected string
|
|
}{
|
|
{
|
|
name: "basic openai",
|
|
channel: store.Channel{
|
|
BaseURL: "https://api.openai.com",
|
|
},
|
|
proto: "chat",
|
|
path: "/chat/completions",
|
|
expected: "https://api.openai.com/v1/chat/completions",
|
|
},
|
|
{
|
|
name: "with trailing slash",
|
|
channel: store.Channel{
|
|
BaseURL: "https://api.openai.com/",
|
|
},
|
|
proto: "chat",
|
|
path: "/chat/completions",
|
|
expected: "https://api.openai.com/v1/chat/completions",
|
|
},
|
|
{
|
|
name: "with version segment",
|
|
channel: store.Channel{
|
|
BaseURL: "https://api.openai.com/v1",
|
|
},
|
|
proto: "chat",
|
|
path: "/chat/completions",
|
|
expected: "https://api.openai.com/v1/chat/completions",
|
|
},
|
|
{
|
|
name: "custom base URL per protocol",
|
|
channel: store.Channel{
|
|
BaseURL: "https://default.openai.com",
|
|
BaseURLs: map[string]string{"chat": "https://chat.openai.com"},
|
|
},
|
|
proto: "chat",
|
|
path: "/chat/completions",
|
|
expected: "https://chat.openai.com/v1/chat/completions",
|
|
},
|
|
{
|
|
name: "empty base",
|
|
channel: store.Channel{
|
|
BaseURL: "",
|
|
},
|
|
proto: "chat",
|
|
path: "/chat/completions",
|
|
expected: "/chat/completions",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := tt.channel.UpstreamURL(tt.proto, tt.path)
|
|
if result != tt.expected {
|
|
t.Errorf("UpstreamURL() = %q, want %q", result, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|