2026-02-12 11:35:10 +01:00
|
|
|
/**
|
2026-02-12 18:26:16 +01:00
|
|
|
* @fileoverview Unit tests for tmux session recovery after server restart.
|
2026-02-12 11:35:10 +01:00
|
|
|
*
|
2026-02-12 18:26:16 +01:00
|
|
|
* Tests verify the test mode safety behavior of TmuxManager:
|
|
|
|
|
* - In test mode (VITEST=1), reconcileSessions never runs real tmux commands
|
|
|
|
|
* - All registered sessions are reported as alive
|
|
|
|
|
* - No real sessions are discovered
|
|
|
|
|
* - No state files are read/written
|
2026-02-12 11:35:10 +01:00
|
|
|
*
|
2026-02-12 18:26:16 +01:00
|
|
|
* SAFETY: No real tmux sessions are created, killed, or interacted with.
|
|
|
|
|
*
|
|
|
|
|
* Port: N/A (no server needed)
|
2026-02-12 11:35:10 +01:00
|
|
|
*/
|
|
|
|
|
|
2026-02-12 18:26:16 +01:00
|
|
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
2026-02-12 11:35:10 +01:00
|
|
|
import { TmuxManager } from '../src/tmux-manager.js';
|
2026-02-12 18:26:16 +01:00
|
|
|
import { execSync } from 'node:child_process';
|
2026-02-12 11:35:10 +01:00
|
|
|
|
2026-02-12 18:26:16 +01:00
|
|
|
// Mock child_process
|
|
|
|
|
vi.mock('node:child_process', async () => {
|
|
|
|
|
const actual = await vi.importActual('node:child_process');
|
|
|
|
|
return {
|
|
|
|
|
...actual,
|
|
|
|
|
execSync: vi.fn(),
|
|
|
|
|
spawn: vi.fn(() => ({
|
|
|
|
|
unref: vi.fn(),
|
|
|
|
|
on: vi.fn(),
|
|
|
|
|
pid: 12345,
|
|
|
|
|
})),
|
|
|
|
|
};
|
|
|
|
|
});
|
2026-02-12 11:35:10 +01:00
|
|
|
|
2026-02-12 18:26:16 +01:00
|
|
|
// Mock fs to avoid reading/writing real state files
|
|
|
|
|
vi.mock('node:fs', async () => {
|
|
|
|
|
const actual = await vi.importActual('node:fs');
|
|
|
|
|
return {
|
|
|
|
|
...actual,
|
|
|
|
|
existsSync: vi.fn().mockReturnValue(false),
|
|
|
|
|
readFileSync: vi.fn(),
|
|
|
|
|
mkdirSync: vi.fn(),
|
|
|
|
|
writeFile: vi.fn((_path: string, _data: string, cb: (err: Error | null) => void) => cb(null)),
|
|
|
|
|
writeFileSync: vi.fn(),
|
|
|
|
|
};
|
|
|
|
|
});
|
2026-02-12 11:35:10 +01:00
|
|
|
|
2026-02-12 18:26:16 +01:00
|
|
|
describe('TmuxManager restart recovery (test mode safety)', () => {
|
|
|
|
|
let manager: TmuxManager;
|
|
|
|
|
const mockedExecSync = vi.mocked(execSync);
|
2026-02-12 11:35:10 +01:00
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2026-02-12 18:26:16 +01:00
|
|
|
vi.clearAllMocks();
|
|
|
|
|
manager = new TmuxManager();
|
2026-02-12 11:35:10 +01:00
|
|
|
});
|
|
|
|
|
|
2026-02-12 18:26:16 +01:00
|
|
|
afterEach(() => {
|
|
|
|
|
manager.destroy();
|
2026-02-12 11:35:10 +01:00
|
|
|
});
|
|
|
|
|
|
2026-02-12 18:26:16 +01:00
|
|
|
it('should report all registered sessions as alive in test mode', async () => {
|
|
|
|
|
manager.registerSession({
|
2026-02-12 11:35:10 +01:00
|
|
|
sessionId: 'test-recovery-1',
|
2026-02-26 16:44:34 +01:00
|
|
|
muxName: 'codeman-de51ecaf',
|
2026-02-12 18:26:16 +01:00
|
|
|
pid: 1,
|
2026-02-12 11:35:10 +01:00
|
|
|
createdAt: Date.now(),
|
|
|
|
|
workingDir: '/tmp',
|
|
|
|
|
mode: 'claude',
|
|
|
|
|
attached: false,
|
|
|
|
|
name: 'Recovery Test',
|
2026-07-12 19:49:39 +02:00
|
|
|
respawnConfig: {
|
|
|
|
|
enabled: true,
|
|
|
|
|
idleTimeoutMs: 10000,
|
|
|
|
|
updatePrompt: 'continue',
|
|
|
|
|
interStepDelayMs: 2000,
|
|
|
|
|
sendClear: false,
|
|
|
|
|
sendInit: true,
|
|
|
|
|
},
|
2026-02-12 18:26:16 +01:00
|
|
|
});
|
2026-02-12 11:35:10 +01:00
|
|
|
|
|
|
|
|
const result = await manager.reconcileSessions();
|
|
|
|
|
expect(result.alive).toContain('test-recovery-1');
|
|
|
|
|
expect(result.dead).toHaveLength(0);
|
2026-02-12 18:26:16 +01:00
|
|
|
expect(result.discovered).toHaveLength(0);
|
2026-02-12 11:35:10 +01:00
|
|
|
|
2026-02-12 18:26:16 +01:00
|
|
|
// Session metadata should be preserved
|
|
|
|
|
const recovered = manager.getSession('test-recovery-1');
|
|
|
|
|
expect(recovered).toBeDefined();
|
|
|
|
|
expect(recovered!.name).toBe('Recovery Test');
|
|
|
|
|
expect(recovered!.respawnConfig?.enabled).toBe(true);
|
2026-02-12 11:35:10 +01:00
|
|
|
});
|
|
|
|
|
|
2026-02-12 18:26:16 +01:00
|
|
|
it('should never discover real sessions in test mode', async () => {
|
|
|
|
|
const result = await manager.reconcileSessions();
|
|
|
|
|
expect(result.discovered).toHaveLength(0);
|
|
|
|
|
});
|
2026-02-12 11:35:10 +01:00
|
|
|
|
2026-07-12 19:49:39 +02:00
|
|
|
it('preserves remote SSH metadata across reconcile (mux-sessions.json round-trip source)', async () => {
|
|
|
|
|
manager.registerSession({
|
|
|
|
|
sessionId: 'remote-recovery-1',
|
|
|
|
|
muxName: 'codeman-de51ecaf',
|
|
|
|
|
pid: 1,
|
|
|
|
|
createdAt: Date.now(),
|
|
|
|
|
workingDir: '/home/ubuntu/work',
|
|
|
|
|
mode: 'claude',
|
|
|
|
|
attached: false,
|
|
|
|
|
name: 'Remote Recovery',
|
|
|
|
|
remote: {
|
|
|
|
|
hostId: 'gpu-box',
|
|
|
|
|
label: 'GPU Box',
|
|
|
|
|
host: '10.0.0.42',
|
|
|
|
|
username: 'ubuntu',
|
|
|
|
|
remotePath: '/home/ubuntu/work',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const result = await manager.reconcileSessions();
|
|
|
|
|
expect(result.alive).toContain('remote-recovery-1');
|
|
|
|
|
|
|
|
|
|
// restoreMuxSessions() reads MuxSession.remote off exactly this map to rebuild
|
|
|
|
|
// the recovered Session — if it were dropped here the session would respawn LOCAL.
|
|
|
|
|
const recovered = manager.getSession('remote-recovery-1');
|
|
|
|
|
expect(recovered?.remote).toMatchObject({ hostId: 'gpu-box', host: '10.0.0.42', remotePath: '/home/ubuntu/work' });
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-12 18:26:16 +01:00
|
|
|
it('should not execute any tmux commands in test mode', async () => {
|
|
|
|
|
manager.registerSession({
|
|
|
|
|
sessionId: 'alive-session',
|
2026-02-26 16:44:34 +01:00
|
|
|
muxName: 'codeman-a11eeaaa',
|
2026-02-12 18:26:16 +01:00
|
|
|
pid: 1,
|
|
|
|
|
createdAt: Date.now(),
|
|
|
|
|
workingDir: '/tmp',
|
|
|
|
|
mode: 'claude',
|
|
|
|
|
attached: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
mockedExecSync.mockClear();
|
|
|
|
|
await manager.reconcileSessions();
|
|
|
|
|
|
|
|
|
|
// Verify no tmux commands were executed
|
2026-07-12 19:49:39 +02:00
|
|
|
const tmuxCalls = mockedExecSync.mock.calls.filter(([cmd]) => typeof cmd === 'string' && cmd.includes('tmux'));
|
2026-02-12 18:26:16 +01:00
|
|
|
expect(tmuxCalls).toHaveLength(0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should handle multiple sessions correctly in test mode', async () => {
|
|
|
|
|
manager.registerSession({
|
|
|
|
|
sessionId: 'session-1',
|
2026-02-26 16:44:34 +01:00
|
|
|
muxName: 'codeman-a11eeaaa',
|
2026-02-12 18:26:16 +01:00
|
|
|
pid: 1,
|
|
|
|
|
createdAt: Date.now(),
|
|
|
|
|
workingDir: '/tmp',
|
|
|
|
|
mode: 'claude',
|
|
|
|
|
attached: false,
|
|
|
|
|
name: 'Session 1',
|
|
|
|
|
});
|
|
|
|
|
manager.registerSession({
|
|
|
|
|
sessionId: 'session-2',
|
2026-02-26 16:44:34 +01:00
|
|
|
muxName: 'codeman-b22ffbbb',
|
2026-02-12 18:26:16 +01:00
|
|
|
pid: 2,
|
|
|
|
|
createdAt: Date.now(),
|
|
|
|
|
workingDir: '/tmp',
|
|
|
|
|
mode: 'shell',
|
|
|
|
|
attached: false,
|
|
|
|
|
name: 'Session 2',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const result = await manager.reconcileSessions();
|
|
|
|
|
expect(result.alive).toContain('session-1');
|
|
|
|
|
expect(result.alive).toContain('session-2');
|
|
|
|
|
expect(result.alive).toHaveLength(2);
|
|
|
|
|
expect(result.dead).toHaveLength(0);
|
|
|
|
|
expect(result.discovered).toHaveLength(0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should safely remove sessions via killSession in test mode', async () => {
|
|
|
|
|
manager.registerSession({
|
|
|
|
|
sessionId: 'kill-me',
|
2026-02-26 16:44:34 +01:00
|
|
|
muxName: 'codeman-deadbeef',
|
2026-02-12 11:35:10 +01:00
|
|
|
pid: 99999,
|
|
|
|
|
createdAt: Date.now(),
|
|
|
|
|
workingDir: '/tmp',
|
|
|
|
|
mode: 'claude',
|
|
|
|
|
attached: false,
|
2026-02-12 18:26:16 +01:00
|
|
|
});
|
2026-02-12 11:35:10 +01:00
|
|
|
|
2026-02-12 18:26:16 +01:00
|
|
|
mockedExecSync.mockClear();
|
2026-02-12 11:35:10 +01:00
|
|
|
|
2026-02-12 18:26:16 +01:00
|
|
|
const result = await manager.killSession('kill-me');
|
|
|
|
|
expect(result).toBe(true);
|
|
|
|
|
expect(manager.getSession('kill-me')).toBeUndefined();
|
2026-02-12 11:35:10 +01:00
|
|
|
|
2026-02-12 18:26:16 +01:00
|
|
|
// Verify no real kill commands were executed
|
2026-07-12 19:49:39 +02:00
|
|
|
const killCalls = mockedExecSync.mock.calls.filter(([cmd]) => typeof cmd === 'string' && cmd.includes('kill'));
|
2026-02-12 18:26:16 +01:00
|
|
|
expect(killCalls).toHaveLength(0);
|
2026-02-12 11:35:10 +01:00
|
|
|
});
|
|
|
|
|
});
|