agenthub/tests/health.test.ts
chahinebrini b8ad801c1d feat(hub): Version + Build-Commit im Header und in /health (v0.11.0)
Zwei Maschinen liessen sich nicht auseinanderhalten. Der Watchdog-Fix von
6fa9411 ging ohne Versions-Bump raus, also meldeten beide Hubs 0.10.2,
waehrend nur einer den Fix hatte. Zusaetzlich faehrt der Hub aus dist/ —
ein Neustart ohne Build zieht still den alten Code wieder hoch. Beides
zusammen hat heute eine Diagnose in die falsche Richtung geschickt.

- Version auf 0.11.0 (Feature + Fix seit 0.10.2).
- version.ts liefert zusaetzlich buildRef(): den kurzen Commit, aus dem der
  laufende Code stammt. Bewusst LAZY und gecacht — VERSION wird von jedem
  CLI-Aufruf importiert, ein git-Aufruf beim Import wuerde Befehle
  verteuern, die ihn nie anzeigen. Bei Nicht-git-Installs faellt
  versionLabel() sauber auf die blosse Version zurueck.
- Board-Header (appHeader) und der aeltere pageHeader zeigen
  "v0.11.0 · <commit>", mit Tooltip warum die Nummer allein nicht reicht.
- /health liefert buildRef, damit sich zwei Maschinen ueber die Leitung
  vergleichen lassen, ohne das Board zu oeffnen.

318 Tests gruen, tsc --noEmit sauber.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-09 16:59:34 +02:00

128 lines
5.4 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; pendingAsks: number; oldestAskAgeSec?: 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);
// The version alone cannot tell two hubs apart — a fix can ship without a
// bump. /health therefore carries the commit, so machines are comparable
// over the wire without opening the board.
expect(typeof h.buildRef).toBe('string');
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, pendingAsks: 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('counts pending Asks and exposes the oldest age', async () => {
await app.inject({
method: 'POST', url: '/asks',
payload: { from: 'codex', to: 'claude', question: 'Need architecture input' },
});
const h = await getHealth();
expect(h.counts.pendingAsks).toBe(1);
expect(h.counts.oldestAskAgeSec).toBeTypeOf('number');
expect(h.counts.oldestAskAgeSec).toBeGreaterThanOrEqual(0);
});
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);
});
});