test(v2): end-to-end cache-hint propagation through Chat.Send
All checks were successful
CI / Lint (push) Successful in 9m35s
CI / Root Module (push) Successful in 10m54s
CI / V2 Module (push) Successful in 11m15s

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:
2026-04-09 19:40:39 +00:00
parent 34b2e29019
commit 9b91b2f794

View File

@@ -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)
}
}