mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-09-30 12:39:42 +02:00
A 15-agent audit of the merged tree confirmed 9 envelope/contract bugs;
all fixed here, with live-server contract tests added:
Blockers (fresh-install quick start broken):
- session-ui.js runClaude/runShell unwrapped .data from the /api/cases/:name
404 error envelope (which has no data key), so a not-yet-created case threw
TypeError instead of triggering the auto-create fallback. Now '?.data ?? {}'.
Contract violations on the new stable surface:
- Unknown /api routes returned HTTP 404 with {success:true,...} (Fastify's
default not-found payload was wrapped by the envelope hook). Added a
setNotFoundHandler returning the standard error envelope for /api paths.
- POST /api/events/subscribe 400 body became {success:true,data:{error}};
now createErrorResponse(INVALID_INPUT).
- POST /api/clipboard validation error lacked errorCode and shipped HTTP 200;
now createErrorResponse(INVALID_INPUT) -> 400.
- POST /api/run catch path returned bare {success:false,sessionId,error}
(HTTP 200, no errorCode); now OPERATION_FAILED envelope -> 422 with the
dead session id in the message.
- DELETE tail-file/:streamId returned {success: closed}, colliding with the
envelope discriminator; now returns {closed}.
Dead/regressed UI paths:
- Plan history modal could never open: route returned the bare history array
under data while the frontend read data.data.history/currentVersion. Route
now returns {history, currentVersion}; modal task count fixed to stats.total.
- Self-update error toast read j.error.message from the string-typed envelope
error, always falling back to the generic message; now reads the string.
Cleanup:
- Removed the stale QuickStartResponse type (unreferenced; documented the
pre-envelope shape and invited success-key collisions).
Tests: new test/http-contract.test.ts boots a real WebServer (port 3168) and
pins the envelope, /api/v1 alias, error statuses, and the /api 404 shape —
the route-test harness does not install the server-level hook, so these need
the live server. Updated file-routes/plan-routes/scheduled-runs tests to the
fixed shapes.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
235 lines
7.5 KiB
TypeScript
235 lines
7.5 KiB
TypeScript
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
|
import { WebServer } from '../src/web/server.js';
|
|
|
|
const TEST_PORT = 3105;
|
|
|
|
describe('Scheduled Runs API', () => {
|
|
let server: WebServer;
|
|
let baseUrl: string;
|
|
const createdRuns: string[] = [];
|
|
|
|
beforeAll(async () => {
|
|
server = new WebServer(TEST_PORT, false, true);
|
|
await server.start();
|
|
baseUrl = `http://localhost:${TEST_PORT}`;
|
|
});
|
|
|
|
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(TEST_PORT + 1, false, true);
|
|
await server.start();
|
|
baseUrl = `http://localhost:${TEST_PORT + 1}`;
|
|
});
|
|
|
|
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
|
|
});
|
|
});
|