-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathcache_integration_test.go
More file actions
216 lines (191 loc) · 6.39 KB
/
Copy pathcache_integration_test.go
File metadata and controls
216 lines (191 loc) · 6.39 KB
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
package main
import (
"bytes"
"context"
"encoding/json"
"gateway/internal/ai"
"net/http"
"net/http/httptest"
"strconv"
"sync/atomic"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/redis/go-redis/v9"
)
func TestCacheIntegration_FullFlow(t *testing.T) {
// 1. Check Redis availability
rdb := redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
})
ctx := context.Background()
if err := rdb.Ping(ctx).Err(); err != nil {
t.Skipf("Redis unavailable, skipping integration test: %v", err)
}
// 3. Setup Dependencies (Environment)
// Mock Verifier
verifier := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Mock validation based on signature
var req VerifyRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "Invalid verification request", http.StatusBadRequest)
return
}
isValid := req.Signature == "0xValidSig"
resp := VerifyResponse{
IsValid: isValid,
RecoveredAddress: "0x14791697260e4c9a71f18484c9f997b308e59325",
Error: "",
}
if !isValid {
resp.Error = "Invalid signature"
}
if err := json.NewEncoder(w).Encode(resp); err != nil {
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
}
}))
defer verifier.Close()
// Mock OpenRouter (AI)
// Use small delay to simulate processing so we can verify cache speedup
var aiCalls atomic.Int32
aiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
aiCalls.Add(1)
time.Sleep(100 * time.Millisecond)
w.WriteHeader(200)
w.Write([]byte(`{"choices":[{"message":{"content":"AI Summary Result"}}]}`))
}))
defer aiServer.Close()
// Set Env Vars using t.Setenv for auto-cleanup
t.Setenv("CACHE_ENABLED", "true")
t.Setenv("RECEIPT_STORE", "memory")
t.Setenv("REDIS_URL", "127.0.0.1:6379")
t.Setenv("VERIFIER_URL", verifier.URL)
t.Setenv("AI_PROVIDER", "openrouter")
t.Setenv("OPENROUTER_URL", aiServer.URL)
t.Setenv("OPENROUTER_API_KEY", "test-key")
t.Setenv("SERVER_WALLET_PRIVATE_KEY", "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
t.Setenv("RECIPIENT_ADDRESS", "0xTestRecipient")
// 4. Initialize Gateway logic
if err := initRedis(); err != nil {
t.Fatalf("Failed to initialize Redis: %v", err)
}
defer func() {
if redisClient != nil {
redisClient.Close()
redisClient = nil
}
}()
// Initialize AI provider for the test
var err error
aiProvider, err = ai.NewProvider()
if err != nil {
t.Fatalf("Failed to initialize AI provider: %v", err)
}
gin.SetMode(gin.TestMode)
r := gin.New()
r.Use(RequestTimeoutMiddleware(5 * time.Second))
r.POST("/api/ai/summarize", CacheMiddleware(), handleSummarize)
// 5. Test execution
textToSummarize := "This is a unique text for cache integration test " + time.Now().String()
model := "z-ai/glm-4.5-air:free" // Default model
cacheKey := getCacheKey(textToSummarize, model)
// Helper to make request
makeRequest := func(sig string) *httptest.ResponseRecorder {
t.Helper()
reqBody := map[string]string{"text": textToSummarize}
jsonBody, err := json.Marshal(reqBody)
if err != nil {
t.Fatalf("Failed to marshal request body: %v", err)
}
req, err := http.NewRequest("POST", "/api/ai/summarize", bytes.NewBuffer(jsonBody))
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-402-Signature", sig)
req.Header.Set("X-402-Payer", "0x14791697260E4c9A71f18484C9f997B308e59325")
req.Header.Set("X-402-Nonce", "nonce-123")
req.Header.Set("X-402-Timestamp", strconv.FormatInt(time.Now().Unix(), 10))
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
}
// Clean up cache key before starting
rdb.Del(ctx, cacheKey)
defer rdb.Del(ctx, cacheKey)
// Request 1: Cache Miss (Valid Sig)
start := time.Now()
w1 := makeRequest("0xValidSig")
duration1 := time.Since(start)
if w1.Code != 200 {
t.Fatalf("Request 1 failed: %d body=%s", w1.Code, w1.Body.String())
}
if aiCalls.Load() != 1 {
t.Errorf("Expected 1 AI call, got %d", aiCalls.Load())
}
if duration1 < 100*time.Millisecond {
t.Errorf("Request 1 was too fast (%v), expected >100ms delay", duration1)
}
// Wait for async cache set (polling)
assertCachePopulated := func() {
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
exists, err := rdb.Exists(ctx, cacheKey).Result()
if err == nil && exists > 0 {
return // Cache populated
}
time.Sleep(50 * time.Millisecond)
}
t.Errorf("Cache key %s not populated after 2s", cacheKey)
}
assertCachePopulated()
// Request 2: Cache Hit (Valid Sig)
start = time.Now()
w2 := makeRequest("0xValidSig")
duration2 := time.Since(start)
if w2.Code != 200 {
t.Fatalf("Request 2 failed: %d body=%s", w2.Code, w2.Body.String())
}
if aiCalls.Load() != 1 {
t.Errorf("Expected AI calls to stay at 1, got %d (Cache Miss?)", aiCalls.Load())
}
// Duration Check (should be significantly faster)
if duration2 > 50*time.Millisecond {
t.Logf("Warning: Cache hit was slow (%v), but logic verified.", duration2)
}
// Security Check: Cache HIT but INVALID Signature
w3 := makeRequest("0xInvalidSig")
if w3.Code != 403 {
t.Errorf("Expected status 403 for invalid signature on cache hit, got %d", w3.Code)
}
// Security Check: Cache HIT but MISSING Signature
reqBody := map[string]string{"text": textToSummarize}
jsonBody, err := json.Marshal(reqBody)
if err != nil {
t.Fatalf("Failed to marshal request body: %v", err)
}
reqNoSig, err := http.NewRequest("POST", "/api/ai/summarize", bytes.NewBuffer(jsonBody))
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
reqNoSig.Header.Set("Content-Type", "application/json")
w4 := httptest.NewRecorder()
r.ServeHTTP(w4, reqNoSig)
if w4.Code != 402 {
t.Errorf("Expected status 402 for missing signature, got %d", w4.Code)
}
// Verify Body
var resp1, resp2 map[string]interface{}
if err := json.Unmarshal(w1.Body.Bytes(), &resp1); err != nil {
t.Fatalf("Failed to unmarshal response 1: %v", err)
}
if err := json.Unmarshal(w2.Body.Bytes(), &resp2); err != nil {
t.Fatalf("Failed to unmarshal response 2: %v", err)
}
if val, ok := resp1["result"].(string); !ok || val != "AI Summary Result" {
t.Errorf("Unexpected result 1: %v", resp1["result"])
}
if val, ok := resp2["result"].(string); !ok || val != "AI Summary Result" {
t.Errorf("Unexpected result 2: %v", resp2["result"])
}
}