mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-10-07 07:59:42 +02:00
chore: bump version to 0.1485
This commit is contained in:
+282
-4
@@ -1,8 +1,8 @@
|
||||
/**
|
||||
* @fileoverview Unit tests for TmuxManager
|
||||
* @fileoverview Unit + integration tests for TmuxManager
|
||||
*
|
||||
* Tests validation functions, command construction, and parsing logic
|
||||
* using mocked exec calls. Does NOT create or kill real tmux sessions.
|
||||
* Unit tests (mocked): validation, command construction, parsing logic.
|
||||
* Integration tests (real tmux): session creation, input, kill, reconciliation.
|
||||
*
|
||||
* Port: N/A (no server needed)
|
||||
*/
|
||||
@@ -10,6 +10,11 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { TmuxManager } from '../src/tmux-manager.js';
|
||||
import { execSync } from 'node:child_process';
|
||||
import { registerTestTmuxSession, unregisterTestTmuxSession } from './setup.js';
|
||||
|
||||
// ============================================================================
|
||||
// Unit Tests (mocked)
|
||||
// ============================================================================
|
||||
|
||||
// Mock child_process
|
||||
vi.mock('node:child_process', async () => {
|
||||
@@ -37,7 +42,7 @@ vi.mock('node:fs', async () => {
|
||||
};
|
||||
});
|
||||
|
||||
describe('TmuxManager', () => {
|
||||
describe('TmuxManager (unit)', () => {
|
||||
let manager: TmuxManager;
|
||||
const mockedExecSync = vi.mocked(execSync);
|
||||
|
||||
@@ -299,6 +304,97 @@ describe('TmuxManager', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('killSession self-kill protection', () => {
|
||||
it('should block kill when session matches CLAUDEMAN_SCREEN_NAME', async () => {
|
||||
const originalEnv = process.env.CLAUDEMAN_SCREEN_NAME;
|
||||
process.env.CLAUDEMAN_SCREEN_NAME = 'claudeman-5e1f1111';
|
||||
|
||||
try {
|
||||
manager.registerSession({
|
||||
sessionId: 'self-kill-test',
|
||||
muxName: 'claudeman-5e1f1111',
|
||||
pid: 999,
|
||||
createdAt: Date.now(),
|
||||
workingDir: '/tmp',
|
||||
mode: 'claude',
|
||||
attached: false,
|
||||
});
|
||||
|
||||
const result = await manager.killSession('self-kill-test');
|
||||
expect(result).toBe(false);
|
||||
|
||||
// Session should still exist (not removed)
|
||||
expect(manager.getSession('self-kill-test')).toBeDefined();
|
||||
} finally {
|
||||
if (originalEnv === undefined) {
|
||||
delete process.env.CLAUDEMAN_SCREEN_NAME;
|
||||
} else {
|
||||
process.env.CLAUDEMAN_SCREEN_NAME = originalEnv;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it('should allow kill when session does NOT match CLAUDEMAN_SCREEN_NAME', async () => {
|
||||
const originalEnv = process.env.CLAUDEMAN_SCREEN_NAME;
|
||||
process.env.CLAUDEMAN_SCREEN_NAME = 'claudeman-0ther1111';
|
||||
|
||||
try {
|
||||
manager.registerSession({
|
||||
sessionId: 'other-kill-test',
|
||||
muxName: 'claudeman-d1ff1111',
|
||||
pid: 888,
|
||||
createdAt: Date.now(),
|
||||
workingDir: '/tmp',
|
||||
mode: 'claude',
|
||||
attached: false,
|
||||
});
|
||||
|
||||
// Mock the kill flow
|
||||
mockedExecSync.mockImplementation(() => '');
|
||||
|
||||
const result = await manager.killSession('other-kill-test');
|
||||
expect(result).toBe(true);
|
||||
|
||||
// Session should be removed
|
||||
expect(manager.getSession('other-kill-test')).toBeUndefined();
|
||||
} finally {
|
||||
if (originalEnv === undefined) {
|
||||
delete process.env.CLAUDEMAN_SCREEN_NAME;
|
||||
} else {
|
||||
process.env.CLAUDEMAN_SCREEN_NAME = originalEnv;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it('should allow kill when CLAUDEMAN_SCREEN_NAME is not set', async () => {
|
||||
const originalEnv = process.env.CLAUDEMAN_SCREEN_NAME;
|
||||
delete process.env.CLAUDEMAN_SCREEN_NAME;
|
||||
|
||||
try {
|
||||
manager.registerSession({
|
||||
sessionId: 'no-env-test',
|
||||
muxName: 'claudeman-aaa11111',
|
||||
pid: 777,
|
||||
createdAt: Date.now(),
|
||||
workingDir: '/tmp',
|
||||
mode: 'claude',
|
||||
attached: false,
|
||||
});
|
||||
|
||||
mockedExecSync.mockImplementation(() => '');
|
||||
|
||||
const result = await manager.killSession('no-env-test');
|
||||
expect(result).toBe(true);
|
||||
} finally {
|
||||
if (originalEnv === undefined) {
|
||||
delete process.env.CLAUDEMAN_SCREEN_NAME;
|
||||
} else {
|
||||
process.env.CLAUDEMAN_SCREEN_NAME = originalEnv;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('metadata operations', () => {
|
||||
beforeEach(() => {
|
||||
manager.registerSession({
|
||||
@@ -385,3 +481,185 @@ describe('TmuxManager', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// ============================================================================
|
||||
// Integration Tests (real tmux sessions)
|
||||
// ============================================================================
|
||||
|
||||
describe('TmuxManager (integration)', () => {
|
||||
// Skip entire block if tmux is not available
|
||||
const tmuxAvailable = (() => {
|
||||
try {
|
||||
const { execSync: realExecSync } = require('node:child_process');
|
||||
realExecSync('which tmux', { encoding: 'utf-8', timeout: 5000 });
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
})();
|
||||
|
||||
if (!tmuxAvailable) {
|
||||
it.skip('tmux not available — skipping integration tests', () => {});
|
||||
return;
|
||||
}
|
||||
|
||||
// Real execSync for integration tests (bypasses mock)
|
||||
const { execSync: realExecSync } = require('node:child_process') as typeof import('node:child_process');
|
||||
|
||||
// Helper: create a test tmux session directly via tmux CLI
|
||||
function createRawTmuxSession(name: string): void {
|
||||
realExecSync(`tmux new-session -ds "${name}" -x 80 -y 24 bash`, { timeout: 5000 });
|
||||
registerTestTmuxSession(name);
|
||||
}
|
||||
|
||||
// Helper: check if tmux session exists
|
||||
function tmuxSessionExists(name: string): boolean {
|
||||
try {
|
||||
realExecSync(`tmux has-session -t "${name}" 2>/dev/null`, { timeout: 5000 });
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Helper: kill a test tmux session directly
|
||||
function killRawTmuxSession(name: string): void {
|
||||
try {
|
||||
realExecSync(`tmux kill-session -t "${name}" 2>/dev/null`, { timeout: 5000 });
|
||||
} catch {
|
||||
// May already be dead
|
||||
}
|
||||
unregisterTestTmuxSession(name);
|
||||
}
|
||||
|
||||
// Track sessions created during integration tests for cleanup
|
||||
const createdSessions: string[] = [];
|
||||
|
||||
afterEach(() => {
|
||||
// Clean up any sessions created during the test
|
||||
for (const name of createdSessions) {
|
||||
killRawTmuxSession(name);
|
||||
}
|
||||
createdSessions.length = 0;
|
||||
});
|
||||
|
||||
it('should create a real tmux session', () => {
|
||||
const sessionName = 'claudeman-test-create';
|
||||
createRawTmuxSession(sessionName);
|
||||
createdSessions.push(sessionName);
|
||||
|
||||
expect(tmuxSessionExists(sessionName)).toBe(true);
|
||||
});
|
||||
|
||||
it('should send input to a real tmux session and verify output', async () => {
|
||||
const sessionName = 'claudeman-test-input';
|
||||
createRawTmuxSession(sessionName);
|
||||
createdSessions.push(sessionName);
|
||||
|
||||
// Send text to the session
|
||||
realExecSync(`tmux send-keys -t "${sessionName}" -l 'echo TMUX_INPUT_TEST_OK'`, { timeout: 5000 });
|
||||
realExecSync(`tmux send-keys -t "${sessionName}" Enter`, { timeout: 5000 });
|
||||
|
||||
// Wait for command to execute
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
|
||||
// Capture pane contents
|
||||
const output = realExecSync(`tmux capture-pane -t "${sessionName}" -p`, { encoding: 'utf-8', timeout: 5000 });
|
||||
expect(output).toContain('TMUX_INPUT_TEST_OK');
|
||||
});
|
||||
|
||||
it('should kill a real tmux session', () => {
|
||||
const sessionName = 'claudeman-test-kill';
|
||||
createRawTmuxSession(sessionName);
|
||||
// Don't push to createdSessions since we'll kill it manually
|
||||
|
||||
expect(tmuxSessionExists(sessionName)).toBe(true);
|
||||
|
||||
realExecSync(`tmux kill-session -t "${sessionName}" 2>/dev/null`, { timeout: 5000 });
|
||||
unregisterTestTmuxSession(sessionName);
|
||||
|
||||
expect(tmuxSessionExists(sessionName)).toBe(false);
|
||||
});
|
||||
|
||||
it('should discover unknown claudeman sessions via reconcile', async () => {
|
||||
// Create a tmux session directly (not via TmuxManager) — simulates a "ghost"
|
||||
const sessionName = 'claudeman-te51abcd';
|
||||
createRawTmuxSession(sessionName);
|
||||
createdSessions.push(sessionName);
|
||||
|
||||
// existsSync is already mocked to return false (module-level mock),
|
||||
// so TmuxManager won't load any persisted sessions from disk
|
||||
const freshManager = new TmuxManager();
|
||||
|
||||
// Verify it doesn't know about the session yet
|
||||
expect(freshManager.getSessions()).toHaveLength(0);
|
||||
|
||||
// Note: Full reconcile with real tmux requires unmocked execSync,
|
||||
// which is covered by the tmux-restart-recovery.test.ts integration tests.
|
||||
freshManager.destroy();
|
||||
});
|
||||
|
||||
it('should verify self-kill protection with real env var', async () => {
|
||||
const sessionName = 'claudeman-te515e1f';
|
||||
createRawTmuxSession(sessionName);
|
||||
createdSessions.push(sessionName);
|
||||
|
||||
const originalEnv = process.env.CLAUDEMAN_SCREEN_NAME;
|
||||
process.env.CLAUDEMAN_SCREEN_NAME = sessionName;
|
||||
|
||||
try {
|
||||
// existsSync is already mocked to return false (module-level mock)
|
||||
const testManager = new TmuxManager();
|
||||
testManager.registerSession({
|
||||
sessionId: 'self-test',
|
||||
muxName: sessionName,
|
||||
pid: 99999,
|
||||
createdAt: Date.now(),
|
||||
workingDir: '/tmp',
|
||||
mode: 'claude',
|
||||
attached: false,
|
||||
});
|
||||
|
||||
// killSession should refuse
|
||||
const result = await testManager.killSession('self-test');
|
||||
expect(result).toBe(false);
|
||||
|
||||
// Session should still be alive in tmux
|
||||
expect(tmuxSessionExists(sessionName)).toBe(true);
|
||||
|
||||
testManager.destroy();
|
||||
} finally {
|
||||
if (originalEnv === undefined) {
|
||||
delete process.env.CLAUDEMAN_SCREEN_NAME;
|
||||
} else {
|
||||
process.env.CLAUDEMAN_SCREEN_NAME = originalEnv;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it('should persist and load session metadata', () => {
|
||||
// This test verifies the persistence format is correct by checking
|
||||
// that registerSession + getSessions round-trips properly
|
||||
// existsSync is already mocked to return false (module-level mock)
|
||||
const manager1 = new TmuxManager();
|
||||
manager1.registerSession({
|
||||
sessionId: 'persist-test',
|
||||
muxName: 'claudeman-be51aaa1',
|
||||
pid: 12345,
|
||||
createdAt: 1700000000000,
|
||||
workingDir: '/home/test',
|
||||
mode: 'claude',
|
||||
attached: false,
|
||||
name: 'Test Session',
|
||||
respawnConfig: { enabled: true, idleTimeoutMs: 5000, updatePrompt: 'test', interStepDelayMs: 1000, sendClear: true, sendInit: true },
|
||||
});
|
||||
|
||||
const sessions = manager1.getSessions();
|
||||
expect(sessions).toHaveLength(1);
|
||||
expect(sessions[0].sessionId).toBe('persist-test');
|
||||
expect(sessions[0].name).toBe('Test Session');
|
||||
expect(sessions[0].respawnConfig?.enabled).toBe(true);
|
||||
|
||||
manager1.destroy();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user