|
| 1 | +// Copyright (C) 2026 Yota Hamada |
| 2 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | + |
| 4 | +package llm |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +// TestNormalizeChatRequest verifies that request normalization preserves caller |
| 15 | +// state while enforcing provider-independent tool invariants. |
| 16 | +func TestNormalizeChatRequest(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + t.Run("deduplicates tools by name and disables native web search on collision", func(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + |
| 22 | + req := &ChatRequest{ |
| 23 | + Model: "test", |
| 24 | + Tools: []Tool{ |
| 25 | + testTool(WebSearchToolName, "first"), |
| 26 | + testTool(WebSearchToolName, "second"), |
| 27 | + testTool("read", "read"), |
| 28 | + }, |
| 29 | + WebSearch: &WebSearchRequest{Enabled: true}, |
| 30 | + } |
| 31 | + |
| 32 | + normalized := NormalizeChatRequest(req) |
| 33 | + |
| 34 | + require.NotSame(t, req, normalized) |
| 35 | + require.Len(t, normalized.Tools, 2) |
| 36 | + assert.Equal(t, "first", normalized.Tools[0].Function.Description) |
| 37 | + assert.Equal(t, "read", normalized.Tools[1].Function.Name) |
| 38 | + require.NotNil(t, normalized.WebSearch) |
| 39 | + assert.False(t, normalized.WebSearch.Enabled) |
| 40 | + |
| 41 | + require.Len(t, req.Tools, 3, "normalization must not mutate caller tools") |
| 42 | + assert.True(t, req.WebSearch.Enabled, "normalization must not mutate caller web search config") |
| 43 | + }) |
| 44 | + |
| 45 | + t.Run("keeps native web search when no explicit web_search tool exists", func(t *testing.T) { |
| 46 | + t.Parallel() |
| 47 | + |
| 48 | + req := &ChatRequest{ |
| 49 | + Model: "test", |
| 50 | + Tools: []Tool{testTool("read", "read")}, |
| 51 | + WebSearch: &WebSearchRequest{Enabled: true}, |
| 52 | + } |
| 53 | + |
| 54 | + normalized := NormalizeChatRequest(req) |
| 55 | + |
| 56 | + require.NotNil(t, normalized.WebSearch) |
| 57 | + assert.True(t, normalized.WebSearch.Enabled) |
| 58 | + }) |
| 59 | +} |
| 60 | + |
| 61 | +// TestNewProviderNormalizesRequests verifies that factory-created providers |
| 62 | +// normalize both synchronous and streaming chat requests. |
| 63 | +func TestNewProviderNormalizesRequests(t *testing.T) { |
| 64 | + orig := registry |
| 65 | + defer func() { registry = orig }() |
| 66 | + registry = make(map[ProviderType]ProviderFactory) |
| 67 | + |
| 68 | + capturedChat := make(chan *ChatRequest, 1) |
| 69 | + capturedStream := make(chan *ChatRequest, 1) |
| 70 | + RegisterProvider(ProviderType("normalize-test"), func(_ Config) (Provider, error) { |
| 71 | + return &normalizingTestProvider{ |
| 72 | + chat: capturedChat, |
| 73 | + stream: capturedStream, |
| 74 | + }, nil |
| 75 | + }) |
| 76 | + |
| 77 | + provider, err := NewProvider(ProviderType("normalize-test"), Config{}) |
| 78 | + require.NoError(t, err) |
| 79 | + |
| 80 | + req := &ChatRequest{ |
| 81 | + Model: "test", |
| 82 | + Tools: []Tool{ |
| 83 | + testTool(WebSearchToolName, "first"), |
| 84 | + testTool(WebSearchToolName, "second"), |
| 85 | + }, |
| 86 | + WebSearch: &WebSearchRequest{Enabled: true}, |
| 87 | + } |
| 88 | + |
| 89 | + _, err = provider.Chat(context.Background(), req) |
| 90 | + require.NoError(t, err) |
| 91 | + chatReq := <-capturedChat |
| 92 | + require.Len(t, chatReq.Tools, 1) |
| 93 | + assert.False(t, chatReq.WebSearch.Enabled) |
| 94 | + |
| 95 | + events, err := provider.ChatStream(context.Background(), req) |
| 96 | + require.NoError(t, err) |
| 97 | + for range events { |
| 98 | + } |
| 99 | + streamReq := <-capturedStream |
| 100 | + require.Len(t, streamReq.Tools, 1) |
| 101 | + assert.False(t, streamReq.WebSearch.Enabled) |
| 102 | +} |
| 103 | + |
| 104 | +// testTool builds a minimal function tool definition for normalization tests. |
| 105 | +func testTool(name, description string) Tool { |
| 106 | + return Tool{ |
| 107 | + Type: "function", |
| 108 | + Function: ToolFunction{ |
| 109 | + Name: name, |
| 110 | + Description: description, |
| 111 | + Parameters: map[string]any{"type": "object"}, |
| 112 | + }, |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +// normalizingTestProvider captures requests after the provider wrapper has |
| 117 | +// normalized them. |
| 118 | +type normalizingTestProvider struct { |
| 119 | + chat chan<- *ChatRequest |
| 120 | + stream chan<- *ChatRequest |
| 121 | +} |
| 122 | + |
| 123 | +// Chat captures the normalized request received by the test provider. |
| 124 | +func (p *normalizingTestProvider) Chat(_ context.Context, req *ChatRequest) (*ChatResponse, error) { |
| 125 | + p.chat <- req |
| 126 | + return &ChatResponse{Content: "ok"}, nil |
| 127 | +} |
| 128 | + |
| 129 | +// ChatStream captures the normalized request received by the streaming path. |
| 130 | +func (p *normalizingTestProvider) ChatStream(_ context.Context, req *ChatRequest) (<-chan StreamEvent, error) { |
| 131 | + p.stream <- req |
| 132 | + ch := make(chan StreamEvent, 1) |
| 133 | + ch <- StreamEvent{Done: true} |
| 134 | + close(ch) |
| 135 | + return ch, nil |
| 136 | +} |
| 137 | + |
| 138 | +// Name returns the test provider's registry name. |
| 139 | +func (p *normalizingTestProvider) Name() string { |
| 140 | + return "normalize-test" |
| 141 | +} |
0 commit comments