bash-execution-width.test.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * Test that BashExecutionComponent's collapsed output respects the render-time width,
  3. * not a stale captured width. Regression test for #2569.
  4. */
  5. import { visibleWidth } from "@earendil-works/pi-tui";
  6. import { beforeAll, describe, expect, it } from "vitest";
  7. import { BashExecutionComponent } from "../src/modes/interactive/components/bash-execution.ts";
  8. import { initTheme } from "../src/modes/interactive/theme/theme.ts";
  9. /** Minimal TUI stub that only exposes terminal.columns */
  10. function createTuiStub(columns: number): { columns: number; stub: any } {
  11. const state = { columns };
  12. const stub = {
  13. terminal: {
  14. get columns() {
  15. return state.columns;
  16. },
  17. get rows() {
  18. return 24;
  19. },
  20. },
  21. // Loader calls ui.addInterval / ui.removeInterval
  22. addInterval: (_cb: () => void, _ms: number) => ({ dispose: () => {} }),
  23. removeInterval: () => {},
  24. requestRender: () => {},
  25. };
  26. return { columns: state.columns, stub };
  27. }
  28. describe("BashExecutionComponent width handling (#2569)", () => {
  29. beforeAll(() => {
  30. initTheme(undefined, false);
  31. });
  32. it("collapsed preview lines respect render-time width, not construction-time width", () => {
  33. const wideWidth = 200;
  34. const narrowWidth = 80;
  35. const { stub } = createTuiStub(wideWidth);
  36. const component = new BashExecutionComponent("pwd", stub);
  37. // Add output with long lines that will wrap differently at different widths
  38. const longLine = "x".repeat(150);
  39. component.appendOutput(`${longLine}\n${longLine}\n`);
  40. // Complete the command so it enters collapsed mode
  41. component.setComplete(0, false);
  42. // Render at the narrow width (simulating a resize or split pane)
  43. const lines = component.render(narrowWidth);
  44. // Every rendered line must fit within the narrow width
  45. for (let i = 0; i < lines.length; i++) {
  46. const w = visibleWidth(lines[i]);
  47. expect(w, `Line ${i} visibleWidth=${w} > ${narrowWidth}`).toBeLessThanOrEqual(narrowWidth);
  48. }
  49. });
  50. it("re-computes lines when width changes between renders", () => {
  51. const { stub } = createTuiStub(200);
  52. const component = new BashExecutionComponent("echo hello", stub);
  53. const longLine = "abcdefghij".repeat(20); // 200 chars
  54. component.appendOutput(`${longLine}\n`);
  55. component.setComplete(0, false);
  56. // First render at width 200
  57. const lines200 = component.render(200);
  58. for (const line of lines200) {
  59. expect(visibleWidth(line)).toBeLessThanOrEqual(200);
  60. }
  61. // Second render at width 60 (split pane scenario)
  62. const lines60 = component.render(60);
  63. for (let i = 0; i < lines60.length; i++) {
  64. const w = visibleWidth(lines60[i]);
  65. expect(w, `Line ${i} visibleWidth=${w} > 60`).toBeLessThanOrEqual(60);
  66. }
  67. });
  68. });