thinking_test.go

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