config.spec.ts 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { describe, expect, it } from 'vitest'
  2. import { parseCodexConfig, CODEX_EVENTS } from '@deepseek-ai/dsh-hooks-codex/src/config.ts'
  3. describe('parseCodexConfig', () => {
  4. it('honors only the five bridge-supported Codex events, dropping the rest', () => {
  5. const { config } = parseCodexConfig({
  6. PreToolUse: [{ hooks: [{ type: 'command', command: 'a.sh' }] }],
  7. SubagentStop: [{ hooks: [{ type: 'command', command: 'b.sh' }] }], // current Codex event, unsupported by this bridge
  8. Notification: [{ hooks: [{ type: 'command', command: 'c.sh' }] }], // unknown to current Codex
  9. })
  10. expect(Object.keys(config)).toEqual(['PreToolUse'])
  11. expect(CODEX_EVENTS).toContain('PreToolUse')
  12. expect(CODEX_EVENTS).not.toContain('SubagentStop' as never)
  13. })
  14. it('accepts both timeout and the timeoutSec alias, no substitution', () => {
  15. const { config } = parseCodexConfig({
  16. Stop: [{ hooks: [{ type: 'command', command: '${NOT_SUBSTITUTED}/s.sh', timeout: 10 }] }],
  17. UserPromptSubmit: [{ hooks: [{ type: 'command', command: 'u.sh', timeoutSec: 20 }] }],
  18. })
  19. // The parser performs no config-time substitution; shell expansion happens later.
  20. expect(config.Stop).toEqual([{ hooks: [{ command: '${NOT_SUBSTITUTED}/s.sh', timeoutSec: 10 }] }])
  21. expect(config.UserPromptSubmit).toEqual([{ hooks: [{ command: 'u.sh', timeoutSec: 20 }] }])
  22. })
  23. it('skips non-command and async:true hooks (recorded)', () => {
  24. const { config, skipped } = parseCodexConfig({
  25. PreToolUse: [{ hooks: [
  26. { type: 'prompt' },
  27. { type: 'command', command: 'sync.sh' },
  28. { type: 'command', command: 'bg.sh', async: true },
  29. ] }],
  30. })
  31. expect(config.PreToolUse).toEqual([{ hooks: [{ command: 'sync.sh' }] }])
  32. expect(skipped).toEqual([{ event: 'PreToolUse', reason: 'unsupported "prompt" hook' }, { event: 'PreToolUse', reason: 'async hook' }])
  33. })
  34. it('parses the { hooks: … } wrapper and the bare map identically', () => {
  35. const groups = { Stop: [{ hooks: [{ type: 'command', command: 's.sh' }] }] }
  36. expect(parseCodexConfig(groups).config).toEqual(parseCodexConfig({ hooks: groups }).config)
  37. })
  38. it('drops malformed entries and a non-object top level without throwing', () => {
  39. expect(parseCodexConfig(null).config).toEqual({})
  40. expect(parseCodexConfig({ PreToolUse: 'no' }).config).toEqual({})
  41. expect(parseCodexConfig({ Stop: [7, { hooks: 'x' }, { hooks: [{ type: 'command', command: 9 }] }] }).config).toEqual({})
  42. })
  43. it('skips a non-object element inside a hooks array, keeping the valid sibling', () => {
  44. const { config } = parseCodexConfig({ Stop: [{ hooks: [null, 7, { type: 'command', command: 's.sh' }] }] })
  45. expect(config.Stop).toEqual([{ hooks: [{ command: 's.sh' }] }])
  46. })
  47. it('treats a hook with no `type` field as a command (the default)', () => {
  48. const { config } = parseCodexConfig({ Stop: [{ hooks: [{ command: 's.sh' }] }] })
  49. expect(config.Stop).toEqual([{ hooks: [{ command: 's.sh' }] }])
  50. })
  51. it('omits the matcher key for a match-all group', () => {
  52. const { config } = parseCodexConfig({ Stop: [{ hooks: [{ type: 'command', command: 's.sh' }] }] })
  53. expect('matcher' in config.Stop![0]!).toBe(false)
  54. })
  55. it('keeps a matcher when present', () => {
  56. const { config } = parseCodexConfig({ PreToolUse: [{ matcher: '^Bash$', hooks: [{ type: 'command', command: 'b.sh' }] }] })
  57. expect(config.PreToolUse![0]!.matcher).toBe('^Bash$')
  58. })
  59. it('rejects an invalid regex matcher with its event name', () => {
  60. expect(() => parseCodexConfig({
  61. PreToolUse: [{ matcher: '[', hooks: [{ type: 'command', command: 's.sh' }] }],
  62. })).toThrow('invalid codex regex matcher "[" on event "PreToolUse"')
  63. })
  64. it('discards matcher fields on events without matcher subjects before validation', () => {
  65. const { config } = parseCodexConfig({
  66. UserPromptSubmit: [{ matcher: '[', hooks: [{ type: 'command', command: 'prompt.sh' }] }],
  67. Stop: [{ matcher: '(', hooks: [{ type: 'command', command: 'stop.sh' }] }],
  68. })
  69. expect(config).toEqual({
  70. UserPromptSubmit: [{ hooks: [{ command: 'prompt.sh' }] }],
  71. Stop: [{ hooks: [{ command: 'stop.sh' }] }],
  72. })
  73. })
  74. })