package chatstream import "testing" func TestPartialReply(t *testing.T) { var s Stream for _, raw := range []string{ `{"event":{"type":"content_block_start","index":0,"content_block":{"type":"tool_use","name":"mcp__equipo__post_message"}}}`, `{"event":{"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"{\"body\":\"Hello \\u263a"}}}`, } { s.Add([]byte(raw)) } if s.Text() != "Hello ☺" { t.Fatal(s.Text()) } s.Add([]byte(`{"event":{"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":" friend\",\"source_refs\":[]}"}}}`)) if s.Text() != "Hello ☺ friend" { t.Fatal(s.Text()) } s.Add([]byte(`{"event":{"type":"content_block_start","index":1,"content_block":{"type":"tool_use","name":"other"}}}`)) s.Add([]byte(`{"event":{"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"{\"body\":\"secret\"}"}}}`)) if s.Text() != "Hello ☺ friend" { t.Fatal("other tool leaked") } } func TestNativeTextAndPartialEscapes(t *testing.T) { var s Stream s.Add([]byte(`{"event":{"type":"content_block_delta","delta":{"type":"text_delta","text":"Hello "}}}`)) s.Add([]byte(`{"event":{"type":"content_block_delta","delta":{"type":"text_delta","text":"world"}}}`)) if s.Text() != "Hello world" { t.Fatal(s.Text()) } for _, v := range []struct{ raw, want string }{ {`{"body":"a\`, "a"}, {`{"body":"a\u26`, "a"}, {`{"body":"a\u263a`, "a☺"}, {`{"body":"\ud83d`, ""}, {`{"body":"\ud83d\ude00`, "😀"}, {`{"source_refs":[],"body":"after`, "after"}, } { got, ok := partialBody(v.raw) if !ok || got != v.want { t.Fatalf("partial %q: %q %v", v.raw, got, ok) } } }