config.spec.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { describe, expect, it } from 'vitest'
  2. import { parseClaudeConfig, substituteCommand } from '@deepseek-ai/dsh-hooks-claude/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('parseClaudeConfig', () => {
  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 = parseClaudeConfig(groups)
  17. const wrapped = parseClaudeConfig({ 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 } = parseClaudeConfig(
  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 } = parseClaudeConfig({
  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 } = parseClaudeConfig({ 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(parseClaudeConfig({ PreToolUse: 'nope' }).config).toEqual({})
  45. expect(parseClaudeConfig({ 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(parseClaudeConfig({ Stop: [{ hooks: [{ type: 'command', command: 5 }] }] }).config).toEqual({})
  48. })
  49. it('returns empty for a non-object / null / array top level', () => {
  50. expect(parseClaudeConfig(null).config).toEqual({})
  51. expect(parseClaudeConfig(42).config).toEqual({})
  52. expect(parseClaudeConfig([1, 2]).config).toEqual({})
  53. })
  54. it('omits the matcher key when the group has none (match-all)', () => {
  55. const { config } = parseClaudeConfig({ Stop: [{ hooks: [{ type: 'command', command: 's.sh' }] }] })
  56. expect('matcher' in config.Stop![0]!).toBe(false)
  57. })
  58. })