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 { parseCodexConfig, CODEX_EVENTS } from '@deepseek-ai/dsh-hooks-codex/src/config.ts'
import { afterEach, describe, expect, it } from 'vitest'
import { parseCodexConfig as parseRawCodexConfig, CODEX_EVENTS } from '@deepseek-ai/dsh-hooks-codex/src/config.ts'
const matcherSets: Array<ReturnType<typeof parseRawCodexConfig>['matchers']> = []
afterEach(() => { for (const matchers of matcherSets.splice(0)) matchers.dispose() })
function parseCodexConfig(...args: Parameters<typeof parseRawCodexConfig>): ReturnType<typeof parseRawCodexConfig> {
const result = parseRawCodexConfig(...args)
matcherSets.push(result.matchers)
return result
}
describe('parseCodexConfig', () => {
it('honors only the five bridge-supported Codex events, dropping the rest', () => {
@@ -62,8 +71,9 @@ describe('parseCodexConfig', () => {
})
it('keeps a valid Rust-regex matcher when present', () => {
const { config } = parseCodexConfig({ PreToolUse: [{ matcher: '(?i)^bash$', hooks: [{ type: 'command', command: 'b.sh' }] }] })
const { config, matchers } = parseCodexConfig({ PreToolUse: [{ matcher: '(?i)^bash$', hooks: [{ type: 'command', command: 'b.sh' }] }] })
expect(config.PreToolUse![0]!.matcher).toBe('(?i)^bash$')
expect(matchers.matches('(?i)^bash$', 'BASH')).toBe(true)
})
it('rejects an invalid regex matcher with its event name', () => {

View File

@@ -9,6 +9,7 @@ import LocalSubprocessService from '@deepseek-ai/dsh-subprocess-local'
const matcherLifecycle = vi.hoisted(() => {
const registry = {
matches: vi.fn(() => true),
diagnostic: vi.fn<(matcher: string | undefined) => string | undefined>(() => undefined),
dispose: vi.fn<() => void>(),
}
return {
@@ -26,9 +27,30 @@ const dirs: string[] = []
afterEach(() => {
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true })
vi.clearAllMocks()
matcherLifecycle.registry.diagnostic.mockReturnValue(undefined)
})
describe('hooks-codex matcher lifecycle', () => {
it('disposes the compiled set when one event-specific diagnostic rejects the config', async () => {
const { parseCodexConfig } = await import('@deepseek-ai/dsh-hooks-codex/src/config.ts')
matcherLifecycle.registry.diagnostic.mockImplementation((matcher: string | undefined) => (
matcher === '[' ? 'invalid codex regex matcher "["' : undefined
))
expect(() => parseCodexConfig({
PreToolUse: [
{ matcher: '(?i)^bash$', hooks: [{ type: 'command', command: 'first' }] },
{ matcher: '[', hooks: [{ type: 'command', command: 'second' }] },
],
})).toThrow('invalid codex regex matcher "[" on event "PreToolUse"')
expect(matcherLifecycle.compileMatchers).toHaveBeenCalledExactlyOnceWith(
new Set(['(?i)^bash$', '[']),
'codex',
)
expect(matcherLifecycle.registry.dispose).toHaveBeenCalledOnce()
})
it('gives the loaded config one matcher registry and disposes it on plugin teardown', async () => {
const dir = mkdtempSync(join(tmpdir(), 'dsh-hooks-codex-matchers-'))
dirs.push(dir)
@@ -44,10 +66,10 @@ describe('hooks-codex matcher lifecycle', () => {
await ctx.plugin(LocalBashExecutor, { timeoutMs: 10_000 })
const fiber = await ctx.plugin(HooksCodex, { configPath, model: 'm' })
expect(matcherLifecycle.compileMatchers).toHaveBeenCalledExactlyOnceWith([
expect(matcherLifecycle.compileMatchers).toHaveBeenCalledExactlyOnceWith(new Set([
'(?i)^bash$',
'(?i)^bash$',
], 'codex')
]), 'codex')
expect(matcherLifecycle.registry.diagnostic).toHaveBeenCalledTimes(2)
expect(matcherLifecycle.registry.dispose).not.toHaveBeenCalled()
await fiber.dispose()