From 9b91b2f7948054b79b3df46e555e49cc40cd5d1e Mon Sep 17 00:00:00 2001 From: Steve Dudenhoeffer Date: Thu, 9 Apr 2026 19:40:39 +0000 Subject: [PATCH] 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 --- v2/chat_test.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/v2/chat_test.go b/v2/chat_test.go index a201890..fa626cc 100644 --- a/v2/chat_test.go +++ b/v2/chat_test.go @@ -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) + } +}