mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-10-09 16:59:43 +02:00
fix: clean up orphaned test screen sessions in afterAll
Integration tests create screen sessions via the web server, but server.stop() intentionally preserves them (for reattachment in production). This left 30+ detached screens after each test run. Fix by recording pre-existing screens in beforeAll, then killing any new detached claudeman-* screens in afterAll that weren't there at test start. Never kills attached sessions (user's active work). Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
+56
-4
@@ -158,11 +158,57 @@ export function forceCleanupAllTestResources(): void {
|
|||||||
// Global Hooks
|
// Global Hooks
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
|
|
||||||
|
/** Screens that existed before tests started (never killed by cleanup) */
|
||||||
|
const preExistingScreens = new Set<string>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List all current claudeman-* screen session names
|
||||||
|
*/
|
||||||
|
function listClaudemanScreens(): string[] {
|
||||||
|
try {
|
||||||
|
const output = execSync('screen -ls 2>/dev/null || true', { encoding: 'utf-8' });
|
||||||
|
const screens: string[] = [];
|
||||||
|
for (const line of output.split('\n')) {
|
||||||
|
const match = line.match(/\d+\.(claudeman-\S+)/);
|
||||||
|
if (match) screens.push(match[1]);
|
||||||
|
}
|
||||||
|
return screens;
|
||||||
|
} catch {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Kill detached claudeman screens that were created during the test run
|
||||||
|
*/
|
||||||
|
function killOrphanedTestScreens(): number {
|
||||||
|
let killed = 0;
|
||||||
|
try {
|
||||||
|
const output = execSync('screen -ls 2>/dev/null || true', { encoding: 'utf-8' });
|
||||||
|
for (const line of output.split('\n')) {
|
||||||
|
// Only kill detached screens (never attached/user sessions)
|
||||||
|
if (!line.includes('Detached')) continue;
|
||||||
|
const match = line.match(/(\d+\.(claudeman-\S+))/);
|
||||||
|
if (!match) continue;
|
||||||
|
const fullName = match[1];
|
||||||
|
const screenName = match[2];
|
||||||
|
// Skip screens that existed before tests started
|
||||||
|
if (preExistingScreens.has(screenName)) continue;
|
||||||
|
try {
|
||||||
|
execSync(`screen -S ${fullName} -X quit 2>/dev/null || true`, { encoding: 'utf-8' });
|
||||||
|
killed++;
|
||||||
|
} catch { /* ignore */ }
|
||||||
|
}
|
||||||
|
} catch { /* ignore */ }
|
||||||
|
return killed;
|
||||||
|
}
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
// Initialize test environment
|
// Record pre-existing screens so we never kill them
|
||||||
console.log('[Test Setup] Initializing test environment...');
|
for (const name of listClaudemanScreens()) {
|
||||||
console.log('[Test Setup] Only test-created resources will be cleaned up (safe for Claudeman sessions)');
|
preExistingScreens.add(name);
|
||||||
console.log('[Test Setup] Starting tests...');
|
}
|
||||||
|
console.log(`[Test Setup] ${preExistingScreens.size} pre-existing screens preserved`);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
@@ -171,6 +217,12 @@ afterAll(async () => {
|
|||||||
// Only cleanup resources that tests have registered
|
// Only cleanup resources that tests have registered
|
||||||
forceCleanupAllTestResources();
|
forceCleanupAllTestResources();
|
||||||
|
|
||||||
|
// Kill orphaned screens created during the test run (not registered but detached)
|
||||||
|
const orphansKilled = killOrphanedTestScreens();
|
||||||
|
if (orphansKilled > 0) {
|
||||||
|
console.log(`[Test Setup] Killed ${orphansKilled} orphaned test screens`);
|
||||||
|
}
|
||||||
|
|
||||||
// Wait for cleanup
|
// Wait for cleanup
|
||||||
await new Promise(resolve => setTimeout(resolve, 500));
|
await new Promise(resolve => setTimeout(resolve, 500));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user