File size: 3,184 Bytes
0e6d51f | 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 | package executor
import (
"testing"
"time"
miniredis "github.com/alicebob/miniredis/v2"
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
)
func TestGetOrCreateCodexPromptCacheRedis_StableKey(t *testing.T) {
resetCodexPromptCacheRedisForTest()
defer resetCodexPromptCacheRedisForTest()
mr, err := miniredis.Run()
if err != nil {
t.Fatalf("start miniredis: %v", err)
}
defer mr.Close()
cfg := &config.Config{}
cfg.CodexPromptCache.RedisURL = "redis://" + mr.Addr() + "/0"
cfg.CodexPromptCache.KeyPrefix = "cliproxy:test:"
cfg.CodexPromptCache.TTLSeconds = 3600
cfg.CodexPromptCache.TimeoutMS = 500
id1, ok1 := getOrCreateCodexPromptCacheRedis(cfg, "gpt-5-user-1", 0)
if !ok1 || id1 == "" {
t.Fatalf("first redis cache id not created: ok=%v id=%q", ok1, id1)
}
id2, ok2 := getOrCreateCodexPromptCacheRedis(cfg, "gpt-5-user-1", 0)
if !ok2 || id2 == "" {
t.Fatalf("second redis cache id not returned: ok=%v id=%q", ok2, id2)
}
if id1 != id2 {
t.Fatalf("redis cache id mismatch: %q vs %q", id1, id2)
}
}
func TestGetOrCreateCodexPromptCacheRedis_FallbackOnInvalidURL(t *testing.T) {
resetCodexPromptCacheRedisForTest()
defer resetCodexPromptCacheRedisForTest()
cfg := &config.Config{}
cfg.CodexPromptCache.RedisURL = "::://invalid"
cfg.CodexPromptCache.TimeoutMS = 100
id, ok := getOrCreateCodexPromptCacheRedis(cfg, "gpt-5-user-1", 0)
if ok {
t.Fatalf("expected redis disabled/fallback on invalid URL, got ok=true id=%q", id)
}
}
func TestGetOrCreateCodexPromptCacheRedis_UsesConfiguredTTL(t *testing.T) {
resetCodexPromptCacheRedisForTest()
defer resetCodexPromptCacheRedisForTest()
mr, err := miniredis.Run()
if err != nil {
t.Fatalf("start miniredis: %v", err)
}
defer mr.Close()
cfg := &config.Config{}
cfg.CodexPromptCache.RedisURL = "redis://" + mr.Addr() + "/0"
cfg.CodexPromptCache.KeyPrefix = "cliproxy:test:"
cfg.CodexPromptCache.TTLSeconds = 7
cfg.CodexPromptCache.TimeoutMS = 500
_, ok := getOrCreateCodexPromptCacheRedis(cfg, "gpt-5-user-ttl", 0)
if !ok {
t.Fatalf("expected redis set to succeed")
}
ttl := mr.TTL("cliproxy:test:gpt-5-user-ttl")
if ttl <= 0 {
t.Fatalf("expected positive ttl, got %v", ttl)
}
if ttl > 8*time.Second || ttl < 6*time.Second {
t.Fatalf("expected ttl around 7s, got %v", ttl)
}
}
func TestGetOrCreateCodexPromptCacheRedis_RequestTTLOverridesConfiguredTTL(t *testing.T) {
resetCodexPromptCacheRedisForTest()
defer resetCodexPromptCacheRedisForTest()
mr, err := miniredis.Run()
if err != nil {
t.Fatalf("start miniredis: %v", err)
}
defer mr.Close()
cfg := &config.Config{}
cfg.CodexPromptCache.RedisURL = "redis://" + mr.Addr() + "/0"
cfg.CodexPromptCache.KeyPrefix = "cliproxy:test:"
cfg.CodexPromptCache.TTLSeconds = 3600
cfg.CodexPromptCache.TimeoutMS = 500
_, ok := getOrCreateCodexPromptCacheRedis(cfg, "gpt-5-user-ttl-override", 75*time.Second)
if !ok {
t.Fatalf("expected redis set to succeed")
}
ttl := mr.TTL("cliproxy:test:gpt-5-user-ttl-override")
if ttl <= 0 {
t.Fatalf("expected positive ttl, got %v", ttl)
}
if ttl > 76*time.Second || ttl < 74*time.Second {
t.Fatalf("expected ttl around 75s, got %v", ttl)
}
}
|