Add @deepseek-ai/dsh-experimental-claude-code-mods, an alpha bridge that loads Claude Code mods (plugin directories whose hooks/hooks.json names a hooks module), runs register(on, options) unchanged, and raises the mod events from harness extension points: session.start on agent/created, prompt.submit and turn.start on agent/pre-step, tool.call around tools/execute, turn.complete on turn/end, session.end on agent/disposed, and command.run from registered commands. The $ mods API is served over composed services (commands, tools, user-questions, fs, subprocess, storage domain, projections); unserved members fail by name. Includes a middleware chain with Claude Code's failure rules and budget clock, a claude-code/testing-style test kit, fixtures from the mods tutorial, full-loop and Loader composition tests, the attribution-only claude-code-mods message source with its persistence record, and the regenerated catalogs.
179 lines
8.5 KiB
TypeScript
179 lines
8.5 KiB
TypeScript
import { resolve } from 'node:path'
|
|
import { describe, expect, it } from 'vitest'
|
|
import { createModTestKit, mock } from '../src/testing.ts'
|
|
import type { ModsApi } from '../src/types.ts'
|
|
|
|
const FIXTURES = resolve(import.meta.dirname, 'fixtures')
|
|
|
|
describe('createModTestKit: the tutorial mod', () => {
|
|
it('/tally reports the tool calls the mod has seen (the test from Claude Code\'s "Test a mod" page)', async () => {
|
|
const kit = await createModTestKit({ dirs: ['first-mod'], cwd: FIXTURES })
|
|
try {
|
|
// Answer each tool call in Claude Code's place, so no tool runs
|
|
kit.on('tool.call', () => ({ result: 'ok' }))
|
|
|
|
// Raise two tool calls, which the mod's tool.call hook counts
|
|
await kit.$.tool.call({ tool: 'Bash', command: 'ls' })
|
|
await kit.$.tool.call({ tool: 'Read', file_path: 'README.md' })
|
|
|
|
// Run /tally and check the text its hook returns
|
|
const answer = await kit.$.command.run({ command: 'tally', args: '' })
|
|
expect(answer.text).toBe('Claude has made 2 tool calls since this mod loaded')
|
|
expect(kit.mods.map(mod => mod.name)).toEqual(['first-mod'])
|
|
expect(kit.reports).toEqual([])
|
|
} finally {
|
|
kit.dispose()
|
|
}
|
|
})
|
|
|
|
it('passes configured options to register and answers session.start by default', async () => {
|
|
const kit = await createModTestKit({ dirs: [resolve(FIXTURES, 'first-mod')], options: { 'first-mod': { greeting: 'The model made' } } })
|
|
try {
|
|
const registered: unknown[] = []
|
|
kit.on('command.register', (_$, e) => {
|
|
registered.push(e)
|
|
return { value: undefined }
|
|
})
|
|
expect(await kit.$.session.start({ cwd: '/work', surface: null, isInteractive: true })).toEqual({ cwd: '/work' })
|
|
expect(registered).toEqual([{ name: 'tally', description: 'Show how many tool calls Claude has made' }])
|
|
expect((await kit.$.command.run({ command: 'tally', args: '' })).text).toBe('The model made 0 tool calls since this mod loaded')
|
|
expect(await kit.$.command.run({ command: 'other', args: '' })).toEqual({})
|
|
} finally {
|
|
kit.dispose()
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('createModTestKit: stubs, defaults, and inline mods', () => {
|
|
it('keeps $.state for the test, reports a fire-and-forget call nobody answers, and refuses $.ui.resolve', async () => {
|
|
const kit = await createModTestKit({ modules: [{
|
|
name: 'stateful',
|
|
register(on) {
|
|
on('turn.start', async ($, e, next) => {
|
|
const { value = 0 } = await $.state.get({ plugin: 'stateful', key: 'turns' }) as { value?: number }
|
|
await $.state.set({ plugin: 'stateful', key: 'turns' }, value + 1)
|
|
$.ui.toast(`turn ${value + 1}`, { timeoutMs: 10 })
|
|
let resolved = 'resolved'
|
|
try {
|
|
$.ui.resolve(e)
|
|
} catch (error) {
|
|
resolved = (error as Error).message
|
|
}
|
|
return { ...(await next(e) as object), turns: value + 1, resolved }
|
|
})
|
|
},
|
|
}] })
|
|
try {
|
|
expect(await kit.$.turn.start({ turnId: '1', text: 'a' })).toEqual({ turnId: '1', turns: 1, resolved: 'stateful: $.ui.resolve has no element table: this bridge raises no ui.render events' })
|
|
expect(await kit.$.turn.start({ turnId: '2', text: 'b' })).toMatchObject({ turns: 2 })
|
|
await new Promise(resolve => setTimeout(resolve, 0))
|
|
expect(kit.reports).toEqual(['stateful: $.ui.toast failed: no implementation for ui.toast', 'stateful: $.ui.toast failed: no implementation for ui.toast'])
|
|
} finally {
|
|
kit.dispose()
|
|
}
|
|
})
|
|
|
|
it('requires a stub for tool.call and reports a mods API call nobody answers', async () => {
|
|
const kit = await createModTestKit({ modules: [{
|
|
name: 'reader',
|
|
register(on) {
|
|
on('tool.call', async ($, e, next) => {
|
|
await $.store.set('last', (e as { tool: string }).tool)
|
|
return next(e)
|
|
})
|
|
},
|
|
}] })
|
|
try {
|
|
await expect(kit.$.tool.call({ tool: 'Bash', command: 'ls' })).rejects.toThrow('no implementation for tool.call')
|
|
expect(kit.reports).toEqual(['reader: tool.call hook skipped: threw Error: no implementation for store.set'])
|
|
} finally {
|
|
kit.dispose()
|
|
}
|
|
})
|
|
|
|
it('answers mods API calls from stubs returning { value } or { deny }, and the kit answers ui.invalidate itself', async () => {
|
|
const kit = await createModTestKit({ modules: [{
|
|
name: 'grader',
|
|
register(on) {
|
|
on('command.run', { command: 'grade' }, async ($, e) => {
|
|
$.ui.invalidate('ui.render')
|
|
const reply = await $.fs.read((e as { args: string }).args)
|
|
.then(text => ({ ok: true, text }), (error: unknown) => ({ ok: false, text: (error as Error).message }))
|
|
return { text: reply.ok ? 'Passed: ' + reply.text : 'Try again: ' + reply.text }
|
|
})
|
|
},
|
|
}] })
|
|
try {
|
|
kit.on('fs.read', (_$, e: { path: string }) => ({ value: e.path.endsWith('grade.txt') ? 'PASS' : '' }))
|
|
expect((await kit.$.command.run({ command: 'grade', args: 'grade.txt' })).text).toBe('Passed: PASS')
|
|
kit.on('fs.read', () => ({ deny: 'no files in tests' }))
|
|
expect((await kit.$.command.run({ command: 'grade', args: 'x' })).text).toBe('Try again: fs.read refused: no files in tests')
|
|
kit.on('fs.read', () => 'bare value')
|
|
expect((await kit.$.command.run({ command: 'grade', args: 'x' })).text).toMatch(/returned neither \{ value \} nor \{ deny \}/)
|
|
kit.on('fs.read', () => ({ nothing: true }))
|
|
expect((await kit.$.command.run({ command: 'grade', args: 'x' })).text).toMatch(/returned neither \{ value \} nor \{ deny \}/)
|
|
} finally {
|
|
kit.dispose()
|
|
}
|
|
})
|
|
|
|
it('mock.store and mock.env answer whole namespaces from memory', async () => {
|
|
const kit = await createModTestKit({ modules: [{
|
|
name: 'notes',
|
|
options: { prefix: 'note:' },
|
|
async register(on, options) {
|
|
on('session.start', async ($, e, next) => {
|
|
const saved = await $.store.get('count')
|
|
await $.store.set('count', (typeof saved === 'number' ? saved : 0) + 1)
|
|
await $.store.set('prefix', options.prefix)
|
|
await $.store.delete('stale')
|
|
await $.env.set('NOTES_HOME', await $.env.get('HOME_DIR') ?? 'unset')
|
|
await $.env.set('GONE', undefined)
|
|
return { ...(await next(e) as object), keys: await $.store.keys() }
|
|
})
|
|
},
|
|
}] })
|
|
try {
|
|
const on = kit.on.bind(kit)
|
|
const saved = mock.store(on, { count: 7, stale: true })
|
|
const env = mock.env(on, { HOME_DIR: '/home/me', GONE: 'yes' })
|
|
const result = await kit.$.session.start({ cwd: '/work', surface: null, isInteractive: false })
|
|
expect(result).toEqual({ cwd: '/work', keys: ['count', 'prefix'] })
|
|
expect(saved.get('count')).toBe(8)
|
|
expect(saved.get('prefix')).toBe('note:')
|
|
expect(env).toEqual({ HOME_DIR: '/home/me', NOTES_HOME: '/home/me', GONE: undefined })
|
|
} finally {
|
|
kit.dispose()
|
|
}
|
|
})
|
|
|
|
it('raises the other bridged events with their default answers and lets a stub replace them', async () => {
|
|
const seen: string[] = []
|
|
const kit = await createModTestKit({ modules: [{
|
|
name: 'observer',
|
|
register(on) {
|
|
on('*', (_$: ModsApi, e, next) => {
|
|
const fields = e as { turnId?: string; text?: string; sessionId?: string }
|
|
seen.push(String(fields.turnId ?? fields.text ?? fields.sessionId))
|
|
return next(e)
|
|
})
|
|
},
|
|
}] })
|
|
try {
|
|
expect(await kit.$.turn.start({ turnId: '1', text: 'go' })).toEqual({ turnId: '1' })
|
|
expect(await kit.$.turn.complete({ turnId: '1', answer: 'done', durationMs: 5, isAborted: false, reason: 'answer' })).toEqual({ text: '' })
|
|
expect(await kit.$.prompt.submit({ text: 'hello', context: ['extra'] })).toEqual({ text: 'hello', context: ['extra'] })
|
|
expect(await kit.$.prompt.submit({ text: 'plain' })).toEqual({ text: 'plain' })
|
|
expect(await kit.$.session.end({ reason: 'other', sessionId: 's1' })).toEqual({ sessionId: 's1' })
|
|
await expect(kit.raise('agent.spawn', { prompt: 'x' })).rejects.toThrow('no implementation for agent.spawn')
|
|
kit.on('turn.complete', () => ({ text: 'from stub' }))
|
|
expect(await kit.$.turn.complete({ turnId: '2', answer: '', durationMs: 0, isAborted: true, reason: 'aborted' })).toEqual({ text: 'from stub' })
|
|
kit.on('turn.complete', () => undefined)
|
|
await expect(kit.$.turn.complete({ turnId: '3', answer: '', durationMs: 0, isAborted: false, reason: 'answer' })).rejects.toThrow('the stub returned no result')
|
|
expect(seen).toEqual(['1', '1', 'hello', 'plain', 's1', 'undefined', '2', '3'])
|
|
} finally {
|
|
kit.dispose()
|
|
}
|
|
})
|
|
})
|