| 1 | import { test, before } from 'node:test' |
| 2 | import assert from 'node:assert/strict' |
| 3 | import { mkdtempSync, rmSync } from 'node:fs' |
| 4 | import { join, dirname } from 'node:path' |
| 5 | import { fileURLToPath, pathToFileURL } from 'node:url' |
| 6 | import { build } from 'esbuild' |
| 7 | |
| 8 | const here = dirname(fileURLToPath(import.meta.url)) |
| 9 | let workspaceAdapter |
| 10 | let Context |
| 11 | let ErrorCode |
| 12 | |
| 13 | before(async () => { |
| 14 | const dir = mkdtempSync(join(here, '..', 'node_modules', '.cw-workspace-')) |
| 15 | const bundle = async (entry, name) => { |
| 16 | const outfile = join(dir, name) |
| 17 | await build({ entryPoints: [join(here, entry)], outfile, bundle: true, platform: 'node', format: 'esm', packages: 'external', absWorkingDir: join(here, '..'), logLevel: 'silent' }) |
| 18 | return import(pathToFileURL(outfile).href) |
| 19 | } |
| 20 | ;({ workspaceAdapter } = await bundle('../src/dsh/workspace.ts', 'workspace.mjs')) |
| 21 | ;({ ErrorCode } = await bundle('../src/protocol.ts', 'protocol.mjs')) |
| 22 | ;({ Context } = await import('@deepseek-ai/cordis')) |
| 23 | process.on('exit', () => rmSync(dir, { recursive: true, force: true })) |
| 24 | }) |
| 25 | |
| 26 | const settle = () => new Promise((resolve) => setTimeout(resolve, 0)) |
| 27 | |
| 28 | async function harness() { |
| 29 | const calls = [] |
| 30 | const host = { |
| 31 | tier: 'plugin', |
| 32 | rpc: { request: async () => { throw new Error('workspace never calls the core directly') }, notify() {} }, |
| 33 | ownerOf: () => undefined, |
| 34 | liveInvocation: () => undefined, |
| 35 | broker: async (ctx, service, method, input) => { calls.push([service, method, input]); return { ok: method } }, |
| 36 | log() {}, |
| 37 | } |
| 38 | const ctx = new Context() |
| 39 | ctx.plugin(workspaceAdapter.define(host)) |
| 40 | await settle() |
| 41 | return { ctx, calls } |
| 42 | } |
| 43 | |
| 44 | test('workspace sends every method through the workspace broker with a typed payload and nothing else', async () => { |
| 45 | const { ctx, calls } = await harness() |
| 46 | assert.deepEqual(await ctx.workspace.read('src/a.ts'), { ok: 'read' }) |
| 47 | await ctx.workspace.read('src/a.ts', { startLine: 3, maxLines: 10, follow: true }) |
| 48 | await ctx.workspace.list() |
| 49 | await ctx.workspace.list('src') |
| 50 | await ctx.workspace.write('src/b.ts', 'x', { expectedHash: 'h', mode: 0o777 }) |
| 51 | await ctx.workspace.write('src/c.ts', '') |
| 52 | await ctx.workspace.exec(['git', 'rev-parse', '--show-toplevel'], { cwd: '/w', env: { GIT_DIR: '/' }, shell: true }) |
| 53 | assert.deepEqual(calls, [ |
| 54 | ['workspace', 'read', { path: 'src/a.ts' }], |
| 55 | ['workspace', 'read', { path: 'src/a.ts', start_line: 3, max_lines: 10 }], |
| 56 | ['workspace', 'list', {}], |
| 57 | ['workspace', 'list', { path: 'src' }], |
| 58 | ['workspace', 'write', { path: 'src/b.ts', content: 'x', expected_hash: 'h' }], |
| 59 | ['workspace', 'write', { path: 'src/c.ts', content: '' }], |
| 60 | ['workspace', 'exec', { argv: ['git', 'rev-parse', '--show-toplevel'], cwd: '/w' }], |
| 61 | ]) |
| 62 | }) |
| 63 | |
| 64 | test('workspace refuses malformed arguments locally, before any broker call', async () => { |
| 65 | const { ctx, calls } = await harness() |
| 66 | const refused = (act) => assert.throws(act, (error) => error.code === ErrorCode.InvalidParams) |
| 67 | for (const bad of [undefined, '', 7, ' a', 'a\nb', 'p'.repeat(513), ['a']]) { |
| 68 | refused(() => ctx.workspace.read(bad)) |
| 69 | refused(() => ctx.workspace.write(bad, 'x')) |
| 70 | if (bad !== undefined) refused(() => ctx.workspace.list(bad)) |
| 71 | } |
| 72 | for (const lines of [0, 10_001, 1.5, '3']) refused(() => ctx.workspace.read('a', { maxLines: lines })) |
| 73 | refused(() => ctx.workspace.read('a', { startLine: 0 })) |
| 74 | for (const content of [undefined, 7, 'c'.repeat(1024 * 1024 + 1)]) refused(() => ctx.workspace.write('a', content)) |
| 75 | refused(() => ctx.workspace.write('a', 'x', { expectedHash: '' })) |
| 76 | for (const command of [undefined, 'git status', [], [7], ['git', 'a\u0000b'], new Array(10).fill('git')]) refused(() => ctx.workspace.exec(command)) |
| 77 | refused(() => ctx.workspace.exec(['git'], { cwd: '' })) |
| 78 | for (const absent of ['delete', 'rename', 'spawn', 'shell', 'watch', 'readSync']) assert.equal(typeof ctx.workspace[absent], 'undefined') |
| 79 | assert.deepEqual(calls, []) |
| 80 | }) |
| 81 |