File size: 7,675 Bytes
d61c097 0e6d51f d61c097 0e6d51f d61c097 0e6d51f d61c097 0e6d51f d61c097 0e6d51f d61c097 0e6d51f d61c097 0e6d51f d61c097 0e6d51f d61c097 0e6d51f d61c097 0e6d51f d61c097 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | package executor
import (
"bytes"
"context"
"io"
"testing"
"time"
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor"
sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator"
"github.com/tidwall/gjson"
)
func resetCodexCacheForTest() {
codexCacheMu.Lock()
codexCacheMap = make(map[string]codexCache)
codexCacheMu.Unlock()
}
func readRequestBodyForTest(t *testing.T, reqBody io.Reader) []byte {
t.Helper()
data, err := io.ReadAll(reqBody)
if err != nil {
t.Fatalf("read request body: %v", err)
}
return data
}
func TestApplyCodexPromptCache_ClaudeCachesByModelAndUserID(t *testing.T) {
resetCodexCacheForTest()
reqPayload := []byte(`{"metadata":{"user_id":"user-123"}}`)
rawJSON := []byte(`{"input":[]}`)
out, cacheID, headers := applyCodexPromptCache(nil, sdktranslator.FromString("claude"), reqPayload, "gpt-5", rawJSON, false)
if cacheID == "" {
t.Fatal("expected non-empty cache ID")
}
if got := gjson.GetBytes(out, "prompt_cache_key").String(); got != cacheID {
t.Fatalf("prompt_cache_key = %q, want %q", got, cacheID)
}
if got := headers.Get("Conversation_id"); got != cacheID {
t.Fatalf("Conversation_id = %q, want %q", got, cacheID)
}
if got := headers.Get("Session_id"); got != cacheID {
t.Fatalf("Session_id = %q, want %q", got, cacheID)
}
_, cacheID2, _ := applyCodexPromptCache(nil, sdktranslator.FromString("claude"), reqPayload, "gpt-5", []byte(`{"input":[1]}`), false)
if cacheID2 != cacheID {
t.Fatalf("expected stable cache ID for same model+user: first=%q second=%q", cacheID, cacheID2)
}
_, cacheIDOtherModel, _ := applyCodexPromptCache(nil, sdktranslator.FromString("claude"), reqPayload, "gpt-4.1", []byte(`{"input":[]}`), false)
if cacheIDOtherModel == cacheID {
t.Fatalf("expected different cache ID for different model, got %q", cacheIDOtherModel)
}
}
func TestApplyCodexPromptCache_OpenAIResponseUsesPromptCacheKey(t *testing.T) {
resetCodexCacheForTest()
reqPayload := []byte(`{"prompt_cache_key":"pck-123"}`)
rawJSON := []byte(`{"input":[]}`)
out, cacheID, headers := applyCodexPromptCache(nil, sdktranslator.FromString("openai-response"), reqPayload, "gpt-5", rawJSON, false)
if cacheID != "pck-123" {
t.Fatalf("cacheID = %q, want %q", cacheID, "pck-123")
}
if got := gjson.GetBytes(out, "prompt_cache_key").String(); got != "pck-123" {
t.Fatalf("prompt_cache_key = %q, want %q", got, "pck-123")
}
if got := headers.Get("Conversation_id"); got != "pck-123" {
t.Fatalf("Conversation_id = %q, want %q", got, "pck-123")
}
if got := headers.Get("Session_id"); got != "pck-123" {
t.Fatalf("Session_id = %q, want %q", got, "pck-123")
}
}
func TestApplyCodexPromptCache_OpenAIResponseRetentionDoesNotDisableExplicitKey(t *testing.T) {
resetCodexCacheForTest()
reqPayload := []byte(`{"prompt_cache_key":"pck-retained","prompt_cache_retention":"off"}`)
rawJSON := []byte(`{"input":[]}`)
out, cacheID, headers := applyCodexPromptCache(nil, sdktranslator.FromString("openai-response"), reqPayload, "gpt-5", rawJSON, false)
if cacheID != "pck-retained" {
t.Fatalf("cacheID = %q, want %q", cacheID, "pck-retained")
}
if got := gjson.GetBytes(out, "prompt_cache_key").String(); got != "pck-retained" {
t.Fatalf("prompt_cache_key = %q, want %q", got, "pck-retained")
}
if got := headers.Get("Conversation_id"); got != "pck-retained" {
t.Fatalf("Conversation_id = %q, want %q", got, "pck-retained")
}
if got := headers.Get("Session_id"); got != "pck-retained" {
t.Fatalf("Session_id = %q, want %q", got, "pck-retained")
}
}
func TestApplyCodexPromptCache_NoCacheInputLeavesPayloadUnchanged(t *testing.T) {
resetCodexCacheForTest()
rawJSON := []byte(`{"input":[]}`)
out, cacheID, headers := applyCodexPromptCache(nil, sdktranslator.FromString("codex"), []byte(`{"input":[]}`), "gpt-5", rawJSON, false)
if cacheID != "" {
t.Fatalf("cacheID = %q, want empty", cacheID)
}
if len(headers) != 0 {
t.Fatalf("headers = %#v, want empty", headers)
}
if !bytes.Equal(out, rawJSON) {
t.Fatalf("payload changed unexpectedly: got=%s want=%s", string(out), string(rawJSON))
}
if gjson.GetBytes(out, "prompt_cache_key").Exists() {
t.Fatalf("unexpected prompt_cache_key in payload: %s", string(out))
}
}
func TestApplyCodexPromptCacheHeaders_SkipsWhenBodyEmpty(t *testing.T) {
resetCodexCacheForTest()
out, headers := applyCodexPromptCacheHeaders(
nil,
sdktranslator.FromString("claude"),
[]byte(`{"metadata":{"user_id":"user-123"}}`),
"gpt-5",
[]byte{},
)
if len(out) != 0 {
t.Fatalf("expected empty output body, got: %s", string(out))
}
if len(headers) != 0 {
t.Fatalf("expected empty headers, got: %#v", headers)
}
}
func TestApplyCodexPromptCache_CodexRetentionAndUserAutoCache(t *testing.T) {
resetCodexCacheForTest()
reqPayload := []byte(`{"user":"user-xyz","prompt_cache_retention":"24h"}`)
rawJSON := []byte(`{"input":[]}`)
out, cacheID, headers := applyCodexPromptCache(nil, sdktranslator.FromString("codex"), reqPayload, "gpt-5", rawJSON, false)
if cacheID == "" {
t.Fatal("expected non-empty cache ID")
}
if got := gjson.GetBytes(out, "prompt_cache_key").String(); got != cacheID {
t.Fatalf("prompt_cache_key = %q, want %q", got, cacheID)
}
if got := headers.Get("Conversation_id"); got != cacheID {
t.Fatalf("Conversation_id = %q, want %q", got, cacheID)
}
if got := headers.Get("Session_id"); got != cacheID {
t.Fatalf("Session_id = %q, want %q", got, cacheID)
}
}
func TestApplyCodexPromptCache_RetentionDisabledSkipsAutoCache(t *testing.T) {
resetCodexCacheForTest()
reqPayload := []byte(`{"user":"user-xyz","prompt_cache_retention":"off"}`)
rawJSON := []byte(`{"input":[]}`)
out, cacheID, headers := applyCodexPromptCache(nil, sdktranslator.FromString("codex"), reqPayload, "gpt-5", rawJSON, false)
if cacheID != "" {
t.Fatalf("cacheID = %q, want empty", cacheID)
}
if len(headers) != 0 {
t.Fatalf("headers = %#v, want empty", headers)
}
if gjson.GetBytes(out, "prompt_cache_key").Exists() {
t.Fatalf("prompt_cache_key should not be set when retention disables cache")
}
}
func TestCodexPromptCacheEffectiveTTL_PreservesSubMinuteConfiguredTTL(t *testing.T) {
ttl := codexPromptCacheEffectiveTTL(&config.Config{CodexPromptCache: config.CodexPromptCacheConfig{TTLSeconds: 7}}, nil)
if ttl != 7*time.Second {
t.Fatalf("ttl = %v, want %v", ttl, 7*time.Second)
}
}
func TestCodexPromptCacheEffectiveTTL_PreservesSubMinuteRetentionTTL(t *testing.T) {
reqPayload := []byte(`{"prompt_cache_retention":5}`)
ttl := codexPromptCacheEffectiveTTL(nil, reqPayload)
if ttl != 5*time.Second {
t.Fatalf("ttl = %v, want %v", ttl, 5*time.Second)
}
}
func TestCodexExecutorCacheHelper_AppliesUnifiedCacheLogic(t *testing.T) {
resetCodexCacheForTest()
exec := &CodexExecutorRefactored{}
httpReq, err := exec.cacheHelper(
context.Background(),
sdktranslator.FromString("openai-response"),
"https://example.com/responses",
cliproxyexecutor.Request{
Model: "gpt-5",
Payload: []byte(`{"prompt_cache_key":"pck-456"}`),
},
[]byte(`{"input":[]}`),
)
if err != nil {
t.Fatalf("cacheHelper error: %v", err)
}
if got := httpReq.Header.Get("Conversation_id"); got != "pck-456" {
t.Fatalf("Conversation_id = %q, want %q", got, "pck-456")
}
if got := httpReq.Header.Get("Session_id"); got != "pck-456" {
t.Fatalf("Session_id = %q, want %q", got, "pck-456")
}
body := readRequestBodyForTest(t, httpReq.Body)
if got := gjson.GetBytes(body, "prompt_cache_key").String(); got != "pck-456" {
t.Fatalf("prompt_cache_key = %q, want %q", got, "pck-456")
}
}
|