test(v2): end-to-end cache-hint propagation through Chat.Send
Verifies that WithPromptCaching() on a Chat results in CacheHints being set on the provider.Request that reaches the provider layer, and that omitting the option leaves CacheHints nil (no behavior change for existing callers). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -509,3 +509,59 @@ func TestChat_Send_UsageWithDetails(t *testing.T) {
|
||||
t.Errorf("expected reasoning_tokens=2, got %d", usage.Details["reasoning_tokens"])
|
||||
}
|
||||
}
|
||||
|
||||
// TestChatSend_WithPromptCaching_PopulatesCacheHints is an end-to-end check
|
||||
// that WithPromptCaching() on a Chat causes CacheHints to be populated on the
|
||||
// provider.Request that reaches the provider layer.
|
||||
func TestChatSend_WithPromptCaching_PopulatesCacheHints(t *testing.T) {
|
||||
mp := newMockProvider(provider.Response{Text: "hello back"})
|
||||
model := newMockModel(mp)
|
||||
|
||||
chat := NewChat(model, WithPromptCaching())
|
||||
chat.SetSystem("you are helpful")
|
||||
|
||||
_, _, err := chat.Send(context.Background(), "hi")
|
||||
if err != nil {
|
||||
t.Fatalf("Send failed: %v", err)
|
||||
}
|
||||
|
||||
if len(mp.Requests) != 1 {
|
||||
t.Fatalf("expected 1 provider request, got %d", len(mp.Requests))
|
||||
}
|
||||
req := mp.Requests[0]
|
||||
|
||||
if req.CacheHints == nil {
|
||||
t.Fatal("expected CacheHints to be set on provider.Request")
|
||||
}
|
||||
if !req.CacheHints.CacheSystem {
|
||||
t.Error("expected CacheSystem=true")
|
||||
}
|
||||
// Last non-system message index should be the user "hi" at index 1
|
||||
// (SetSystem prepended a system message at index 0).
|
||||
if req.CacheHints.LastCacheableMessageIndex != 1 {
|
||||
t.Errorf("expected LastCacheableMessageIndex=1, got %d", req.CacheHints.LastCacheableMessageIndex)
|
||||
}
|
||||
}
|
||||
|
||||
// TestChatSend_WithoutPromptCaching_NoCacheHints verifies that omitting
|
||||
// WithPromptCaching() leaves CacheHints nil on the provider.Request, so
|
||||
// existing callers see no behavior change.
|
||||
func TestChatSend_WithoutPromptCaching_NoCacheHints(t *testing.T) {
|
||||
mp := newMockProvider(provider.Response{Text: "hello back"})
|
||||
model := newMockModel(mp)
|
||||
|
||||
chat := NewChat(model)
|
||||
chat.SetSystem("you are helpful")
|
||||
|
||||
_, _, err := chat.Send(context.Background(), "hi")
|
||||
if err != nil {
|
||||
t.Fatalf("Send failed: %v", err)
|
||||
}
|
||||
|
||||
if len(mp.Requests) != 1 {
|
||||
t.Fatalf("expected 1 provider request, got %d", len(mp.Requests))
|
||||
}
|
||||
if mp.Requests[0].CacheHints != nil {
|
||||
t.Errorf("expected nil CacheHints when caching not requested, got %+v", mp.Requests[0].CacheHints)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user