| 1 | import assert from 'node:assert/strict' |
| 2 | import { mkdtempSync, readFileSync, rmSync } from 'node:fs' |
| 3 | import { build } from 'esbuild' |
| 4 | import { dirname, join } from 'node:path' |
| 5 | import { after, test } from 'node:test' |
| 6 | import { fileURLToPath, pathToFileURL } from 'node:url' |
| 7 | import { buildCatalog, parseSeatTable, renderCatalog } from '../scripts/gen-inspect-catalog.mjs' |
| 8 | import { createCordisInspect } from '../src/dsh/inspect.ts' |
| 9 | |
| 10 | const here = dirname(fileURLToPath(import.meta.url)) |
| 11 | const src = (path) => join(here, '..', 'src', path) |
| 12 | const text = (path) => readFileSync(path, 'utf8').replace(/\r\n/g, '\n') |
| 13 | const compatibility = JSON.parse(text(src('dsh/compatibility.json'))) |
| 14 | const catalog = JSON.parse(text(src('dsh/inspect-catalog.json'))) |
| 15 | const seatSource = text(join(here, '..', '..', '..', 'app-server', 'src', 'runtime_api', 'plugins.rs')) |
| 16 | const rootSource = text(src('root.ts')) |
| 17 | const inspect = createCordisInspect() |
| 18 | const ask = (platform, provider, method, input) => inspect.query(platform, provider, method, input) |
| 19 | |
| 20 | test('the committed catalog is what the generator writes now', () => { |
| 21 | assert.equal(text(src('dsh/inspect-catalog.json')), renderCatalog(buildCatalog())) |
| 22 | }) |
| 23 | |
| 24 | test('list names the providers DSH calls and leaves out Tool until a source exists', async () => { |
| 25 | const ids = inspect.list().map((p) => `${p.platform}.${p.id}.${p.methods.map((m) => m.name).join(',')}`) |
| 26 | assert.deepEqual(ids, ['host.Service.listService', 'host.Event.listEvents', 'host.Config.listConfigs', 'client.Slots.listSubTree', 'client.Theme.listTokens']) |
| 27 | const withTools = createCordisInspect({ tools: () => [{ name: 'read_file' }] }) |
| 28 | assert.ok(withTools.list().some((p) => p.id === 'Tool')) |
| 29 | assert.deepEqual(await withTools.query('host', 'Tool', 'listTools', undefined, {}), { tools: [{ name: 'read_file' }] }) |
| 30 | }) |
| 31 | |
| 32 | test('errors use DSH registry wording', async () => { |
| 33 | await assert.rejects(ask('host', 'Nope', 'x'), /Host Cordis inspect provider "Nope" is not registered/) |
| 34 | await assert.rejects(ask('client', 'Nope', 'x'), /Client Cordis inspect provider "Nope" is not registered/) |
| 35 | await assert.rejects(ask('host', 'Service', 'nope'), /Cordis inspect provider "Service" has no method "nope"/) |
| 36 | await assert.rejects(ask('host', 'Service', 'listService', { service: 3 }), /Host Cordis inspect Service.listService rejected input/) |
| 37 | await assert.rejects(ask('host', 'Tool', 'listTools'), /not registered/) |
| 38 | await assert.rejects(ask('page', 'Service', 'listService'), /platform must be/) |
| 39 | }) |
| 40 | |
| 41 | test('every service the catalog names is provided, and no provided service is missing', () => { |
| 42 | const provided = Object.values(compatibility.provided_services).flat().sort() |
| 43 | assert.deepEqual(catalog.host.services.map((s) => s.key).sort(), provided) |
| 44 | assert.deepEqual(catalog.host.refusedServices, compatibility.refused_services) |
| 45 | }) |
| 46 | |
| 47 | test('methods match the compatibility tables where it has them', () => { |
| 48 | const tables = { sessions: 'session_methods', userQuestions: 'user_question_methods', subagents: 'subagent_methods', agents: 'agents_methods', systemPrompt: 'system_prompt_methods' } |
| 49 | for (const [service, table] of Object.entries(tables)) { |
| 50 | const named = catalog.host.services.find((s) => s.key === service).methods.map((m) => m.name).sort() |
| 51 | assert.deepEqual(named, Object.keys(compatibility[table]).sort(), service) |
| 52 | } |
| 53 | }) |
| 54 | |
| 55 | const classBody = (source, name) => { |
| 56 | const start = source.indexOf(`class ${name} extends Service`) |
| 57 | assert.ok(start >= 0, `class ${name}`) |
| 58 | const rest = source.slice(start + 1) |
| 59 | const next = rest.search(/\n\s*(export )?(class |function )/) |
| 60 | return next < 0 ? rest : rest.slice(0, next) |
| 61 | } |
| 62 | const defines = (body, name) => new RegExp(`^\\s*(?:async\\s*(?:\\*\\s*)?|\\*\\s*)?${name}\\s*\\(`, 'm').test(body) |
| 63 | |
| 64 | test('every curated method is defined on the class that serves it', () => { |
| 65 | const served = { |
| 66 | tools: ['root.ts', 'ToolsShim'], commands: ['shims/commands.ts', 'CommandsShim'], storage: ['root.ts', 'StorageShim'], |
| 67 | skills: ['shims/skills.ts', 'SkillsShim'], prompt: ['shims/prompt.ts', 'PromptShim'], avatars: ['shims/avatars.ts', 'AvatarsShim'], |
| 68 | mcp: ['shims/mcp.ts', 'McpShim'], shellHooks: ['shims/shell-hooks.ts', 'ShellHooks'], remote: ['dsh/remote.ts', 'RemoteService'], |
| 69 | sessions: ['dsh/session-prompt.ts', 'SessionsService'], userQuestions: ['root.ts', 'UserQuestionsShim'], |
| 70 | subagents: ['dsh/agents.ts', 'SubagentsShim'], systemPrompt: ['dsh/session-prompt.ts', 'SystemPromptService'], |
| 71 | workspace: ['dsh/workspace.ts', 'WorkspaceService'], llm: ['dsh/llm.ts', 'LlmService'], |
| 72 | credentials: ['dsh/network.ts', 'CredentialsService'], settings: ['dsh/settings.ts', 'SettingsService'], |
| 73 | pluginManager: ['dsh/catalog.ts', 'PluginManagerService'], deepseekAccount: ['dsh/catalog.ts', 'DeepSeekAccountService'], |
| 74 | terminal: ['dsh/terminal.ts', 'TerminalService'], attachments: ['dsh/attachments.ts', 'AttachmentsService'], |
| 75 | agentPresets: ['dsh/host-services.ts', 'AgentPresetsService'], agentDefaultModel: ['dsh/host-services.ts', 'AgentDefaultModelService'], |
| 76 | sessionProjections: ['dsh/host-services.ts', 'SessionProjectionsService'], workspaceRegistry: ['dsh/host-services.ts', 'WorkspaceRegistryService'], |
| 77 | typertGateway: ['dsh/host-services.ts', 'TypertGatewayService'], web: ['dsh/host-services.ts', 'WebService'], |
| 78 | sessionPersistence: ['dsh/host-services.ts', 'SessionPersistenceService'], storageDomain: ['dsh/host-services.ts', 'StorageDomainService'], |
| 79 | invariants: ['dsh/host-services.ts', 'InvariantsService'], permissionPresets: ['dsh/host-services.ts', 'PermissionPresetsService'], |
| 80 | webServer: ['dsh/webserver.ts', 'WebServerService'], connection: ['dsh/webserver.ts', 'ConnectionService'], |
| 81 | sessionTitle: ['dsh/webserver.ts', 'SessionTitleService'], profileContext: ['dsh/webserver.ts', 'ProfileContextService'], |
| 82 | } |
| 83 | const cordis = { events: ['events', ['on', 'once', 'emit']], reflect: ['reflect', ['provide', 'get']], registry: ['registry', ['get', 'resolve']] } |
| 84 | const agentsSource = text(src('dsh/agents.ts')) |
| 85 | for (const service of catalog.host.services) { |
| 86 | if (service.key === 'logger' || service.key === 'agents') continue |
| 87 | if (cordis[service.key]) { |
| 88 | const [file, names] = cordis[service.key] |
| 89 | const body = text(join(here, '..', 'node_modules', '@deepseek-ai', 'cordis', 'lib', 'types', `${file}.d.ts`)) |
| 90 | assert.deepEqual(service.methods.map((m) => m.name).sort(), [...names].sort(), service.key) |
| 91 | for (const name of names) assert.ok(defines(body, name), `${service.key}.${name}`) |
| 92 | continue |
| 93 | } |
| 94 | const [file, cls] = served[service.key] |
| 95 | const body = classBody(text(src(file)), cls) |
| 96 | for (const m of service.methods) { |
| 97 | if (service.key === 'subagents' && m.name === 'result') assert.match(agentsSource, /^\s*get result\(\)/m, 'subagents.result is a getter') |
| 98 | else if (service.key === 'subagents' && m.name === 'dispose') assert.match(agentsSource, /^\s*dispose\(\)/m, 'subagents.dispose') |
| 99 | else if (service.key === 'connection' && (m.name === 'fetch.register' || m.name === 'rpc.handle')) assert.match(body, new RegExp(`^\\s*get\\s+${m.name.split('.')[0]}\\(\\)`, 'm'), `${service.key}.${m.name}`) |
| 100 | else if (service.key === 'profileContext' && (m.name === 'dir' || m.name === 'installAnchor')) assert.match(body, new RegExp(`^\\s*get\\s+${m.name}\\(\\)`, 'm'), `${service.key}.${m.name}`) |
| 101 | else if (service.key === 'permissionPresets' && (m.name === 'availability' || m.name === 'defaultPreset' || m.name === 'names')) assert.match(body, new RegExp(`^\\s*get\\s+${m.name}\\(\\)`, 'm'), `${service.key}.${m.name}`) |
| 102 | else assert.ok(defines(body, m.name), `${service.key}.${m.name}`) |
| 103 | } |
| 104 | } |
| 105 | assert.match(agentsSource, /^\s*section\(section: unknown\)/m) |
| 106 | assert.match(agentsSource, /^\s*getSectionOrder\(name: unknown\)/m) |
| 107 | }) |
| 108 | |
| 109 | test('the catalog states the contracts the host actually enforces', () => { |
| 110 | const agentsSource = text(src('dsh/agents.ts')) |
| 111 | const personaName = text(src('dsh/dsh-message-compat.js')).match(/PERSONA_PREFIX_SECTION = "([^"]+)"/)[1] |
| 112 | const section = catalog.host.services.find((s) => s.key === 'systemPrompt').methods[0] |
| 113 | assert.ok(section.signature.includes(`name: '${personaName}'`)) |
| 114 | assert.ok(section.signature.includes("getSectionOrder('DEPLOYMENT_PERSONA_PREFIX')")) |
| 115 | assert.match(agentsSource, /PERSONA_ORDER = 0/) |
| 116 | const subagents = catalog.host.services.find((s) => s.key === 'subagents').methods |
| 117 | const result = subagents.find((m) => m.name === 'result') |
| 118 | assert.match(result.signature, /^activation\.result: Promise<\{ stopReason: string, output: \{ type: 'text', text: string \}\[\]/) |
| 119 | assert.ok(!result.signature.includes('result()')) |
| 120 | assert.match(subagents.find((m) => m.name === 'startActivation').signature, /Promise<\{ childId: string, messageId: string, result: Promise</) |
| 121 | }) |
| 122 | |
| 123 | test('an output that is not JSON is refused as DSH refuses it', async () => { |
| 124 | const bad = createCordisInspect({ tools: () => [{ name: 'x', extra: undefined }] }) |
| 125 | await assert.rejects(bad.query('host', 'Tool', 'listTools', undefined, {}), /returned a non-JSON value/) |
| 126 | }) |
| 127 | |
| 128 | test('every slot and its seat equal the Engine seat table', () => { |
| 129 | const block = seatSource.slice(seatSource.indexOf('const DSH_SEAT_TABLE'), seatSource.indexOf('];', seatSource.indexOf('const DSH_SEAT_TABLE'))) |
| 130 | const names = [...block.matchAll(/\(\s*"([^"]+)",\s*DshSeat::/g)].map((m) => m[1]) |
| 131 | assert.equal(names.length, 41) |
| 132 | assert.deepEqual(catalog.client.slots.map((s) => s.slot), names) |
| 133 | const unsupported = [...block.matchAll(/\(\s*"([^"]+)",\s*DshSeat::Unsupported/g)].map((m) => m[1]) |
| 134 | assert.deepEqual(catalog.client.slots.filter((s) => !s.supported).map((s) => s.slot), unsupported) |
| 135 | const seats = [...seatSource.slice(seatSource.indexOf('const PLUGIN_UI_SEATS'), seatSource.indexOf('];', seatSource.indexOf('const PLUGIN_UI_SEATS'))).matchAll(/"([^"]+)"/g)].map((m) => m[1]) |
| 136 | for (const slot of catalog.client.slots.filter((s) => s.supported)) assert.ok(slot.seat === 'customize.plugins.detail' || seats.includes(slot.seat), slot.slot) |
| 137 | assert.equal(catalog.client.seats.length, 6) |
| 138 | assert.deepEqual(parseSeatTable(seatSource.replace(/\n/g, '\r\n')), parseSeatTable(seatSource)) |
| 139 | }) |
| 140 | |
| 141 | test('the refused events equal the list in root.ts', () => { |
| 142 | const body = rootSource.slice(rootSource.indexOf('unsupportedDshLifecycle = new Set(['), rootSource.indexOf('])', rootSource.indexOf('unsupportedDshLifecycle = new Set(['))) |
| 143 | const refused = [...body.matchAll(/'([^']+)'/g)].map((m) => m[1]) |
| 144 | assert.equal(refused.length, 5) |
| 145 | assert.deepEqual(catalog.host.events.filter((e) => e.status === 'refused').map((e) => e.name), refused) |
| 146 | for (const name of catalog.host.events.filter((e) => e.status === 'supported').map((e) => e.name)) assert.ok(rootSource.includes(`'${name}'`) || text(src('dsh/agents.ts')).includes(`'${name}'`), name) |
| 147 | }) |
| 148 | |
| 149 | test('the Service provider answers a directory, one contract, and refuses a name the host lacks', async () => { |
| 150 | const directory = await ask('host', 'Service', 'listService') |
| 151 | assert.equal(directory.services.length, 40) |
| 152 | assert.ok(!directory.services.some((s) => s.key === 'fs')) |
| 153 | const one = await ask('host', 'Service', 'listService', { service: 'prompt' }) |
| 154 | assert.equal(one.service.methods[0].name, 'registerSection') |
| 155 | await assert.rejects(ask('host', 'Service', 'listService', { service: 'fs' }), /not provided by this host/) |
| 156 | }) |
| 157 | |
| 158 | test('the Event provider separates supported from refused', async () => { |
| 159 | const { events } = await ask('host', 'Event', 'listEvents') |
| 160 | assert.deepEqual(events.map((e) => e.status), ['supported', 'supported', 'supported', 'supported', 'refused', 'refused', 'refused', 'refused', 'refused']) |
| 161 | assert.equal((await ask('host', 'Event', 'listEvents', { event: 'tools/pre-execute' })).event.name, 'tools/pre-execute') |
| 162 | await assert.rejects(ask('host', 'Event', 'listEvents', { event: 'subagent/start-x' }), /not known/) |
| 163 | }) |
| 164 | |
| 165 | test('the Config provider lists exactly the row packages, pages, and shows installed rows', async () => { |
| 166 | const page = await ask('host', 'Config', 'listConfigs') |
| 167 | assert.deepEqual(page.entries.map((e) => e.name).sort(), Object.keys(compatibility.row_bridges).sort()) |
| 168 | assert.equal(page.total, 14) |
| 169 | const paged = await ask('host', 'Config', 'listConfigs', { limit: 2 }) |
| 170 | assert.equal(paged.entries.length, 2) |
| 171 | assert.equal(paged.nextOffset, 2) |
| 172 | const schema = await ask('host', 'Config', 'listConfigs', { entry: 'row:@deepseek-ai/dsh-persona' }) |
| 173 | assert.deepEqual(schema.schema.required, ['prefix']) |
| 174 | await assert.rejects(ask('host', 'Config', 'listConfigs', { limit: 500 }), /limit/) |
| 175 | await assert.rejects(ask('host', 'Config', 'listConfigs', { entry: 'row:nope' }), /not listed/) |
| 176 | const live = createCordisInspect({ installed: () => [{ id: 'abc', name: 'hello', state: 'active' }] }) |
| 177 | const after = await live.query('host', 'Config', 'listConfigs', { name: 'hello' }) |
| 178 | assert.deepEqual(after.entries, [{ id: 'plugin:abc', name: 'hello', kind: 'installed-plugin', status: 'active' }]) |
| 179 | }) |
| 180 | |
| 181 | test('each row example passes the bridge that reads it', async () => { |
| 182 | const dir = mkdtempSync(join(here, '..', 'node_modules', '.cw-inspect-')) |
| 183 | after(() => rmSync(dir, { recursive: true, force: true })) |
| 184 | const bridges = join(dir, 'bridges.mjs') |
| 185 | await build({ |
| 186 | stdin: { contents: "export { normalizePersonaConfig } from './src/dsh/persona.ts'\nexport { reviewedMcpModule } from './src/dsh/mcp.ts'\nexport { reviewedSkillModule } from './src/dsh/skill-filesystem.ts'", resolveDir: join(here, '..'), loader: 'ts' }, |
| 187 | outfile: bridges, bundle: true, platform: 'node', format: 'esm', packages: 'external', absWorkingDir: join(here, '..'), logLevel: 'silent', |
| 188 | }) |
| 189 | const { normalizePersonaConfig, reviewedMcpModule, reviewedSkillModule } = await import(pathToFileURL(bridges).href) |
| 190 | const example = (name) => catalog.host.config.find((r) => r.name === name).example |
| 191 | assert.doesNotThrow(() => normalizePersonaConfig(example('@deepseek-ai/dsh-persona'))) |
| 192 | assert.throws(() => normalizePersonaConfig({ prefix: 'x', complete: true })) |
| 193 | const servers = [] |
| 194 | reviewedMcpModule().apply({ mcp: { registerServer: (d) => servers.push(d) } }, example('@deepseek-ai/dsh-mcp-client')) |
| 195 | assert.equal(servers[0].serverName, 'docs') |
| 196 | const roots = [] |
| 197 | reviewedSkillModule().apply({ skills: { registerRoot: (d) => roots.push(d) } }, example('@deepseek-ai/dsh-skill-filesystem')) |
| 198 | assert.equal(roots.length, 1) |
| 199 | }) |
| 200 | |
| 201 | test('Slots says no client is connected in a terminal and marks no slot drawn', async () => { |
| 202 | const out = await ask('client', 'Slots', 'listSubTree') |
| 203 | assert.equal(out.client.connected, false) |
| 204 | assert.match(out.client.message, /No client is connected/) |
| 205 | assert.equal(out.trees.length, 41) |
| 206 | assert.ok(out.trees.every((s) => s.drawn === false)) |
| 207 | assert.deepEqual((await ask('client', 'Slots', 'listSubTree', { root: 'conversation.composer.dock' })).selected.seat, 'conversation.composer.dock') |
| 208 | assert.equal((await ask('client', 'Slots', 'listSubTree', { root: 'nope' })).requestedRoot.available, false) |
| 209 | }) |
| 210 | |
| 211 | test('Slots marks only the drawn seats when a client is connected', async () => { |
| 212 | const connected = createCordisInspect({ client: () => ({ connected: true }) }) |
| 213 | const out = await connected.query('client', 'Slots', 'listSubTree') |
| 214 | const drawn = out.trees.filter((s) => s.drawn).map((s) => s.name) |
| 215 | assert.deepEqual(drawn, ['plugins.bundle.config', 'plugins.row.config', 'plugins.tab', 'plugins.detail.section', 'plugins.item', 'plugins.detail.actions', 'plugins.detail.badge', 'settings.section', 'settings.section.icon', 'settings.plugin.item', 'settings.general.item', 'settings.plugins.tab', 'web-ui.plugin.item']) |
| 216 | const all = createCordisInspect({ client: () => ({ connected: true, drawnSeats: ['conversation.composer.dock'] }) }) |
| 217 | const composer = (await all.query('client', 'Slots', 'listSubTree')).trees.filter((s) => s.drawn).map((s) => s.name) |
| 218 | assert.ok(composer.includes('conversation.input.dock') && !composer.includes('plugins.tab')) |
| 219 | assert.ok((await ask('client', 'Slots', 'listSubTree')).trees.filter((s) => !s.supported).every((s) => s.seat === null && s.reason)) |
| 220 | }) |
| 221 | |
| 222 | test('Theme reports that the frame defines no named tokens', async () => { |
| 223 | const out = await ask('client', 'Theme', 'listTokens') |
| 224 | assert.deepEqual(out.tokens, []) |
| 225 | assert.deepEqual(out.colorSchemes, ['light', 'dark']) |
| 226 | assert.ok(out.api.some((a) => a.name === 'ctx.theme.overrideTokens')) |
| 227 | }) |
| 228 | |
| 229 | test('Tool.listTools description says name, not schema', () => { |
| 230 | const withTools = createCordisInspect({ tools: () => [{ name: 'read_file' }] }) |
| 231 | const toolProvider = withTools.list().find((p) => p.id === 'Tool') |
| 232 | const listTools = toolProvider.methods.find((m) => m.name === 'listTools') |
| 233 | assert.equal(listTools.description, 'Return the name of every tool currently callable by this agent.') |
| 234 | }) |
| 235 |