thinking_test.go

 1package providertests
 2
 3import (
 4	"testing"
 5
 6	"github.com/charmbracelet/fantasy/ai"
 7	"github.com/charmbracelet/fantasy/anthropic"
 8	"github.com/charmbracelet/fantasy/google"
 9	"github.com/stretchr/testify/require"
10)
11
12func testThinkingSteps(t *testing.T, providerName string, steps []ai.StepResult) {
13	switch providerName {
14	case anthropic.Name:
15		testAnthropicThinking(t, steps)
16	case google.Name:
17		testGoogleThinking(t, steps)
18	}
19}
20
21func testGoogleThinking(t *testing.T, steps []ai.StepResult) {
22	reasoningContentCount := 0
23	// Test if we got the signature
24	for _, step := range steps {
25		for _, msg := range step.Messages {
26			for _, content := range msg.Content {
27				if content.GetType() == ai.ContentTypeReasoning {
28					reasoningContentCount += 1
29				}
30			}
31		}
32	}
33	require.Greater(t, reasoningContentCount, 0)
34}
35
36func testAnthropicThinking(t *testing.T, steps []ai.StepResult) {
37	reasoningContentCount := 0
38	signaturesCount := 0
39	// Test if we got the signature
40	for _, step := range steps {
41		for _, msg := range step.Messages {
42			for _, content := range msg.Content {
43				if content.GetType() == ai.ContentTypeReasoning {
44					reasoningContentCount += 1
45					reasoningContent, ok := ai.AsContentType[ai.ReasoningPart](content)
46					if !ok {
47						continue
48					}
49					if len(reasoningContent.ProviderOptions) == 0 {
50						continue
51					}
52
53					anthropicReasoningMetadata, ok := reasoningContent.ProviderOptions[anthropic.Name]
54					if !ok {
55						continue
56					}
57					if reasoningContent.Text != "" {
58						if typed, ok := anthropicReasoningMetadata.(*anthropic.ReasoningOptionMetadata); ok {
59							require.NotEmpty(t, typed.Signature)
60							signaturesCount += 1
61						}
62					}
63				}
64			}
65		}
66	}
67	require.Greater(t, reasoningContentCount, 0)
68	require.Greater(t, signaturesCount, 0)
69	require.Equal(t, reasoningContentCount, signaturesCount)
70}