|
1 | 1 | // The server's other observable output: unless DISABLE_THOUGHT_LOGGING is |
2 | 2 | // "true" (any case), each accepted thought is drawn as a box on stderr, with |
3 | | -// a header that differs for plain thoughts, revisions and branches. Thoughts |
| 3 | +// a header that differs for plain thoughts, revisions and branches, and one |
| 4 | +// row per line of the thought. Thoughts |
4 | 5 | // are driven through the in-process client and stderr is captured by spying |
5 | 6 | // on console.error, which also keeps the boxes off the test output. chalk's |
6 | 7 | // colour level depends on the terminal the tests run in, so it is pinned to 0 |
@@ -41,14 +42,20 @@ describe("sequentialthinking thought logging", () => { |
41 | 42 | return errorSpy.mock.calls.map((args) => String(args[0])); |
42 | 43 | } |
43 | 44 |
|
44 | | - /** The box the server draws, for an uncoloured header. */ |
45 | | - function box(header: string, text: string): string { |
46 | | - const border = "─".repeat(Math.max(header.length, text.length) + 4); |
| 45 | + /** |
| 46 | + * The box the server draws, for an uncoloured header: one row per line of |
| 47 | + * the thought (given as an array), sized to the longest row. |
| 48 | + */ |
| 49 | + function box(header: string, text: string | string[]): string { |
| 50 | + const lines = typeof text === "string" ? [text] : text; |
| 51 | + const width = Math.max(header.length, ...lines.map((l) => l.length)); |
| 52 | + const border = "─".repeat(width + 4); |
| 53 | + const rows = lines.map((l) => `│ ${l.padEnd(border.length - 2)} │`); |
47 | 54 | return ` |
48 | 55 | ┌${border}┐ |
49 | 56 | │ ${header.padEnd(border.length - 2)} │ |
50 | 57 | ├${border}┤ |
51 | | -│ ${text.padEnd(border.length - 2)} │ |
| 58 | +${rows.join("\n")} |
52 | 59 | └${border}┘`; |
53 | 60 | } |
54 | 61 |
|
@@ -178,6 +185,58 @@ describe("sequentialthinking thought logging", () => { |
178 | 185 | expect(new Set(rows.map((row) => row.length)).size).toBe(1); |
179 | 186 | }); |
180 | 187 |
|
| 188 | + // #5205: a thought with line breaks is drawn one row per line, the box as |
| 189 | + // wide as its longest line, whichever line ending the client sent. |
| 190 | + describe("a multi-line thought", () => { |
| 191 | + beforeEach(async () => { |
| 192 | + conn = await connect({ disableThoughtLogging: undefined }); |
| 193 | + }); |
| 194 | + |
| 195 | + it.each([ |
| 196 | + ["\\n", "\n"], |
| 197 | + ["\\r\\n", "\r\n"], |
| 198 | + ["a lone \\r", "\r"], |
| 199 | + ])("draws one row per line split by %s", async (_l, eol) => { |
| 200 | + const lines = [ |
| 201 | + "first line", |
| 202 | + "a much longer second line of the thought", |
| 203 | + "3", |
| 204 | + ]; |
| 205 | + await conn!.think(thought({ thought: lines.join(eol) })); |
| 206 | + expect(logged()).toEqual([box("💭 Thought 1/3", lines)]); |
| 207 | + expect(logged()[0]).not.toContain("\r"); |
| 208 | + }); |
| 209 | + |
| 210 | + it("sizes the box from the longest line, not the whole thought", async () => { |
| 211 | + await conn!.think(thought({ thought: "ab\ncd\nef" })); |
| 212 | + // Shorter than the header, so the header sets the width. |
| 213 | + expect(logged()).toEqual([box("💭 Thought 1/3", ["ab", "cd", "ef"])]); |
| 214 | + }); |
| 215 | + |
| 216 | + it("keeps blank lines as blank rows", async () => { |
| 217 | + await conn!.think(thought({ thought: "top\n\nbottom\n" })); |
| 218 | + expect(logged()).toEqual([ |
| 219 | + box("💭 Thought 1/3", ["top", "", "bottom", ""]), |
| 220 | + ]); |
| 221 | + }); |
| 222 | + |
| 223 | + it("draws every row the same width", async () => { |
| 224 | + await conn!.think( |
| 225 | + thought({ thought: "short\na considerably longer line here\nmid" }), |
| 226 | + ); |
| 227 | + const rows = logged()[0].split("\n").slice(1); |
| 228 | + expect(rows).toHaveLength(7); |
| 229 | + expect(new Set(rows.map((row) => row.length)).size).toBe(1); |
| 230 | + }); |
| 231 | + |
| 232 | + it("draws a thought with very many lines", async () => { |
| 233 | + const lines = Array.from({ length: 200_000 }, (_, i) => `${i % 10}`); |
| 234 | + const result = await conn!.think(thought({ thought: lines.join("\n") })); |
| 235 | + expect(result.isError).toBeFalsy(); |
| 236 | + expect(logged()[0].split("\n")).toHaveLength(200_000 + 5); |
| 237 | + }); |
| 238 | + }); |
| 239 | + |
181 | 240 | // #5000: when colour is on, the header carries chalk's escape codes, but the |
182 | 241 | // border is sized from the visible text, so the box matches the uncoloured one. |
183 | 242 | it("sizes the border from the visible header, not its escape codes", async () => { |
|
0 commit comments