fix(hooks): share matcher validation instances

This commit is contained in:
ZiyaZhang
2026-07-28 20:10:21 -07:00
parent 774755ef2d
commit ec72d0b57e
17 changed files with 221 additions and 128 deletions

View File

@@ -1,5 +1,14 @@
import { describe, expect, it } from 'vitest'
import { parseClaudeConfig, substituteCommand } from '@deepseek-ai/dsh-hooks-claude/src/config.ts'
import { afterEach, describe, expect, it } from 'vitest'
import { parseClaudeConfig as parseRawClaudeConfig, substituteCommand } from '@deepseek-ai/dsh-hooks-claude/src/config.ts'
const matcherSets: Array<ReturnType<typeof parseRawClaudeConfig>['matchers']> = []
afterEach(() => { for (const matchers of matcherSets.splice(0)) matchers.dispose() })
function parseClaudeConfig(...args: Parameters<typeof parseRawClaudeConfig>): ReturnType<typeof parseRawClaudeConfig> {
const result = parseRawClaudeConfig(...args)
matcherSets.push(result.matchers)
return result
}
describe('substituteCommand', () => {
it('replaces CLAUDE_PLUGIN_ROOT and CLAUDE_PROJECT_DIR (all occurrences)', () => {
@@ -64,6 +73,13 @@ describe('parseClaudeConfig', () => {
expect('matcher' in config.Stop![0]!).toBe(false)
})
it('returns the same validated matcher registry for runtime use', () => {
const { matchers } = parseClaudeConfig({
PreToolUse: [{ matcher: '^Bash$', hooks: [{ type: 'command', command: 'x.sh' }] }],
})
expect(matchers.matches('^Bash$', 'Bash')).toBe(true)
})
it('rejects an invalid regex matcher with its event name', () => {
expect(() => parseClaudeConfig({
PreToolUse: [{ matcher: '(', hooks: [{ type: 'command', command: 'x.sh' }] }],