test: add shared mock infrastructure and route tests (phase 7)

Consolidate duplicated MockSession/MockStateStore into test/mocks/,
migrate respawn tests to shared mocks, and add 58 route tests for
session, system, and respawn endpoints using Fastify app.inject().

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
arkon
2026-03-01 14:33:18 +01:00
co-authored by Claude Opus 4.6
parent 97ca13916a
commit 442cc19c3c
13 changed files with 2421 additions and 428 deletions
+1 -58
View File
@@ -1,7 +1,7 @@
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { RespawnController, RespawnState, RespawnConfig } from '../src/respawn-controller.js';
import { Session } from '../src/session.js';
import { EventEmitter } from 'node:events';
import { MockSession } from './mocks/index.js';
/**
* RespawnController Tests
@@ -10,63 +10,6 @@ import { EventEmitter } from 'node:events';
* State flow: WATCHING → SENDING_UPDATE → WAITING_UPDATE → SENDING_CLEAR → WAITING_CLEAR → SENDING_INIT → WAITING_INIT → WATCHING
*/
// Mock Session for testing
class MockSession extends EventEmitter {
id = 'mock-session-id';
workingDir = '/tmp';
status = 'idle';
pid = 12345; // Mock PID for P1-006 health check
isWorking = false; // P0-006 Session.isWorking integration
writeBuffer: string[] = [];
write(data: string): void {
this.writeBuffer.push(data);
}
async writeViaMux(data: string): Promise<boolean> {
this.writeBuffer.push(data);
return true;
}
// Simulate terminal output
simulateTerminalOutput(data: string): void {
this.emit('terminal', data);
}
// Simulate prompt appearing (basic prompt character) - legacy fallback
simulatePrompt(): void {
this.emit('terminal', '❯ ');
}
// Simulate ready state with the definitive indicator - legacy fallback
simulateReady(): void {
this.emit('terminal', '↵ send');
}
// Simulate completion message (NEW - primary idle detection in Claude Code 2024+)
// This pattern triggers the multi-layer detection: "for Xm Xs" indicates work finished
simulateCompletionMessage(): void {
this.emit('terminal', '✻ Worked for 2m 46s');
}
// Simulate working state
simulateWorking(): void {
this.emit('terminal', 'Thinking... ⠋');
}
// Simulate clear completion (followed by completion message)
simulateClearComplete(): void {
this.emit('terminal', 'conversation cleared');
setTimeout(() => this.simulateCompletionMessage(), 50);
}
// Simulate init completion (followed by completion message)
simulateInitComplete(): void {
this.emit('terminal', 'Analyzing CLAUDE.md...');
setTimeout(() => this.simulateCompletionMessage(), 100);
}
}
describe('RespawnController', () => {
let session: MockSession;
let controller: RespawnController;