|
| 1 | +package pluginhost |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginabi" |
| 10 | + "github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginapi" |
| 11 | +) |
| 12 | + |
| 13 | +type testWebSocketObserverFunc func(context.Context, pluginapi.WebSocketResponseEvent) error |
| 14 | + |
| 15 | +func (f testWebSocketObserverFunc) ObserveWebSocketResponseEvent(ctx context.Context, event pluginapi.WebSocketResponseEvent) error { |
| 16 | + return f(ctx, event) |
| 17 | +} |
| 18 | + |
| 19 | +func TestObserveWebSocketResponseEventInvokesPlugin(t *testing.T) { |
| 20 | + var got pluginapi.WebSocketResponseEvent |
| 21 | + called := false |
| 22 | + host := newHostWithRecords(capabilityRecord{ |
| 23 | + id: "quota-tracker", |
| 24 | + plugin: pluginapi.Plugin{ |
| 25 | + Capabilities: pluginapi.Capabilities{ |
| 26 | + WebSocketResponseObserver: testWebSocketObserverFunc(func(_ context.Context, event pluginapi.WebSocketResponseEvent) error { |
| 27 | + got = event |
| 28 | + called = true |
| 29 | + return nil |
| 30 | + }), |
| 31 | + }, |
| 32 | + }, |
| 33 | + }) |
| 34 | + |
| 35 | + rawPayload := []byte(`{"type":"codex.rate_limits","rate_limits":{"primary":{"used_percent":42}}}`) |
| 36 | + host.ObserveWebSocketResponseEvent(context.Background(), pluginapi.WebSocketResponseEvent{ |
| 37 | + RequestID: "req-123", |
| 38 | + SourceFormat: "openai", |
| 39 | + Model: "gpt-5.3-codex", |
| 40 | + RequestedModel: "gpt-5.3-codex", |
| 41 | + Provider: "codex", |
| 42 | + AuthID: "auth-abc", |
| 43 | + AuthLabel: "test-auth", |
| 44 | + AuthType: "oauth", |
| 45 | + EventType: "codex.rate_limits", |
| 46 | + Payload: rawPayload, |
| 47 | + }) |
| 48 | + |
| 49 | + if !called { |
| 50 | + t.Fatal("observer callback was not invoked") |
| 51 | + } |
| 52 | + if got.RequestID != "req-123" { |
| 53 | + t.Fatalf("RequestID = %q, want req-123", got.RequestID) |
| 54 | + } |
| 55 | + if got.Provider != "codex" { |
| 56 | + t.Fatalf("Provider = %q, want codex", got.Provider) |
| 57 | + } |
| 58 | + if got.AuthID != "auth-abc" || got.AuthLabel != "test-auth" { |
| 59 | + t.Fatalf("Auth = (%q, %q), want (auth-abc, test-auth)", got.AuthID, got.AuthLabel) |
| 60 | + } |
| 61 | + if got.EventType != "codex.rate_limits" { |
| 62 | + t.Fatalf("EventType = %q, want codex.rate_limits", got.EventType) |
| 63 | + } |
| 64 | + if !bytes.Equal(got.Payload, rawPayload) { |
| 65 | + t.Fatalf("Payload = %s, want %s", got.Payload, rawPayload) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestObserveWebSocketResponseEventClonesPayloadAndMetadata(t *testing.T) { |
| 70 | + ctx, cancel := context.WithCancel(context.Background()) |
| 71 | + cancel() |
| 72 | + originalPayload := []byte(`{"type":"codex.rate_limits","rate_limits":{"primary":{"used_percent":1}}}`) |
| 73 | + originalMetadata := map[string]any{"key": "value"} |
| 74 | + called := false |
| 75 | + |
| 76 | + host := newHostWithRecords(capabilityRecord{ |
| 77 | + id: "observer-clone", |
| 78 | + plugin: pluginapi.Plugin{ |
| 79 | + Capabilities: pluginapi.Capabilities{ |
| 80 | + WebSocketResponseObserver: testWebSocketObserverFunc(func(_ context.Context, event pluginapi.WebSocketResponseEvent) error { |
| 81 | + event.Payload[0] = 'X' |
| 82 | + event.Metadata["key"] = "mutated" |
| 83 | + called = true |
| 84 | + return nil |
| 85 | + }), |
| 86 | + }, |
| 87 | + }, |
| 88 | + }) |
| 89 | + |
| 90 | + host.ObserveWebSocketResponseEvent(ctx, pluginapi.WebSocketResponseEvent{ |
| 91 | + RequestID: "req-clone", |
| 92 | + Payload: originalPayload, |
| 93 | + Metadata: originalMetadata, |
| 94 | + }) |
| 95 | + |
| 96 | + if !called { |
| 97 | + t.Fatal("observer callback was not invoked") |
| 98 | + } |
| 99 | + if originalPayload[0] != '{' { |
| 100 | + t.Fatalf("original payload was mutated: %s", originalPayload) |
| 101 | + } |
| 102 | + if originalMetadata["key"] != "value" { |
| 103 | + t.Fatalf("original metadata was mutated: %#v", originalMetadata) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func TestObserveWebSocketResponseEventFusesOnPanic(t *testing.T) { |
| 108 | + host := newHostWithRecords(capabilityRecord{ |
| 109 | + id: "panicking-observer", |
| 110 | + plugin: pluginapi.Plugin{ |
| 111 | + Capabilities: pluginapi.Capabilities{ |
| 112 | + WebSocketResponseObserver: testWebSocketObserverFunc(func(context.Context, pluginapi.WebSocketResponseEvent) error { |
| 113 | + panic("observer panic") |
| 114 | + }), |
| 115 | + }, |
| 116 | + }, |
| 117 | + }) |
| 118 | + |
| 119 | + if !host.HasWebSocketResponseObservers() { |
| 120 | + t.Fatal("HasWebSocketResponseObservers() = false, want true") |
| 121 | + } |
| 122 | + |
| 123 | + host.ObserveWebSocketResponseEvent(context.Background(), pluginapi.WebSocketResponseEvent{ |
| 124 | + RequestID: "req-panic", |
| 125 | + Payload: []byte(`{"type":"test"}`), |
| 126 | + }) |
| 127 | + |
| 128 | + if !host.isPluginFused("panicking-observer") { |
| 129 | + t.Fatal("isPluginFused(panicking-observer) = false, want true") |
| 130 | + } |
| 131 | + if host.HasWebSocketResponseObservers() { |
| 132 | + t.Fatal("HasWebSocketResponseObservers() = true after fusing, want false") |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func TestObserveWebSocketResponseEventSkipPlugin(t *testing.T) { |
| 137 | + calls := 0 |
| 138 | + host := newHostWithRecords(capabilityRecord{ |
| 139 | + id: "skipped-plugin", |
| 140 | + plugin: pluginapi.Plugin{ |
| 141 | + Capabilities: pluginapi.Capabilities{ |
| 142 | + WebSocketResponseObserver: testWebSocketObserverFunc(func(context.Context, pluginapi.WebSocketResponseEvent) error { |
| 143 | + calls++ |
| 144 | + return nil |
| 145 | + }), |
| 146 | + }, |
| 147 | + }, |
| 148 | + }) |
| 149 | + |
| 150 | + host.ObserveWebSocketResponseEventExcept(context.Background(), pluginapi.WebSocketResponseEvent{ |
| 151 | + RequestID: "req-skip", |
| 152 | + }, "skipped-plugin") |
| 153 | + |
| 154 | + if calls != 0 { |
| 155 | + t.Fatalf("observer calls = %d, want 0 when skipped", calls) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func TestRegisterRPCPluginRegistersWebSocketResponseObserver(t *testing.T) { |
| 160 | + lookup := newTestSymbolLookup(&testPlugin{ |
| 161 | + registerResult: pluginapi.Plugin{ |
| 162 | + Capabilities: pluginapi.Capabilities{ |
| 163 | + WebSocketResponseObserver: testWebSocketObserverFunc(func(context.Context, pluginapi.WebSocketResponseEvent) error { |
| 164 | + return nil |
| 165 | + }), |
| 166 | + }, |
| 167 | + }, |
| 168 | + }) |
| 169 | + |
| 170 | + registered, errRegister := registerRPCPlugin(context.Background(), nil, "rpc-observer", lookup, pluginabi.MethodPluginRegister, nil) |
| 171 | + if errRegister != nil { |
| 172 | + t.Fatalf("registerRPCPlugin() error = %v", errRegister) |
| 173 | + } |
| 174 | + if registered.Capabilities.WebSocketResponseObserver == nil { |
| 175 | + t.Fatal("WebSocketResponseObserver = nil, want RPC adapter") |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +type rpcObserverRecordingClient struct { |
| 180 | + lastMethod string |
| 181 | + lastRequest []byte |
| 182 | +} |
| 183 | + |
| 184 | +func (c *rpcObserverRecordingClient) Call(_ context.Context, method string, request []byte) ([]byte, error) { |
| 185 | + c.lastMethod = method |
| 186 | + c.lastRequest = bytes.Clone(request) |
| 187 | + return json.Marshal(pluginabi.Envelope{OK: true, Result: json.RawMessage(`{}`)}) |
| 188 | +} |
| 189 | + |
| 190 | +func (c *rpcObserverRecordingClient) Shutdown() {} |
| 191 | + |
| 192 | +func TestObserveWebSocketResponseEventRPCSanitizesMetadata(t *testing.T) { |
| 193 | + client := &rpcObserverRecordingClient{} |
| 194 | + adapter := &rpcPluginAdapter{ |
| 195 | + id: "rpc-observer-sanitize", |
| 196 | + client: client, |
| 197 | + } |
| 198 | + |
| 199 | + rawPayload := []byte(`{"type":"codex.rate_limits","rate_limits":{"primary":{"used_percent":99}}}`) |
| 200 | + unserializableMetadata := map[string]any{ |
| 201 | + "safe_key": "safe_value", |
| 202 | + "func_field": func() {}, |
| 203 | + "chan_field": make(chan int), |
| 204 | + } |
| 205 | + |
| 206 | + err := adapter.ObserveWebSocketResponseEvent(context.Background(), pluginapi.WebSocketResponseEvent{ |
| 207 | + RequestID: "req-rpc-sanitize", |
| 208 | + SourceFormat: "openai", |
| 209 | + Model: "gpt-5.3-codex", |
| 210 | + RequestedModel: "gpt-5.3-codex", |
| 211 | + Provider: "codex", |
| 212 | + AuthID: "auth-xyz", |
| 213 | + AuthLabel: "test-auth-rpc", |
| 214 | + AuthType: "oauth", |
| 215 | + EventType: "codex.rate_limits", |
| 216 | + Payload: rawPayload, |
| 217 | + Metadata: unserializableMetadata, |
| 218 | + }) |
| 219 | + if err != nil { |
| 220 | + t.Fatalf("ObserveWebSocketResponseEvent() error = %v", err) |
| 221 | + } |
| 222 | + |
| 223 | + if client.lastMethod != pluginabi.MethodWebSocketResponseEvent { |
| 224 | + t.Fatalf("lastMethod = %q, want %q", client.lastMethod, pluginabi.MethodWebSocketResponseEvent) |
| 225 | + } |
| 226 | + |
| 227 | + var decoded rpcWebSocketResponseEvent |
| 228 | + if errUnmarshal := json.Unmarshal(client.lastRequest, &decoded); errUnmarshal != nil { |
| 229 | + t.Fatalf("unmarshal rpc request: %v", errUnmarshal) |
| 230 | + } |
| 231 | + |
| 232 | + if decoded.RequestID != "req-rpc-sanitize" { |
| 233 | + t.Fatalf("decoded RequestID = %q, want req-rpc-sanitize", decoded.RequestID) |
| 234 | + } |
| 235 | + if decoded.EventType != "codex.rate_limits" { |
| 236 | + t.Fatalf("decoded EventType = %q, want codex.rate_limits", decoded.EventType) |
| 237 | + } |
| 238 | + if decoded.Metadata["safe_key"] != "safe_value" { |
| 239 | + t.Fatalf("decoded safe_key = %v, want safe_value", decoded.Metadata["safe_key"]) |
| 240 | + } |
| 241 | + if _, exists := decoded.Metadata["func_field"]; exists { |
| 242 | + t.Fatalf("func_field was not sanitized: %#v", decoded.Metadata) |
| 243 | + } |
| 244 | + if _, exists := decoded.Metadata["chan_field"]; exists { |
| 245 | + t.Fatalf("chan_field was not sanitized: %#v", decoded.Metadata) |
| 246 | + } |
| 247 | +} |
0 commit comments