agenthub/tests/server.test.ts
chahinebrini b405b268a5 feat(board): fix gaps A+B — full task status transitions + handoff who→whom
GAP A: extend PATCH /tasks/:id to support all five TaskStatus values
(open, in_progress, review, done, cancelled). Add reviewTask, cancelTask,
reopenTask helpers in taskService. Claim semantics preserved: in_progress
requires assignedTo. Board columns for review and cancelled now reachable
via the API.

GAP B: index fromRole/toRole/fromAgent/toAgent on handoff upsert. SQLite
migration guard adds columns to pre-existing DBs without data loss. Board
renderHandoffs shows "fromRole[@agent] → toRole[@agent]" via → arrow.
GET /handoffs now carries the routing fields in every index entry.

Tests: +14 (56 total, 21 files, all green). Covers every new status
transition, index field presence, board markup assertions, and a guard for
the claim-without-assignedTo 400.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-25 23:41:54 +02:00

157 lines
6.6 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { mkdtempSync, rmSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
import { buildApp } from '../src/server/index.js';
import { init } from '../src/cli/commands/init.js';
describe('server routes', () => {
let cwd: string;
let app: ReturnType<typeof buildApp>;
beforeEach(() => {
cwd = mkdtempSync(join(tmpdir(), 'ah-server-'));
init(cwd, { projectName: 'server-test', yes: true });
app = buildApp(cwd);
});
afterEach(() => {
rmSync(cwd, { recursive: true, force: true });
});
it('creates a task via POST /tasks', async () => {
const res = await app.inject({
method: 'POST',
url: '/tasks',
payload: { title: 'API task', role: 'implementer' },
});
expect(res.statusCode).toBe(200);
const task = JSON.parse(res.payload);
expect(task.id).toBe('TSK-0001');
});
it('lists tasks via GET /tasks', async () => {
await app.inject({ method: 'POST', url: '/tasks', payload: { title: 'A', role: 'implementer' } });
const res = await app.inject({ method: 'GET', url: '/tasks' });
expect(JSON.parse(res.payload)).toHaveLength(1);
});
it('returns 404 for unknown task', async () => {
const res = await app.inject({ method: 'GET', url: '/tasks/TSK-9999' });
expect(res.statusCode).toBe(404);
});
it('returns 400 for unsupported patch', async () => {
await app.inject({ method: 'POST', url: '/tasks', payload: { title: 'A', role: 'implementer' } });
const res = await app.inject({ method: 'PATCH', url: '/tasks/TSK-0001', payload: { status: 'unknown' } });
expect(res.statusCode).toBe(400);
});
it('returns 400 when claiming without assignedTo', async () => {
await app.inject({ method: 'POST', url: '/tasks', payload: { title: 'A', role: 'implementer' } });
const res = await app.inject({ method: 'PATCH', url: '/tasks/TSK-0001', payload: { status: 'in_progress' } });
expect(res.statusCode).toBe(400);
});
it('PATCH /tasks/:id → review', async () => {
await app.inject({ method: 'POST', url: '/tasks', payload: { title: 'A', role: 'implementer' } });
const res = await app.inject({ method: 'PATCH', url: '/tasks/TSK-0001', payload: { status: 'review' } });
expect(res.statusCode).toBe(200);
expect(JSON.parse(res.payload).status).toBe('review');
});
it('PATCH /tasks/:id → cancelled', async () => {
await app.inject({ method: 'POST', url: '/tasks', payload: { title: 'A', role: 'implementer' } });
const res = await app.inject({ method: 'PATCH', url: '/tasks/TSK-0001', payload: { status: 'cancelled' } });
expect(res.statusCode).toBe(200);
expect(JSON.parse(res.payload).status).toBe('cancelled');
});
it('PATCH /tasks/:id → open (reopen)', async () => {
await app.inject({ method: 'POST', url: '/tasks', payload: { title: 'A', role: 'implementer' } });
// First cancel it, then reopen
await app.inject({ method: 'PATCH', url: '/tasks/TSK-0001', payload: { status: 'cancelled' } });
const res = await app.inject({ method: 'PATCH', url: '/tasks/TSK-0001', payload: { status: 'open' } });
expect(res.statusCode).toBe(200);
expect(JSON.parse(res.payload).status).toBe('open');
});
it('review and cancelled tasks appear in GET /tasks list', async () => {
await app.inject({ method: 'POST', url: '/tasks', payload: { title: 'A', role: 'implementer' } });
await app.inject({ method: 'POST', url: '/tasks', payload: { title: 'B', role: 'implementer' } });
await app.inject({ method: 'PATCH', url: '/tasks/TSK-0001', payload: { status: 'review' } });
await app.inject({ method: 'PATCH', url: '/tasks/TSK-0002', payload: { status: 'cancelled' } });
const allRes = await app.inject({ method: 'GET', url: '/tasks' });
const all = JSON.parse(allRes.payload) as Array<{ status: string }>;
const statuses = all.map((t) => t.status);
expect(statuses).toContain('review');
expect(statuses).toContain('cancelled');
});
it('updates status via POST /status/update', async () => {
await app.inject({ method: 'POST', url: '/tasks', payload: { title: 'A', role: 'implementer' } });
const res = await app.inject({ method: 'POST', url: '/status/update' });
expect(res.statusCode).toBe(200);
expect(JSON.parse(res.payload).body).toContain('Active tasks: 1');
});
it('searches memory via GET /memory/search', async () => {
await app.inject({ method: 'POST', url: '/memory', payload: { title: 'DNS cache', category: 'technical', content: 'Use TTL' } });
const res = await app.inject({ method: 'GET', url: '/memory/search?q=TTL' });
expect(res.statusCode).toBe(200);
expect(JSON.parse(res.payload)).toHaveLength(1);
});
it('serves the task board as HTML via GET /board', async () => {
const res = await app.inject({ method: 'GET', url: '/board' });
expect(res.statusCode).toBe(200);
expect(res.headers['content-type']).toContain('text/html');
const html = res.payload;
expect(html).toContain('<title>AgentHub Board</title>');
// All five status columns are present in the static markup.
for (const col of ['open', 'in_progress', 'review', 'done', 'cancelled']) {
expect(html).toContain(`data-column="${col}"`);
}
// Handoffs + decisions panels and the polling logic are wired in.
expect(html).toContain('Handoffs');
expect(html).toContain('Decisions');
expect(html).toContain("getJSON('/tasks')");
expect(html).toContain('setInterval(refresh');
});
it('board HTML contains the who-arrow rendering logic', async () => {
const res = await app.inject({ method: 'GET', url: '/board' });
const html = res.payload;
// The handoffRoute helper and the &rarr; arrow must be present
expect(html).toContain('handoffRoute');
expect(html).toContain('&rarr;');
// The who-cell class must be used in renderHandoffs
expect(html).toContain('who-cell');
});
it('GET /handoffs returns fromRole and toRole fields', async () => {
await app.inject({
method: 'POST',
url: '/handoffs',
payload: {
fromRole: 'architect',
toRole: 'implementer',
fromAgent: 'claude',
toAgent: 'codex',
summary: 'Design done',
context: 'See decisions',
},
});
const res = await app.inject({ method: 'GET', url: '/handoffs' });
expect(res.statusCode).toBe(200);
const items = JSON.parse(res.payload) as Array<Record<string, unknown>>;
expect(items).toHaveLength(1);
expect(items[0].fromRole).toBe('architect');
expect(items[0].toRole).toBe('implementer');
expect(items[0].fromAgent).toBe('claude');
expect(items[0].toAgent).toBe('codex');
});
});