mirror of
https://github.com/cexll/myclaude.git
synced 2026-02-15 03:32:43 +08:00
test(ParseJSONStream): 增加对超大单行文本和非字符串文本的处理测试
This commit is contained in:
@@ -330,12 +330,16 @@ func TestNormalizeText(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestParseJSONStream(t *testing.T) {
|
func TestParseJSONStream(t *testing.T) {
|
||||||
tests := []struct {
|
type testCase struct {
|
||||||
name string
|
name string
|
||||||
input string
|
input string
|
||||||
wantMessage string
|
wantMessage string
|
||||||
wantThreadID string
|
wantThreadID string
|
||||||
}{
|
}
|
||||||
|
|
||||||
|
longText := strings.Repeat("a", 2*1024*1024) // >1MB agent_message payload
|
||||||
|
|
||||||
|
tests := []testCase{
|
||||||
{
|
{
|
||||||
name: "thread started and agent message",
|
name: "thread started and agent message",
|
||||||
input: `{"type":"thread.started","thread_id":"abc-123"}
|
input: `{"type":"thread.started","thread_id":"abc-123"}
|
||||||
@@ -364,6 +368,12 @@ func TestParseJSONStream(t *testing.T) {
|
|||||||
wantMessage: "Valid",
|
wantMessage: "Valid",
|
||||||
wantThreadID: "",
|
wantThreadID: "",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "super long single line (>1MB)",
|
||||||
|
input: `{"type":"item.completed","item":{"type":"agent_message","text":"` + longText + `"}}`,
|
||||||
|
wantMessage: longText,
|
||||||
|
wantThreadID: "",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "empty input",
|
name: "empty input",
|
||||||
input: "",
|
input: "",
|
||||||
@@ -371,23 +381,25 @@ func TestParseJSONStream(t *testing.T) {
|
|||||||
wantThreadID: "",
|
wantThreadID: "",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "invalid JSON (skipped)",
|
name: "item completed with nil item",
|
||||||
input: "not valid json\n{\"type\":\"thread.started\",\"thread_id\":\"xyz\"}",
|
input: strings.Join([]string{
|
||||||
|
`{"type":"thread.started","thread_id":"nil-item-thread"}`,
|
||||||
|
`{"type":"item.completed","item":null}`,
|
||||||
|
}, "\n"),
|
||||||
wantMessage: "",
|
wantMessage: "",
|
||||||
wantThreadID: "xyz",
|
wantThreadID: "nil-item-thread",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "blank lines ignored",
|
name: "agent message with non-string text",
|
||||||
input: "\n\n{\"type\":\"thread.started\",\"thread_id\":\"test\"}\n\n",
|
input: `{"type":"item.completed","item":{"type":"agent_message","text":12345}}`,
|
||||||
wantMessage: "",
|
wantMessage: "",
|
||||||
wantThreadID: "test",
|
wantThreadID: "",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
r := strings.NewReader(tt.input)
|
gotMessage, gotThreadID := parseJSONStream(strings.NewReader(tt.input))
|
||||||
gotMessage, gotThreadID := parseJSONStream(r)
|
|
||||||
|
|
||||||
if gotMessage != tt.wantMessage {
|
if gotMessage != tt.wantMessage {
|
||||||
t.Errorf("parseJSONStream() message = %q, want %q", gotMessage, tt.wantMessage)
|
t.Errorf("parseJSONStream() message = %q, want %q", gotMessage, tt.wantMessage)
|
||||||
|
|||||||
Reference in New Issue
Block a user