// Tests for the spacing of the streamed text. The bug they guard: the model // ends its sentence with line breaks — more and more of them as the turns go by // — and the screen hollowed out by as much. Reproducible end to end with // `SCENARIO=padding python3 ../03-one-agent-one-tool/docs/fake-engine.py`. package engine import "testing" // next is called once per streamed chunk; we check what gets printed for each // of them, in order. func TestGapHoldsTrailingBlanks(t *testing.T) { cases := []struct { name string fragments []string want []string }{{ name: "plain text", fragments: []string{"Hello"}, want: []string{"Hello"}, }, { name: "trailing whitespace is not printed", fragments: []string{"Let me look:\n\n"}, want: []string{"Let me look:"}, }, { name: "it is printed when text follows, down to one blank line", fragments: []string{"para one\n\n\n\n\n", "para two"}, want: []string{"para one", "\n\npara two"}, }, { name: "leading whitespace is dropped before the first word", fragments: []string{"\n\n", "Hello"}, want: []string{"", "Hello"}, }, { name: "all-whitespace chunks: accumulated, then capped", fragments: []string{"a", "\n", "\n", "\n", "\n", "b"}, want: []string{"a", "", "", "", "", "\n\nb"}, }, { name: "the indentation of a code block survives", fragments: []string{"code:", "\n fmt.Println()"}, want: []string{"code:", "\n fmt.Println()"}, }, { name: "a single break between two lines stays single", fragments: []string{"line one\n", "line two"}, want: []string{"line one", "\nline two"}, }, { name: "empty chunk", fragments: []string{"a", "", "b"}, want: []string{"a", "", "b"}, }} for _, c := range cases { t.Run(c.name, func(t *testing.T) { var g gap for i, frag := range c.fragments { if got := g.next(frag); got != c.want[i] { t.Errorf("chunk %d (%q): printed %q, want %q", i, frag, got, c.want[i]) } } }) } } // drop is called when a tool is about to write: the whitespace held back // belonged to the text before it, and printing it after the 🛠️ line would // reopen the hole. func TestGapDropForgetsHeldBlanks(t *testing.T) { var g gap if got := g.next("Let me look:\n\n\n"); got != "Let me look:" { t.Fatalf("printed %q", got) } g.drop() // 🛠️ bash: … gets printed here if got := g.next("Now let me check:"); got != "Now let me check:" { t.Errorf("after drop: printed %q, want %q with no whitespace in front", got, "Now let me check:") } } // The full scenario of the bug: four turns whose trailing whitespace grows, a // tool between each. None of that whitespace may be printed. func TestGapPaddingScenario(t *testing.T) { var g gap turns := []string{ "Let me look at the file:" + nl(2), "Now let me check something else:" + nl(4), "Let me try another approach:" + nl(7), "Let me rebuild the program:" + nl(11), } for i, turn := range turns { out := g.next(turn) if hasBlank(out) { t.Errorf("turn %d: %q contains trailing whitespace", i, out) } g.drop() // the tool writes its 🛠️ line } } func TestSqueeze(t *testing.T) { cases := map[string]string{ "": "", "\n": "\n", "\n\n": "\n\n", "\n\n\n": "\n\n", "\n\n\n\n\n\n\n\n": "\n\n", "\n ": "\n ", // indentation preserved "\n\n\n ": "\n\n ", // capped, indentation kept "a\n\n\n\nb": "a\n\nb", // in the middle of the text too " ": " ", // spaces are left alone } for in, want := range cases { if got := squeeze(in); got != want { t.Errorf("squeeze(%q) = %q, want %q", in, got, want) } } } func nl(n int) string { s := "" for range n { s += "\n" } return s } func hasBlank(s string) bool { if s == "" { return false } last := s[len(s)-1] return last == '\n' || last == ' ' || last == '\t' || last == '\r' }