agenthub/tests/mcp.test.ts
chahinebrini df698c9dff feat(agenthub): Rollenwechsel, agent-setup + Push-Channel
roleService haelt roles.*.preferredAgent (Routing-Autoritaet fuer asks,
Watchdog, start) und agents.*.role (Health/Board/MCP-Discovery) konsistent —
bisher konnte nur eine Seite gesetzt werden und der Roster lief auseinander.
Dazu agent-setup-Kommando, Push-Channel im MCP und Selbstheilung im
remoteClient.

Build clean, 304 Tests in 55 Dateien gruen.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0113SC5FkaMxcRzXHDBsHmdJ
2026-08-02 21:38:47 +02:00

92 lines
3.0 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { init } from '../src/cli/commands/init.js';
import { loadConfig, saveConfig } from '../src/core/config.js';
import {
DEFAULT_TASK_LIST_LIMIT,
DEFAULT_WORK_TIMEOUT_SEC,
boundedTaskList,
resolveMcpContext,
} from '../src/mcp/server.js';
import { installMcp } from '../src/mcp/install.js';
describe('MCP context resolution', () => {
let cwd: string;
const originalEnv = process.env.AGENTHUB_SERVER;
beforeEach(() => {
cwd = mkdtempSync(join(tmpdir(), 'ah-mcp-'));
init(cwd, { projectName: 'mcp-test', yes: true });
delete process.env.AGENTHUB_SERVER;
});
afterEach(() => {
if (originalEnv === undefined) delete process.env.AGENTHUB_SERVER;
else process.env.AGENTHUB_SERVER = originalEnv;
rmSync(cwd, { recursive: true, force: true });
});
it('self-heals a stale configured server URL before MCP tools call remote APIs', async () => {
const config = loadConfig(cwd);
config.serverUrl = 'http://agenthub.local:3377';
saveConfig(cwd, config);
const context = await resolveMcpContext(cwd, {
probe: async () => false,
discover: async () => 'http://127.0.0.1:3377',
});
expect(context).toEqual({ root: cwd, serverUrl: 'http://127.0.0.1:3377' });
});
});
describe('MCP bounded defaults', () => {
it('uses a client-safe work timeout', () => {
expect(DEFAULT_WORK_TIMEOUT_SEC).toBe(50);
});
it('returns task lists newest-first with explicit truncation metadata', () => {
const tasks = Array.from({ length: 55 }, (_, i) => ({
id: `TSK-${String(i).padStart(4, '0')}`,
updatedAt: new Date(Date.UTC(2026, 0, i + 1)).toISOString(),
}));
const result = boundedTaskList(tasks);
expect(result).toMatchObject({ total: 55, returned: DEFAULT_TASK_LIST_LIMIT, omitted: 5 });
expect(result.tasks[0].id).toBe('TSK-0054');
expect(result.tasks.at(-1)?.id).toBe('TSK-0005');
});
});
describe('MCP installation', () => {
let cwd: string;
beforeEach(() => {
cwd = mkdtempSync(join(tmpdir(), 'ah-mcp-install-'));
init(cwd, { projectName: 'mcp-install-test', yes: true });
});
afterEach(() => {
rmSync(cwd, { recursive: true, force: true });
});
it('binds the push bridge at process startup when --agent is provided', () => {
installMcp(cwd, { agent: 'codex' });
const json = JSON.parse(readFileSync(join(cwd, '.mcp.json'), 'utf-8')) as {
mcpServers: { agenthub: { args: string[] } };
};
expect(json.mcpServers.agenthub.args).toEqual(['mcp', '--agent', 'codex']);
});
it('keeps the shared project config agent-neutral by default', () => {
installMcp(cwd);
const json = JSON.parse(readFileSync(join(cwd, '.mcp.json'), 'utf-8')) as {
mcpServers: { agenthub: { args: string[] } };
};
expect(json.mcpServers.agenthub.args).toEqual(['mcp']);
});
});