|
| 1 | +// Copyright 2026, Command Line Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package openaichat |
| 5 | + |
| 6 | +import ( |
| 7 | + "encoding/json" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/wavetermdev/waveterm/pkg/aiusechat/uctypes" |
| 12 | +) |
| 13 | + |
| 14 | +func TestReasoningContentRoundTrip(t *testing.T) { |
| 15 | + original := ChatRequestMessage{ |
| 16 | + Role: "assistant", |
| 17 | + Content: "The answer is 42.", |
| 18 | + ReasoningContent: "Let me think about this carefully...", |
| 19 | + ToolCalls: []ToolCall{ |
| 20 | + {ID: "call_1", Type: "function", Function: ToolFunctionCall{Name: "search", Arguments: `{}`}}, |
| 21 | + }, |
| 22 | + } |
| 23 | + |
| 24 | + data, err := json.Marshal(original) |
| 25 | + if err != nil { |
| 26 | + t.Fatalf("Marshal failed: %v", err) |
| 27 | + } |
| 28 | + |
| 29 | + var restored ChatRequestMessage |
| 30 | + if err := json.Unmarshal(data, &restored); err != nil { |
| 31 | + t.Fatalf("Unmarshal failed: %v", err) |
| 32 | + } |
| 33 | + |
| 34 | + if restored.Role != original.Role { |
| 35 | + t.Errorf("Role: got %q, want %q", restored.Role, original.Role) |
| 36 | + } |
| 37 | + if restored.Content != original.Content { |
| 38 | + t.Errorf("Content: got %q, want %q", restored.Content, original.Content) |
| 39 | + } |
| 40 | + if restored.ReasoningContent != original.ReasoningContent { |
| 41 | + t.Errorf("ReasoningContent: got %q, want %q", restored.ReasoningContent, original.ReasoningContent) |
| 42 | + } |
| 43 | + if len(restored.ToolCalls) != len(original.ToolCalls) { |
| 44 | + t.Fatalf("ToolCalls length: got %d, want %d", len(restored.ToolCalls), len(original.ToolCalls)) |
| 45 | + } |
| 46 | + if restored.ToolCalls[0].ID != original.ToolCalls[0].ID { |
| 47 | + t.Errorf("ToolCalls[0].ID: got %q, want %q", restored.ToolCalls[0].ID, original.ToolCalls[0].ID) |
| 48 | + } |
| 49 | + if restored.ToolCalls[0].Function.Name != original.ToolCalls[0].Function.Name { |
| 50 | + t.Errorf("ToolCalls[0].Function.Name: got %q, want %q", restored.ToolCalls[0].Function.Name, original.ToolCalls[0].Function.Name) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestReasoningContentOmittedWhenEmpty(t *testing.T) { |
| 55 | + msg := ChatRequestMessage{ |
| 56 | + Role: "user", |
| 57 | + Content: "Hello", |
| 58 | + } |
| 59 | + |
| 60 | + data, err := json.Marshal(msg) |
| 61 | + if err != nil { |
| 62 | + t.Fatalf("Marshal failed: %v", err) |
| 63 | + } |
| 64 | + |
| 65 | + jsonStr := string(data) |
| 66 | + if strings.Contains(jsonStr, "reasoning_content") { |
| 67 | + t.Errorf("JSON should NOT contain 'reasoning_content' when empty, got: %s", jsonStr) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestStreamChunkWithReasoningContent(t *testing.T) { |
| 72 | + chunkJSON := `{"choices":[{"delta":{"reasoning_content":"I need to search for this...","content":"Let me search."}}]}` |
| 73 | + |
| 74 | + var chunk StreamChunk |
| 75 | + if err := json.Unmarshal([]byte(chunkJSON), &chunk); err != nil { |
| 76 | + t.Fatalf("Unmarshal failed: %v", err) |
| 77 | + } |
| 78 | + |
| 79 | + if len(chunk.Choices) == 0 { |
| 80 | + t.Fatal("expected at least one choice") |
| 81 | + } |
| 82 | + |
| 83 | + delta := chunk.Choices[0].Delta |
| 84 | + if delta.ReasoningContent != "I need to search for this..." { |
| 85 | + t.Errorf("ReasoningContent: got %q, want %q", delta.ReasoningContent, "I need to search for this...") |
| 86 | + } |
| 87 | + if delta.Content != "Let me search." { |
| 88 | + t.Errorf("Content: got %q, want %q", delta.Content, "Let me search.") |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestCleanPreservesReasoningContent(t *testing.T) { |
| 93 | + msg := &ChatRequestMessage{ |
| 94 | + Role: "assistant", |
| 95 | + Content: "text", |
| 96 | + ReasoningContent: "thinking", |
| 97 | + ToolCalls: []ToolCall{ |
| 98 | + {ID: "call_1", Type: "function", Function: ToolFunctionCall{Name: "f", Arguments: "{}"}, ToolUseData: &uctypes.UIMessageDataToolUse{}}, |
| 99 | + }, |
| 100 | + } |
| 101 | + |
| 102 | + cleaned := msg.clean() |
| 103 | + |
| 104 | + if cleaned == msg { |
| 105 | + t.Error("clean() should return a different pointer") |
| 106 | + } |
| 107 | + |
| 108 | + if cleaned.ReasoningContent != "thinking" { |
| 109 | + t.Errorf("ReasoningContent: got %q, want %q", cleaned.ReasoningContent, "thinking") |
| 110 | + } |
| 111 | + |
| 112 | + if cleaned.Content != "text" { |
| 113 | + t.Errorf("Content: got %q, want %q", cleaned.Content, "text") |
| 114 | + } |
| 115 | + |
| 116 | + if len(cleaned.ToolCalls) != 1 { |
| 117 | + t.Fatalf("ToolCalls length: got %d, want 1", len(cleaned.ToolCalls)) |
| 118 | + } |
| 119 | + if cleaned.ToolCalls[0].ToolUseData != nil { |
| 120 | + t.Error("ToolCalls[0].ToolUseData should be nil after clean()") |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func TestExtractPartialTextMessageWithReasoning(t *testing.T) { |
| 125 | + msg := extractPartialTextMessage("msg-1", "partial text", "partial reasoning") |
| 126 | + if msg == nil { |
| 127 | + t.Fatal("expected non-nil message when text is present") |
| 128 | + } |
| 129 | + if msg.MessageId != "msg-1" { |
| 130 | + t.Errorf("MessageId: got %q, want %q", msg.MessageId, "msg-1") |
| 131 | + } |
| 132 | + if msg.Message.Content != "partial text" { |
| 133 | + t.Errorf("Content: got %q, want %q", msg.Message.Content, "partial text") |
| 134 | + } |
| 135 | + if msg.Message.ReasoningContent != "partial reasoning" { |
| 136 | + t.Errorf("ReasoningContent: got %q, want %q", msg.Message.ReasoningContent, "partial reasoning") |
| 137 | + } |
| 138 | + if msg.Message.Role != "assistant" { |
| 139 | + t.Errorf("Role: got %q, want %q", msg.Message.Role, "assistant") |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func TestExtractPartialTextMessageWithOnlyReasoning(t *testing.T) { |
| 144 | + msg := extractPartialTextMessage("msg-2", "", "some reasoning") |
| 145 | + if msg == nil { |
| 146 | + t.Fatal("expected non-nil message when reasoning is present") |
| 147 | + } |
| 148 | + if msg.Message.Content != "" { |
| 149 | + t.Errorf("Content: got %q, want empty", msg.Message.Content) |
| 150 | + } |
| 151 | + if msg.Message.ReasoningContent != "some reasoning" { |
| 152 | + t.Errorf("ReasoningContent: got %q, want %q", msg.Message.ReasoningContent, "some reasoning") |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +func TestExtractPartialTextMessageEmpty(t *testing.T) { |
| 157 | + msg := extractPartialTextMessage("msg-3", "", "") |
| 158 | + if msg != nil { |
| 159 | + t.Fatal("expected nil when both text and reasoning are empty") |
| 160 | + } |
| 161 | +} |
0 commit comments