import { describe, it, expect, beforeEach, afterEach } from 'vitest'; import { mkdtempSync, 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'; 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'); }); });