|
| 1 | +package executor |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "strings" |
| 9 | + "sync" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/gorilla/websocket" |
| 14 | + "github.com/router-for-me/CLIProxyAPI/v7/internal/config" |
| 15 | + "github.com/router-for-me/CLIProxyAPI/v7/internal/wsrelay" |
| 16 | + cliproxyauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" |
| 17 | + cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/executor" |
| 18 | + "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/usage" |
| 19 | + sdktranslator "github.com/router-for-me/CLIProxyAPI/v7/sdk/translator" |
| 20 | +) |
| 21 | + |
| 22 | +func TestAIStudioExecutorExecuteStartsTTFTBeforeRelayWait(t *testing.T) { |
| 23 | + const authID = "aistudio-ttft-auth" |
| 24 | + delay := 40 * time.Millisecond |
| 25 | + connected := make(chan struct{}) |
| 26 | + var connectedOnce sync.Once |
| 27 | + relay := wsrelay.NewManager(wsrelay.Options{ |
| 28 | + ProviderFactory: func(*http.Request) (string, error) { |
| 29 | + return authID, nil |
| 30 | + }, |
| 31 | + OnConnected: func(provider string) { |
| 32 | + if provider == authID { |
| 33 | + connectedOnce.Do(func() { |
| 34 | + close(connected) |
| 35 | + }) |
| 36 | + } |
| 37 | + }, |
| 38 | + }) |
| 39 | + server := httptest.NewServer(relay.Handler()) |
| 40 | + defer server.Close() |
| 41 | + defer func() { |
| 42 | + if errStop := relay.Stop(context.Background()); errStop != nil { |
| 43 | + t.Errorf("relay stop error = %v", errStop) |
| 44 | + } |
| 45 | + }() |
| 46 | + |
| 47 | + wsURL := "ws" + strings.TrimPrefix(server.URL, "http") + relay.Path() |
| 48 | + conn, _, errDial := websocket.DefaultDialer.Dial(wsURL, nil) |
| 49 | + if errDial != nil { |
| 50 | + t.Fatalf("dial websocket: %v", errDial) |
| 51 | + } |
| 52 | + defer func() { |
| 53 | + if errClose := conn.Close(); errClose != nil { |
| 54 | + t.Errorf("websocket close error = %v", errClose) |
| 55 | + } |
| 56 | + }() |
| 57 | + select { |
| 58 | + case <-connected: |
| 59 | + case <-time.After(time.Second): |
| 60 | + t.Fatal("timed out waiting for relay connection") |
| 61 | + } |
| 62 | + |
| 63 | + clientDone := make(chan error, 1) |
| 64 | + go func() { |
| 65 | + var msg wsrelay.Message |
| 66 | + if errReadJSON := conn.ReadJSON(&msg); errReadJSON != nil { |
| 67 | + clientDone <- fmt.Errorf("read relay request: %w", errReadJSON) |
| 68 | + return |
| 69 | + } |
| 70 | + if msg.Type != wsrelay.MessageTypeHTTPReq { |
| 71 | + clientDone <- fmt.Errorf("relay message type = %q, want %q", msg.Type, wsrelay.MessageTypeHTTPReq) |
| 72 | + return |
| 73 | + } |
| 74 | + time.Sleep(delay) |
| 75 | + response := wsrelay.Message{ |
| 76 | + ID: msg.ID, |
| 77 | + Type: wsrelay.MessageTypeHTTPResp, |
| 78 | + Payload: map[string]any{ |
| 79 | + "status": float64(http.StatusOK), |
| 80 | + "headers": map[string]any{"Content-Type": "application/json"}, |
| 81 | + "body": `{"candidates":[{"content":{"role":"model","parts":[{"text":"ok"}]},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":1,"candidatesTokenCount":1,"totalTokenCount":2}}`, |
| 82 | + }, |
| 83 | + } |
| 84 | + if errWriteJSON := conn.WriteJSON(response); errWriteJSON != nil { |
| 85 | + clientDone <- fmt.Errorf("write relay response: %w", errWriteJSON) |
| 86 | + return |
| 87 | + } |
| 88 | + clientDone <- nil |
| 89 | + }() |
| 90 | + |
| 91 | + plugin := &captureAIStudioUsagePlugin{records: make(chan usage.Record, 16)} |
| 92 | + usage.RegisterPlugin(plugin) |
| 93 | + exec := NewAIStudioExecutor(&config.Config{}, "aistudio", relay) |
| 94 | + _, errExecute := exec.Execute(context.Background(), &cliproxyauth.Auth{ID: authID, Provider: "aistudio"}, cliproxyexecutor.Request{ |
| 95 | + Model: "gemini-3.1-pro-preview", |
| 96 | + Payload: []byte(`{"contents":[{"role":"user","parts":[{"text":"hi"}]}]}`), |
| 97 | + }, cliproxyexecutor.Options{SourceFormat: sdktranslator.FormatGemini}) |
| 98 | + if errExecute != nil { |
| 99 | + t.Fatalf("Execute() error = %v", errExecute) |
| 100 | + } |
| 101 | + if errClient := <-clientDone; errClient != nil { |
| 102 | + t.Fatal(errClient) |
| 103 | + } |
| 104 | + |
| 105 | + record := waitForAIStudioUsageRecord(t, plugin.records, "gemini-3.1-pro-preview") |
| 106 | + if record.TTFT < delay { |
| 107 | + t.Fatalf("ttft = %v, want >= %v", record.TTFT, delay) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +type captureAIStudioUsagePlugin struct { |
| 112 | + records chan usage.Record |
| 113 | +} |
| 114 | + |
| 115 | +func (p *captureAIStudioUsagePlugin) HandleUsage(_ context.Context, record usage.Record) { |
| 116 | + if p == nil { |
| 117 | + return |
| 118 | + } |
| 119 | + select { |
| 120 | + case p.records <- record: |
| 121 | + default: |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func waitForAIStudioUsageRecord(t *testing.T, records <-chan usage.Record, model string) usage.Record { |
| 126 | + t.Helper() |
| 127 | + timeout := time.After(2 * time.Second) |
| 128 | + for { |
| 129 | + select { |
| 130 | + case record := <-records: |
| 131 | + if record.Provider == "aistudio" && record.Model == model { |
| 132 | + return record |
| 133 | + } |
| 134 | + case <-timeout: |
| 135 | + t.Fatalf("timed out waiting for AI Studio usage record") |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments