import { describe, expect, test } from 'vitest';
import { errCode, errMessage } from './errors';
import { ERROR_COPY } from './error-copy';

describe('errCode', () => {
  test('returns code from object', () => {
    expect(errCode({ code: 'foo' })).toBe('foo');
  });

  test("returns 'unknown' for null", () => {
    expect(errCode(null)).toBe('unknown');
  });

  test("returns 'unknown' for undefined", () => {
    expect(errCode(undefined)).toBe('unknown');
  });

  test("returns 'unknown' for non-object values", () => {
    expect(errCode('boom')).toBe('unknown');
    expect(errCode(42)).toBe('unknown');
    expect(errCode(true)).toBe('unknown');
  });

  test('handles missing code field', () => {
    expect(errCode({})).toBe('unknown');
    expect(errCode({ message: 'hi' })).toBe('unknown');
  });
});

describe('errMessage', () => {
  test('returns mapped copy when code is known', () => {
    const expected = ERROR_COPY.track_not_found;
    expect(errMessage({ code: 'track_not_found' })).toBe(expected);
  });

  test('returns the unknown-fallback copy when code is missing', () => {
    const result = errMessage({ code: 'definitely_not_a_real_code_xyz' });
    expect(result).toBeTruthy();
    expect(typeof result).toBe('string');
    expect(result.length).toBeGreaterThan(0);
  });

  test('accepts custom fallback parameter without throwing', () => {
    // copyForCode itself returns a non-empty string for any input, so the
    // explicit fallback only matters in narrow cases. Just exercise the
    // signature.
    const result = errMessage(null, 'custom fallback');
    expect(typeof result).toBe('string');
    expect(result.length).toBeGreaterThan(0);
  });
});
