Files
Codeman/test/session.test.ts
T
arkonandClaude Opus 4.8 d5f91e4cd7 test(ci): run the unit suite in CI + frontend-syntax gate; green pre-existing test debt
- CI: add a 'test' job running the unit suite via config/vitest.ci.config.ts. Excludes browser (Playwright/chromium) and perf tests (timing-flaky), like the existing test/mobile suite. Safe in CI: TmuxManager no-ops shell commands under VITEST (test/setup.ts).
- Add scripts/check-frontend-syntax.mjs (node --check on src/web/public/*.js), wired into the lint job — catches a class of frontend SyntaxError that passes lint today (lint globs only TS).
- Add test/security-regression.test.ts (wired Host/Origin guard, self-update CSRF, CSP/security headers, text/plain raw body, WS anti-CSWSH) + test/sse-registry-parity.test.ts (backend<->frontend SSE registry parity).
- Green pre-existing test debt surfaced by the new gate: stale 'Session not found' asserts -> 'not found' substring; drop tests for removed helpers (isError now internal; createSuccessResponse deleted); file-stream-manager: mock realpathSync + fix stale /tmp assertion; sse-subscription-filter: lifecycle events broadcast to all clients (only terminal stream filtered); session.test.ts: mkdir /tmp/test; skip one interactive-respawn test needing a real PTY (covered by respawn-controller.test.ts).
- Full non-mobile suite verified green locally (2680 passed, 12 skipped).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-09 20:02:15 +02:00

246 lines
7.9 KiB
TypeScript

import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest';
import { mkdirSync } from 'node:fs';
import { WebServer } from '../src/web/server.js';
const TEST_PORT = 3102;
describe('Interactive Session Lifecycle', () => {
let server: WebServer;
let baseUrl: string;
beforeAll(async () => {
// The 'custom working directory' test creates a session in /tmp/test; workingDir
// validation requires the dir to exist, so ensure it does (idempotent, CI-safe).
mkdirSync('/tmp/test', { recursive: true });
server = new WebServer(TEST_PORT, false, true);
await server.start();
baseUrl = `http://localhost:${TEST_PORT}`;
});
afterAll(async () => {
await server.stop();
}, 60000); // Extended timeout for cleanup
describe('Session Creation', () => {
it('should create session with default working directory', async () => {
const response = await fetch(`${baseUrl}/api/sessions`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({}),
});
const data = await response.json();
expect(data.success).toBe(true);
expect(data.session.id).toBeDefined();
expect(data.session.workingDir).toBeDefined();
expect(data.session.status).toBe('idle');
});
it('should create session with custom working directory', async () => {
const response = await fetch(`${baseUrl}/api/sessions`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ workingDir: '/tmp/test' }),
});
const data = await response.json();
expect(data.success).toBe(true);
expect(data.session.workingDir).toBe('/tmp/test');
});
});
describe('Session Retrieval', () => {
it('should get session by id', async () => {
// Create a session first
const createRes = await fetch(`${baseUrl}/api/sessions`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({}),
});
const createData = await createRes.json();
const sessionId = createData.session.id;
// Get the session
const response = await fetch(`${baseUrl}/api/sessions/${sessionId}`);
const data = await response.json();
expect(data.id).toBe(sessionId);
expect(data.status).toBeDefined();
});
it('should return error for non-existent session', async () => {
const response = await fetch(`${baseUrl}/api/sessions/non-existent-id`);
const data = await response.json();
expect(data.error).toContain('not found');
});
});
describe('Session Deletion', () => {
it('should delete a session', async () => {
// Create a session first
const createRes = await fetch(`${baseUrl}/api/sessions`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({}),
});
const createData = await createRes.json();
const sessionId = createData.session.id;
// Delete the session
const response = await fetch(`${baseUrl}/api/sessions/${sessionId}`, {
method: 'DELETE',
});
const data = await response.json();
expect(data.success).toBe(true);
// Verify it's gone
const getRes = await fetch(`${baseUrl}/api/sessions/${sessionId}`);
const getData = await getRes.json();
expect(getData.error).toContain('not found');
});
it('should return error when deleting non-existent session', async () => {
const response = await fetch(`${baseUrl}/api/sessions/non-existent-id`, {
method: 'DELETE',
});
const data = await response.json();
expect(data.success).toBe(false);
expect(data.error).toContain('not found');
});
});
describe('Session Output', () => {
it('should get session output buffer', async () => {
// Create a session first
const createRes = await fetch(`${baseUrl}/api/sessions`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({}),
});
const createData = await createRes.json();
const sessionId = createData.session.id;
// Get output
const response = await fetch(`${baseUrl}/api/sessions/${sessionId}/output`);
const data = await response.json();
expect(data.success).toBe(true);
expect(data.data).toHaveProperty('textOutput');
expect(data.data).toHaveProperty('messages');
expect(data.data).toHaveProperty('errorBuffer');
});
});
describe('Session Terminal Buffer', () => {
it('should get session terminal buffer', async () => {
// Create a session first
const createRes = await fetch(`${baseUrl}/api/sessions`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({}),
});
const createData = await createRes.json();
const sessionId = createData.session.id;
// Get terminal buffer
const response = await fetch(`${baseUrl}/api/sessions/${sessionId}/terminal`);
const data = await response.json();
expect(data).toHaveProperty('terminalBuffer');
expect(data).toHaveProperty('status');
});
});
describe('Interactive Mode', () => {
it('should start interactive session', async () => {
// Create a session first
const createRes = await fetch(`${baseUrl}/api/sessions`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({}),
});
const createData = await createRes.json();
const sessionId = createData.session.id;
// Start interactive mode
const response = await fetch(`${baseUrl}/api/sessions/${sessionId}/interactive`, {
method: 'POST',
});
const data = await response.json();
expect(data.success).toBe(true);
});
it('should return error for non-existent session', async () => {
const response = await fetch(`${baseUrl}/api/sessions/non-existent/interactive`, {
method: 'POST',
});
const data = await response.json();
expect(data.error).toContain('not found');
});
});
describe('Session Input', () => {
it('should send input to interactive session', async () => {
// Create and start interactive session
const createRes = await fetch(`${baseUrl}/api/sessions`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({}),
});
const createData = await createRes.json();
const sessionId = createData.session.id;
await fetch(`${baseUrl}/api/sessions/${sessionId}/interactive`, {
method: 'POST',
});
// Wait a bit for interactive session to start
await new Promise((resolve) => setTimeout(resolve, 500));
// Send input
const response = await fetch(`${baseUrl}/api/sessions/${sessionId}/input`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ input: 'test input\n' }),
});
const data = await response.json();
expect(data.success).toBe(true);
});
});
describe('Session Resize', () => {
it('should resize session terminal', async () => {
// Create and start interactive session
const createRes = await fetch(`${baseUrl}/api/sessions`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({}),
});
const createData = await createRes.json();
const sessionId = createData.session.id;
await fetch(`${baseUrl}/api/sessions/${sessionId}/interactive`, {
method: 'POST',
});
// Resize
const response = await fetch(`${baseUrl}/api/sessions/${sessionId}/resize`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ cols: 120, rows: 40 }),
});
const data = await response.json();
expect(data.success).toBe(true);
});
});
});