From d5fe0adfe7375d7a28407cd5981bb2f934b8bb84 Mon Sep 17 00:00:00 2001 From: Codeman maintainer Date: Fri, 27 Nov 2026 06:10:21 +0100 Subject: [PATCH] test(tmux): the review item of #840 that goes on top The live-pane close case moves into test/kill-pid-guard.test.ts, whose harness answers ps from a fixed table. In tmux-manager.test.ts the mock keeps the real execFile, so the walk read this machine's process table. Co-Authored-By: Claude Fable 5.2 --- test/kill-pid-guard.test.ts | 25 ++++++++++++++-- test/tmux-manager.test.ts | 60 ------------------------------------- 2 files changed, 23 insertions(+), 62 deletions(-) diff --git a/test/kill-pid-guard.test.ts b/test/kill-pid-guard.test.ts index b2a3ec64..bd3cacf7 100644 --- a/test/kill-pid-guard.test.ts +++ b/test/kill-pid-guard.test.ts @@ -25,8 +25,12 @@ vi.mock('node:child_process', async () => { /** The kill path's pane read, as killSession() sends it. */ const KILL_READ = "'#{pane_dead}|#{pane_pid}|#{pane_dead_time}'"; -/** A machine: init, kthreadd, the user's own programs, and one agent (4242) with a child. */ -const PS = ['1 0', '2 0', '900 1', '901 900', '902 900', '4242 901', '4243 4242'].join('\n') + '\n'; +/** + * A machine: init, kthreadd, the user's own programs, one agent (4242) with a child, and a + * second program (4343) with a child, for a pane whose process is not the recorded one. + */ +const PS = + ['1 0', '2 0', '900 1', '901 900', '902 900', '4242 901', '4243 4242', '4343 901', '4344 4343'].join('\n') + '\n'; describe('isSignalablePid', () => { it('admits only a process of the user', () => { @@ -129,6 +133,23 @@ describe('killSession and a pane without a process', () => { // `0|0|` on 3.7, and `0||` is tmux 3.8's closing window (pty closed, exit not // collected). Each falls back to the recorded agent pid, as master does, and // nothing outside that pid's tree is signalled. + // The counterpart of the dead-pane rule: it must not stop a close from killing a pane + // whose program still runs. Here, never in tmux-manager.test.ts: that file's mock keeps + // the real execFile, so the walk would read this machine's process table. + it('signals the tree of the pid tmux prints for a live pane, not the recorded one', async () => { + paneRead = '0|4343|\n'; + const m = await manager(4242); + expect(await m.killSession('feed1234')).toBe(true); + expect(signals).toContainEqual([4344, 'SIGTERM']); + expect(signals).toContainEqual([-4343, 'SIGTERM']); + expect(signals.every(([pid]) => [4343, 4344].includes(Math.abs(pid)))).toBe(true); + // The whole kill read: dropping `#{pane_dead_time}` would trust a pre-3.7 dead flag again. + expect(readTheKillFormat()).toBe(true); + expect(closedByTmux()).toBe(true); + expect(m.getSession('feed1234')).toBeUndefined(); + m.destroy(); + }); + for (const answer of ['1|0|', '0|0|', '0||']) { it(`falls back to the recorded pid when the kill read answers ${answer}`, async () => { paneRead = `${answer}\n`; diff --git a/test/tmux-manager.test.ts b/test/tmux-manager.test.ts index 17a8565c..b2e1161d 100644 --- a/test/tmux-manager.test.ts +++ b/test/tmux-manager.test.ts @@ -1406,66 +1406,6 @@ describe('TmuxManager (unit)', () => { nonTestManager.destroy(); } }); - - // The counterpart: the dead-pane rule must not stop a close from killing a - // pane whose program still runs. - it('still signals the process tree of a live pane on close', async () => { - const defaultExec = mockedExec.getMockImplementation(); - mockedExec.mockImplementation(((cmd: string, optionsOrCallback?: unknown, maybeCallback?: unknown) => { - const callback = (typeof optionsOrCallback === 'function' ? optionsOrCallback : maybeCallback) as - | ((err: Error | null, out: unknown, stderr: string) => void) - | undefined; - // promisify(exec) on this mock resolves with the second argument. A live - // pane: not dead, its own pid, no exit time. - const stdout = cmd.includes(' display-message ') ? (cmd.includes('#{pane_dead}') ? '0|4343|\n' : '4343\n') : ''; - if (callback) setImmediate(() => callback(null, { stdout, stderr: '' }, '')); - return { on: vi.fn(), kill: vi.fn(), pid: 12345 }; - }) as unknown as typeof exec); - // The pane's process exits on its first real signal, as a live CLI does. - // Afterwards the liveness probe (`kill(pid, 0)`) throws, so the kill path - // stops waiting before it reads /proc, which this file mocks (Linux CI). - let paneAlive = true; - const kill = vi.spyOn(process, 'kill').mockImplementation(((pid: number, signal?: string | number) => { - if (Math.abs(pid) !== 4343) return true; - if (signal === 0) { - if (paneAlive) return true; - throw Object.assign(new Error('kill ESRCH'), { code: 'ESRCH' }); - } - paneAlive = false; - return true; - }) as typeof process.kill); - const NonTestTmuxManager = await importWithTmuxCommandsEnabled(); - const nonTestManager = new NonTestTmuxManager(); - nonTestManager.registerSession({ - sessionId: 'beef5555', - muxName: 'codeman-beef5555', - pid: 4242, - createdAt: Date.now(), - workingDir: '/tmp', - mode: 'claude', - attached: false, - }); - - try { - expect(await nonTestManager.killSession('beef5555')).toBe(true); - // The pid tmux printed is signalled, not the recorded one. - expect(kill.mock.calls.some(([pid]) => Math.abs(Number(pid)) === 4343)).toBe(true); - expect(kill.mock.calls.filter(([pid]) => Math.abs(Number(pid)) === 4242)).toEqual([]); - const commands = mockedExec.mock.calls.map(([cmd]) => String(cmd)); - // The whole kill read: dropping `#{pane_dead_time}` would trust a pre-3.7 dead flag again. - expect( - commands.some( - (cmd) => cmd.includes('display-message') && cmd.includes("'#{pane_dead}|#{pane_pid}|#{pane_dead_time}'") - ) - ).toBe(true); - expect(commands.some((cmd) => cmd.includes('kill-session -t "codeman-beef5555"'))).toBe(true); - expect(nonTestManager.getSession('beef5555')).toBeUndefined(); - } finally { - kill.mockRestore(); - mockedExec.mockImplementation(defaultExec!); - nonTestManager.destroy(); - } - }); }); });