| 1 | import { test, before } from 'node:test' |
| 2 | import assert from 'node:assert/strict' |
| 3 | import { mkdtempSync, rmSync, writeFileSync } from 'node:fs' |
| 4 | import { tmpdir } from 'node:os' |
| 5 | import { join, dirname } from 'node:path' |
| 6 | import { fileURLToPath, pathToFileURL } from 'node:url' |
| 7 | import { build } from 'esbuild' |
| 8 | import { activate, startHost } from './harness.mjs' |
| 9 | |
| 10 | const here = dirname(fileURLToPath(import.meta.url)) |
| 11 | let remoteAdapter |
| 12 | let Context |
| 13 | let ErrorCode |
| 14 | |
| 15 | before(async () => { |
| 16 | const dir = mkdtempSync(join(here, '..', 'node_modules', '.cw-remote-')) |
| 17 | const bundle = async (entry, name) => { |
| 18 | const outfile = join(dir, name) |
| 19 | await build({ entryPoints: [join(here, entry)], outfile, bundle: true, platform: 'node', format: 'esm', packages: 'external', absWorkingDir: join(here, '..'), logLevel: 'silent' }) |
| 20 | return import(pathToFileURL(outfile).href) |
| 21 | } |
| 22 | ;({ remoteAdapter } = await bundle('../src/dsh/remote.ts', 'remote.mjs')) |
| 23 | ;({ ErrorCode } = await bundle('../src/protocol.ts', 'protocol.mjs')) |
| 24 | ;({ Context } = await import('@deepseek-ai/cordis')) |
| 25 | process.on('exit', () => rmSync(dir, { recursive: true, force: true })) |
| 26 | }) |
| 27 | |
| 28 | const settle = () => new Promise((resolve) => setTimeout(resolve, 0)) |
| 29 | |
| 30 | async function harness() { |
| 31 | const calls = [] |
| 32 | const host = { |
| 33 | tier: 'plugin', |
| 34 | rpc: { request: async () => { throw new Error('remote never calls the core directly') }, notify() {} }, |
| 35 | ownerOf: () => undefined, |
| 36 | liveInvocation: () => undefined, |
| 37 | broker: async (ctx, service, method, input) => { calls.push([service, method, input]); return { ok: method } }, |
| 38 | log() {}, |
| 39 | } |
| 40 | const ctx = new Context() |
| 41 | ctx.plugin(remoteAdapter.define(host)) |
| 42 | await settle() |
| 43 | return { ctx, calls } |
| 44 | } |
| 45 | |
| 46 | test('remote sends every method through the remote broker with a typed payload and nothing else', async () => { |
| 47 | const { ctx, calls } = await harness() |
| 48 | assert.deepEqual(await ctx.remote.status(), { ok: 'status' }) |
| 49 | await ctx.remote.pair() |
| 50 | await ctx.remote.pair({ label: 'phone' }) |
| 51 | await ctx.remote.clients() |
| 52 | await ctx.remote.revoke('cwrg_1') |
| 53 | assert.deepEqual(calls, [ |
| 54 | ['remote', 'status', {}], |
| 55 | ['remote', 'pair', {}], |
| 56 | ['remote', 'pair', { label: 'phone' }], |
| 57 | ['remote', 'clients', {}], |
| 58 | ['remote', 'revoke', { grant: 'cwrg_1' }], |
| 59 | ]) |
| 60 | }) |
| 61 | |
| 62 | test('remote refuses malformed arguments locally, before any broker call', async () => { |
| 63 | const { ctx, calls } = await harness() |
| 64 | assert.throws(() => ctx.remote.pair({ label: 3 }), (error) => error.code === ErrorCode.InvalidParams) |
| 65 | for (const id of [undefined, '', 7, 'x'.repeat(129)]) { |
| 66 | assert.throws(() => ctx.remote.revoke(id), (error) => error.code === ErrorCode.InvalidParams) |
| 67 | } |
| 68 | assert.equal(typeof ctx.remote.listen, 'undefined') |
| 69 | assert.deepEqual(calls, []) |
| 70 | }) |
| 71 | |
| 72 | test('a plugin may inject `remote` and may never provide it, in the shared scope or an isolated one', async (t) => { |
| 73 | const host = await startHost() |
| 74 | t.after(() => host.stop()) |
| 75 | const plugin = (source) => { |
| 76 | const dir = mkdtempSync(join(tmpdir(), 'cw-ext-host-remote-')) |
| 77 | t.after(() => rmSync(dir, { recursive: true, force: true })) |
| 78 | const entry = join(dir, 'index.mjs') |
| 79 | writeFileSync(entry, source) |
| 80 | return entry |
| 81 | } |
| 82 | const fake = `{ status() { return 'fake' }, pair() { return 'fake' }, clients() { return 'fake' }, revoke() { return 'fake' } }` |
| 83 | const providers = { |
| 84 | 'remote-root-provider': `ctx.root.provide('remote', ${fake})`, |
| 85 | 'remote-own-provider': `ctx.provide('remote', ${fake})`, |
| 86 | 'remote-isolated-provider': `ctx.isolate('remote').provide('remote', ${fake})`, |
| 87 | } |
| 88 | for (const [name, body] of Object.entries(providers)) { |
| 89 | const { result } = await activate(host, name, plugin(`export const inject = ['tools']\nexport function apply(ctx) { ${body} }\n`)) |
| 90 | assert.equal(result.status, 'failed', name) |
| 91 | assert.match(result.diagnostic, /may not provide core service `remote`/, name) |
| 92 | } |
| 93 | const user = await activate(host, 'remote-user', plugin(`export const inject = ['remote'] |
| 94 | export function apply(ctx) { |
| 95 | for (const method of ['status', 'pair', 'clients', 'revoke']) if (typeof ctx.remote[method] !== 'function') throw new Error('ctx.remote.' + method + ' is missing') |
| 96 | if (ctx.remote.constructor.name !== 'RemoteService') throw new Error('ctx.remote is not the host service') |
| 97 | } |
| 98 | `)) |
| 99 | assert.equal(user.result.status, 'ok', user.result.diagnostic) |
| 100 | }) |
| 101 |