|
| 1 | +/** |
| 2 | + * Layer 1 — Docker coordinator config (pure, no Docker required) |
| 3 | + * |
| 4 | + * Fast unit tests for the pure functions that generate MCP config for Docker coordinators. |
| 5 | + * No Docker, no network, no filesystem writes. |
| 6 | + */ |
| 7 | + |
| 8 | +import { describe, expect, it } from 'vitest'; |
| 9 | +import { |
| 10 | + buildCoordinatorMCPConfig, |
| 11 | + getDockerMcpServerDestPath, |
| 12 | + selectMcpJsonDir, |
| 13 | +} from './register.js'; |
| 14 | +import { getMCPRemoteServerUrl } from '../mcp/config.js'; |
| 15 | + |
| 16 | +// ── MCP server URL ───────────────────────────────────────────────────────────── |
| 17 | + |
| 18 | +describe('getMCPRemoteServerUrl — host resolution', () => { |
| 19 | + it('uses host.docker.internal on macOS Docker Desktop', () => { |
| 20 | + expect(getMCPRemoteServerUrl(3001, 'my-container', 'darwin')).toBe( |
| 21 | + 'http://host.docker.internal:3001', |
| 22 | + ); |
| 23 | + }); |
| 24 | + |
| 25 | + it('uses 127.0.0.1 on Linux (--network host makes localhost IS the host)', () => { |
| 26 | + // --add-host=host.docker.internal:host-gateway is incompatible with --network host on Linux. |
| 27 | + // With --network host the container shares the host network stack, so 127.0.0.1 IS the host. |
| 28 | + expect(getMCPRemoteServerUrl(3001, 'my-container', 'linux')).toBe('http://127.0.0.1:3001'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('uses 127.0.0.1 when no container name (non-Docker)', () => { |
| 32 | + expect(getMCPRemoteServerUrl(3001, undefined)).toBe('http://127.0.0.1:3001'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('uses 127.0.0.1 when container name is empty string', () => { |
| 36 | + expect(getMCPRemoteServerUrl(3001, '')).toBe('http://127.0.0.1:3001'); |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +// ── .mcp.json placement ──────────────────────────────────────────────────────── |
| 41 | + |
| 42 | +describe('selectMcpJsonDir — .mcp.json placement', () => { |
| 43 | + it('places .mcp.json in worktreePath when provided', () => { |
| 44 | + expect(selectMcpJsonDir('/worktrees/coord-abc', '/project')).toBe('/worktrees/coord-abc'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('falls back to projectRoot when worktreePath is undefined', () => { |
| 48 | + expect(selectMcpJsonDir(undefined, '/project')).toBe('/project'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('worktreePath wins over projectRoot (Docker: container only mounts worktree)', () => { |
| 52 | + const worktreePath = '/Users/alice/repo/.worktrees/task/coord-abc123'; |
| 53 | + const projectRoot = '/Users/alice/repo'; |
| 54 | + const dir = selectMcpJsonDir(worktreePath, projectRoot); |
| 55 | + // .mcp.json must be inside the volume-mounted worktree, not the projectRoot (not mounted) |
| 56 | + expect(dir).toBe(worktreePath); |
| 57 | + expect(dir).not.toBe(projectRoot); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +// ── copied mcp-server.cjs path ───────────────────────────────────────────────── |
| 62 | + |
| 63 | +describe('getDockerMcpServerDestPath — copied mcp-server.cjs location', () => { |
| 64 | + it('places mcp-server.cjs in worktree .parallel-code dir', () => { |
| 65 | + const dest = getDockerMcpServerDestPath('/worktrees/coord', '/project'); |
| 66 | + expect(dest).toBe('/worktrees/coord/.parallel-code/mcp-server.cjs'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('falls back to projectRoot when worktreePath is undefined', () => { |
| 70 | + const dest = getDockerMcpServerDestPath(undefined, '/project'); |
| 71 | + expect(dest).toBe('/project/.parallel-code/mcp-server.cjs'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('dest is under the mounted worktree, not the unmounted projectRoot', () => { |
| 75 | + const worktreePath = '/home/user/repo/.worktrees/task/coord-abc123'; |
| 76 | + const projectRoot = '/home/user/repo'; |
| 77 | + const dest = getDockerMcpServerDestPath(worktreePath, projectRoot); |
| 78 | + // The container mounts worktreePath (not projectRoot), so the script must live there |
| 79 | + expect(dest.startsWith(worktreePath)).toBe(true); |
| 80 | + expect(dest.startsWith(projectRoot + '/.parallel-code')).toBe(false); |
| 81 | + }); |
| 82 | + |
| 83 | + it('filename is always mcp-server.cjs', () => { |
| 84 | + const dest = getDockerMcpServerDestPath('/worktrees/coord', '/project'); |
| 85 | + expect(dest.endsWith('/mcp-server.cjs')).toBe(true); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +// ── .mcp.json config content ─────────────────────────────────────────────────── |
| 90 | + |
| 91 | +describe('buildCoordinatorMCPConfig — config content', () => { |
| 92 | + const baseOpts = { |
| 93 | + mcpServerPath: '/worktrees/coord/.parallel-code/mcp-server.cjs', |
| 94 | + serverUrl: 'http://host.docker.internal:3001', |
| 95 | + token: 'test-token-abc', |
| 96 | + coordinatorTaskId: 'coord-task-1', |
| 97 | + }; |
| 98 | + |
| 99 | + it('has type:stdio and command:node', () => { |
| 100 | + const cfg = buildCoordinatorMCPConfig(baseOpts); |
| 101 | + const server = cfg.mcpServers['parallel-code']; |
| 102 | + expect(server.type).toBe('stdio'); |
| 103 | + expect(server.command).toBe('node'); |
| 104 | + }); |
| 105 | + |
| 106 | + it('args[0] is the mcp-server.cjs path (the copied worktree path, not host path)', () => { |
| 107 | + const cfg = buildCoordinatorMCPConfig(baseOpts); |
| 108 | + expect(cfg.mcpServers['parallel-code'].args[0]).toBe(baseOpts.mcpServerPath); |
| 109 | + }); |
| 110 | + |
| 111 | + it('args contain --url pointing to host.docker.internal', () => { |
| 112 | + const cfg = buildCoordinatorMCPConfig(baseOpts); |
| 113 | + const args = cfg.mcpServers['parallel-code'].args; |
| 114 | + const urlIdx = args.indexOf('--url'); |
| 115 | + expect(urlIdx).toBeGreaterThan(0); |
| 116 | + expect(args[urlIdx + 1]).toBe('http://host.docker.internal:3001'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('token is passed via env var, not args', () => { |
| 120 | + const cfg = buildCoordinatorMCPConfig(baseOpts); |
| 121 | + const args = cfg.mcpServers['parallel-code'].args; |
| 122 | + expect(args).not.toContain('--token'); |
| 123 | + expect(cfg.mcpServers['parallel-code'].env['PARALLEL_CODE_MCP_TOKEN']).toBe(baseOpts.token); |
| 124 | + }); |
| 125 | + |
| 126 | + it('args contain --coordinator-id', () => { |
| 127 | + const cfg = buildCoordinatorMCPConfig(baseOpts); |
| 128 | + const args = cfg.mcpServers['parallel-code'].args; |
| 129 | + const coordIdx = args.indexOf('--coordinator-id'); |
| 130 | + expect(coordIdx).toBeGreaterThan(0); |
| 131 | + expect(args[coordIdx + 1]).toBe(baseOpts.coordinatorTaskId); |
| 132 | + }); |
| 133 | + |
| 134 | + it('omits --skip-permissions by default', () => { |
| 135 | + const cfg = buildCoordinatorMCPConfig(baseOpts); |
| 136 | + expect(cfg.mcpServers['parallel-code'].args).not.toContain('--skip-permissions'); |
| 137 | + }); |
| 138 | + |
| 139 | + it('adds --skip-permissions when both flags are true', () => { |
| 140 | + const cfg = buildCoordinatorMCPConfig({ |
| 141 | + ...baseOpts, |
| 142 | + skipPermissions: true, |
| 143 | + propagateSkipPermissions: true, |
| 144 | + }); |
| 145 | + expect(cfg.mcpServers['parallel-code'].args).toContain('--skip-permissions'); |
| 146 | + }); |
| 147 | + |
| 148 | + it('does NOT add --skip-permissions when propagateSkipPermissions is false', () => { |
| 149 | + const cfg = buildCoordinatorMCPConfig({ |
| 150 | + ...baseOpts, |
| 151 | + skipPermissions: true, |
| 152 | + propagateSkipPermissions: false, |
| 153 | + }); |
| 154 | + expect(cfg.mcpServers['parallel-code'].args).not.toContain('--skip-permissions'); |
| 155 | + }); |
| 156 | + |
| 157 | + it('does NOT add --skip-permissions when skipPermissions is false', () => { |
| 158 | + const cfg = buildCoordinatorMCPConfig({ |
| 159 | + ...baseOpts, |
| 160 | + skipPermissions: false, |
| 161 | + propagateSkipPermissions: true, |
| 162 | + }); |
| 163 | + expect(cfg.mcpServers['parallel-code'].args).not.toContain('--skip-permissions'); |
| 164 | + }); |
| 165 | + |
| 166 | + it('JSON-serialised output is valid JSON with the parallel-code key', () => { |
| 167 | + const cfg = buildCoordinatorMCPConfig(baseOpts); |
| 168 | + const json = JSON.stringify(cfg, null, 2); |
| 169 | + const parsed = JSON.parse(json) as typeof cfg; |
| 170 | + expect(parsed.mcpServers['parallel-code']).toBeDefined(); |
| 171 | + }); |
| 172 | +}); |
0 commit comments