|
| 1 | +import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'; |
| 2 | +import { tmpdir } from 'node:os'; |
| 3 | +import { join } from 'node:path'; |
1 | 4 | import { Writable } from 'node:stream'; |
2 | | -import { describe, expect, it } from 'vitest'; |
| 5 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 6 | +import type { Mode, ServeMcpStdioOpts } from '@deepcode/core'; |
3 | 7 | import { runMcpCommand } from './mcp-cmd.js'; |
4 | 8 |
|
5 | 9 | function sink(): { stream: Writable; text: () => string } { |
@@ -51,3 +55,96 @@ describe('runMcpCommand', () => { |
51 | 55 | expect(err.text()).toMatch(/\[mcp\] ready: Read, Write/); |
52 | 56 | }); |
53 | 57 | }); |
| 58 | + |
| 59 | +// The served tools are Read/Write/Edit/Bash in a real project, and nobody is on |
| 60 | +// the other end of the pipe to approve anything. |
| 61 | +describe('runMcpCommand serve — permission posture', () => { |
| 62 | + let home: string; |
| 63 | + let cwd: string; |
| 64 | + |
| 65 | + beforeEach(async () => { |
| 66 | + home = await mkdtemp(join(tmpdir(), 'dc-mcp-home-')); |
| 67 | + cwd = await mkdtemp(join(tmpdir(), 'dc-mcp-cwd-')); |
| 68 | + }); |
| 69 | + afterEach(async () => { |
| 70 | + await rm(home, { recursive: true, force: true }); |
| 71 | + await rm(cwd, { recursive: true, force: true }); |
| 72 | + }); |
| 73 | + |
| 74 | + async function serve(opts: { mode?: Mode } = {}): Promise<{ |
| 75 | + err: string; |
| 76 | + captured: ServeMcpStdioOpts | undefined; |
| 77 | + }> { |
| 78 | + const out = sink(); |
| 79 | + const err = sink(); |
| 80 | + let captured: ServeMcpStdioOpts | undefined; |
| 81 | + await runMcpCommand(['serve'], { |
| 82 | + cwd, |
| 83 | + home, |
| 84 | + output: out.stream, |
| 85 | + errOutput: err.stream, |
| 86 | + mode: opts.mode, |
| 87 | + serve: async (o) => { |
| 88 | + captured = o; |
| 89 | + }, |
| 90 | + }); |
| 91 | + return { err: err.text(), captured }; |
| 92 | + } |
| 93 | + |
| 94 | + async function writeUserSettings(settings: Record<string, unknown>): Promise<void> { |
| 95 | + await mkdir(join(home, '.deepcode'), { recursive: true }); |
| 96 | + await writeFile(join(home, '.deepcode', 'settings.json'), JSON.stringify(settings)); |
| 97 | + } |
| 98 | + |
| 99 | + it('passes a gate at all — the server cannot be built without one', async () => { |
| 100 | + const { captured } = await serve(); |
| 101 | + expect(captured?.gate).toBeTypeOf('function'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('refuses a call that would need approval', async () => { |
| 105 | + const { captured } = await serve(); |
| 106 | + const verdict = await captured!.gate({ |
| 107 | + tool: 'Write', |
| 108 | + input: { file_path: join(cwd, 'x.txt'), content: 'x' }, |
| 109 | + }); |
| 110 | + expect(verdict.allowed).toBe(false); |
| 111 | + expect(verdict.reason).toMatch(/no attached user/); |
| 112 | + }); |
| 113 | + |
| 114 | + it('clamps a permissive ambient mode and says so', async () => { |
| 115 | + // `bypassPermissions` in settings.json is a choice about sitting at a REPL. |
| 116 | + // Inheriting it here would hand "never ask me" to whatever connected. |
| 117 | + await writeUserSettings({ permissions: { defaultMode: 'bypassPermissions' } }); |
| 118 | + const { err, captured } = await serve(); |
| 119 | + expect(err).toMatch(/was not applied to this unattended run/); |
| 120 | + expect(err).toMatch(/mode=default/); |
| 121 | + |
| 122 | + const verdict = await captured!.gate({ |
| 123 | + tool: 'Bash', |
| 124 | + input: { command: 'rm -rf /' }, |
| 125 | + }); |
| 126 | + expect(verdict.allowed).toBe(false); |
| 127 | + }); |
| 128 | + |
| 129 | + it('--mode is the explicit opt-in back out of the clamp', async () => { |
| 130 | + await writeUserSettings({ permissions: { defaultMode: 'bypassPermissions' } }); |
| 131 | + const { err, captured } = await serve({ mode: 'bypassPermissions' }); |
| 132 | + expect(err).not.toMatch(/was not applied/); |
| 133 | + expect(err).toMatch(/mode=bypassPermissions/); |
| 134 | + expect((await captured!.gate({ tool: 'Bash', input: { command: 'echo hi' } })).allowed).toBe( |
| 135 | + true, |
| 136 | + ); |
| 137 | + }); |
| 138 | + |
| 139 | + it('honours permissions.allow without any mode change', async () => { |
| 140 | + await writeUserSettings({ permissions: { allow: ['Read'] } }); |
| 141 | + const { captured } = await serve(); |
| 142 | + expect( |
| 143 | + (await captured!.gate({ tool: 'Read', input: { file_path: join(cwd, 'a') } })).allowed, |
| 144 | + ).toBe(true); |
| 145 | + expect( |
| 146 | + (await captured!.gate({ tool: 'Write', input: { file_path: join(cwd, 'a'), content: '' } })) |
| 147 | + .allowed, |
| 148 | + ).toBe(false); |
| 149 | + }); |
| 150 | +}); |
0 commit comments