File size: 18,058 Bytes
8d3471e | 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 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 165 166 167 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 194 195 196 197 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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 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 279 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 316 317 318 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 345 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 378 379 380 381 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 417 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 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 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 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 | package sse
import "testing"
// βββ ParseDeepSeekSSELine edge cases βββββββββββββββββββββββββββββββββ
func TestParseDeepSeekSSELineEmptyLine(t *testing.T) {
_, _, ok := ParseDeepSeekSSELine([]byte(""))
if ok {
t.Fatal("expected not parsed for empty line")
}
}
func TestParseDeepSeekSSELineNoDataPrefix(t *testing.T) {
_, _, ok := ParseDeepSeekSSELine([]byte("event: message"))
if ok {
t.Fatal("expected not parsed for non-data line")
}
}
func TestParseDeepSeekSSELineInvalidJSON(t *testing.T) {
_, _, ok := ParseDeepSeekSSELine([]byte("data: {invalid json"))
if ok {
t.Fatal("expected not parsed for invalid JSON")
}
}
func TestParseDeepSeekSSELineWhitespaceOnly(t *testing.T) {
_, _, ok := ParseDeepSeekSSELine([]byte(" "))
if ok {
t.Fatal("expected not parsed for whitespace-only line")
}
}
func TestParseDeepSeekSSELineDataWithExtraSpaces(t *testing.T) {
chunk, done, ok := ParseDeepSeekSSELine([]byte(`data: {"v":"hello"} `))
if !ok || done {
t.Fatalf("expected parsed chunk for spaced data line")
}
if chunk["v"] != "hello" {
t.Fatalf("unexpected chunk: %#v", chunk)
}
}
// βββ shouldSkipPath edge cases βββββββββββββββββββββββββββββββββββββββ
func TestShouldSkipPathQuasiStatus(t *testing.T) {
if !shouldSkipPath("response/quasi_status") {
t.Fatal("expected skip for quasi_status path")
}
}
func TestShouldSkipPathPendingFragment(t *testing.T) {
if !shouldSkipPath("response/pending_fragment") {
t.Fatal("expected skip for pending_fragment path")
}
}
func TestShouldSkipPathConversationMode(t *testing.T) {
if !shouldSkipPath("response/conversation_mode") {
t.Fatal("expected skip for conversation_mode path")
}
}
func TestShouldSkipPathSearchStatus(t *testing.T) {
if !shouldSkipPath("response/search_status") {
t.Fatal("expected skip for search_status path")
}
}
func TestShouldSkipPathFragmentStatus(t *testing.T) {
if !shouldSkipPath("response/fragments/-1/status") {
t.Fatal("expected skip for fragment -1 status")
}
if !shouldSkipPath("response/fragments/-2/status") {
t.Fatal("expected skip for fragment -2 status")
}
if !shouldSkipPath("response/fragments/-3/status") {
t.Fatal("expected skip for fragment -3 status")
}
if !shouldSkipPath("response/fragments/-16/status") {
t.Fatal("expected skip for fragment -16 status")
}
if !shouldSkipPath("response/fragments/7/status") {
t.Fatal("expected skip for fragment 7 status")
}
if shouldSkipPath("response/status") {
t.Fatal("expected response/status to be handled by finish logic, not skipped")
}
}
func TestShouldSkipPathRegularContent(t *testing.T) {
if shouldSkipPath("response/content") {
t.Fatal("expected not skip for content path")
}
if shouldSkipPath("response/thinking_content") {
t.Fatal("expected not skip for thinking_content path")
}
}
// βββ ParseSSEChunkForContent edge cases ββββββββββββββββββββββββββββββ
func TestParseSSEChunkForContentNoVField(t *testing.T) {
parts, finished, nextType := ParseSSEChunkForContent(map[string]any{"p": "response/content"}, false, "text")
if finished {
t.Fatal("expected not finished")
}
if len(parts) != 0 {
t.Fatalf("expected no parts when v is missing, got %#v", parts)
}
if nextType != "text" {
t.Fatalf("expected type preserved, got %q", nextType)
}
}
func TestParseSSEChunkForContentSkippedPath(t *testing.T) {
parts, finished, nextType := ParseSSEChunkForContent(map[string]any{
"p": "response/quasi_status",
"v": "some data",
}, false, "text")
if finished || len(parts) > 0 {
t.Fatalf("expected skipped path to produce no output")
}
if nextType != "text" {
t.Fatalf("expected type preserved for skipped path")
}
}
func TestParseSSEChunkForContentFinishedStatus(t *testing.T) {
parts, finished, _ := ParseSSEChunkForContent(map[string]any{
"p": "response/status",
"v": "FINISHED",
}, false, "text")
if !finished {
t.Fatal("expected finished on status FINISHED")
}
if len(parts) != 0 {
t.Fatalf("expected no parts on finished, got %d", len(parts))
}
}
func TestParseSSEChunkForContentStatusNotFinished(t *testing.T) {
parts, finished, _ := ParseSSEChunkForContent(map[string]any{
"p": "response/status",
"v": "IN_PROGRESS",
}, false, "text")
if finished {
t.Fatal("expected not finished for non-FINISHED status")
}
if len(parts) != 0 {
t.Fatalf("expected non-finished status to be filtered, got %#v", parts)
}
}
func TestParseSSEChunkForContentEmptyStringV(t *testing.T) {
parts, finished, _ := ParseSSEChunkForContent(map[string]any{
"p": "response/content",
"v": "",
}, false, "text")
if finished {
t.Fatal("expected not finished")
}
if len(parts) != 0 {
t.Fatalf("expected no parts for empty string v, got %#v", parts)
}
}
func TestParseSSEChunkForContentFinishedOnEmptyPath(t *testing.T) {
parts, finished, _ := ParseSSEChunkForContent(map[string]any{
"p": "",
"v": "FINISHED",
}, false, "text")
if !finished {
t.Fatal("expected finished on empty path with FINISHED value")
}
if len(parts) != 0 {
t.Fatalf("expected no parts on finished")
}
}
func TestParseSSEChunkForContentFinishedOnStatusPath(t *testing.T) {
_, finished, _ := ParseSSEChunkForContent(map[string]any{
"p": "status",
"v": "FINISHED",
}, false, "text")
if !finished {
t.Fatal("expected finished on status path with FINISHED value")
}
}
func TestParseSSEChunkForContentThinkingPathEmptyPath(t *testing.T) {
parts, _, nextType := ParseSSEChunkForContent(map[string]any{
"v": "some thought",
}, true, "thinking")
if len(parts) != 1 || parts[0].Type != "thinking" {
t.Fatalf("expected thinking part on empty path, got %#v", parts)
}
if nextType != "thinking" {
t.Fatalf("expected nextType thinking, got %q", nextType)
}
}
func TestParseSSEChunkForContentThinkingEnabledTextType(t *testing.T) {
parts, _, nextType := ParseSSEChunkForContent(map[string]any{
"v": "text content",
}, true, "text")
if len(parts) != 1 || parts[0].Type != "text" {
t.Fatalf("expected text part when currentType=text, got %#v", parts)
}
if nextType != "text" {
t.Fatalf("expected nextType text, got %q", nextType)
}
}
// βββ ParseSSEChunkForContent: fragments path with THINK type βββββββββ
func TestParseSSEChunkForContentFragmentsAppendThink(t *testing.T) {
chunk := map[string]any{
"p": "response/fragments",
"o": "APPEND",
"v": []any{
map[string]any{
"type": "THINK",
"content": "ζ·±ε
₯ζθ...",
},
},
}
parts, finished, nextType := ParseSSEChunkForContent(chunk, true, "text")
if finished {
t.Fatal("expected not finished")
}
if nextType != "thinking" {
t.Fatalf("expected nextType thinking, got %q", nextType)
}
if len(parts) != 1 || parts[0].Type != "thinking" || parts[0].Text != "ζ·±ε
₯ζθ..." {
t.Fatalf("unexpected parts: %#v", parts)
}
}
func TestParseSSEChunkForContentFragmentsAppendEmptyContent(t *testing.T) {
chunk := map[string]any{
"p": "response/fragments",
"o": "APPEND",
"v": []any{
map[string]any{
"type": "RESPONSE",
"content": "",
},
},
}
parts, finished, nextType := ParseSSEChunkForContent(chunk, true, "thinking")
if finished {
t.Fatal("expected not finished")
}
if nextType != "text" {
t.Fatalf("expected nextType text, got %q", nextType)
}
if len(parts) != 0 {
t.Fatalf("expected no parts for empty content, got %#v", parts)
}
}
func TestParseSSEChunkForContentFragmentsAppendDefaultType(t *testing.T) {
chunk := map[string]any{
"p": "response/fragments",
"o": "APPEND",
"v": []any{
map[string]any{
"type": "UNKNOWN",
"content": "some text",
},
},
}
parts, _, _ := ParseSSEChunkForContent(chunk, true, "text")
if len(parts) != 1 || parts[0].Type != "text" {
t.Fatalf("expected text type for unknown fragment type, got %#v", parts)
}
}
func TestParseSSEChunkForContentFragmentsAppendNonArray(t *testing.T) {
chunk := map[string]any{
"p": "response/fragments",
"o": "APPEND",
"v": "not an array",
}
parts, finished, _ := ParseSSEChunkForContent(chunk, true, "text")
if finished {
t.Fatal("expected not finished")
}
// "not an array" should be treated as string value at the end
if len(parts) != 1 || parts[0].Text != "not an array" {
t.Fatalf("unexpected parts: %#v", parts)
}
}
func TestParseSSEChunkForContentFragmentsAppendNonMap(t *testing.T) {
chunk := map[string]any{
"p": "response/fragments",
"o": "APPEND",
"v": []any{"string item"},
}
parts, _, _ := ParseSSEChunkForContent(chunk, false, "text")
// Non-map items in fragment array are skipped; the []any itself is handled later
_ = parts // just checking it doesn't panic
}
// βββ ParseSSEChunkForContent: response path with nested fragment βββββ
func TestParseSSEChunkForContentResponsePathFragmentsAppend(t *testing.T) {
chunk := map[string]any{
"p": "response",
"v": []any{
map[string]any{
"p": "fragments",
"o": "APPEND",
"v": []any{
map[string]any{
"type": "THINKING",
},
},
},
},
}
_, _, nextType := ParseSSEChunkForContent(chunk, true, "text")
if nextType != "thinking" {
t.Fatalf("expected nextType thinking from response path fragments, got %q", nextType)
}
}
func TestParseSSEChunkForContentResponsePathResponseFragment(t *testing.T) {
chunk := map[string]any{
"p": "response",
"v": []any{
map[string]any{
"p": "fragments",
"o": "APPEND",
"v": []any{
map[string]any{
"type": "RESPONSE",
},
},
},
},
}
_, _, nextType := ParseSSEChunkForContent(chunk, true, "thinking")
if nextType != "text" {
t.Fatalf("expected nextType text for RESPONSE fragment, got %q", nextType)
}
}
// βββ ParseSSEChunkForContent: map value with wrapped response ββββββββ
func TestParseSSEChunkForContentMapValueWithFragments(t *testing.T) {
chunk := map[string]any{
"v": map[string]any{
"response": map[string]any{
"fragments": []any{
map[string]any{
"type": "THINK",
"content": "ζθ...",
},
map[string]any{
"type": "RESPONSE",
"content": "εη...",
},
},
},
},
}
parts, finished, nextType := ParseSSEChunkForContent(chunk, true, "text")
if finished {
t.Fatal("expected not finished")
}
if nextType != "text" {
t.Fatalf("expected nextType text after RESPONSE, got %q", nextType)
}
if len(parts) != 2 {
t.Fatalf("expected 2 parts, got %d: %#v", len(parts), parts)
}
if parts[0].Type != "thinking" || parts[0].Text != "ζθ..." {
t.Fatalf("first part mismatch: %#v", parts[0])
}
if parts[1].Type != "text" || parts[1].Text != "εη..." {
t.Fatalf("second part mismatch: %#v", parts[1])
}
}
func TestParseSSEChunkForContentMapValueDirectFragments(t *testing.T) {
chunk := map[string]any{
"v": map[string]any{
"fragments": []any{
map[string]any{
"type": "RESPONSE",
"content": "η΄ζ₯εη",
},
},
},
}
parts, _, _ := ParseSSEChunkForContent(chunk, false, "text")
if len(parts) != 1 || parts[0].Text != "η΄ζ₯εη" || parts[0].Type != "text" {
t.Fatalf("unexpected parts for direct fragments: %#v", parts)
}
}
func TestParseSSEChunkForContentMapValueUnknownType(t *testing.T) {
chunk := map[string]any{
"v": map[string]any{
"fragments": []any{
map[string]any{
"type": "CUSTOM",
"content": "custom content",
},
},
},
}
parts, _, _ := ParseSSEChunkForContent(chunk, false, "text")
if len(parts) != 1 || parts[0].Type != "text" {
t.Fatalf("expected partType fallback for unknown type, got %#v", parts)
}
}
func TestParseSSEChunkForContentMapValueEmptyFragmentContent(t *testing.T) {
chunk := map[string]any{
"v": map[string]any{
"fragments": []any{
map[string]any{
"type": "RESPONSE",
"content": "",
},
},
},
}
parts, _, _ := ParseSSEChunkForContent(chunk, false, "text")
if len(parts) != 0 {
t.Fatalf("expected no parts for empty fragment content, got %#v", parts)
}
}
// βββ ParseSSEChunkForContent: fragments/-1/content path ββββββββββββββ
func TestParseSSEChunkForContentFragmentContentPathInheritsType(t *testing.T) {
chunk := map[string]any{
"p": "response/fragments/-1/content",
"v": "η»§η»ζθ",
}
parts, _, _ := ParseSSEChunkForContent(chunk, true, "thinking")
if len(parts) != 1 || parts[0].Type != "thinking" {
t.Fatalf("expected inherited thinking type, got %#v", parts)
}
}
// βββ IsCitation edge cases βββββββββββββββββββββββββββββββββββββββββββ
func TestIsCitationWithLeadingWhitespace(t *testing.T) {
if !IsCitation(" [citation:2] text") {
t.Fatal("expected citation true with leading whitespace")
}
}
func TestIsCitationEmpty(t *testing.T) {
if IsCitation("") {
t.Fatal("expected citation false for empty string")
}
}
func TestIsCitationSimilarPrefix(t *testing.T) {
if IsCitation("[cite:1] text") {
t.Fatal("expected citation false for [cite: prefix")
}
}
// βββ extractContentRecursive edge cases ββββββββββββββββββββββββββββββ
func TestExtractContentRecursiveFinishedStatus(t *testing.T) {
items := []any{
map[string]any{"p": "status", "v": "FINISHED"},
}
parts, finished := extractContentRecursive(items, "text")
if !finished {
t.Fatal("expected finished on status FINISHED")
}
if len(parts) != 0 {
t.Fatalf("expected no parts, got %#v", parts)
}
}
func TestExtractContentRecursiveSkipsPath(t *testing.T) {
items := []any{
map[string]any{"p": "quasi_status", "v": "data"},
}
parts, finished := extractContentRecursive(items, "text")
if finished {
t.Fatal("expected not finished")
}
if len(parts) != 0 {
t.Fatalf("expected no parts for skipped path, got %#v", parts)
}
}
func TestExtractContentRecursiveContentField(t *testing.T) {
items := []any{
map[string]any{"p": "x", "v": "val", "content": "actual content", "type": "RESPONSE"},
}
parts, _ := extractContentRecursive(items, "text")
if len(parts) != 1 || parts[0].Text != "actual content" || parts[0].Type != "text" {
t.Fatalf("unexpected parts: %#v", parts)
}
}
func TestExtractContentRecursiveContentFieldThinkType(t *testing.T) {
items := []any{
map[string]any{"p": "x", "v": "val", "content": "think text", "type": "THINK"},
}
parts, _ := extractContentRecursive(items, "text")
if len(parts) != 1 || parts[0].Type != "thinking" {
t.Fatalf("expected thinking type for THINK content, got %#v", parts)
}
}
func TestExtractContentRecursiveThinkingPath(t *testing.T) {
items := []any{
map[string]any{"p": "thinking_content", "v": "deep thought"},
}
parts, _ := extractContentRecursive(items, "text")
if len(parts) != 1 || parts[0].Type != "thinking" || parts[0].Text != "deep thought" {
t.Fatalf("unexpected parts for thinking path: %#v", parts)
}
}
func TestExtractContentRecursiveContentPath(t *testing.T) {
items := []any{
map[string]any{"p": "content", "v": "text content"},
}
parts, _ := extractContentRecursive(items, "thinking")
if len(parts) != 1 || parts[0].Type != "text" {
t.Fatalf("expected text type for content path, got %#v", parts)
}
}
func TestExtractContentRecursiveResponsePath(t *testing.T) {
items := []any{
map[string]any{"p": "response", "v": "text content"},
}
parts, _ := extractContentRecursive(items, "thinking")
if len(parts) != 1 || parts[0].Type != "text" {
t.Fatalf("expected text type for response path, got %#v", parts)
}
}
func TestExtractContentRecursiveFragmentsPath(t *testing.T) {
items := []any{
map[string]any{"p": "fragments", "v": "fragment text"},
}
parts, _ := extractContentRecursive(items, "thinking")
if len(parts) != 1 || parts[0].Type != "text" {
t.Fatalf("expected text type for fragments path, got %#v", parts)
}
}
func TestExtractContentRecursiveNestedArrayWithTypes(t *testing.T) {
items := []any{
map[string]any{
"p": "fragments",
"v": []any{
map[string]any{"content": "thought", "type": "THINKING"},
map[string]any{"content": "answer", "type": "RESPONSE"},
"raw string",
},
},
}
parts, _ := extractContentRecursive(items, "text")
if len(parts) != 3 {
t.Fatalf("expected 3 parts, got %d: %#v", len(parts), parts)
}
if parts[0].Type != "thinking" || parts[0].Text != "thought" {
t.Fatalf("first part mismatch: %#v", parts[0])
}
if parts[1].Type != "text" || parts[1].Text != "answer" {
t.Fatalf("second part mismatch: %#v", parts[1])
}
if parts[2].Type != "text" || parts[2].Text != "raw string" {
t.Fatalf("third part mismatch: %#v", parts[2])
}
}
func TestExtractContentRecursiveEmptyContentSkipped(t *testing.T) {
items := []any{
map[string]any{
"p": "fragments",
"v": []any{
map[string]any{"content": "", "type": "RESPONSE"},
},
},
}
parts, _ := extractContentRecursive(items, "text")
if len(parts) != 0 {
t.Fatalf("expected no parts for empty nested content, got %#v", parts)
}
}
func TestExtractContentRecursiveFinishedString(t *testing.T) {
items := []any{
map[string]any{"p": "content", "v": "FINISHED"},
}
parts, _ := extractContentRecursive(items, "text")
// "FINISHED" string value on non-status path should be skipped
if len(parts) != 0 {
t.Fatalf("expected FINISHED string to be skipped, got %#v", parts)
}
}
func TestExtractContentRecursiveNoVField(t *testing.T) {
items := []any{
map[string]any{"p": "content"},
}
parts, _ := extractContentRecursive(items, "text")
if len(parts) != 0 {
t.Fatalf("expected no parts for missing v field, got %#v", parts)
}
}
func TestExtractContentRecursiveNonMapItem(t *testing.T) {
items := []any{"just a string", 42}
parts, _ := extractContentRecursive(items, "text")
if len(parts) != 0 {
t.Fatalf("expected no parts for non-map items, got %#v", parts)
}
}
|