agenthub/tests/server.test.ts
chahinebrini 7ca6c859f2 feat(board+team): interactive board, animated team tree, token budget, mDNS, realtime architect review + codex/kimi autostart
Board (/board):
- Drag an AGENT chip onto a task card to (re)assign it (realtime-notified).
- Drag a task card to a column to change status; open→in_progress auto-assigns
  from the title's "<agent>:" prefix — no manual agent picking.
- "+ New task" composer (agent dropdown removed; agent comes from the title).
- Live Cost & Budget panel.

Team (/team): live SSE sync of busy state + always-on ambient animation
(connector shimmer, idle glow) that brightens to a busy pulse when an agent
works. Org-chart hierarchy stays.

Token accounting: new budgetService/rosterService + GET /budget and /agents.
Real doneTokens + time-on-task estimate capped at 45 min/task (avoids the
wall-clock overcount that produced multi-million-token totals), blended
per-model EUR cost + optional budget bars. All estimates flagged "~".

Autostart: `agent setup` writes deterministic SessionStart hooks for Codex
(~/.codex/config.toml) and Kimi (~/.kimi-code/config.toml), not just Claude
Code. Verified: both auto-enter the agenthub_work loop.

Realtime architect review: agenthub_work is role-aware — architect/reviewer
blocks on SSE and wakes when a task hits review (no manual watcher re-arm).

mDNS: server advertises agenthub.local (bonjour-service) so the hub is
reachable in a browser on the LAN without an IP.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-08 03:23:48 +02:00

287 lines
12 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');
});
// ── Activity timeline endpoint ─────────────────────────────────────────
it('GET /tasks/:id/activity returns 404 for unknown task', async () => {
const res = await app.inject({ method: 'GET', url: '/tasks/TSK-9999/activity' });
expect(res.statusCode).toBe(404);
});
it('GET /tasks/:id/activity returns a created event', async () => {
await app.inject({ method: 'POST', url: '/tasks', payload: { title: 'Auth', role: 'implementer' } });
const res = await app.inject({ method: 'GET', url: '/tasks/TSK-0001/activity' });
expect(res.statusCode).toBe(200);
const items = JSON.parse(res.payload) as Array<{ kind: string; summary: string }>;
expect(items.some((i) => i.kind === 'created')).toBe(true);
});
it('GET /tasks/:id/activity includes handoff events linked by taskId', async () => {
await app.inject({ method: 'POST', url: '/tasks', payload: { title: 'Auth', role: 'implementer' } });
await app.inject({
method: 'POST',
url: '/handoffs',
payload: { fromRole: 'architect', toRole: 'implementer', taskId: 'TSK-0001', summary: 'Design done' },
});
const res = await app.inject({ method: 'GET', url: '/tasks/TSK-0001/activity' });
const items = JSON.parse(res.payload) as Array<{ kind: string; summary: string }>;
const handoff = items.find((i) => i.kind === 'handoff');
expect(handoff).toBeDefined();
expect(handoff!.summary).toBe('Design done');
});
it('GET /tasks/:id/activity includes memory result events linked by relatedTasks', async () => {
await app.inject({ method: 'POST', url: '/tasks', payload: { title: 'DNS task', role: 'implementer' } });
await app.inject({
method: 'POST',
url: '/memory',
payload: { title: 'TTL insight', category: 'technical', content: 'Use 300s', relatedTasks: ['TSK-0001'] },
});
const res = await app.inject({ method: 'GET', url: '/tasks/TSK-0001/activity' });
const items = JSON.parse(res.payload) as Array<{ kind: string; summary: string }>;
const result = items.find((i) => i.kind === 'result');
expect(result).toBeDefined();
expect(result!.summary).toBe('TTL insight');
});
it('GET /tasks/:id/activity includes tokens/duration from done metadata', async () => {
await app.inject({ method: 'POST', url: '/tasks', payload: { title: 'Work', role: 'implementer' } });
await app.inject({
method: 'PATCH',
url: '/tasks/TSK-0001',
payload: { status: 'done', doneTokens: 5000, doneDuration: 60000, doneBy: 'claude' },
});
const res = await app.inject({ method: 'GET', url: '/tasks/TSK-0001/activity' });
const items = JSON.parse(res.payload) as Array<{ kind: string; meta?: Record<string, unknown> }>;
const status = items.find((i) => i.kind === 'status');
expect(status).toBeDefined();
expect(status!.meta?.tokens).toBe(5000);
expect(status!.meta?.duration).toBe(60000);
expect(status!.meta?.by).toBe('claude');
});
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('board HTML keeps cards slim and links to task detail pages', async () => {
const res = await app.inject({ method: 'GET', url: '/board' });
const html = res.payload;
// Cards carry data-id for realtime refresh bookkeeping and link to detail pages.
expect(html).toContain('data-id');
expect(html).toContain('href="/tasks/');
expect(html).not.toContain('class="timeline"');
// Cards must not expand inline (aria-expanded on the composer button is fine).
expect(html).not.toMatch(/class="[^"]*\bexpanded\b/);
expect(html).not.toContain("'/tasks/' + encodeURIComponent(id) + '/activity'");
});
it('task detail HTML shows activity metadata and linked decisions with reasoning', async () => {
await app.inject({ method: 'POST', url: '/tasks', payload: { title: 'Detail transparency', role: 'implementer' } });
await app.inject({
method: 'POST',
url: '/decisions',
payload: {
title: 'Keep cards slim',
context: 'Transparency belongs on the task detail page.',
decision: 'Task cards link to detail pages instead of expanding inline.',
},
});
await app.inject({
method: 'POST',
url: '/handoffs',
payload: {
fromRole: 'architect',
toRole: 'implementer',
taskId: 'TSK-0001',
summary: 'Implement detail view',
decisions: ['DEC-0001'],
},
});
await app.inject({
method: 'PATCH',
url: '/tasks/TSK-0001',
payload: { status: 'done', doneTokens: 3210, doneDuration: 65000, doneBy: 'codex' },
});
const res = await app.inject({ method: 'GET', url: '/tasks/TSK-0001', headers: { accept: 'text/html' } });
expect(res.statusCode).toBe(200);
expect(res.headers['content-type']).toContain('text/html');
expect(res.payload).toContain('Decisions &amp; Reasoning');
expect(res.payload).toContain('Keep cards slim');
expect(res.payload).toContain('Transparency belongs on the task detail page.');
expect(res.payload).toContain('3210&thinsp;tok');
expect(res.payload).toContain('1m');
});
it('serves the activity page with recent activity and done archive', async () => {
await app.inject({ method: 'POST', url: '/tasks', payload: { title: 'Done item', role: 'implementer' } });
await app.inject({
method: 'PATCH',
url: '/tasks/TSK-0001',
payload: { status: 'done', doneBy: 'codex' },
});
await app.inject({
method: 'POST',
url: '/messages',
payload: { from: 'claude', to: 'codex', text: 'Please check the board', taskId: 'TSK-0001' },
});
const res = await app.inject({ method: 'GET', url: '/activity' });
expect(res.statusCode).toBe(200);
expect(res.headers['content-type']).toContain('text/html');
expect(res.payload).toContain('Recent Activity');
expect(res.payload).toContain('Done Archive');
expect(res.payload).toContain('MSG-0001');
expect(res.payload).toContain('TSK-0001');
});
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');
});
});