X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/cb514b662bcf9d90b2cf02789c989f777d774d3f..c76d3832afce8da932860294d27bd167bf8a928c:/model_test.go?ds=sidebyside diff --git a/model_test.go b/model_test.go index 11a7d00..97be6e2 100644 --- a/model_test.go +++ b/model_test.go @@ -50,6 +50,7 @@ func TestClientSendsTheFixedRequestFields(t *testing.T) { map[string]any{"type": "input_text", "text": "hello"}, }, }}, body["input"]) + require.Len(t, body["tools"], 1) } @@ -64,12 +65,12 @@ func TestClientSendsTheSessionHeaderToChatGPT(t *testing.T) { want string }{ { - name: "ChatGPT backend", + name: "sends the session header to ChatGPT", auth: auth, want: "agent-key", }, { - name: "OpenAI API", + name: "omits the session header for the API", auth: ModelAPIKey(modelTestKey), want: "", }, @@ -104,7 +105,7 @@ func TestClientSendsTheSessionHeaderToChatGPT(t *testing.T) { func TestClientReadsTheStream(t *testing.T) { server := newModelFake(t) - server.reply = func(w http.ResponseWriter, call ModelFakeCall) { + server.reply = func(w http.ResponseWriter, _ ModelFakeCall) { modelWriteEvents(w, map[string]any{"type": "response.created", "response": map[string]any{}}, map[string]any{"type": "response.output_item.done", "item": map[string]any{"type": "reasoning", "encrypted_content": "opaque"}}, @@ -129,7 +130,7 @@ func TestClientReadsTheStream(t *testing.T) { func TestClientTakesTheOutputOfTheCompletedResponse(t *testing.T) { server := newModelFake(t) - server.reply = func(w http.ResponseWriter, call ModelFakeCall) { + server.reply = func(w http.ResponseWriter, _ ModelFakeCall) { modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem("only here")}, 10, 0, 2)) } @@ -145,8 +146,10 @@ func TestClientRecordsEachCall(t *testing.T) { w.Header().Set("X-Codex-Plan-Type", "plus") w.Header().Set("X-Other", "not quota") w.Header().Set("X-Codex-Turn-State", "opaque state") + if call.number == 1 { w.WriteHeader(http.StatusTooManyRequests) + _, _ = io.WriteString(w, `{"detail": "slow down"}`) return @@ -176,6 +179,7 @@ func TestClientRecordsEachCall(t *testing.T) { "x-codex-primary-used-percent": "12", "x-codex-plan-type": "plus", } + require.Equal(t, "gpt-test", records[0].Model) require.Equal(t, http.StatusOK, records[0].Status) require.Equal(t, 500, records[0].InputTokens) @@ -213,11 +217,13 @@ func TestClientRefusesCallsOverTheLimit(t *testing.T) { for range 2 { require.False(t, client.LimitReached()) + _, err := client.Create(t.Context(), modelTestRequest()) require.NoError(t, err) } require.True(t, client.LimitReached()) + _, err := client.Create(t.Context(), modelTestRequest()) require.ErrorIs(t, err, errModelCallLimit) require.Equal(t, 2, server.Calls()) @@ -231,22 +237,22 @@ func TestClientFailsWhenTheResponseFails(t *testing.T) { want string }{ { - name: "failed response", + name: "reports the response failure cause", events: []any{map[string]any{"type": "response.failed", "response": map[string]any{"error": map[string]any{"code": "server_error", "message": "busy"}}}}, want: "server_error: busy", }, { - name: "incomplete response", + name: "reports why the response is incomplete", events: []any{map[string]any{"type": "response.incomplete", "response": map[string]any{"incomplete_details": map[string]any{"reason": "content_filter"}}}}, want: "content_filter", }, { - name: "error event", + name: "reports the stream error cause", events: []any{map[string]any{"type": "error", "code": "rate_limit", "message": "too many"}}, want: "rate_limit: too many", }, { - name: "stream ends early", + name: "refuses an unfinished response stream", events: []any{map[string]any{"type": "response.output_item.done", "item": modelMessageItem("cut")}}, want: "ended before the response completed", }, @@ -255,7 +261,7 @@ func TestClientFailsWhenTheResponseFails(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { server := newModelFake(t) - server.reply = func(w http.ResponseWriter, call ModelFakeCall) { + server.reply = func(w http.ResponseWriter, _ ModelFakeCall) { modelWriteEvents(w, tc.events...) }