|
| 1 | +package http |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/openai/openai-go" |
| 9 | +) |
| 10 | + |
| 11 | +func TestCreateCacheHitResponse_NonStreaming(t *testing.T) { |
| 12 | + // Create a sample cached response |
| 13 | + cachedCompletion := openai.ChatCompletion{ |
| 14 | + ID: "chatcmpl-test-123", |
| 15 | + Object: "chat.completion", |
| 16 | + Created: 1234567890, |
| 17 | + Model: "test-model", |
| 18 | + Choices: []openai.ChatCompletionChoice{ |
| 19 | + { |
| 20 | + Index: 0, |
| 21 | + Message: openai.ChatCompletionMessage{ |
| 22 | + Role: "assistant", |
| 23 | + Content: "This is a cached response.", |
| 24 | + }, |
| 25 | + FinishReason: "stop", |
| 26 | + }, |
| 27 | + }, |
| 28 | + Usage: openai.CompletionUsage{ |
| 29 | + PromptTokens: 10, |
| 30 | + CompletionTokens: 5, |
| 31 | + TotalTokens: 15, |
| 32 | + }, |
| 33 | + } |
| 34 | + |
| 35 | + cachedResponse, err := json.Marshal(cachedCompletion) |
| 36 | + if err != nil { |
| 37 | + t.Fatalf("Failed to marshal cached response: %v", err) |
| 38 | + } |
| 39 | + |
| 40 | + // Test non-streaming response |
| 41 | + response := CreateCacheHitResponse(cachedResponse, false) |
| 42 | + |
| 43 | + // Verify response structure |
| 44 | + if response == nil { |
| 45 | + t.Fatal("Response is nil") |
| 46 | + } |
| 47 | + |
| 48 | + immediateResp := response.GetImmediateResponse() |
| 49 | + if immediateResp == nil { |
| 50 | + t.Fatal("ImmediateResponse is nil") |
| 51 | + } |
| 52 | + |
| 53 | + // Verify status code |
| 54 | + if immediateResp.Status.Code.String() != "OK" { |
| 55 | + t.Errorf("Expected status OK, got %s", immediateResp.Status.Code.String()) |
| 56 | + } |
| 57 | + |
| 58 | + // Verify content-type header |
| 59 | + var contentType string |
| 60 | + var cacheHit string |
| 61 | + for _, header := range immediateResp.Headers.SetHeaders { |
| 62 | + if header.Header.Key == "content-type" { |
| 63 | + contentType = string(header.Header.RawValue) |
| 64 | + } |
| 65 | + if header.Header.Key == "x-vsr-cache-hit" { |
| 66 | + cacheHit = string(header.Header.RawValue) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if contentType != "application/json" { |
| 71 | + t.Errorf("Expected content-type application/json, got %s", contentType) |
| 72 | + } |
| 73 | + |
| 74 | + if cacheHit != "true" { |
| 75 | + t.Errorf("Expected x-vsr-cache-hit true, got %s", cacheHit) |
| 76 | + } |
| 77 | + |
| 78 | + // Verify body is unchanged |
| 79 | + if string(immediateResp.Body) != string(cachedResponse) { |
| 80 | + t.Error("Body was modified for non-streaming response") |
| 81 | + } |
| 82 | + |
| 83 | + // Verify body can be parsed as JSON |
| 84 | + var parsedResponse openai.ChatCompletion |
| 85 | + if err := json.Unmarshal(immediateResp.Body, &parsedResponse); err != nil { |
| 86 | + t.Errorf("Failed to parse response body as JSON: %v", err) |
| 87 | + } |
| 88 | + |
| 89 | + if parsedResponse.Object != "chat.completion" { |
| 90 | + t.Errorf("Expected object chat.completion, got %s", parsedResponse.Object) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestCreateCacheHitResponse_Streaming(t *testing.T) { |
| 95 | + // Create a sample cached response |
| 96 | + cachedCompletion := openai.ChatCompletion{ |
| 97 | + ID: "chatcmpl-test-456", |
| 98 | + Object: "chat.completion", |
| 99 | + Created: 1234567890, |
| 100 | + Model: "test-model", |
| 101 | + Choices: []openai.ChatCompletionChoice{ |
| 102 | + { |
| 103 | + Index: 0, |
| 104 | + Message: openai.ChatCompletionMessage{ |
| 105 | + Role: "assistant", |
| 106 | + Content: "This is a cached streaming response.", |
| 107 | + }, |
| 108 | + FinishReason: "stop", |
| 109 | + }, |
| 110 | + }, |
| 111 | + Usage: openai.CompletionUsage{ |
| 112 | + PromptTokens: 10, |
| 113 | + CompletionTokens: 5, |
| 114 | + TotalTokens: 15, |
| 115 | + }, |
| 116 | + } |
| 117 | + |
| 118 | + cachedResponse, err := json.Marshal(cachedCompletion) |
| 119 | + if err != nil { |
| 120 | + t.Fatalf("Failed to marshal cached response: %v", err) |
| 121 | + } |
| 122 | + |
| 123 | + // Test streaming response |
| 124 | + response := CreateCacheHitResponse(cachedResponse, true) |
| 125 | + |
| 126 | + // Verify response structure |
| 127 | + if response == nil { |
| 128 | + t.Fatal("Response is nil") |
| 129 | + } |
| 130 | + |
| 131 | + immediateResp := response.GetImmediateResponse() |
| 132 | + if immediateResp == nil { |
| 133 | + t.Fatal("ImmediateResponse is nil") |
| 134 | + } |
| 135 | + |
| 136 | + // Verify status code |
| 137 | + if immediateResp.Status.Code.String() != "OK" { |
| 138 | + t.Errorf("Expected status OK, got %s", immediateResp.Status.Code.String()) |
| 139 | + } |
| 140 | + |
| 141 | + // Verify content-type header |
| 142 | + var contentType string |
| 143 | + var cacheHit string |
| 144 | + for _, header := range immediateResp.Headers.SetHeaders { |
| 145 | + if header.Header.Key == "content-type" { |
| 146 | + contentType = string(header.Header.RawValue) |
| 147 | + } |
| 148 | + if header.Header.Key == "x-vsr-cache-hit" { |
| 149 | + cacheHit = string(header.Header.RawValue) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + if contentType != "text/event-stream" { |
| 154 | + t.Errorf("Expected content-type text/event-stream, got %s", contentType) |
| 155 | + } |
| 156 | + |
| 157 | + if cacheHit != "true" { |
| 158 | + t.Errorf("Expected x-vsr-cache-hit true, got %s", cacheHit) |
| 159 | + } |
| 160 | + |
| 161 | + // Verify body is in SSE format |
| 162 | + bodyStr := string(immediateResp.Body) |
| 163 | + if !strings.HasPrefix(bodyStr, "data: ") { |
| 164 | + t.Error("Body does not start with 'data: ' prefix") |
| 165 | + } |
| 166 | + |
| 167 | + if !strings.Contains(bodyStr, "data: [DONE]") { |
| 168 | + t.Error("Body does not contain 'data: [DONE]' terminator") |
| 169 | + } |
| 170 | + |
| 171 | + // Parse the SSE data |
| 172 | + lines := strings.Split(bodyStr, "\n") |
| 173 | + var dataLine string |
| 174 | + for _, line := range lines { |
| 175 | + if strings.HasPrefix(line, "data: ") && !strings.Contains(line, "[DONE]") { |
| 176 | + dataLine = strings.TrimPrefix(line, "data: ") |
| 177 | + break |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + if dataLine == "" { |
| 182 | + t.Fatal("No data line found in SSE response") |
| 183 | + } |
| 184 | + |
| 185 | + // Parse the JSON chunk |
| 186 | + var chunk map[string]interface{} |
| 187 | + if err := json.Unmarshal([]byte(dataLine), &chunk); err != nil { |
| 188 | + t.Fatalf("Failed to parse SSE data as JSON: %v", err) |
| 189 | + } |
| 190 | + |
| 191 | + // Verify chunk structure |
| 192 | + if chunk["object"] != "chat.completion.chunk" { |
| 193 | + t.Errorf("Expected object chat.completion.chunk, got %v", chunk["object"]) |
| 194 | + } |
| 195 | + |
| 196 | + if chunk["id"] != "chatcmpl-test-456" { |
| 197 | + t.Errorf("Expected id chatcmpl-test-456, got %v", chunk["id"]) |
| 198 | + } |
| 199 | + |
| 200 | + // Verify choices structure |
| 201 | + choices, ok := chunk["choices"].([]interface{}) |
| 202 | + if !ok || len(choices) == 0 { |
| 203 | + t.Fatal("Choices not found or empty") |
| 204 | + } |
| 205 | + |
| 206 | + choice := choices[0].(map[string]interface{}) |
| 207 | + delta, ok := choice["delta"].(map[string]interface{}) |
| 208 | + if !ok { |
| 209 | + t.Fatal("Delta not found in choice") |
| 210 | + } |
| 211 | + |
| 212 | + if delta["role"] != "assistant" { |
| 213 | + t.Errorf("Expected role assistant, got %v", delta["role"]) |
| 214 | + } |
| 215 | + |
| 216 | + if delta["content"] != "This is a cached streaming response." { |
| 217 | + t.Errorf("Expected content 'This is a cached streaming response.', got %v", delta["content"]) |
| 218 | + } |
| 219 | + |
| 220 | + if choice["finish_reason"] != "stop" { |
| 221 | + t.Errorf("Expected finish_reason stop, got %v", choice["finish_reason"]) |
| 222 | + } |
| 223 | +} |
| 224 | + |
| 225 | +func TestCreateCacheHitResponse_StreamingWithInvalidJSON(t *testing.T) { |
| 226 | + // Test with invalid JSON |
| 227 | + invalidJSON := []byte("invalid json") |
| 228 | + |
| 229 | + response := CreateCacheHitResponse(invalidJSON, true) |
| 230 | + |
| 231 | + // Verify response structure |
| 232 | + if response == nil { |
| 233 | + t.Fatal("Response is nil") |
| 234 | + } |
| 235 | + |
| 236 | + immediateResp := response.GetImmediateResponse() |
| 237 | + if immediateResp == nil { |
| 238 | + t.Fatal("ImmediateResponse is nil") |
| 239 | + } |
| 240 | + |
| 241 | + // Verify error response |
| 242 | + bodyStr := string(immediateResp.Body) |
| 243 | + if !strings.Contains(bodyStr, "error") { |
| 244 | + t.Error("Expected error message in response body") |
| 245 | + } |
| 246 | + |
| 247 | + if !strings.Contains(bodyStr, "data: [DONE]") { |
| 248 | + t.Error("Expected SSE terminator even in error case") |
| 249 | + } |
| 250 | +} |
0 commit comments