import { describe, it, expect } from 'vitest';
import { extractErrorMessage } from './extract-error-message';

describe('extractErrorMessage', () => {
  it('returns string errors as-is', () => {
    expect(extractErrorMessage('simple error')).toBe('simple error');
  });

  it('extracts .message from Error objects', () => {
    expect(extractErrorMessage(new Error('boom'))).toBe('boom');
  });

  it('extracts .message from TypeError', () => {
    expect(extractErrorMessage(new TypeError('bad type'))).toBe('bad type');
  });

  it('handles Rust AppError with string detail', () => {
    expect(extractErrorMessage({ kind: 'Database', detail: 'connection refused' }))
      .toBe('Database: connection refused');
  });

  it('handles Rust AppError with object detail', () => {
    expect(extractErrorMessage({ kind: 'Auth', detail: { reason: 'expired' } }))
      .toBe('Auth: {"reason":"expired"}');
  });

  it('handles Rust AppError with kind only (no detail)', () => {
    expect(extractErrorMessage({ kind: 'NotFound' })).toBe('NotFound');
  });

  it('handles objects with message property', () => {
    expect(extractErrorMessage({ message: 'IPC error' })).toBe('IPC error');
  });

  it('prefers kind over message when both present', () => {
    expect(extractErrorMessage({ kind: 'Timeout', detail: 'too slow', message: 'fail' }))
      .toBe('Timeout: too slow');
  });

  it('handles null', () => {
    expect(extractErrorMessage(null)).toBe('null');
  });

  it('handles undefined', () => {
    expect(extractErrorMessage(undefined)).toBe('undefined');
  });

  it('handles numbers', () => {
    expect(extractErrorMessage(42)).toBe('42');
  });

  it('handles empty string', () => {
    expect(extractErrorMessage('')).toBe('');
  });

  it('handles empty object', () => {
    expect(extractErrorMessage({})).toBe('[object Object]');
  });
});
