import { describe, it, expect } from 'vitest';
import { normalizeAgentEvent } from '../normalize-agent-status.js';

describe('normalizeAgentEvent', () => {
  describe('working bucket', () => {
    const cases = [
      'SessionStart',
      'UserPromptSubmit',
      'UserPromptSubmitted',
      'PostToolUse',
      'PostToolUseFailure',
      'BeforeAgent',
      'AfterTool',
      'task_started',
    ];
    for (const name of cases) {
      it(`maps ${name} → working`, () => {
        expect(normalizeAgentEvent(name)).toBe('working');
      });
    }
  });

  describe('blocked bucket', () => {
    const cases = [
      'PreToolUse',
      'Notification',
      'PermissionRequest',
      'exec_approval_request',
      'apply_patch_approval_request',
      'request_user_input',
    ];
    for (const name of cases) {
      it(`maps ${name} → blocked`, () => {
        expect(normalizeAgentEvent(name)).toBe('blocked');
      });
    }
  });

  describe('done bucket', () => {
    const cases = [
      'Stop',
      'AfterAgent',
      'SessionEnd',
      'task_complete',
      'agent-turn-complete',
    ];
    for (const name of cases) {
      it(`maps ${name} → done`, () => {
        expect(normalizeAgentEvent(name)).toBe('done');
      });
    }
  });

  describe('unknown / nullish → null', () => {
    it('returns null for an unrecognized event', () => {
      expect(normalizeAgentEvent('SomeRandomEvent')).toBeNull();
    });
    it('returns null for empty string', () => {
      expect(normalizeAgentEvent('')).toBeNull();
    });
    it('returns null for undefined', () => {
      expect(normalizeAgentEvent(undefined)).toBeNull();
    });
  });

  describe('case- and separator-insensitive matching', () => {
    it('matches snake_case spelling of a PascalCase event', () => {
      expect(normalizeAgentEvent('session_start')).toBe('working');
      expect(normalizeAgentEvent('post_tool_use')).toBe('working');
      expect(normalizeAgentEvent('pre_tool_use')).toBe('blocked');
    });
    it('matches camelCase spelling', () => {
      expect(normalizeAgentEvent('userPromptSubmitted')).toBe('working');
      expect(normalizeAgentEvent('postToolUse')).toBe('working');
      expect(normalizeAgentEvent('preToolUse')).toBe('blocked');
      expect(normalizeAgentEvent('sessionEnd')).toBe('done');
    });
    it('matches arbitrary case', () => {
      expect(normalizeAgentEvent('STOP')).toBe('done');
      expect(normalizeAgentEvent('notification')).toBe('blocked');
    });
  });
});
