gateway: the 24MB CR test reads a mended line (#4256)

TestCRTidyTwentyFourMBLine's "one call mended" case never mended
anything. Its line ended its output array with a trailing comma, so the
JSON could not be read and crTidy passed it as it came. Its want was
strings.Replace(line, `x\ry`, `xy`), but on the wire the escape is
`x\\ry`, so the Replace changed nothing either. Both sides equalled the
input, and the subtest passed while timing a failed parse.

The fixture now joins its items without the trailing comma. The mended
case reads the output back: the line is still one data line, fc_1's
arguments are {"a":"xy"}, and every text item is there. The healthy case
is still compared byte for byte.

Run on mote (macOS), go test -tags nogui -count=1 -v -run
TestCRTidyTwentyFourMBLine ./internal/gateway/:
- the healthy 24MB line ~25ms, the mended one ~280ms (a whole decode and
  marshal, only on glm-5.3-flash* lines that carry a \r escape);
- with crTidy.responses' item mend turned off by hand, the mended case
  is red ("arguments ... want {"a":"xy"}"); it was green on the old
  fixture.
This commit is contained in:
yetone
2026-10-31 01:53:21 +08:00
parent dd279e9314
commit 0fcb3acb3f
+32 -3
View File
@@ -944,19 +944,46 @@ func TestCRTidyTwentyFourMBLine(t *testing.T) {
b.WriteString(`{"type":"function_call","id":"fc_1","call_id":"call_1","name":"n","arguments":"{\"a\":\"x\\ry\"}"},`)
}
for i := 0; i < rep; i++ {
if i > 0 {
b.WriteString(",")
}
b.WriteString(unit)
b.WriteString(",")
}
b.WriteString(`]}}` + "\n\n")
return b.String()
}
// the mended line is said again with its keys sorted, so it is read
// back rather than compared byte for byte
mended := func(t *testing.T, got string) {
if strings.Count(got, "data: ") != 1 || !strings.HasSuffix(got, "\n\n") {
t.Fatal("the mended line is not one data line")
}
var ev struct {
Response struct {
Output []map[string]any `json:"output"`
} `json:"response"`
}
if err := json.Unmarshal([]byte(strings.TrimSuffix(strings.TrimPrefix(got, "data: "), "\n\n")), &ev); err != nil {
t.Fatalf("the mended line does not read: %v", err)
}
out := ev.Response.Output
if len(out) != rep+1 {
t.Fatalf("%d items came out, want %d", len(out), rep+1)
}
if out[0]["id"] != "fc_1" || out[0]["arguments"] != `{"a":"xy"}` {
t.Fatalf("arguments %q, want {\"a\":\"xy\"}", out[0]["arguments"])
}
if out[rep]["text"] != `hello "world" padding ` {
t.Fatalf("the last text item came out as %v", out[rep])
}
}
for _, c := range []struct {
name string
line string
want string
}{
{"no escape", big(false), big(false)},
{"one call mended", big(true), strings.Replace(big(true), `x\ry`, `xy`, 1)},
{"one call mended", big(true), ""},
} {
t.Run(c.name, func(t *testing.T) {
tidy := &crTidy{proto: provider.Responses, sse: true}
@@ -973,7 +1000,9 @@ func TestCRTidyTwentyFourMBLine(t *testing.T) {
}
elapsed := time.Since(start)
t.Logf("24MB line in 32KB reads: %s", elapsed)
if string(got) != c.want {
if c.want == "" {
mended(t, string(got))
} else if string(got) != c.want {
t.Fatal("the line came out other than wanted")
}
if elapsed > 8*time.Second {