agenthub/tests/health.test.ts
chahinebrini 6d088648da feat(hub): Korrektheits- und Realtime-Härtung — Alias, Lifecycle, Presence, Architekten-Pulse (TSK-0242/0245/0249)
Drei vom Architekten abgenommene Tasks, gebündelt als Checkpoint:

- TSK-0242: Agent-Alias-Mapping (kimi-ah → kimi kanonisiert, Rollen → preferredAgent),
  reopenTask räumt claimedBy ab, fsWatch reindiziert direkte Datei-Edits,
  work-Default 300s → 50s, task_list mit Limit.
- TSK-0245: zwei Agent-Klassen (dispatch loop|architect). Watchdog mahnt
  architekt-getriebene Agenten nur noch EINMAL statt im Minutentakt;
  `task dispatch` startet sie explizit, `task record` trägt extern
  erledigte Arbeit mit origin=external nach.
- TSK-0249: Lifecycle wird serverseitig erzwungen (open→review scheitert mit
  klarer Meldung), claimedBy/doneBy überleben bis done, Presence pro Agent,
  Review-Watchdog, GET /architect/pulse (1.4 kB statt 34 kB, since-Cursor,
  omitted statt stillem Abschneiden), unbekannter Agent → 400 statt 500.

Alle Punkte live am laufenden Hub nachgemessen, nicht aus Agenten-Logs übernommen.
Tests: 242 → 279 grün, tsc sauber.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-29 17:48:24 +02:00

112 lines
4.7 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';
import { resetPresence } from '../src/core/services/presenceService.js';
import { VERSION } from '../src/version.js';
interface HealthBody {
status: string;
version: string;
startedAt: string;
uptimeSec: number;
counts: { tasks: number; open: number; inProgress: number; review: number; unreadMessages: number };
agents: Array<{
name: string; role: string; state: string; taskId?: string; lastSeen?: string; lastSeenAgoSec?: number;
inLoop: boolean; loopSince?: string; loopExitReason?: string;
}>;
}
describe('GET /health + lastSeen stamping (TSK-0226)', () => {
let cwd: string;
let app: ReturnType<typeof buildApp>;
beforeEach(() => {
cwd = mkdtempSync(join(tmpdir(), 'ah-health-'));
init(cwd, { projectName: 'health-test', yes: true });
resetPresence();
app = buildApp(cwd);
});
afterEach(() => {
rmSync(cwd, { recursive: true, force: true });
});
const getHealth = async (): Promise<HealthBody> => {
const res = await app.inject({ method: 'GET', url: '/health' });
expect(res.statusCode).toBe(200);
return JSON.parse(res.payload) as HealthBody;
};
it('returns the compact hub health shape', async () => {
const h = await getHealth();
expect(h.status).toBe('ok');
expect(h.version).toBe(VERSION);
expect(typeof h.uptimeSec).toBe('number');
expect(h.uptimeSec).toBeGreaterThanOrEqual(0);
expect(typeof h.startedAt).toBe('string');
expect(h.counts).toMatchObject({ tasks: 0, open: 0, inProgress: 0, review: 0, unreadMessages: 0 });
expect(Array.isArray(h.agents)).toBe(true);
// The init roster seeds the default preferred agents.
expect(h.agents.map((a) => a.name)).toContain('claude');
});
it('stamps lastSeen on announce → agent shows active', async () => {
await app.inject({ method: 'POST', url: '/announce', payload: { agent: 'kimi', role: 'implementer' } });
const h = await getHealth();
const kimi = h.agents.find((a) => a.name === 'kimi');
expect(kimi).toBeDefined();
expect(kimi!.state).toBe('active');
expect(typeof kimi!.lastSeen).toBe('string');
expect(kimi!.lastSeenAgoSec).toBeLessThan(120);
});
it('shows work-loop entry and the latest exit reason', async () => {
await app.inject({ method: 'POST', url: '/agents/codex/loop', payload: { active: true } });
let codex = (await getHealth()).agents.find((a) => a.name === 'codex')!;
expect(codex.inLoop).toBe(true);
expect(codex.loopSince).toBeDefined();
await app.inject({
method: 'POST',
url: '/agents/codex/loop',
payload: { active: false, reason: 'client timeout' },
});
codex = (await getHealth()).agents.find((a) => a.name === 'codex')!;
expect(codex.inLoop).toBe(false);
expect(codex.loopExitReason).toBe('client timeout');
});
it('stamps lastSeen on message send and counts the unread message', async () => {
await app.inject({ method: 'POST', url: '/messages', payload: { from: 'codex', to: 'claude', text: 'ping' } });
const h = await getHealth();
const codex = h.agents.find((a) => a.name === 'codex');
expect(codex!.state).toBe('active');
expect(h.counts.unreadMessages).toBe(1);
});
it('stamps lastSeen on claim → agent shows busy-on-TSK-X', async () => {
await app.inject({ method: 'POST', url: '/tasks', payload: { title: 'Work', role: 'implementer', assignedTo: 'codex' } });
await app.inject({ method: 'PATCH', url: '/tasks/TSK-0001', payload: { status: 'in_progress', assignedTo: 'codex' } });
const h = await getHealth();
const codex = h.agents.find((a) => a.name === 'codex');
expect(codex!.state).toBe('busy');
expect(codex!.taskId).toBe('TSK-0001');
expect(h.counts.inProgress).toBe(1);
});
it('stamps lastSeen on task-log lines and review submissions', async () => {
await app.inject({ method: 'POST', url: '/tasks', payload: { title: 'Work', role: 'implementer', assignedTo: 'kimi' } });
await app.inject({ method: 'PATCH', url: '/tasks/TSK-0001', payload: { status: 'in_progress', assignedTo: 'kimi' } });
await app.inject({ method: 'POST', url: '/tasks/TSK-0001/log', payload: { text: 'working', agent: 'kimi' } });
await app.inject({ method: 'PATCH', url: '/tasks/TSK-0001', payload: { status: 'review' } });
const h = await getHealth();
const kimi = h.agents.find((a) => a.name === 'kimi');
expect(kimi!.state).toBe('active');
expect(kimi!.lastSeen).toBeDefined();
expect(h.counts.review).toBe(1);
});
});