Repository navigation
test: prove Grok uses xAI responses upstream #809
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ package cliproxyapi_grok | |
| import ( | ||
| "context" | ||
| "errors" | ||
| "io" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
|
|
@@ -159,6 +160,81 @@ func TestEnsureOpenAIStreamFinishBeforeDoneDoesNotDuplicateFinishReason(t *testi | |
| } | ||
| } | ||
|
|
||
| func TestExecuteOpenAIChatUsesXAIResponsesUpstreamAndReturnsOpenAIShape(t *testing.T) { | ||
| var gotPath string | ||
| var gotAuth string | ||
| var gotAccept string | ||
| var gotBody []byte | ||
| upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| gotPath = r.URL.Path | ||
| gotAuth = r.Header.Get("Authorization") | ||
| gotAccept = r.Header.Get("Accept") | ||
| var err error | ||
| gotBody, err = io.ReadAll(r.Body) | ||
| if err != nil { | ||
| t.Fatalf("read upstream body: %v", err) | ||
| } | ||
| w.Header().Set("Content-Type", "text/event-stream") | ||
| _, _ = w.Write([]byte(`data: {"type":"response.completed","response":{"id":"resp_grok_1","object":"response","created_at":0,"status":"completed","model":"grok-4","output":[{"type":"message","role":"assistant","content":[{"type":"output_text","text":"mock-grok-ok via /responses"}]}],"usage":{"input_tokens":3,"output_tokens":4,"total_tokens":7}}}` + "\n\n")) | ||
| })) | ||
| defer upstream.Close() | ||
|
|
||
| provider := &domain.Provider{ | ||
| ID: 42, | ||
| Type: "grok", | ||
| Name: "Grok Test", | ||
| Config: &domain.ProviderConfig{Grok: &domain.ProviderConfigGrok{ | ||
| Type: "xai", | ||
| AuthKind: "oauth", | ||
| AccessToken: "access-token", | ||
| RefreshToken: "refresh-token", | ||
| BaseURL: upstream.URL, | ||
| }}, | ||
| } | ||
| adapter, err := NewAdapter(provider) | ||
| if err != nil { | ||
| t.Fatalf("NewAdapter() error = %v", err) | ||
| } | ||
| grok := adapter.(*CLIProxyAPIGrokAdapter) | ||
|
|
||
| body := []byte(`{"model":"grok-4","messages":[{"role":"user","content":"say ok"}]}`) | ||
| req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(string(body))) | ||
| rec := httptest.NewRecorder() | ||
| c := flow.NewCtx(rec, req) | ||
| c.Set(flow.KeyClientType, domain.ClientTypeOpenAI) | ||
| c.Set(flow.KeyRequestBody, body) | ||
| c.Set(flow.KeyRequestModel, "grok-4") | ||
| c.Set(flow.KeyMappedModel, "grok-4") | ||
| c.Set(flow.KeyIsStream, false) | ||
| c.Set(flow.KeyRequestURI, "/v1/chat/completions") | ||
|
|
||
| if err := grok.Execute(c, provider); err != nil { | ||
| t.Fatalf("Execute() error = %v", err) | ||
| } | ||
| got := rec.Body.String() | ||
| if rec.Code != http.StatusOK { | ||
| t.Fatalf("status = %d, want 200; body=%s", rec.Code, got) | ||
| } | ||
| if gotPath != "/responses" { | ||
| t.Fatalf("upstream path = %q, want /responses", gotPath) | ||
| } | ||
| if gotAuth != "Bearer access-token" { | ||
| t.Fatalf("Authorization = %q, want Bearer access-token", gotAuth) | ||
| } | ||
| if gotAccept != "text/event-stream" { | ||
| t.Fatalf("Accept = %q, want text/event-stream", gotAccept) | ||
| } | ||
| if strings.Contains(string(gotBody), "chat/completions") { | ||
| t.Fatalf("upstream body leaked client chat endpoint: %s", string(gotBody)) | ||
| } | ||
| if !strings.Contains(string(gotBody), `"model":"grok-4"`) || !strings.Contains(string(gotBody), `"stream":true`) { | ||
| t.Fatalf("upstream body was not shaped as xAI Responses payload: %s", string(gotBody)) | ||
| } | ||
|
Comment on lines
+227
to
+232
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win 验证 xAI Responses 请求字段。 当前断言只检查 解析 🤖 Prompt for AI Agents |
||
| if !strings.Contains(got, `"object":"chat.completion"`) || !strings.Contains(got, "mock-grok-ok via /responses") { | ||
| t.Fatalf("client-visible OpenAI response missing translated content: %s", got) | ||
| } | ||
| } | ||
|
|
||
| func TestExecuteStreamReturnsClientVisibleGrokContentAndFinishReason(t *testing.T) { | ||
| provider := &domain.Provider{ | ||
| ID: 42, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: awsl-project/maxx
Length of output: 484
🏁 Script executed:
Repository: awsl-project/maxx
Length of output: 6153
🌐 Web query:
Go httptest.NewRequestWithContext introduced version💡 Result:
The httptest.NewRequestWithContext function was introduced in Go 1.23.0 [1][2][3]. It allows for creating an incoming server request for testing purposes with a specified context.Context [1][4]. This addition was tracked in issue
#59473and implemented to align the httptest API with the existing net/http.NewRequestWithContext functionality [5]. Note that net/http.NewRequestWithContext (for client requests) was introduced much earlier, in Go 1.13 [6].Citations:
🏁 Script executed:
Repository: awsl-project/maxx
Length of output: 934
🏁 Script executed:
Repository: awsl-project/maxx
Length of output: 17599
使用带 Context 的测试请求构造器。
仓库声明使用 Go 1.26.0,支持
httptest.NewRequestWithContext。将此调用改为该 API,并传入测试 Context 或context.Background(),以避免noctx检查失败。🧰 Tools
🪛 golangci-lint (2.12.2)
[error] 201-201: net/http/httptest.NewRequest must not be called. use net/http/httptest.NewRequestWithContext
(noctx)
🤖 Prompt for AI Agents
Source: Linters/SAST tools