Repository navigation
Expand file tree
/
Copy pathremote_control_mixed_relay_test.go
More file actions
150 lines (138 loc) · 5.45 KB
/
Copy pathremote_control_mixed_relay_test.go
File metadata and controls
150 lines (138 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gorilla/websocket"
"github.com/klauspost/compress/zstd"
)
func TestRemoteControlOpenAIBaseURLFollowsOfficialMixMode(t *testing.T) {
managed := "http://127.0.0.1:57323/v1"
mixed := relayProfile{RelayMode: "mixedApi", OfficialMixAPIKey: true}
config := applyRemoteControlOpenAIBaseURL(`model = "gpt-test"`, mixed)
if strings.Count(config, "openai_base_url = ") != 1 || rootKeyString(config, "openai_base_url") != managed {
t.Fatalf("mixed config must contain one managed openai_base_url:\n%s", config)
}
config = applyRemoteControlOpenAIBaseURL(config, mixed)
if strings.Count(config, "openai_base_url = ") != 1 {
t.Fatalf("managed openai_base_url must be idempotent:\n%s", config)
}
custom := `openai_base_url = "https://user-openai.example/v1"
model = "gpt-test"`
if got := applyRemoteControlOpenAIBaseURL(custom, mixed); rootKeyString(got, "openai_base_url") != "https://user-openai.example/v1" {
t.Fatalf("mixed mode replaced a user openai_base_url:\n%s", got)
}
pure := applyRemoteControlOpenAIBaseURL(config, relayProfile{RelayMode: "pureApi"})
if strings.Contains(pure, managed) {
t.Fatalf("pure API mode retained managed openai_base_url:\n%s", pure)
}
official := officialRelayConfigSnapshot(config)
if strings.Contains(official, managed) {
t.Fatalf("official mode retained managed openai_base_url:\n%s", official)
}
}
func TestRelayConfigWritesManagedRemoteControlBaseURLOnlyForMixedMode(t *testing.T) {
settings := defaultSettings()
profile := relayProfile{
ID: "mixed", RelayMode: "mixedApi", OfficialMixAPIKey: true, Protocol: "responses",
UseCommonConfig: true,
}
config, err := relayConfigWithCommonAndLimits(settings, profile, `model_provider = "CodexPlusPlus"`)
if err != nil {
t.Fatal(err)
}
if got := rootKeyString(config, "openai_base_url"); got != "http://127.0.0.1:57323/v1" {
t.Fatalf("mixed remote-control base URL = %q\n%s", got, config)
}
profile.RelayMode = "pureApi"
profile.OfficialMixAPIKey = false
config, err = relayConfigWithCommonAndLimits(settings, profile, config)
if err != nil {
t.Fatal(err)
}
if got := rootKeyString(config, "openai_base_url"); got != "" {
t.Fatalf("pure API config retained managed remote-control base URL = %q\n%s", got, config)
}
}
func TestDecodeProtocolProxyRequestBodyZstd(t *testing.T) {
body := []byte(`{"model":"gpt-5.6-sol","input":"probe","stream":false}`)
encoder, err := zstd.NewWriter(nil)
if err != nil {
t.Fatal(err)
}
compressed := encoder.EncodeAll(body, nil)
encoder.Close()
decoded, err := decodeProtocolProxyRequestBody(compressed, "zstd")
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(decoded, body) {
t.Fatalf("decoded body = %q", decoded)
}
if _, err := decodeProtocolProxyRequestBody(body, "gzip"); err == nil {
t.Fatal("unsupported compression must fail closed")
}
}
func TestMixedRemoteControlZstdResponsesReachConfiguredProvider(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
var upstreamBody []byte
var upstreamAuthorization string
provider := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
upstreamAuthorization = req.Header.Get("authorization")
upstreamBody, _ = io.ReadAll(req.Body)
w.Header().Set("content-type", "application/json")
_, _ = w.Write([]byte(`{"id":"resp_remote","object":"response"}`))
}))
defer provider.Close()
settings := defaultSettings()
settings.RelayProfiles = []relayProfile{{
ID: "mixed", Name: "Mixed", RelayMode: "mixedApi", OfficialMixAPIKey: true,
Protocol: "responses", BaseURL: provider.URL + "/v1", UpstreamBaseURL: provider.URL + "/v1", APIKey: "sk-provider",
ImageGenerationEnabled: true,
}}
settings.ActiveRelayID = "mixed"
runtime := &launcherRuntime{settings: settings}
body := []byte(`{"model":"gpt-5.6-sol","input":"probe","stream":false}`)
encoder, err := zstd.NewWriter(nil)
if err != nil {
t.Fatal(err)
}
compressed := encoder.EncodeAll(body, nil)
encoder.Close()
req := httptest.NewRequest(http.MethodPost, "http://127.0.0.1:57323/v1/responses", bytes.NewReader(compressed))
req.Header.Set("content-type", "application/json")
req.Header.Set("content-encoding", "zstd")
req.Header.Set("authorization", "Bearer chatgpt-secret")
recorder := httptest.NewRecorder()
runtime.handleRelayProxyHTTP(recorder, req)
if recorder.Code != http.StatusOK {
t.Fatalf("proxy status = %d body=%s", recorder.Code, recorder.Body.String())
}
if upstreamAuthorization != "Bearer sk-provider" {
t.Fatalf("upstream authorization = %q", upstreamAuthorization)
}
var request map[string]any
if err := json.Unmarshal(upstreamBody, &request); err != nil || request["model"] != "gpt-5.6-sol" {
t.Fatalf("upstream body = %q err=%v", upstreamBody, err)
}
}
func TestResponsesWebSocketRequestsFallBackToHTTP(t *testing.T) {
runtime := &launcherRuntime{settings: defaultSettings()}
req := httptest.NewRequest(http.MethodGet, "http://127.0.0.1:57323/v1/responses", nil)
req.Header.Set("connection", "Upgrade")
req.Header.Set("upgrade", "websocket")
if !websocket.IsWebSocketUpgrade(req) {
t.Fatal("test request is not a WebSocket upgrade")
}
recorder := httptest.NewRecorder()
runtime.handleRelayProxyHTTP(recorder, req)
if recorder.Code != http.StatusUpgradeRequired || !strings.Contains(recorder.Body.String(), "protocol_proxy_http_only") {
t.Fatalf("upgrade response = HTTP %d %s", recorder.Code, recorder.Body.String())
}
}