| 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 | const rpc = { request: async () => ({}), notify: () => {}, tier: 'plugin' } |
| 10 | let HostRoot |
| 11 | let Service |
| 12 | let DSH_ADAPTERS |
| 13 | |
| 14 | before(async () => { |
| 15 | const dir = mkdtempSync(join(here, '..', 'node_modules', '.cw-adapters-')) |
| 16 | const outfile = join(dir, 'root.mjs') |
| 17 | await build({ |
| 18 | entryPoints: [join(here, '../src/root.ts')], |
| 19 | outfile, |
| 20 | bundle: true, |
| 21 | platform: 'node', |
| 22 | format: 'esm', |
| 23 | packages: 'external', |
| 24 | absWorkingDir: join(here, '..'), |
| 25 | logLevel: 'silent', |
| 26 | }) |
| 27 | const adapters = join(dir, 'adapters.mjs') |
| 28 | await build({ |
| 29 | entryPoints: [join(here, '../src/dsh/adapters.ts')], |
| 30 | outfile: adapters, |
| 31 | bundle: true, |
| 32 | platform: 'node', |
| 33 | format: 'esm', |
| 34 | packages: 'external', |
| 35 | absWorkingDir: join(here, '..'), |
| 36 | logLevel: 'silent', |
| 37 | }) |
| 38 | ;({ HostRoot } = await import(pathToFileURL(outfile).href)) |
| 39 | ;({ DSH_ADAPTERS } = await import(pathToFileURL(adapters).href)) |
| 40 | ;({ Service } = await import('@deepseek-ai/cordis')) |
| 41 | process.on('exit', () => rmSync(dir, { recursive: true, force: true })) |
| 42 | }) |
| 43 | |
| 44 | const adapterFor = (service) => ({ service, define: () => class extends Service { constructor(ctx) { super(ctx, service) } } }) |
| 45 | |
| 46 | test('the shipped adapter table holds the reviewed adapters, and the root still builds', () => { |
| 47 | assert.deepEqual(DSH_ADAPTERS.map((adapter) => adapter.service), [ |
| 48 | 'sessions', |
| 49 | 'systemPrompt', |
| 50 | 'subagents', |
| 51 | 'agents', |
| 52 | 'remote', |
| 53 | 'workspace', |
| 54 | 'llm', |
| 55 | 'credentials', |
| 56 | 'settings', |
| 57 | 'pluginManager', |
| 58 | 'deepseekAccount', |
| 59 | 'terminal', |
| 60 | 'attachments', |
| 61 | 'agentPresets', |
| 62 | 'agentDefaultModel', |
| 63 | 'sessionProjections', |
| 64 | 'workspaceRegistry', |
| 65 | 'typertGateway', |
| 66 | 'web', |
| 67 | 'sessionPersistence', |
| 68 | 'storageDomain', |
| 69 | 'invariants', |
| 70 | 'permissionPresets', |
| 71 | 'webServer', |
| 72 | 'connection', |
| 73 | 'sessionTitle', |
| 74 | 'profileContext', |
| 75 | ]) |
| 76 | assert.ok(new HostRoot(rpc, 'plugin')) |
| 77 | assert.ok(new HostRoot(rpc, 'builtin')) |
| 78 | }) |
| 79 | |
| 80 | test('an adapter for a service compatibility.json does not declare is refused at construction', () => { |
| 81 | assert.throws(() => new HostRoot(rpc, 'plugin', [...DSH_ADAPTERS, adapterFor('fs')]), /fs is installed but not declared in compatibility\.json/) |
| 82 | }) |
| 83 | |
| 84 | test('an adapter may not replace a service the root already provides', () => { |
| 85 | assert.throws(() => new HostRoot(rpc, 'plugin', [adapterFor('storage')]), /duplicates a service the host root already provides/) |
| 86 | assert.throws(() => new HostRoot(rpc, 'plugin', [...DSH_ADAPTERS, adapterFor('sessions')]), /duplicates a service/) |
| 87 | }) |
| 88 | |
| 89 | test('the adapter broker refuses locally, without sending, when no live invocation holds a ticket for the service', async () => { |
| 90 | const sent = [] |
| 91 | const peer = { ...rpc, request: async (method, params) => { sent.push([method, params]); return {} } } |
| 92 | let host |
| 93 | const adapters = DSH_ADAPTERS.map((adapter) => |
| 94 | adapter.service === 'llm' |
| 95 | ? { service: 'llm', define: (adapterHost) => { host = adapterHost; return adapter.define(adapterHost) } } |
| 96 | : adapter, |
| 97 | ) |
| 98 | assert.ok(new HostRoot(peer, 'plugin', adapters)) |
| 99 | for (const service of ['workspace', 'llm', 'vault']) { |
| 100 | await assert.rejects(host.broker({}, service, 'read', {}), (error) => error.code === -32001 && /broker is only available to a live tool invocation/.test(error.message)) |
| 101 | } |
| 102 | assert.deepEqual(sent, []) |
| 103 | }) |
| 104 |