| 1 | package chatstream |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestPartialReply(t *testing.T) { |
| 6 | var s Stream |
| 7 | for _, raw := range []string{ |
| 8 | `{"event":{"type":"content_block_start","index":0,"content_block":{"type":"tool_use","name":"mcp__equipo__post_message"}}}`, |
| 9 | `{"event":{"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"{\"body\":\"Hello \\u263a"}}}`, |
| 10 | } { |
| 11 | s.Add([]byte(raw)) |
| 12 | } |
| 13 | if s.Text() != "Hello ☺" { |
| 14 | t.Fatal(s.Text()) |
| 15 | } |
| 16 | s.Add([]byte(`{"event":{"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":" friend\",\"source_refs\":[]}"}}}`)) |
| 17 | if s.Text() != "Hello ☺ friend" { |
| 18 | t.Fatal(s.Text()) |
| 19 | } |
| 20 | s.Add([]byte(`{"event":{"type":"content_block_start","index":1,"content_block":{"type":"tool_use","name":"other"}}}`)) |
| 21 | s.Add([]byte(`{"event":{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"{\"body\":\"secret\"}"}}}`)) |
| 22 | if s.Text() != "Hello ☺ friend" { |
| 23 | t.Fatal("other tool leaked") |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func TestNativeTextAndPartialEscapes(t *testing.T) { |
| 28 | var s Stream |
| 29 | s.Add([]byte(`{"event":{"type":"content_block_delta","delta":{"type":"text_delta","text":"Hello "}}}`)) |
| 30 | s.Add([]byte(`{"event":{"type":"content_block_delta","delta":{"type":"text_delta","text":"world"}}}`)) |
| 31 | if s.Text() != "Hello world" { |
| 32 | t.Fatal(s.Text()) |
| 33 | } |
| 34 | for _, v := range []struct{ raw, want string }{ |
| 35 | {`{"body":"a\`, "a"}, {`{"body":"a\u26`, "a"}, {`{"body":"a\u263a`, "a☺"}, {`{"body":"\ud83d`, ""}, {`{"body":"\ud83d\ude00`, "😀"}, {`{"source_refs":[],"body":"after`, "after"}, |
| 36 | } { |
| 37 | got, ok := partialBody(v.raw) |
| 38 | if !ok || got != v.want { |
| 39 | t.Fatalf("partial %q: %q %v", v.raw, got, ok) |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |