]> git.giacomolli.uk - onechat/blobdiff - responses_test.go
Send the cache key in the session_id header
[onechat] / responses_test.go
index c1a7b4da78421149cb67e1ba0fe938dac1baade5..de928bd426ccd0f32f9ddfa0274d448fe1cb183a 100644 (file)
@@ -11,6 +11,7 @@ import (
        "path/filepath"
        "sync"
        "testing"
+       "time"
 
        "github.com/stretchr/testify/require"
 )
@@ -60,6 +61,42 @@ func TestClientSendsTheFixedRequestFields(t *testing.T) {
        require.Len(t, body["tools"], 1)
 }
 
+func TestClientSendsTheSessionHeaderToChatGPT(t *testing.T) {
+       login := newFakeAuth(t)
+       auth := newTestAuth(t, login)
+       writeTestTokens(t, auth, login.token(time.Hour), "refresh-0")
+
+       testCases := []struct {
+               name string
+               auth authorizer
+               want string
+       }{
+               {
+                       name: "ChatGPT backend",
+                       auth: auth,
+                       want: "agent-key",
+               },
+               {
+                       name: "OpenAI API",
+                       auth: apiKey(testKey),
+                       want: "",
+               },
+       }
+
+       for _, tc := range testCases {
+               t.Run(tc.name, func(t *testing.T) {
+                       server := newFakeResponses(t)
+                       client := newClient(server.URL, tc.auth, "agent-key", filepath.Join(t.TempDir(), "usage.jsonl"))
+                       _, err := client.Create(t.Context(), testRequest())
+                       require.NoError(t, err)
+
+                       call := server.Call(0)
+                       require.Equal(t, tc.want, call.header.Get("session_id"))
+                       require.Equal(t, "agent-key", call.body["prompt_cache_key"])
+               })
+       }
+}
+
 func TestClientReadsTheStream(t *testing.T) {
        server := newFakeResponses(t)
        server.reply = func(w http.ResponseWriter, call fakeCall) {
@@ -102,6 +139,7 @@ func TestClientRecordsEachCall(t *testing.T) {
                w.Header().Set("X-Codex-Primary-Used-Percent", "12")
                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"}`)