|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { mkdtempSync, rmSync, realpathSync } from 'fs'; |
| 3 | +import { tmpdir } from 'os'; |
| 4 | +import { join } from 'path'; |
| 5 | + |
| 6 | +describe('updateAllowedDirectoriesFromRoots merge behaviour', () => { |
| 7 | + let cliDir: string; |
| 8 | + let mcpDir1: string; |
| 9 | + let mcpDir2: string; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + cliDir = realpathSync(mkdtempSync(join(tmpdir(), 'mcp-cli-'))); |
| 13 | + mcpDir1 = realpathSync(mkdtempSync(join(tmpdir(), 'mcp-roots1-'))); |
| 14 | + mcpDir2 = realpathSync(mkdtempSync(join(tmpdir(), 'mcp-roots2-'))); |
| 15 | + }); |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + rmSync(cliDir, { recursive: true, force: true }); |
| 19 | + rmSync(mcpDir1, { recursive: true, force: true }); |
| 20 | + rmSync(mcpDir2, { recursive: true, force: true }); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should preserve CLI dirs after init with roots', () => { |
| 24 | + // Simulates the snapshot: cliAllowedDirectories = [cliDir] |
| 25 | + const cliAllowedDirectories = [cliDir]; |
| 26 | + const validatedRootDirs = [mcpDir1]; |
| 27 | + |
| 28 | + const merged = new Set([...cliAllowedDirectories, ...validatedRootDirs]); |
| 29 | + const allowedDirectories = [...merged]; |
| 30 | + |
| 31 | + expect(allowedDirectories).toContain(cliDir); |
| 32 | + expect(allowedDirectories).toContain(mcpDir1); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should replace MCP roots on roots/list_changed without accumulating', () => { |
| 36 | + // Simulates: CLI dir + first MCP root |
| 37 | + const cliAllowedDirectories = [cliDir]; |
| 38 | + const firstMcpRoots = [mcpDir1]; |
| 39 | + |
| 40 | + let merged = new Set([...cliAllowedDirectories, ...firstMcpRoots]); |
| 41 | + let allowedDirectories = [...merged]; |
| 42 | + |
| 43 | + expect(allowedDirectories).toContain(cliDir); |
| 44 | + expect(allowedDirectories).toContain(mcpDir1); |
| 45 | + expect(allowedDirectories).toHaveLength(2); |
| 46 | + |
| 47 | + // Simulates: roots/list_changed with new root (mcpDir2 replaces mcpDir1) |
| 48 | + const secondMcpRoots = [mcpDir2]; |
| 49 | + merged = new Set([...cliAllowedDirectories, ...secondMcpRoots]); |
| 50 | + allowedDirectories = [...merged]; |
| 51 | + |
| 52 | + expect(allowedDirectories).toContain(cliDir); |
| 53 | + expect(allowedDirectories).toContain(mcpDir2); |
| 54 | + expect(allowedDirectories).not.toContain(mcpDir1); |
| 55 | + expect(allowedDirectories).toHaveLength(2); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should not accumulate stale roots across multiple updates', () => { |
| 59 | + const cliAllowedDirectories = [cliDir]; |
| 60 | + let allowedDirectories: string[] = []; |
| 61 | + |
| 62 | + for (let i = 0; i < 5; i++) { |
| 63 | + const tmpDir = realpathSync(mkdtempSync(join(tmpdir(), `mcp-root-${i}-`))); |
| 64 | + const merged = new Set([...cliAllowedDirectories, tmpDir]); |
| 65 | + allowedDirectories = [...merged]; |
| 66 | + rmSync(tmpDir, { recursive: true, force: true }); |
| 67 | + } |
| 68 | + |
| 69 | + // After each iteration, only CLI dir + latest root should be present |
| 70 | + expect(allowedDirectories).toHaveLength(2); |
| 71 | + expect(allowedDirectories).toContain(cliDir); |
| 72 | + }); |
| 73 | +}); |
0 commit comments