Files
Codeman/test/scheduled-runs.test.ts
T
RandalixandClaude Opus 5.5 aa4217b8b1 test: build every in-process WebServer on port 0 (#440)
The files on the guard's legacy list build new WebServer(0, …) and read server.boundPort after start(), as sse-tile-grid-filter does. Files with several servers read each server's own port; quick-start's local helper lost its port parameter so the literal 0 sits where the server is built. Header comments that named a port say "ephemeral". No assertion changed except where it embedded the port.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-09 16:28:26 +02:00

233 lines
7.5 KiB
TypeScript

import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { WebServer } from '../src/web/server.js';
describe('Scheduled Runs API', () => {
let server: WebServer;
let baseUrl: string;
const createdRuns: string[] = [];
beforeAll(async () => {
server = new WebServer(0, false, true);
await server.start();
baseUrl = `http://localhost:${server.boundPort}`;
});
afterAll(async () => {
// Cancel any remaining runs
for (const runId of createdRuns) {
try {
await fetch(`${baseUrl}/api/scheduled/${runId}`, { method: 'DELETE' });
} catch {}
}
await server.stop();
});
describe('GET /api/scheduled', () => {
it('should return empty list initially', async () => {
const response = await fetch(`${baseUrl}/api/scheduled`);
const data = await response.json();
expect(Array.isArray(data.data)).toBe(true);
});
});
describe('POST /api/scheduled', () => {
it('should create a scheduled run', async () => {
const response = await fetch(`${baseUrl}/api/scheduled`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
prompt: 'echo test',
workingDir: '/tmp',
durationMinutes: 1,
}),
});
const data = await response.json();
expect(data.success).toBe(true);
expect(data.data.run).toBeDefined();
expect(data.data.run.id).toBeDefined();
expect(data.data.run.prompt).toBe('echo test');
expect(data.data.run.durationMinutes).toBe(1);
expect(data.data.run.status).toBe('running');
createdRuns.push(data.data.run.id);
// Stop the run immediately to avoid resource usage
await fetch(`${baseUrl}/api/scheduled/${data.data.run.id}`, { method: 'DELETE' });
});
it('should set correct timestamps', async () => {
const beforeCreate = Date.now();
const response = await fetch(`${baseUrl}/api/scheduled`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
prompt: 'test timestamps',
durationMinutes: 5,
}),
});
const data = await response.json();
const afterCreate = Date.now();
expect(data.data.run.startedAt).toBeGreaterThanOrEqual(beforeCreate);
expect(data.data.run.startedAt).toBeLessThanOrEqual(afterCreate);
expect(data.data.run.endAt).toBe(data.data.run.startedAt + 5 * 60 * 1000);
createdRuns.push(data.data.run.id);
await fetch(`${baseUrl}/api/scheduled/${data.data.run.id}`, { method: 'DELETE' });
});
it('should initialize with zero completed tasks and cost', async () => {
const response = await fetch(`${baseUrl}/api/scheduled`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
prompt: 'test initial values',
durationMinutes: 1,
}),
});
const data = await response.json();
expect(data.data.run.completedTasks).toBe(0);
expect(data.data.run.totalCost).toBe(0);
// Logs may have 1 or more entries depending on timing
expect(data.data.run.logs.length).toBeGreaterThanOrEqual(1);
createdRuns.push(data.data.run.id);
await fetch(`${baseUrl}/api/scheduled/${data.data.run.id}`, { method: 'DELETE' });
});
});
describe('GET /api/scheduled/:id', () => {
it('should return scheduled run details', async () => {
// Create a run first
const createRes = await fetch(`${baseUrl}/api/scheduled`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
prompt: 'test get',
durationMinutes: 1,
}),
});
const createData = await createRes.json();
const runId = createData.data.run.id;
createdRuns.push(runId);
// Get the run
const response = await fetch(`${baseUrl}/api/scheduled/${runId}`);
const data = await response.json();
expect(data.data.id).toBe(runId);
expect(data.data.prompt).toBe('test get');
await fetch(`${baseUrl}/api/scheduled/${runId}`, { method: 'DELETE' });
});
it('should return error for non-existent run', async () => {
const response = await fetch(`${baseUrl}/api/scheduled/non-existent-id`);
const data = await response.json();
expect(data.error).toBe('Scheduled run not found');
});
});
describe('DELETE /api/scheduled/:id', () => {
it('should stop a scheduled run', async () => {
// Create a run
const createRes = await fetch(`${baseUrl}/api/scheduled`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
prompt: 'test delete',
durationMinutes: 10,
}),
});
const createData = await createRes.json();
const runId = createData.data.run.id;
// Delete/stop the run
const response = await fetch(`${baseUrl}/api/scheduled/${runId}`, {
method: 'DELETE',
});
const data = await response.json();
expect(data.success).toBe(true);
// Verify it's stopped
const getRes = await fetch(`${baseUrl}/api/scheduled/${runId}`);
const runData = await getRes.json();
expect(runData.data.status).toBe('stopped');
});
it('should return error for non-existent run', async () => {
const response = await fetch(`${baseUrl}/api/scheduled/non-existent-id`, {
method: 'DELETE',
});
const data = await response.json();
expect(data.success).toBe(false);
expect(data.error).toBe('Scheduled run not found');
});
});
});
describe('Quick Run API', () => {
let server: WebServer;
let baseUrl: string;
beforeAll(async () => {
server = new WebServer(0, false, true);
await server.start();
baseUrl = `http://localhost:${server.boundPort}`;
});
afterAll(async () => {
await server.stop();
});
describe('POST /api/run', () => {
it('should create session and run prompt', async () => {
// Note: This test will actually try to run Claude
// We use a simple echo to minimize API usage
const response = await fetch(`${baseUrl}/api/run`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
prompt: 'say "test"',
workingDir: '/tmp',
}),
});
const data = await response.json();
// On success the payload is wrapped ({ success:true, data:{ sessionId, ... } });
// on failure the handler returns the standard error envelope
// ({ success:false, error, errorCode }) with the dead session id in the message.
if (data.success) {
expect(data.data?.sessionId).toBeDefined();
} else {
expect(data.errorCode).toBeDefined();
expect(data.error).toMatch(/session /);
}
// Note: success/failure depends on Claude actually running
});
// Skip this test - it runs real Claude CLI which is slow and flaky in CI
// The functionality is tested via the first test which uses workingDir: '/tmp'
it.skip('should use current directory when workingDir not provided', async () => {
const response = await fetch(`${baseUrl}/api/run`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
prompt: 'pwd',
}),
});
const data = await response.json();
expect(data.sessionId).toBeDefined();
}, 60000); // Increase timeout since this runs real Claude CLI
});
});