pi-mono/packages/coding-agent/test/bash-execution-width.test.ts
Gangqiang Yao 7453388114
fix(coding-agent): name the interrupt key in the ! command cancel hint (#12518)
Closes #12517.

**Problem.** While a `!` command runs, its hint reads
`tui.select.cancel`, whose defaults are escape and ctrl+c:

```
⠇ Running... (escape/ctrl+c to cancel)
```

The command is cancelled through `app.interrupt` (escape by default):
`onEscape` calls `abortBash()`. Ctrl+C goes to `app.clear`, which clears
the editor, and a second press within 500 ms quits pi. So following the
hint quits pi instead of stopping the command. The warning shown for a
second `!` command while one runs also hardcodes "Press Esc to cancel it
first", which goes stale when `app.interrupt` is remapped.

**Fix.** Both now use `keyText("app.interrupt")`, like the retry,
compaction and branch-summary hints in `status-indicator.ts` and the
"Working..." hint in `interactive-mode.ts`. No key handling changes.

I checked the other `tui.select.cancel` hints (session delete and
rename, thinking selector, trust, first-time setup, tree labels): each
one belongs to a handler that does match `tui.select.cancel`, so this
was the only mismatch.

**Validation** on main `c11a73bfba`:

- New tests in `test/bash-execution-width.test.ts`, the component's test
file: the running hint names `escape` with default bindings and `ctrl+g`
with `app.interrupt` remapped. Both fail without the fix.
- In tmux, with an empty agent dir, no extensions and no model, running
`!sleep 30` and then `!echo second`:
- before: `Running... (escape/ctrl+c to cancel)`; Ctrl+C does not stop
the command and a second Ctrl+C quits pi.
- after: `Running... (escape to cancel)` and `Press escape to cancel it
first.`; with `{"app.interrupt": "ctrl+g"}`, both say `ctrl+g`.
- `npm run check` passes. In `./test.sh` the only failures are unrelated
to this change:
- The find tool tests fail because fd is not installed here and the test
config prevents downloading it.
  - The pi-env tests need cargo, which is not installed here.
- chord's `http` close test failed once under load and passes on its
own.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 15:57:22 +01:00

109 lines
3.6 KiB
TypeScript

/**
* Test that BashExecutionComponent's collapsed output respects the render-time width,
* not a stale captured width. Regression test for #2569.
*/
import { setKeybindings, visibleWidth } from "@earendil-works/pi-tui";
import { afterEach, beforeAll, describe, expect, it } from "vitest";
import { KeybindingsManager } from "../src/core/keybindings.ts";
import { BashExecutionComponent } from "../src/modes/interactive/components/bash-execution.ts";
import { initTheme } from "../src/modes/interactive/theme/theme.ts";
/** Minimal TUI stub that only exposes terminal.columns */
function createTuiStub(columns: number): { columns: number; stub: any } {
const state = { columns };
const stub = {
terminal: {
get columns() {
return state.columns;
},
get rows() {
return 24;
},
},
// Loader calls ui.addInterval / ui.removeInterval
addInterval: (_cb: () => void, _ms: number) => ({ dispose: () => {} }),
removeInterval: () => {},
requestRender: () => {},
};
return { columns: state.columns, stub };
}
describe("BashExecutionComponent width handling (#2569)", () => {
beforeAll(() => {
initTheme(undefined, false);
});
it("collapsed preview lines respect render-time width, not construction-time width", () => {
const wideWidth = 200;
const narrowWidth = 80;
const { stub } = createTuiStub(wideWidth);
const component = new BashExecutionComponent("pwd", stub);
// Add output with long lines that will wrap differently at different widths
const longLine = "x".repeat(150);
component.appendOutput(`${longLine}\n${longLine}\n`);
// Complete the command so it enters collapsed mode
component.setComplete(0, false);
// Render at the narrow width (simulating a resize or split pane)
const lines = component.render(narrowWidth);
// Every rendered line must fit within the narrow width
for (let i = 0; i < lines.length; i++) {
const w = visibleWidth(lines[i]);
expect(w, `Line ${i} visibleWidth=${w} > ${narrowWidth}`).toBeLessThanOrEqual(narrowWidth);
}
});
it("re-computes lines when width changes between renders", () => {
const { stub } = createTuiStub(200);
const component = new BashExecutionComponent("echo hello", stub);
const longLine = "abcdefghij".repeat(20); // 200 chars
component.appendOutput(`${longLine}\n`);
component.setComplete(0, false);
// First render at width 200
const lines200 = component.render(200);
for (const line of lines200) {
expect(visibleWidth(line)).toBeLessThanOrEqual(200);
}
// Second render at width 60 (split pane scenario)
const lines60 = component.render(60);
for (let i = 0; i < lines60.length; i++) {
const w = visibleWidth(lines60[i]);
expect(w, `Line ${i} visibleWidth=${w} > 60`).toBeLessThanOrEqual(60);
}
});
});
// Regression test for https://github.com/earendil-works/pi/issues/12517
describe("BashExecutionComponent cancel hint", () => {
beforeAll(() => {
initTheme(undefined, false);
});
afterEach(() => {
setKeybindings(new KeybindingsManager());
});
function runningHint(): string | undefined {
const component = new BashExecutionComponent("sleep 30", createTuiStub(120).stub);
return component.render(120).find((line) => line.includes("Running..."));
}
it("names the interrupt key, which is the key that cancels the command", () => {
setKeybindings(new KeybindingsManager());
expect(runningHint()).toContain("Running... (escape to cancel)");
});
it("follows a remapped interrupt key", () => {
setKeybindings(new KeybindingsManager({ "app.interrupt": "ctrl+g" }));
expect(runningHint()).toContain("Running... (ctrl+g to cancel)");
});
});