config.spec.ts 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { describe, expect, it } from 'vitest'
  2. import { parseClaudeCodeConfig, substituteCommand } from '@deepseek-ai/dsh-hooks-claude-code/src/config.ts'
  3. describe('substituteCommand', () => {
  4. it('replaces CLAUDE_PLUGIN_ROOT and CLAUDE_PROJECT_DIR (all occurrences)', () => {
  5. expect(substituteCommand('${CLAUDE_PLUGIN_ROOT}/x.sh', { pluginRoot: '/p' })).toBe('/p/x.sh')
  6. expect(substituteCommand('${CLAUDE_PROJECT_DIR}/a ${CLAUDE_PROJECT_DIR}/b', { projectDir: '/proj' })).toBe('/proj/a /proj/b')
  7. expect(substituteCommand('${CLAUDE_PLUGIN_ROOT}-${CLAUDE_PROJECT_DIR}', { pluginRoot: '/p', projectDir: '/d' })).toBe('/p-/d')
  8. })
  9. it('leaves the command untouched when no vars are supplied', () => {
  10. expect(substituteCommand('${CLAUDE_PLUGIN_ROOT}/x', {})).toBe('${CLAUDE_PLUGIN_ROOT}/x')
  11. })
  12. })
  13. describe('parseClaudeCodeConfig', () => {
  14. it('parses a bare event map and a settings-style { hooks: … } wrapper identically', () => {
  15. const groups = { PreToolUse: [{ matcher: 'Bash', hooks: [{ type: 'command', command: 'x.sh' }] }] }
  16. const bare = parseClaudeCodeConfig(groups)
  17. const wrapped = parseClaudeCodeConfig({ hooks: groups })
  18. expect(bare.config).toEqual(wrapped.config)
  19. expect(bare.config.PreToolUse).toEqual([{ matcher: 'Bash', hooks: [{ command: 'x.sh' }] }])
  20. })
  21. it('carries timeout → timeoutSec and substitutes the command', () => {
  22. const { config } = parseClaudeCodeConfig(
  23. { Stop: [{ hooks: [{ type: 'command', command: '${CLAUDE_PLUGIN_ROOT}/s.sh', timeout: 30 }] }] },
  24. { pluginRoot: '/p' },
  25. )
  26. expect(config.Stop).toEqual([{ hooks: [{ command: '/p/s.sh', timeoutSec: 30 }] }])
  27. })
  28. it('skips non-command hooks (recorded) and keeps the command ones in the same group', () => {
  29. const { config, skipped } = parseClaudeCodeConfig({
  30. PreToolUse: [{ hooks: [
  31. { type: 'prompt', prompt: 'hi' },
  32. { type: 'command', command: 'ok.sh' },
  33. { type: 'http', url: 'http://x' },
  34. ] }],
  35. })
  36. expect(config.PreToolUse).toEqual([{ hooks: [{ command: 'ok.sh' }] }])
  37. expect(skipped).toEqual([{ event: 'PreToolUse', type: 'prompt' }, { event: 'PreToolUse', type: 'http' }])
  38. })
  39. it('treats a hook with no `type` as a command (CC default)', () => {
  40. const { config } = parseClaudeCodeConfig({ Stop: [{ hooks: [{ command: 'd.sh' }] }] })
  41. expect(config.Stop).toEqual([{ hooks: [{ command: 'd.sh' }] }])
  42. })
  43. it('drops malformed entries without throwing: non-array groups, non-object group/hook, missing command, empty groups', () => {
  44. expect(parseClaudeCodeConfig({ PreToolUse: 'nope' }).config).toEqual({})
  45. expect(parseClaudeCodeConfig({ PreToolUse: [42, { hooks: 'no' }, { hooks: [7, { type: 'command' }] }] }).config).toEqual({})
  46. // a group whose only hook lacks a command string drops the whole (empty) group
  47. expect(parseClaudeCodeConfig({ Stop: [{ hooks: [{ type: 'command', command: 5 }] }] }).config).toEqual({})
  48. })
  49. it('returns empty for a non-object / null / array top level', () => {
  50. expect(parseClaudeCodeConfig(null).config).toEqual({})
  51. expect(parseClaudeCodeConfig(42).config).toEqual({})
  52. expect(parseClaudeCodeConfig([1, 2]).config).toEqual({})
  53. })
  54. it('omits the matcher key when the group has none (match-all)', () => {
  55. const { config } = parseClaudeCodeConfig({ Stop: [{ hooks: [{ type: 'command', command: 's.sh' }] }] })
  56. expect('matcher' in config.Stop![0]!).toBe(false)
  57. })
  58. it('rejects an invalid regex matcher with its event name', () => {
  59. expect(() => parseClaudeCodeConfig({
  60. PreToolUse: [{ matcher: '(', hooks: [{ type: 'command', command: 'x.sh' }] }],
  61. })).toThrow('invalid claude-code regex matcher "(" on event "PreToolUse"')
  62. })
  63. it('discards matcher fields on events without matcher subjects before validation', () => {
  64. const { config } = parseClaudeCodeConfig({
  65. UserPromptSubmit: [{ matcher: '[', hooks: [{ type: 'command', command: 'prompt.sh' }] }],
  66. Stop: [{ matcher: '(', hooks: [{ type: 'command', command: 'stop.sh' }] }],
  67. })
  68. expect(config).toEqual({
  69. UserPromptSubmit: [{ hooks: [{ command: 'prompt.sh' }] }],
  70. Stop: [{ hooks: [{ command: 'stop.sh' }] }],
  71. })
  72. })
  73. it('ignores invalid matchers on unsupported events without dropping supported hooks', () => {
  74. const { config } = parseClaudeCodeConfig({
  75. Setup: [{ matcher: '(', hooks: [{ type: 'command', command: 'ignored.sh' }] }],
  76. PreToolUse: [{ matcher: 'Bash', hooks: [{ type: 'command', command: 'kept.sh' }] }],
  77. })
  78. expect(config).toEqual({
  79. PreToolUse: [{ matcher: 'Bash', hooks: [{ command: 'kept.sh' }] }],
  80. })
  81. })
  82. })