import { describe, it, expect } from 'vitest'
import { parseHookEvent } from '../src/http/hook.js'

const SID = 'f47ac10b-58cc-4372-a567-0e02b2c3d479'

describe('parseHookEvent', () => {
  it('maps tool events to "working"', () => {
    for (const e of ['PreToolUse', 'PostToolUse', 'UserPromptSubmit']) {
      expect(parseHookEvent(SID, { hook_event_name: e })).toEqual({
        sessionId: SID,
        status: 'working',
      })
    }
  })

  it('maps PermissionRequest to "waiting" with the tool name as detail', () => {
    expect(parseHookEvent(SID, { hook_event_name: 'PermissionRequest', tool_name: 'Bash' })).toEqual(
      { sessionId: SID, status: 'waiting', detail: 'Bash' },
    )
  })

  it('maps Notification permission_prompt to "waiting", idle_prompt to "idle"', () => {
    expect(
      parseHookEvent(SID, { hook_event_name: 'Notification', notification_type: 'permission_prompt' }),
    ).toEqual({ sessionId: SID, status: 'waiting' })
    expect(
      parseHookEvent(SID, { hook_event_name: 'Notification', notification_type: 'idle_prompt' }),
    ).toEqual({ sessionId: SID, status: 'idle' })
  })

  it('maps Stop / SessionEnd to "idle"', () => {
    expect(parseHookEvent(SID, { hook_event_name: 'Stop' })).toEqual({ sessionId: SID, status: 'idle' })
    expect(parseHookEvent(SID, { hook_event_name: 'SessionEnd' })).toEqual({
      sessionId: SID,
      status: 'idle',
    })
  })

  it('returns null for missing sessionId, bad body, or unknown event', () => {
    expect(parseHookEvent(undefined, { hook_event_name: 'Stop' })).toBeNull()
    expect(parseHookEvent('', { hook_event_name: 'Stop' })).toBeNull()
    expect(parseHookEvent(SID, null)).toBeNull()
    expect(parseHookEvent(SID, 'nope')).toBeNull()
    expect(parseHookEvent(SID, { hook_event_name: 'SomethingElse' })).toBeNull()
    expect(parseHookEvent(SID, {})).toBeNull()
  })
})
