test(tmux): close a dead-pane session end to end without signalling its pid

The kill rule was pinned only through parsePaneKillRead(). Drive
killSession() with tmux commands enabled and exec answering like tmux
3.8 (a dead pane, pid blank), and assert nothing signals the recorded
pid while kill-session still runs. Against bee4948e it fails: pid 4242
is signalled 201 times. Group the three no-pid tests in their own
describe instead of the launch-cwd one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kamil KaliszandClaude Opus 5.5 committed 2026-11-24 15:41:39 +01:00
1 parent df494b21ce
commit 67aa28ac4f
1 file changed
+64 -21
+64 -21
View File
@@ -1238,6 +1238,35 @@ describe('TmuxManager (unit)', () => {
}
});
it('treats the resolved spelling of the launch directory as no answer too (macOS /private/tmp)', async () => {
// On macOS /tmp is a symlink and a pane reports its process cwd resolved.
realpathOverrides.set('/tmp', '/private/tmp');
mockedExecSync.mockImplementation((cmd: string) => {
if (typeof cmd === 'string' && cmd.includes(' list-panes ')) {
return ['codeman-aaaa1111|101|0||||/private/tmp', 'codeman-bbbb2222|102|0||||/tmp'].join('\n');
}
return '';
});
try {
const NonTestTmuxManager = await importWithTmuxCommandsEnabled();
const nonTestManager = new NonTestTmuxManager();
try {
await nonTestManager.reconcileSessions();
const dirs = Object.fromEntries(nonTestManager.getSessions().map((s) => [s.muxName, s.workingDir]));
expect(dirs).toEqual({
'codeman-aaaa1111': process.cwd(),
'codeman-bbbb2222': process.cwd(),
});
} finally {
nonTestManager.destroy();
}
} finally {
realpathOverrides.clear();
}
});
});
describe('a dead pane tmux printed no pid for (tmux 3.8)', () => {
// tmux 3.8 prints a blank `#{pane_pid}` for a dead pane. Reading that as "no such
// session" dropped the record of a session whose agent had exited, while its tmux
// session stayed on the socket where nothing would ever find it again.
@@ -1296,30 +1325,44 @@ describe('TmuxManager (unit)', () => {
}
});
it('treats the resolved spelling of the launch directory as no answer too (macOS /private/tmp)', async () => {
// On macOS /tmp is a symlink and a pane reports its process cwd resolved.
realpathOverrides.set('/tmp', '/private/tmp');
mockedExecSync.mockImplementation((cmd: string) => {
if (typeof cmd === 'string' && cmd.includes(' list-panes ')) {
return ['codeman-aaaa1111|101|0||||/private/tmp', 'codeman-bbbb2222|102|0||||/tmp'].join('\n');
}
return '';
// The recorded pid of a dead pane belongs to the program that exited, and the OS
// may have handed it to an unrelated process since: closing must not signal it.
it('closes a session whose pane is dead without signalling its recorded pid', 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. tmux 3.8
// prints a dead pane's pid blank, so a pid-only read answers an empty line.
const stdout = cmd.includes(' display-message ') ? (cmd.includes('#{pane_dead}') ? '1|\n' : '\n') : '';
if (callback) setImmediate(() => callback(null, { stdout, stderr: '' }, ''));
return { on: vi.fn(), kill: vi.fn(), pid: 12345 };
}) as unknown as typeof exec);
const kill = vi.spyOn(process, 'kill').mockImplementation(() => true);
const NonTestTmuxManager = await importWithTmuxCommandsEnabled();
const nonTestManager = new NonTestTmuxManager();
nonTestManager.registerSession({
sessionId: 'dead5555',
muxName: 'codeman-dead5555',
pid: 4242,
createdAt: Date.now(),
workingDir: '/tmp',
mode: 'claude',
attached: false,
});
try {
const NonTestTmuxManager = await importWithTmuxCommandsEnabled();
const nonTestManager = new NonTestTmuxManager();
try {
await nonTestManager.reconcileSessions();
const dirs = Object.fromEntries(nonTestManager.getSessions().map((s) => [s.muxName, s.workingDir]));
expect(dirs).toEqual({
'codeman-aaaa1111': process.cwd(),
'codeman-bbbb2222': process.cwd(),
});
} finally {
nonTestManager.destroy();
}
expect(await nonTestManager.killSession('dead5555')).toBe(true);
expect(kill.mock.calls.filter(([pid]) => Math.abs(Number(pid)) === 4242)).toEqual([]);
const commands = mockedExec.mock.calls.map(([cmd]) => String(cmd));
expect(commands.some((cmd) => cmd.includes('display-message') && cmd.includes('#{pane_dead}'))).toBe(true);
expect(commands.some((cmd) => cmd.includes('kill-session -t "codeman-dead5555"'))).toBe(true);
expect(nonTestManager.getSession('dead5555')).toBeUndefined();
} finally {
realpathOverrides.clear();
kill.mockRestore();
mockedExec.mockImplementation(defaultExec!);
nonTestManager.destroy();
}
});
});