File size: 16,365 Bytes
8baf122 | 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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 | package semanticcache
import (
"strconv"
"testing"
bifrost "github.com/maximhq/bifrost/core"
"github.com/maximhq/bifrost/core/schemas"
)
// TestConversationHistoryThresholdBasic tests basic conversation history threshold functionality
func TestConversationHistoryThresholdBasic(t *testing.T) {
// Test with threshold of 2 messages
setup := CreateTestSetupWithConversationThreshold(t, 2)
defer setup.Cleanup()
ctx := CreateContextWithCacheKey("test-conversation-threshold-basic")
// Test 1: Conversation with exactly 2 messages (should cache)
conversation1 := BuildConversationHistory("",
[]string{"Hello", "Hi there!"},
)
request1 := CreateConversationRequest(conversation1, 0.7, 50)
t.Log("Testing conversation with exactly 2 messages (at threshold)...")
response1, err1 := setup.Client.ChatCompletionRequest(ctx, request1)
if err1 != nil {
return // Test will be skipped by retry function
}
AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response1}) // Fresh request
WaitForCache(setup.Plugin)
// Verify it was cached
response2, err2 := setup.Client.ChatCompletionRequest(ctx, request1)
if err2 != nil {
if err2.Error != nil {
t.Fatalf("Second request failed: %v", err2.Error.Message)
} else {
t.Fatalf("Second request failed: %v", err2)
}
}
AssertCacheHit(t, &schemas.BifrostResponse{ChatResponse: response2}, "direct") // Should be cached
// Test 2: Conversation with 3 messages (exceeds threshold, should NOT cache)
conversation2 := BuildConversationHistory("",
[]string{"Hello", "Hi there!"},
[]string{"How are you?", "I'm doing well!"},
)
messages2 := AddUserMessage(conversation2, "What's the weather?")
request2 := CreateConversationRequest(messages2, 0.7, 50) // 5 messages total > 2
t.Log("Testing conversation with 5 messages (exceeds threshold)...")
response3, err3 := setup.Client.ChatCompletionRequest(ctx, request2)
if err3 != nil {
return // Test will be skipped by retry function
}
AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response3}) // Should not cache
WaitForCache(setup.Plugin)
// Verify it was NOT cached
t.Log("Verifying conversation exceeding threshold was not cached...")
response4, err4 := setup.Client.ChatCompletionRequest(ctx, request2)
if err4 != nil {
return // Test will be skipped by retry function
}
AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response4}) // Should still be fresh (not cached)
t.Log("β
Conversation history threshold works correctly")
}
// TestConversationHistoryThresholdWithSystemPrompt tests threshold with system messages
func TestConversationHistoryThresholdWithSystemPrompt(t *testing.T) {
// Test with threshold of 3, ExcludeSystemPrompt = false
setup := CreateTestSetupWithConversationThreshold(t, 3)
defer setup.Cleanup()
ctx := CreateContextWithCacheKey("test-threshold-system-prompt")
// System prompt + 2 user/assistant pairs = 5 messages total > 3
conversation := BuildConversationHistory(
"You are a helpful assistant", // System message (counts toward threshold)
[]string{"Hello", "Hi there!"},
[]string{"How are you?", "I'm doing well!"},
)
request := CreateConversationRequest(conversation, 0.7, 50)
t.Log("Testing conversation with system prompt (5 total messages > 3 threshold)...")
response1, err1 := setup.Client.ChatCompletionRequest(ctx, request)
if err1 != nil {
return // Test will be skipped by retry function
}
AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response1}) // Should not cache (exceeds threshold)
WaitForCache(setup.Plugin)
// Verify not cached
response2, err2 := setup.Client.ChatCompletionRequest(ctx, request)
if err2 != nil {
return // Test will be skipped by retry function
}
AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response2}) // Should not be cached
t.Log("β
Conversation threshold correctly counts system messages")
}
// TestConversationHistoryThresholdWithExcludeSystemPrompt tests interaction between threshold and exclude system prompt
func TestConversationHistoryThresholdWithExcludeSystemPrompt(t *testing.T) {
// Create setup with both threshold=3 and ExcludeSystemPrompt=true
setup := CreateTestSetupWithThresholdAndExcludeSystem(t, 3, true)
defer setup.Cleanup()
ctx := CreateContextWithCacheKey("test-threshold-exclude-system")
// Create conversation with exactly 3 non-system messages to test threshold boundary
// System + 1.5 user/assistant pairs = 4 messages total
// With ExcludeSystemPrompt=true, should only count 3 non-system messages for threshold
conversation := BuildConversationHistory(
"You are helpful", // System (excluded from count)
[]string{"Hello", "Hi"}, // User + Assistant = 2 messages
[]string{"Thanks", ""}, // User only = 1 message (no assistant response)
)
// No slicing needed; BuildConversationHistory skips empty assistant entries.
request := CreateConversationRequest(conversation, 0.7, 50) // 3 non-system messages exactly
t.Log("Testing threshold with ExcludeSystemPrompt=true (3 non-system messages = at threshold)...")
// Test logic:
// - Total messages: 4 (1 system + 3 others)
// - With ExcludeSystemPrompt=true: counts as 3 non-system messages
// - Threshold is 3, so 3 <= 3 should allow caching
response1, err1 := setup.Client.ChatCompletionRequest(ctx, request)
if err1 != nil {
return // Test will be skipped by retry function
}
AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response1}) // Fresh request, should not hit cache
WaitForCache(setup.Plugin)
// Second request should hit cache (3 non-system messages <= 3 threshold)
response2, err2 := setup.Client.ChatCompletionRequest(ctx, request)
if err2 != nil {
if err2.Error != nil {
t.Fatalf("Second request failed: %v", err2.Error.Message)
} else {
t.Fatalf("Second request failed: %v", err2)
}
}
AssertCacheHit(t, &schemas.BifrostResponse{ChatResponse: response2}, "direct") // Should cache since 3 <= 3 after excluding system
t.Log("β
Conversation threshold respects ExcludeSystemPrompt setting")
}
// TestConversationHistoryThresholdDifferentValues tests different threshold values
func TestConversationHistoryThresholdDifferentValues(t *testing.T) {
testCases := []struct {
name string
threshold int
messages int
shouldCache bool
}{
{"Threshold 1, 1 message", 1, 1, true},
{"Threshold 1, 2 messages", 1, 2, false},
{"Threshold 5, 4 messages", 5, 4, true},
{"Threshold 5, 6 messages", 5, 6, false},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
setup := CreateTestSetupWithConversationThreshold(t, tc.threshold)
defer setup.Cleanup()
ctx := CreateContextWithCacheKey("test-threshold-" + tc.name)
// Build conversation with specified number of messages
var conversation []schemas.ChatMessage
for i := 0; i < tc.messages; i++ {
role := schemas.ChatMessageRoleUser
if i%2 == 1 {
role = schemas.ChatMessageRoleAssistant
}
message := schemas.ChatMessage{
Role: role,
Content: &schemas.ChatMessageContent{
ContentStr: bifrost.Ptr("Message " + strconv.Itoa(i+1)),
},
}
conversation = append(conversation, message)
}
request := CreateConversationRequest(conversation, 0.7, 50)
response1, err1 := setup.Client.ChatCompletionRequest(ctx, request)
if err1 != nil {
return // Test will be skipped by retry function
}
AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response1}) // Always fresh first time
WaitForCache(setup.Plugin)
response2, err2 := setup.Client.ChatCompletionRequest(ctx, request)
if err2 != nil {
return // Test will be skipped by retry function
}
if tc.shouldCache {
AssertCacheHit(t, &schemas.BifrostResponse{ChatResponse: response2}, "direct")
} else {
AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response2})
}
})
}
t.Log("β
Different conversation threshold values work correctly")
}
// TestExcludeSystemPromptBasic tests basic ExcludeSystemPrompt functionality
func TestExcludeSystemPromptBasic(t *testing.T) {
// Test with ExcludeSystemPrompt = true
setup := CreateTestSetupWithExcludeSystemPrompt(t, true)
defer setup.Cleanup()
ctx := CreateContextWithCacheKey("test-exclude-system-basic")
// Create two conversations with different system prompts but same user/assistant messages
conversation1 := BuildConversationHistory(
"You are a helpful assistant",
[]string{"What is AI?", "AI is artificial intelligence."},
)
conversation2 := BuildConversationHistory(
"You are a technical expert", // Different system prompt
[]string{"What is AI?", "AI is artificial intelligence."}, // Same user/assistant
)
request1 := CreateConversationRequest(conversation1, 0.7, 50)
request2 := CreateConversationRequest(conversation2, 0.7, 50)
t.Log("Caching conversation with system prompt 1...")
response1, err1 := setup.Client.ChatCompletionRequest(ctx, request1)
if err1 != nil {
return // Test will be skipped by retry function
}
AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response1})
WaitForCache(setup.Plugin)
t.Log("Testing conversation with different system prompt (should hit cache due to ExcludeSystemPrompt=true)...")
response2, err2 := setup.Client.ChatCompletionRequest(ctx, request2)
if err2 != nil {
if err2.Error != nil {
t.Fatalf("Second request failed: %v", err2.Error.Message)
} else {
t.Fatalf("Second request failed: %v", err2)
}
}
// Should hit cache because system prompts are excluded from cache key
AssertCacheHit(t, &schemas.BifrostResponse{ChatResponse: response2}, "direct")
t.Log("β
ExcludeSystemPrompt=true correctly ignores system prompts in cache keys")
}
// TestExcludeSystemPromptComparison tests ExcludeSystemPrompt true vs false
func TestExcludeSystemPromptComparison(t *testing.T) {
// Test 1: ExcludeSystemPrompt = false (default)
setup1 := CreateTestSetupWithExcludeSystemPrompt(t, false)
defer setup1.Cleanup()
ctx1 := CreateContextWithCacheKey("test-exclude-system-false")
conversation1 := BuildConversationHistory(
"You are helpful",
[]string{"Hello", "Hi there!"},
)
conversation2 := BuildConversationHistory(
"You are an expert", // Different system prompt
[]string{"Hello", "Hi there!"}, // Same user/assistant
)
request1 := CreateConversationRequest(conversation1, 0.7, 50)
request2 := CreateConversationRequest(conversation2, 0.7, 50)
t.Log("Testing ExcludeSystemPrompt=false...")
response1, err1 := setup1.Client.ChatCompletionRequest(ctx1, request1)
if err1 != nil {
return // Test will be skipped by retry function
}
AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response1})
WaitForCache(setup1.Plugin)
response2, err2 := setup1.Client.ChatCompletionRequest(ctx1, request2)
if err2 != nil {
if err2.Error != nil {
t.Fatalf("Second request failed: %v", err2.Error.Message)
} else {
t.Fatalf("Second request failed: %v", err2)
}
}
// Should NOT hit direct cache, but might hit semantic cache due to similar content
if response2.ExtraFields.CacheDebug != nil && response2.ExtraFields.CacheDebug.CacheHit {
if response2.ExtraFields.CacheDebug.HitType != nil && *response2.ExtraFields.CacheDebug.HitType == "semantic" {
t.Log("β
Found semantic cache match (expected with similar content)")
} else {
t.Error("β Unexpected direct cache hit with different system prompts")
}
} else {
t.Log("β
No cache hit (system prompts create different cache keys)")
}
// Test 2: ExcludeSystemPrompt = true
setup2 := CreateTestSetupWithExcludeSystemPrompt(t, true)
defer setup2.Cleanup()
ctx2 := CreateContextWithCacheKey("test-exclude-system-true")
t.Log("Testing ExcludeSystemPrompt=true...")
response3, err3 := setup2.Client.ChatCompletionRequest(ctx2, request1)
if err3 != nil {
return // Test will be skipped by retry function
}
AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response3})
WaitForCache(setup2.Plugin)
response4, err4 := setup2.Client.ChatCompletionRequest(ctx2, request2)
if err4 != nil {
t.Fatalf("Fourth request failed: %v", err4)
}
// Should hit cache because system prompts are excluded from cache key
AssertCacheHit(t, &schemas.BifrostResponse{ChatResponse: response4}, "direct")
t.Log("β
ExcludeSystemPrompt true vs false comparison works correctly")
}
// TestExcludeSystemPromptWithMultipleSystemMessages tests behavior with multiple system messages
func TestExcludeSystemPromptWithMultipleSystemMessages(t *testing.T) {
setup := CreateTestSetupWithExcludeSystemPrompt(t, true)
defer setup.Cleanup()
ctx := CreateContextWithCacheKey("test-multiple-system-messages")
// Manually create conversation with multiple system messages
conversation1 := []schemas.ChatMessage{
{
Role: schemas.ChatMessageRoleSystem,
Content: &schemas.ChatMessageContent{ContentStr: bifrost.Ptr("You are helpful")},
},
{
Role: schemas.ChatMessageRoleSystem,
Content: &schemas.ChatMessageContent{ContentStr: bifrost.Ptr("Be concise")},
},
{
Role: schemas.ChatMessageRoleUser,
Content: &schemas.ChatMessageContent{ContentStr: bifrost.Ptr("Hello")},
},
{
Role: schemas.ChatMessageRoleAssistant,
Content: &schemas.ChatMessageContent{ContentStr: bifrost.Ptr("Hi!")},
},
}
conversation2 := []schemas.ChatMessage{
{
Role: schemas.ChatMessageRoleSystem,
Content: &schemas.ChatMessageContent{ContentStr: bifrost.Ptr("You are an expert")},
},
{
Role: schemas.ChatMessageRoleSystem,
Content: &schemas.ChatMessageContent{ContentStr: bifrost.Ptr("Be detailed")},
},
{
Role: schemas.ChatMessageRoleUser,
Content: &schemas.ChatMessageContent{ContentStr: bifrost.Ptr("Hello")},
},
{
Role: schemas.ChatMessageRoleAssistant,
Content: &schemas.ChatMessageContent{ContentStr: bifrost.Ptr("Hi!")},
},
}
request1 := CreateConversationRequest(conversation1, 0.7, 50)
request2 := CreateConversationRequest(conversation2, 0.7, 50)
t.Log("Caching conversation with multiple system messages...")
response1, err1 := setup.Client.ChatCompletionRequest(ctx, request1)
if err1 != nil {
return // Test will be skipped by retry function
}
AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response1})
WaitForCache(setup.Plugin)
t.Log("Testing conversation with different multiple system messages...")
response2, err2 := setup.Client.ChatCompletionRequest(ctx, request2)
if err2 != nil {
if err2.Error != nil {
t.Fatalf("Second request failed: %v", err2.Error.Message)
} else {
t.Fatalf("Second request failed: %v", err2)
}
}
// Should hit cache because all system messages are excluded
AssertCacheHit(t, &schemas.BifrostResponse{ChatResponse: response2}, "direct")
t.Log("β
ExcludeSystemPrompt works with multiple system messages")
}
// TestExcludeSystemPromptWithNoSystemMessages tests behavior when there are no system messages
func TestExcludeSystemPromptWithNoSystemMessages(t *testing.T) {
setup := CreateTestSetupWithExcludeSystemPrompt(t, true)
defer setup.Cleanup()
ctx := CreateContextWithCacheKey("test-no-system-messages")
// Conversation with no system messages
conversation := []schemas.ChatMessage{
{
Role: schemas.ChatMessageRoleUser,
Content: &schemas.ChatMessageContent{ContentStr: bifrost.Ptr("Hello")},
},
{
Role: schemas.ChatMessageRoleAssistant,
Content: &schemas.ChatMessageContent{ContentStr: bifrost.Ptr("Hi there!")},
},
}
request := CreateConversationRequest(conversation, 0.7, 50)
t.Log("Testing conversation with no system messages...")
response1, err1 := setup.Client.ChatCompletionRequest(ctx, request)
if err1 != nil {
return // Test will be skipped by retry function
}
AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response1})
WaitForCache(setup.Plugin)
// Should cache normally
response2, err2 := setup.Client.ChatCompletionRequest(ctx, request)
if err2 != nil {
if err2.Error != nil {
t.Fatalf("Second request failed: %v", err2.Error.Message)
} else {
t.Fatalf("Second request failed: %v", err2)
}
}
AssertCacheHit(t, &schemas.BifrostResponse{ChatResponse: response2}, "direct")
t.Log("β
ExcludeSystemPrompt works correctly when no system messages present")
}
|