| package semanticcache |
|
|
| import ( |
| "testing" |
|
|
| "github.com/maximhq/bifrost/core/schemas" |
| ) |
|
|
| |
| func TestCacheNoStoreBasicFunctionality(t *testing.T) { |
| setup := NewTestSetup(t) |
| defer setup.Cleanup() |
|
|
| testRequest := CreateBasicChatRequest("What is artificial intelligence?", 0.7, 100) |
|
|
| |
| ctx1 := CreateContextWithCacheKey("test-no-store-control") |
| t.Log("Making normal request (should be cached)...") |
| response1, err1 := setup.Client.ChatCompletionRequest(ctx1, testRequest) |
| if err1 != nil { |
| return |
| } |
| AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response1}) |
|
|
| WaitForCache(setup.Plugin) |
|
|
| |
| t.Log("Verifying normal caching worked...") |
| response2, err2 := setup.Client.ChatCompletionRequest(ctx1, testRequest) |
| 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") |
|
|
| |
| ctx2 := CreateContextWithCacheKeyAndNoStore("test-no-store-disabled", true) |
| t.Log("Making request with CacheNoStoreKey=true (should not be cached)...") |
| response3, err3 := setup.Client.ChatCompletionRequest(ctx2, testRequest) |
| if err3 != nil { |
| return |
| } |
| AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response3}) |
|
|
| WaitForCache(setup.Plugin) |
|
|
| |
| t.Log("Verifying no-store request was not cached...") |
| response4, err4 := setup.Client.ChatCompletionRequest(ctx2, testRequest) |
| if err4 != nil { |
| return |
| } |
| AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response4}) |
|
|
| |
| ctx3 := CreateContextWithCacheKeyAndNoStore("test-no-store-enabled", false) |
| t.Log("Making request with CacheNoStoreKey=false (should be cached)...") |
| response5, err5 := setup.Client.ChatCompletionRequest(ctx3, testRequest) |
| if err5 != nil { |
| return |
| } |
| AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response5}) |
|
|
| WaitForCache(setup.Plugin) |
|
|
| |
| t.Log("Verifying no-store=false request was cached...") |
| response6, err6 := setup.Client.ChatCompletionRequest(ctx3, testRequest) |
| if err6 != nil { |
| t.Fatalf("Sixth request failed: %v", err6) |
| } |
| AssertCacheHit(t, &schemas.BifrostResponse{ChatResponse: response6}, "direct") |
|
|
| t.Log("✅ CacheNoStoreKey basic functionality works correctly") |
| } |
|
|
| |
| func TestCacheNoStoreWithDifferentRequestTypes(t *testing.T) { |
| t.Skip("Skipping Embedding Tests") |
|
|
| setup := NewTestSetup(t) |
| defer setup.Cleanup() |
|
|
| |
| chatRequest := CreateBasicChatRequest("Test no-store with chat", 0.7, 50) |
| ctx1 := CreateContextWithCacheKeyAndNoStore("test-no-store-chat", true) |
|
|
| t.Log("Testing no-store with chat completion...") |
| response1, err1 := setup.Client.ChatCompletionRequest(ctx1, chatRequest) |
| if err1 != nil { |
| return |
| } |
| AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response1}) |
|
|
| WaitForCache(setup.Plugin) |
|
|
| |
| response2, err2 := setup.Client.ChatCompletionRequest(ctx1, chatRequest) |
| if err2 != nil { |
| return |
| } |
| AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response2}) |
|
|
| |
| embeddingRequest := CreateEmbeddingRequest([]string{"Test no-store with embeddings"}) |
| ctx2 := CreateContextWithCacheKeyAndNoStore("test-no-store-embedding", true) |
|
|
| t.Log("Testing no-store with embedding request...") |
| response3, err3 := setup.Client.EmbeddingRequest(ctx2, embeddingRequest) |
| if err3 != nil { |
| return |
| } |
| AssertNoCacheHit(t, &schemas.BifrostResponse{EmbeddingResponse: response3}) |
|
|
| WaitForCache(setup.Plugin) |
|
|
| |
| response4, err4 := setup.Client.EmbeddingRequest(ctx2, embeddingRequest) |
| if err4 != nil { |
| return |
| } |
| AssertNoCacheHit(t, &schemas.BifrostResponse{EmbeddingResponse: response4}) |
|
|
| t.Log("✅ CacheNoStoreKey works with different request types") |
| } |
|
|
| |
| func TestCacheNoStoreWithConversationHistory(t *testing.T) { |
| setup := NewTestSetup(t) |
| defer setup.Cleanup() |
|
|
| |
| conversation := BuildConversationHistory( |
| "You are a helpful assistant", |
| []string{"Hello", "Hi! How can I help?"}, |
| ) |
| messages := AddUserMessage(conversation, "What is machine learning?") |
| request := CreateConversationRequest(messages, 0.7, 100) |
|
|
| |
| ctx := CreateContextWithCacheKeyAndNoStore("test-no-store-conversation", true) |
|
|
| t.Log("Testing no-store with conversation history...") |
| response1, err1 := setup.Client.ChatCompletionRequest(ctx, request) |
| if err1 != nil { |
| return |
| } |
| AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response1}) |
|
|
| WaitForCache(setup.Plugin) |
|
|
| |
| response2, err2 := setup.Client.ChatCompletionRequest(ctx, request) |
| if err2 != nil { |
| return |
| } |
| AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response2}) |
|
|
| t.Log("✅ CacheNoStoreKey works with conversation history") |
| } |
|
|
| |
| func TestCacheNoStoreWithCacheTypes(t *testing.T) { |
| setup := NewTestSetup(t) |
| defer setup.Cleanup() |
|
|
| testRequest := CreateBasicChatRequest("Test no-store with cache types", 0.7, 50) |
|
|
| |
| ctx1 := CreateContextWithCacheKey("test-no-store-cache-types") |
| ctx1 = ctx1.WithValue(CacheNoStoreKey, true) |
| ctx1 = ctx1.WithValue(CacheTypeKey, CacheTypeDirect) |
|
|
| t.Log("Testing no-store with CacheTypeKey=direct...") |
| response1, err1 := setup.Client.ChatCompletionRequest(ctx1, testRequest) |
| if err1 != nil { |
| return |
| } |
| AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response1}) |
|
|
| WaitForCache(setup.Plugin) |
|
|
| |
| response2, err2 := setup.Client.ChatCompletionRequest(ctx1, testRequest) |
| if err2 != nil { |
| return |
| } |
| AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response2}) |
|
|
| |
| ctx2 := CreateContextWithCacheKey("test-no-store-cache-types") |
| ctx2 = ctx2.WithValue(CacheNoStoreKey, true) |
| ctx2 = ctx2.WithValue(CacheTypeKey, CacheTypeSemantic) |
|
|
| t.Log("Testing no-store with CacheTypeKey=semantic...") |
| response3, err3 := setup.Client.ChatCompletionRequest(ctx2, testRequest) |
| if err3 != nil { |
| return |
| } |
| AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response3}) |
|
|
| WaitForCache(setup.Plugin) |
|
|
| |
| response4, err4 := setup.Client.ChatCompletionRequest(ctx2, testRequest) |
| if err4 != nil { |
| return |
| } |
| AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response4}) |
|
|
| t.Log("✅ CacheNoStoreKey correctly overrides cache type settings") |
| } |
|
|
| |
| func TestCacheNoStoreErrorHandling(t *testing.T) { |
| setup := NewTestSetup(t) |
| defer setup.Cleanup() |
|
|
| testRequest := CreateBasicChatRequest("Test no-store error handling", 0.7, 50) |
|
|
| |
| ctx1 := CreateContextWithCacheKey("test-no-store-errors") |
| ctx1 = ctx1.WithValue(CacheNoStoreKey, "invalid") |
|
|
| t.Log("Testing no-store with invalid value (should cache normally)...") |
| response1, err1 := setup.Client.ChatCompletionRequest(ctx1, testRequest) |
| if err1 != nil { |
| return |
| } |
| AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response1}) |
|
|
| WaitForCache(setup.Plugin) |
|
|
| |
| response2, err2 := setup.Client.ChatCompletionRequest(ctx1, testRequest) |
| 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") |
|
|
| |
| ctx2 := CreateContextWithCacheKey("test-no-store-nil") |
| ctx2 = ctx2.WithValue(CacheNoStoreKey, nil) |
|
|
| t.Log("Testing no-store with nil value (should cache normally)...") |
| response3, err3 := setup.Client.ChatCompletionRequest(ctx2, testRequest) |
| if err3 != nil { |
| return |
| } |
| AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response3}) |
|
|
| WaitForCache(setup.Plugin) |
|
|
| |
| response4, err4 := setup.Client.ChatCompletionRequest(ctx2, testRequest) |
| if err4 != nil { |
| t.Fatalf("Fourth request failed: %v", err4) |
| } |
| AssertCacheHit(t, &schemas.BifrostResponse{ChatResponse: response4}, "direct") |
|
|
| t.Log("✅ CacheNoStoreKey error handling works correctly") |
| } |
|
|
| |
| func TestCacheNoStoreReadButNoWrite(t *testing.T) { |
| setup := NewTestSetup(t) |
| defer setup.Cleanup() |
|
|
| testRequest := CreateBasicChatRequest("Describe Isaac Newton's three laws of motion", 0.7, 50) |
|
|
| |
| ctx1 := CreateContextWithCacheKey("test-no-store-read") |
| t.Log("Caching response normally...") |
| response1, err1 := setup.Client.ChatCompletionRequest(ctx1, testRequest) |
| if err1 != nil { |
| return |
| } |
| AssertNoCacheHit(t, &schemas.BifrostResponse{ChatResponse: response1}) |
|
|
| WaitForCache(setup.Plugin) |
|
|
| |
| ctx2 := CreateContextWithCacheKeyAndNoStore("test-no-store-read", true) |
| t.Log("Reading with no-store enabled (should still hit cache for reads)...") |
| response2, err2 := setup.Client.ChatCompletionRequest(ctx2, testRequest) |
| 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") |
|
|
| |
| newRequest := CreateBasicChatRequest("Describe the three laws of motion by Isaac Newton", 0.7, 50) |
| t.Log("Making semantically similar request with no-store (should get semantic hit, but not cache response)...") |
| response3, err3 := setup.Client.ChatCompletionRequest(ctx2, newRequest) |
| if err3 != nil { |
| t.Fatalf("Third request failed: %v", err3) |
| } |
| |
| AssertCacheHit(t, &schemas.BifrostResponse{ChatResponse: response3}, "semantic") |
|
|
| WaitForCache(setup.Plugin) |
|
|
| |
| t.Log("Repeating similar request with no-store (should still get semantic hit)...") |
| response4, err4 := setup.Client.ChatCompletionRequest(ctx2, newRequest) |
| if err4 != nil { |
| t.Fatalf("Fourth request failed: %v", err4) |
| } |
| |
| AssertCacheHit(t, &schemas.BifrostResponse{ChatResponse: response4}, "semantic") |
|
|
| t.Log("✅ CacheNoStoreKey allows reading but prevents writing") |
| } |
|
|