deepseek3_test.go 22.5 KB
Newer Older
Grace's avatar
Grace committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package parsers

import (
	"testing"

	"github.com/google/go-cmp/cmp"

	"github.com/ollama/ollama/api"
)

func TestDeepSeekParser(t *testing.T) {
	tests := []struct {
		name             string
		input            string
		expectedContent  string
		expectedThinking string
		expectedCalls    []api.ToolCall
		hasThinking      bool
	}{
		{
			name:            "simple_content",
			input:           "Hello, how are you?",
			expectedContent: "Hello, how are you?",
			hasThinking:     false,
		},
		{
			name:             "thinking_content",
			input:            "I need to think about this...</think>The answer is 42.",
			expectedThinking: "I need to think about this...",
			expectedContent:  "The answer is 42.",
			hasThinking:      true,
		},
		{
			name:            "no_thinking_simple",
			input:           "Just a regular response.",
			expectedContent: "Just a regular response.",
			hasThinking:     false,
		},
		{
			name:             "thinking_with_newlines",
			input:            "Let me think:\n- Point 1\n- Point 2</think>\n\nHere's my answer.",
			expectedThinking: "Let me think:\n- Point 1\n- Point 2",
			expectedContent:  "Here's my answer.",
			hasThinking:      true,
		},
		{
			name:            "tool_call_simple",
			input:           "I'll check the weather.<|tool▁calls▁begin|><|tool▁call▁begin|>get_weather<|tool▁sep|>{\"location\":\"Paris\"}<|tool▁call▁end|><|tool▁calls▁end|>",
			expectedContent: "I'll check the weather.",
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name: "get_weather",
54
						Arguments: testArgs(map[string]any{
Grace's avatar
Grace committed
55
							"location": "Paris",
56
						}),
Grace's avatar
Grace committed
57
58
59
60
61
62
63
64
65
66
67
68
69
					},
				},
			},
			hasThinking: false,
		},
		{
			name:            "multiple_tool_calls",
			input:           "Getting weather for both cities.<|tool▁calls▁begin|><|tool▁call▁begin|>get_weather<|tool▁sep|>{\"location\":\"Paris\"}<|tool▁call▁end|><|tool▁call▁begin|>get_weather<|tool▁sep|>{\"location\":\"London\"}<|tool▁call▁end|><|tool▁calls▁end|>",
			expectedContent: "Getting weather for both cities.",
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name: "get_weather",
70
						Arguments: testArgs(map[string]any{
Grace's avatar
Grace committed
71
							"location": "Paris",
72
						}),
Grace's avatar
Grace committed
73
74
75
76
77
					},
				},
				{
					Function: api.ToolCallFunction{
						Name: "get_weather",
78
						Arguments: testArgs(map[string]any{
Grace's avatar
Grace committed
79
							"location": "London",
80
						}),
Grace's avatar
Grace committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
					},
				},
			},
			hasThinking: false,
		},
		{
			name:            "tool_output",
			input:           "Here's the weather: <|tool▁output▁begin|>Temperature: 22°C, Sunny<|tool▁output▁end|> Hope that helps!",
			expectedContent: "Here's the weather: Temperature: 22°C, Sunny Hope that helps!",
			hasThinking:     false,
		},
		{
			name:            "complex_tool_arguments",
			input:           "Processing data.<|tool▁calls▁begin|><|tool▁call▁begin|>process_data<|tool▁sep|>{\"items\":[\"item1\",\"item2\"],\"config\":{\"enabled\":true,\"threshold\":0.95}}<|tool▁call▁end|><|tool▁calls▁end|>",
			expectedContent: "Processing data.",
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name: "process_data",
100
						Arguments: testArgs(map[string]any{
Grace's avatar
Grace committed
101
102
							"items":  []interface{}{"item1", "item2"},
							"config": map[string]interface{}{"enabled": true, "threshold": 0.95},
103
						}),
Grace's avatar
Grace committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
					},
				},
			},
			hasThinking: false,
		},
		{
			name:             "thinking_with_tool_call", // technically this can't happen, but the parser can handle it
			input:            "Let me check the weather...</think>I'll get that for you.<|tool▁calls▁begin|><|tool▁call▁begin|>get_weather<|tool▁sep|>{\"location\":\"Paris\"}<|tool▁call▁end|><|tool▁calls▁end|>",
			expectedThinking: "Let me check the weather...",
			expectedContent:  "I'll get that for you.",
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name: "get_weather",
118
						Arguments: testArgs(map[string]any{
Grace's avatar
Grace committed
119
							"location": "Paris",
120
						}),
Grace's avatar
Grace committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
					},
				},
			},
			hasThinking: true,
		},
		{
			name:            "empty_content",
			input:           "",
			expectedContent: "",
			hasThinking:     false,
		},
		{
			name:             "only_thinking",
			input:            "Just thinking content</think>",
			expectedThinking: "Just thinking content",
			expectedContent:  "",
			hasThinking:      true,
		},
		{
			name:            "multiple_tool_outputs",
			input:           "Results: <|tool▁output▁begin|>Paris: 22°C<|tool▁output▁end|> and <|tool▁output▁begin|>London: 18°C<|tool▁output▁end|>",
			expectedContent: "Results: Paris: 22°C and London: 18°C",
			hasThinking:     false,
		},
		{
			name:            "unicode_content",
			input:           "مرحبا بالعالم! 你好世界! 🌍",
			expectedContent: "مرحبا بالعالم! 你好世界! 🌍",
			hasThinking:     false,
		},
		{
			name:            "emoji_passthrough",
			input:           "Task completed ✅ 🎉",
			expectedContent: "Task completed ✅ 🎉",
			hasThinking:     false,
		},
		{
			name:            "emoji_after_tool_call",
			input:           "I'll help you.<|tool▁calls▁begin|><|tool▁call▁begin|>get_weather<|tool▁sep|>{\"location\":\"Tokyo\"}<|tool▁call▁end|><|tool▁calls▁end|>完成 ✅",
			expectedContent: "I'll help you.完成 ✅",
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name: "get_weather",
165
						Arguments: testArgs(map[string]any{
Grace's avatar
Grace committed
166
							"location": "Tokyo",
167
						}),
Grace's avatar
Grace committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
					},
				},
			},
			hasThinking: false,
		},
		{
			name:            "newlines_and_whitespace",
			input:           "Line 1\n\nLine 3\t\tTabbed content",
			expectedContent: "Line 1\n\nLine 3\t\tTabbed content",
			hasThinking:     false,
		},
		{
			name:             "thinking_with_unicode",
			input:            "我在思考这个问题...</think>答案是42。",
			expectedThinking: "我在思考这个问题...",
			expectedContent:  "答案是42。",
			hasThinking:      true,
		},
		{
			name:            "tool_call_with_unicode_args",
			input:           "Searching for information.<|tool▁calls▁begin|><|tool▁call▁begin|>search<|tool▁sep|>{\"query\":\"北京天气\",\"language\":\"中文\"}<|tool▁call▁end|><|tool▁calls▁end|>",
			expectedContent: "Searching for information.",
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name: "search",
194
						Arguments: testArgs(map[string]any{
Grace's avatar
Grace committed
195
196
							"query":    "北京天气",
							"language": "中文",
197
						}),
Grace's avatar
Grace committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
					},
				},
			},
			hasThinking: false,
		},
		{
			name:            "tool_output_with_unicode",
			input:           "天气信息: <|tool▁output▁begin|>北京: 25°C, 晴天<|tool▁output▁end|> 希望对您有帮助!",
			expectedContent: "天气信息: 北京: 25°C, 晴天 希望对您有帮助!",
			hasThinking:     false,
		},
		{
			name:            "mixed_content_with_special_chars",
			input:           "Price: $100 & tax @ 10% = $110 <|tool▁output▁begin|>Total: $110<|tool▁output▁end|> (final)",
			expectedContent: "Price: $100 & tax @ 10% = $110 Total: $110 (final)",
			hasThinking:     false,
		},
		{
			name:            "tool_call_with_special_chars",
			input:           "Processing data.<|tool▁calls▁begin|><|tool▁call▁begin|>execute_command<|tool▁sep|>{\"command\":\"ls && echo \\\"done\\\"\",\"path\":\"/home/user\"}<|tool▁call▁end|><|tool▁calls▁end|>",
			expectedContent: "Processing data.",
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name: "execute_command",
223
						Arguments: testArgs(map[string]any{
Grace's avatar
Grace committed
224
225
							"command": "ls && echo \"done\"",
							"path":    "/home/user",
226
						}),
Grace's avatar
Grace committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
					},
				},
			},
			hasThinking: false,
		},
		{
			name:             "thinking_with_special_chars",
			input:            "Let me calculate: 2+2=4 & 3*3=9...</think>The results are correct!",
			expectedThinking: "Let me calculate: 2+2=4 & 3*3=9...",
			expectedContent:  "The results are correct!",
			hasThinking:      true,
		},
		{
			name:            "empty_tool_call_args",
			input:           "Pinging server.<|tool▁calls▁begin|><|tool▁call▁begin|>ping<|tool▁sep|>{}<|tool▁call▁end|><|tool▁calls▁end|>",
			expectedContent: "Pinging server.",
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "ping",
247
						Arguments: api.NewToolCallFunctionArguments(),
Grace's avatar
Grace committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
					},
				},
			},
			hasThinking: false,
		},
		{
			name:            "empty_tool_output",
			input:           "Checking status: <|tool▁output▁begin|><|tool▁output▁end|> No output received.",
			expectedContent: "Checking status:  No output received.",
			hasThinking:     false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			parser := &DeepSeek3Parser{hasThinkingSupport: tt.hasThinking}
			parser.Init([]api.Tool{}, nil, &api.ThinkValue{Value: tt.hasThinking})

			content, thinking, calls, err := parser.Add(tt.input, true)
			if err != nil {
				t.Fatalf("Add() error = %v", err)
			}

			if diff := cmp.Diff(tt.expectedContent, content); diff != "" {
				t.Errorf("Content mismatch (-want +got):\n%s", diff)
			}

			if diff := cmp.Diff(tt.expectedThinking, thinking); diff != "" {
				t.Errorf("Thinking mismatch (-want +got):\n%s", diff)
			}

279
			if diff := cmp.Diff(tt.expectedCalls, calls, argsComparer); diff != "" {
Grace's avatar
Grace committed
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
				t.Errorf("Tool calls mismatch (-want +got):\n%s", diff)
			}
		})
	}
}

func TestDeepSeekParser_Streaming(t *testing.T) {
	tests := []struct {
		name             string
		chunks           []string
		expectedContent  string
		expectedThinking string
		expectedCalls    []api.ToolCall
		hasThinking      bool
	}{
		{
			name:            "streaming_simple_content",
			chunks:          []string{"Hello, ", "how are ", "you?"},
			expectedContent: "Hello, how are you?",
			hasThinking:     false,
		},
		{
			name:             "streaming_thinking",
			chunks:           []string{"I need to ", "think about this", "...</think>", "The answer is 42."},
			expectedThinking: "I need to think about this...",
			expectedContent:  "The answer is 42.",
			hasThinking:      true,
		},
		{
			name:            "streaming_tool_call",
			chunks:          []string{"I'll check weather.", "<|tool▁calls▁begin|>", "<|tool▁call▁begin|>get_weather", "<|tool▁sep|>{\"location\":\"Paris\"}", "<|tool▁call▁end|><|tool▁calls▁end|>"},
			expectedContent: "I'll check weather.",
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name: "get_weather",
316
						Arguments: testArgs(map[string]any{
Grace's avatar
Grace committed
317
							"location": "Paris",
318
						}),
Grace's avatar
Grace committed
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
					},
				},
			},
			hasThinking: false,
		},
		{
			name:             "streaming_thinking_with_partial_tag",
			chunks:           []string{"Thinking about this", "...</", "think>", "Done thinking."},
			expectedThinking: "Thinking about this...",
			expectedContent:  "Done thinking.",
			hasThinking:      true,
		},
		{
			name:            "streaming_tool_output",
			chunks:          []string{"Weather info: ", "<|tool▁output▁begin|>", "25°C, Sunny", "<|tool▁output▁end|>", " Enjoy!"},
			expectedContent: "Weather info: 25°C, Sunny Enjoy!",
			hasThinking:     false,
		},
		{
			name:            "streaming_with_split_tags",
			chunks:          []string{"Content before ", "<|tool▁calls▁begin|><|tool▁call▁begin|>test", "<|tool▁sep|>{}", "<|tool▁call▁end|><|tool▁calls▁end|>", " after"},
			expectedContent: "Content before  after",
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name:      "test",
345
						Arguments: api.NewToolCallFunctionArguments(),
Grace's avatar
Grace committed
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
					},
				},
			},
			hasThinking: false,
		},
		{
			name:             "streaming_thinking_with_split_end_tag",
			chunks:           []string{"Thinking content", "</th", "ink>", "Regular content"},
			expectedThinking: "Thinking content",
			expectedContent:  "Regular content",
			hasThinking:      true,
		},
		{
			name:            "streaming_unicode_content",
			chunks:          []string{"مرحبا ", "بالعالم! ", "你好", "世界!"},
			expectedContent: "مرحبا بالعالم! 你好世界!",
			hasThinking:     false,
		},
		{
			name:            "streaming_multiple_tool_outputs",
			chunks:          []string{"Results: ", "<|tool▁output▁begin|>", "Paris: 22°C", "<|tool▁output▁end|>", " and ", "<|tool▁output▁begin|>", "London: 18°C", "<|tool▁output▁end|>"},
			expectedContent: "Results: Paris: 22°C and London: 18°C",
			hasThinking:     false,
		},
		{
			name:            "streaming_tool_call_with_split_json",
			chunks:          []string{"Processing.", "<|tool▁calls▁begin|><|tool▁call▁begin|>calc<|tool▁sep|>{\"x\":", "42,\"y\":", "24}<|tool▁call▁end|><|tool▁calls▁end|>"},
			expectedContent: "Processing.",
			expectedCalls: []api.ToolCall{
				{
					Function: api.ToolCallFunction{
						Name: "calc",
378
						Arguments: testArgs(map[string]any{
Grace's avatar
Grace committed
379
380
							"x": float64(42),
							"y": float64(24),
381
						}),
Grace's avatar
Grace committed
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
					},
				},
			},
			hasThinking: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			parser := &DeepSeek3Parser{hasThinkingSupport: tt.hasThinking}
			parser.Init([]api.Tool{}, nil, &api.ThinkValue{Value: tt.hasThinking})

			var allContent, allThinking string
			var allCalls []api.ToolCall

			for i, chunk := range tt.chunks {
				done := i == len(tt.chunks)-1
				content, thinking, calls, err := parser.Add(chunk, done)
				if err != nil {
					t.Fatalf("Add() error = %v", err)
				}

				allContent += content
				allThinking += thinking
				allCalls = append(allCalls, calls...)
			}

			if diff := cmp.Diff(tt.expectedContent, allContent); diff != "" {
				t.Errorf("Content mismatch (-want +got):\n%s", diff)
			}

			if diff := cmp.Diff(tt.expectedThinking, allThinking); diff != "" {
				t.Errorf("Thinking mismatch (-want +got):\n%s", diff)
			}

417
			if diff := cmp.Diff(tt.expectedCalls, allCalls, argsComparer); diff != "" {
Grace's avatar
Grace committed
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
				t.Errorf("Tool calls mismatch (-want +got):\n%s", diff)
			}
		})
	}
}

func TestDeepSeekParser_HasThinkingSupport(t *testing.T) {
	tests := []struct {
		name            string
		hasThinking     bool
		expectedSupport bool
	}{
		{
			name:            "thinking_enabled",
			hasThinking:     true,
			expectedSupport: true,
		},
		{
			name:            "thinking_disabled",
			hasThinking:     false,
			expectedSupport: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			parser := &DeepSeek3Parser{hasThinkingSupport: tt.hasThinking}
			if got := parser.HasThinkingSupport(); got != tt.expectedSupport {
				t.Errorf("HasThinkingSupport() = %v, want %v", got, tt.expectedSupport)
			}
		})
	}
}

func TestDeepSeekParser_HasToolSupport(t *testing.T) {
	parser := &DeepSeek3Parser{}
	if !parser.HasToolSupport() {
		t.Error("HasToolSupport() should return true")
	}
}

func TestDeepSeekParser_Init(t *testing.T) {
	parser := &DeepSeek3Parser{hasThinkingSupport: true}
	tools := []api.Tool{
		{
			Type: "function",
			Function: api.ToolFunction{
				Name: "test_tool",
			},
		},
	}

	returnedTools := parser.Init(tools, nil, &api.ThinkValue{Value: true})

472
	if diff := cmp.Diff(tools, returnedTools, toolsComparer); diff != "" {
Grace's avatar
Grace committed
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
		t.Errorf("Init() returned tools mismatch (-want +got):\n%s", diff)
	}

	// Test initial state is set to thinking when enabled
	if parser.state != DeepSeekCollectingThinking {
		t.Errorf("Expected initial state to be DeepSeekCollectingThinking, got %v", parser.state)
	}
}

func TestDeepSeek3Parser_parseToolCallContent(t *testing.T) {
	tests := []struct {
		name        string
		content     string
		expected    api.ToolCall
		expectError bool
	}{
		{
			name:    "valid_tool_call",
			content: "get_weather<|tool▁sep|>{\"location\":\"Paris\"}",
			expected: api.ToolCall{
				Function: api.ToolCallFunction{
					Name: "get_weather",
495
					Arguments: testArgs(map[string]any{
Grace's avatar
Grace committed
496
						"location": "Paris",
497
					}),
Grace's avatar
Grace committed
498
499
500
501
502
503
504
505
506
				},
			},
		},
		{
			name:    "complex_arguments",
			content: "process_data<|tool▁sep|>{\"items\":[\"a\",\"b\"],\"config\":{\"enabled\":true}}",
			expected: api.ToolCall{
				Function: api.ToolCallFunction{
					Name: "process_data",
507
					Arguments: testArgs(map[string]any{
Grace's avatar
Grace committed
508
509
						"items":  []interface{}{"a", "b"},
						"config": map[string]interface{}{"enabled": true},
510
					}),
Grace's avatar
Grace committed
511
512
513
514
515
516
517
518
519
				},
			},
		},
		{
			name:    "empty_arguments",
			content: "ping<|tool▁sep|>{}",
			expected: api.ToolCall{
				Function: api.ToolCallFunction{
					Name:      "ping",
520
					Arguments: api.NewToolCallFunctionArguments(),
Grace's avatar
Grace committed
521
522
523
524
525
526
527
528
529
				},
			},
		},
		{
			name:    "unicode_in_tool_name",
			content: "获取天气<|tool▁sep|>{\"城市\":\"北京\"}",
			expected: api.ToolCall{
				Function: api.ToolCallFunction{
					Name: "获取天气",
530
					Arguments: testArgs(map[string]any{
Grace's avatar
Grace committed
531
						"城市": "北京",
532
					}),
Grace's avatar
Grace committed
533
534
535
536
537
538
539
540
541
				},
			},
		},
		{
			name:    "special_chars_in_arguments",
			content: "execute<|tool▁sep|>{\"command\":\"ls && echo \\\"done\\\"\",\"path\":\"/home/user\"}",
			expected: api.ToolCall{
				Function: api.ToolCallFunction{
					Name: "execute",
542
					Arguments: testArgs(map[string]any{
Grace's avatar
Grace committed
543
544
						"command": "ls && echo \"done\"",
						"path":    "/home/user",
545
					}),
Grace's avatar
Grace committed
546
547
548
549
550
551
552
553
554
				},
			},
		},
		{
			name:    "numeric_arguments",
			content: "calculate<|tool▁sep|>{\"x\":3.14,\"y\":42,\"enabled\":true}",
			expected: api.ToolCall{
				Function: api.ToolCallFunction{
					Name: "calculate",
555
					Arguments: testArgs(map[string]any{
Grace's avatar
Grace committed
556
557
558
						"x":       3.14,
						"y":       float64(42),
						"enabled": true,
559
					}),
Grace's avatar
Grace committed
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
				},
			},
		},
		{
			name:        "invalid_format_no_separator",
			content:     "get_weather{\"location\":\"Paris\"}",
			expectError: true,
		},
		{
			name:        "invalid_json",
			content:     "get_weather<|tool▁sep|>{invalid json}",
			expectError: true,
		},
		{
			name:        "empty_tool_name",
			content:     "<|tool▁sep|>{\"arg\":\"value\"}",
			expectError: false, // This should work, just empty name
			expected: api.ToolCall{
				Function: api.ToolCallFunction{
					Name: "",
580
					Arguments: testArgs(map[string]any{
Grace's avatar
Grace committed
581
						"arg": "value",
582
					}),
Grace's avatar
Grace committed
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
				},
			},
		},
		{
			name:        "missing_json_part",
			content:     "tool_name<|tool▁sep|>",
			expectError: true,
		},
	}

	parser := &DeepSeek3Parser{}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result, err := parser.parseToolCallContent(tt.content)

			if tt.expectError {
				if err == nil {
					t.Error("Expected error but got none")
				}
				return
			}

			if err != nil {
				t.Fatalf("Unexpected error: %v", err)
			}

609
			if diff := cmp.Diff(tt.expected, result, argsComparer); diff != "" {
Grace's avatar
Grace committed
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
				t.Errorf("parseToolCallContent() mismatch (-want +got):\n%s", diff)
			}
		})
	}
}

func TestDeepSeekParser_EdgeCases(t *testing.T) {
	tests := []struct {
		name             string
		input            string
		expectedContent  string
		expectedThinking string
		hasThinking      bool
	}{
		{
			name:             "nested_think_tags_in_thinking",
			input:            "Outer thinking <think>inner</think> content</think>Final content",
			expectedThinking: "Outer thinking <think>inner",
			expectedContent:  "content</think>Final content",
			hasThinking:      true,
		},
		{
			name:             "multiple_think_close_tags",
			input:            "First thought</think>Second thought</think>Final content",
			expectedThinking: "First thought",
			expectedContent:  "Second thought</think>Final content",
			hasThinking:      true,
		},
		{
			name:             "empty_thinking_content",
			input:            "</think>Just content",
			expectedThinking: "",
			expectedContent:  "Just content",
			hasThinking:      true,
		},
		{
			name:            "thinking_disabled_with_think_tags",
			input:           "Some content</think>More content",
			expectedContent: "Some content</think>More content",
			hasThinking:     false,
		},
		{
			name:            "malformed_tool_call_missing_sep",
			input:           "Testing.<|tool▁calls▁begin|><|tool▁call▁begin|>bad_tool{\"arg\":\"value\"}<|tool▁call▁end|><|tool▁calls▁end|>",
			expectedContent: "Testing.",
			hasThinking:     false,
		},
		{
			name:            "malformed_tool_call_invalid_json",
			input:           "Testing.<|tool▁calls▁begin|><|tool▁call▁begin|>bad_tool<|tool▁sep|>{invalid json}<|tool▁call▁end|><|tool▁calls▁end|>",
			expectedContent: "Testing.",
			hasThinking:     false,
		},
		{
			name:            "partial_tool_tag_at_end",
			input:           "Content with partial <|tool▁calls▁",
			expectedContent: "Content with partial <|tool▁calls▁",
			hasThinking:     false,
		},
		{
			name:            "partial_think_tag_at_end",
			input:           "Thinking content</th",
			expectedContent: "Thinking content</th",
			hasThinking:     false,
		},
		{
			name:             "partial_think_tag_at_end_with_thinking",
			input:            "Thinking content</th",
			expectedThinking: "Thinking content",
			expectedContent:  "",
			hasThinking:      true,
		},
		{
			name:            "whitespace_only_content",
			input:           "   \n\t   ",
			expectedContent: "   \n\t   ",
			hasThinking:     false,
		},
		{
			name:            "tool_output_with_newlines",
			input:           "Output:\n<|tool▁output▁begin|>Line 1\nLine 2\nLine 3<|tool▁output▁end|>\nDone.",
			expectedContent: "Output:\nLine 1\nLine 2\nLine 3\nDone.",
			hasThinking:     false,
		},
		{
			name:            "consecutive_tool_calls",
			input:           "First.<|tool▁calls▁begin|><|tool▁call▁begin|>tool1<|tool▁sep|>{}<|tool▁call▁end|><|tool▁calls▁end|>Second.<|tool▁calls▁begin|><|tool▁call▁begin|>tool2<|tool▁sep|>{}<|tool▁call▁end|><|tool▁calls▁end|>",
			expectedContent: "First.",
			hasThinking:     false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			parser := &DeepSeek3Parser{hasThinkingSupport: tt.hasThinking}
			parser.Init([]api.Tool{}, nil, &api.ThinkValue{Value: tt.hasThinking})

			content, thinking, _, err := parser.Add(tt.input, true)
			if err != nil {
				t.Fatalf("Add() error = %v", err)
			}

			if diff := cmp.Diff(tt.expectedContent, content); diff != "" {
				t.Errorf("Content mismatch (-want +got):\n%s", diff)
			}

			if diff := cmp.Diff(tt.expectedThinking, thinking); diff != "" {
				t.Errorf("Thinking mismatch (-want +got):\n%s", diff)
			}
		})
	}
}