import { describe, expect, it } from 'vitest';
import { DEFAULT_COURIERS, parseCouriers } from '../api/settings.routes.js';

describe('parseCouriers', () => {
  it('returns the default list when the setting is missing or blank', () => {
    expect(parseCouriers(undefined)).toEqual(DEFAULT_COURIERS);
    expect(parseCouriers('')).toEqual(DEFAULT_COURIERS);
    expect(parseCouriers('   ')).toEqual(DEFAULT_COURIERS);
  });

  it('parses a stored JSON array and trims entries', () => {
    expect(parseCouriers(JSON.stringify([' Correos ', 'SEUR']))).toEqual(['Correos', 'SEUR']);
  });

  it('drops non-string and empty entries', () => {
    expect(parseCouriers(JSON.stringify(['MRW', 42, '', null, 'GLS']))).toEqual(['MRW', 'GLS']);
  });

  it('falls back to defaults on invalid JSON or empty results', () => {
    expect(parseCouriers('not json')).toEqual(DEFAULT_COURIERS);
    expect(parseCouriers('{"a":1}')).toEqual(DEFAULT_COURIERS);
    expect(parseCouriers(JSON.stringify(['', '  ']))).toEqual(DEFAULT_COURIERS);
  });

  it('caps the list at 30 couriers', () => {
    const many = Array.from({ length: 40 }, (_, index) => `Courier ${index}`);
    expect(parseCouriers(JSON.stringify(many))).toHaveLength(30);
  });
});
