|
2 | 2 | // "true" (any case), each accepted thought is drawn as a box on stderr, with |
3 | 3 | // a header that differs for plain thoughts, revisions and branches, and one |
4 | 4 | // row per line of the thought, wrapped at MAX_BOX_TEXT_WIDTH and cut off |
5 | | -// after MAX_BOX_ROWS. Thoughts |
| 5 | +// once header and thought fill MAX_BOX_ROWS rows. Thoughts |
6 | 6 | // are driven through the in-process client and stderr is captured by spying |
7 | 7 | // on console.error, which also keeps the boxes off the test output. chalk's |
8 | 8 | // colour level depends on the terminal the tests run in, so it is pinned to 0 |
@@ -232,15 +232,18 @@ ${rows.join("\n")} |
232 | 232 | }); |
233 | 233 |
|
234 | 234 | // #5433: past MAX_BOX_ROWS the box stops drawing and says how many lines |
235 | | - // it left out; the call itself is unaffected. |
| 235 | + // it left out; the call itself is unaffected. The header's one row counts |
| 236 | + // toward the cap (#5526), so the thought gets the rest. |
236 | 237 | it("draws a thought with very many lines", async () => { |
237 | 238 | const lines = Array.from({ length: 200_000 }, (_, i) => `${i % 10}`); |
238 | 239 | const result = await conn!.think(thought({ thought: lines.join("\n") })); |
239 | 240 | expect(result.isError).toBeFalsy(); |
240 | 241 | const out = logged()[0].split("\n"); |
241 | | - expect(out).toHaveLength(MAX_BOX_ROWS + 1 + 5); |
| 242 | + // A leading blank, the borders and separator, MAX_BOX_ROWS rows of |
| 243 | + // header and thought, and the summary row. |
| 244 | + expect(out).toHaveLength(MAX_BOX_ROWS + 5); |
242 | 245 | expect(out.at(-2)).toContain( |
243 | | - `… ${200_000 - MAX_BOX_ROWS} more lines not shown`, |
| 246 | + `… ${200_000 - (MAX_BOX_ROWS - 1)} more lines not shown`, |
244 | 247 | ); |
245 | 248 | // The summary is wider than every one-character line and the header, |
246 | 249 | // so it sets the box's width; it must not stick out of it. |
@@ -284,28 +287,33 @@ ${row("short")} |
284 | 287 | const result = await conn!.think(thought({ thought: text })); |
285 | 288 | expect(result.isError).toBeFalsy(); |
286 | 289 | const out = logged()[0]; |
287 | | - // 200 wrapped rows for the long line, short lines up to the row cap, |
288 | | - // then the summary row; each row is W + 6 characters and a newline. |
289 | | - const shortShown = MAX_BOX_ROWS - n / W; |
290 | | - expect(out.split("\n")).toHaveLength(MAX_BOX_ROWS + 1 + 5); |
| 290 | + // The header row, 200 wrapped rows for the long line, short lines up to |
| 291 | + // the row cap, then the summary row; each row is W + 6 characters and a |
| 292 | + // newline. |
| 293 | + const shortShown = MAX_BOX_ROWS - 1 - n / W; |
| 294 | + expect(out.split("\n")).toHaveLength(MAX_BOX_ROWS + 5); |
291 | 295 | expect(out).toContain(`… ${n - 1 - shortShown} more lines not shown`); |
292 | 296 | expect(out.length).toBeLessThan((MAX_BOX_ROWS + 10) * (W + 7)); |
293 | 297 | }); |
294 | 298 |
|
295 | 299 | it("counts a line the row cap cuts off as not shown", async () => { |
296 | | - const text = `${"s\n".repeat(MAX_BOX_ROWS - 1)}${"L".repeat(2 * W)}`; |
| 300 | + // The header row and these short lines leave one row for the long one. |
| 301 | + const text = `${"s\n".repeat(MAX_BOX_ROWS - 2)}${"L".repeat(2 * W)}`; |
297 | 302 | await conn!.think(thought({ thought: text })); |
298 | 303 | const rows = logged()[0].split("\n"); |
299 | | - expect(rows).toHaveLength(MAX_BOX_ROWS + 1 + 5); |
| 304 | + expect(rows).toHaveLength(MAX_BOX_ROWS + 5); |
300 | 305 | expect(rows.at(-3)).toBe(row("L".repeat(W))); |
301 | 306 | expect(rows.at(-2)).toBe(row("… 1 more line not shown")); |
302 | 307 | }); |
303 | 308 |
|
304 | 309 | it("draws a thought that exactly fills the cap with no summary row", async () => { |
305 | | - const text = Array.from({ length: MAX_BOX_ROWS }, () => "s").join("\n"); |
| 310 | + // With the header's one row, these lines fill the cap exactly. |
| 311 | + const text = Array.from({ length: MAX_BOX_ROWS - 1 }, () => "s").join( |
| 312 | + "\n", |
| 313 | + ); |
306 | 314 | await conn!.think(thought({ thought: text })); |
307 | 315 | const rows = logged()[0].split("\n"); |
308 | | - expect(rows).toHaveLength(MAX_BOX_ROWS + 5); |
| 316 | + expect(rows).toHaveLength(MAX_BOX_ROWS + 4); |
309 | 317 | expect(logged()[0]).not.toContain("not shown"); |
310 | 318 | }); |
311 | 319 |
|
@@ -338,6 +346,81 @@ ${row("short")} |
338 | 346 | expect(out.length).toBeLessThan((lines + 10) * (W + 7)); |
339 | 347 | }); |
340 | 348 |
|
| 349 | + // #5526: a long branchId's header rows count toward MAX_BOX_ROWS, so they |
| 350 | + // cannot add rows past the cap the thought's rows are held to. |
| 351 | + it("counts a wrapped header's rows toward the row cap", async () => { |
| 352 | + const branchId = "b".repeat(2 * W); |
| 353 | + const header = `🌿 Branch 1/3 (from thought 1, ID: ${branchId})`; |
| 354 | + const headerRows = Math.ceil(header.length / W); |
| 355 | + expect(headerRows).toBeGreaterThan(1); |
| 356 | + await conn!.think( |
| 357 | + thought({ |
| 358 | + thought: Array.from({ length: MAX_BOX_ROWS }, () => "s").join("\n"), |
| 359 | + branchFromThought: 1, |
| 360 | + branchId, |
| 361 | + }), |
| 362 | + ); |
| 363 | + const rows = logged()[0].split("\n"); |
| 364 | + expect(rows).toHaveLength(MAX_BOX_ROWS + 5); |
| 365 | + expect(rows.at(-2)).toBe(row(`… ${headerRows} more lines not shown`)); |
| 366 | + }); |
| 367 | + |
| 368 | + // #5526: a line ending in a branchId would start a line of output the |
| 369 | + // row cap never counted; the header shows it escaped instead. |
| 370 | + it("escapes line endings in a branchId so the header stays on its rows", async () => { |
| 371 | + const branchId = "x\r\ny\rz\n".repeat(500); |
| 372 | + const escaped = "x\\r\\ny\\rz\\n".repeat(500); |
| 373 | + const header = `🌿 Branch 1/3 (from thought 1, ID: ${escaped})`; |
| 374 | + await conn!.think( |
| 375 | + thought({ thought: "t", branchFromThought: 1, branchId }), |
| 376 | + ); |
| 377 | + const out = logged()[0]; |
| 378 | + expect(out).not.toContain("\r"); |
| 379 | + const rows = out.split("\n"); |
| 380 | + const headerRows = Math.ceil(header.length / W); |
| 381 | + // A leading blank, the borders and separator, the header rows and the |
| 382 | + // thought's one row. |
| 383 | + expect(rows).toHaveLength(headerRows + 5); |
| 384 | + expect( |
| 385 | + rows |
| 386 | + .slice(2, 2 + headerRows) |
| 387 | + .map((r) => r.slice(2, 2 + W).trimEnd()) |
| 388 | + .join(""), |
| 389 | + ).toBe(header); |
| 390 | + }); |
| 391 | + |
| 392 | + // #5526: a header too long for every row is cut, so a huge branchId logs |
| 393 | + // no more than the cap; its last row says how much was left out. |
| 394 | + it("cuts a header longer than the row cap", async () => { |
| 395 | + const branchId = "b".repeat(MAX_BOX_ROWS * W); |
| 396 | + const header = `🌿 Branch 1/3 (from thought 1, ID: ${branchId})`; |
| 397 | + const result = await conn!.think( |
| 398 | + thought({ thought: "t", branchFromThought: 1, branchId }), |
| 399 | + ); |
| 400 | + expect(result.isError).toBeFalsy(); |
| 401 | + const out = logged()[0]; |
| 402 | + const rows = out.split("\n"); |
| 403 | + // A leading blank, the top border, MAX_BOX_ROWS header rows, the |
| 404 | + // separator, the thought's summary row and the bottom border. |
| 405 | + expect(rows).toHaveLength(MAX_BOX_ROWS + 5); |
| 406 | + const shown = (MAX_BOX_ROWS - 1) * W; |
| 407 | + expect( |
| 408 | + rows |
| 409 | + .slice(2, MAX_BOX_ROWS + 1) |
| 410 | + .map((r) => r.slice(2, 2 + W)) |
| 411 | + .join(""), |
| 412 | + ).toBe(header.slice(0, shown)); |
| 413 | + expect(rows[MAX_BOX_ROWS + 1]).toBe( |
| 414 | + row(`… ${header.length - shown} more characters not shown`), |
| 415 | + ); |
| 416 | + expect(rows[MAX_BOX_ROWS + 2]).toBe(`├${border}┤`); |
| 417 | + expect(rows.at(-2)).toBe(row("… 1 more line not shown")); |
| 418 | + expect(new Set(rows.slice(1).map((r) => r.length))).toEqual( |
| 419 | + new Set([W + 6]), |
| 420 | + ); |
| 421 | + expect(out.length).toBeLessThan((MAX_BOX_ROWS + 10) * (W + 7)); |
| 422 | + }); |
| 423 | + |
341 | 424 | it("never splits an emoji across rows", async () => { |
342 | 425 | // The emoji's two UTF-16 halves would straddle the cut at W. |
343 | 426 | const text = `${"a".repeat(W - 1)}😀tail`; |
|
0 commit comments