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

describe('normalizeFavoriteModels', () => {
  it('returns empty array for non-array input', () => {
    expect(normalizeFavoriteModels(null)).toEqual([]);
    expect(normalizeFavoriteModels(undefined)).toEqual([]);
    expect(normalizeFavoriteModels('string')).toEqual([]);
    expect(normalizeFavoriteModels(42)).toEqual([]);
    expect(normalizeFavoriteModels({})).toEqual([]);
  });

  it('drops malformed entries that are not strings', () => {
    expect(normalizeFavoriteModels(['valid/provider', 42, null, false])).toEqual(['valid/provider']);
  });

  it('drops entries without a slash (bare ids)', () => {
    expect(normalizeFavoriteModels(['bare-model', 'another-bare'])).toEqual([]);
  });

  it('drops empty or whitespace-only strings', () => {
    expect(normalizeFavoriteModels(['', '   ', 'valid/provider'])).toEqual(['valid/provider']);
  });

  it('dedupes preserving insertion order', () => {
    const result = normalizeFavoriteModels([
      'a/foo',
      'b/bar',
      'a/foo',
      'c/baz',
      'b/bar',
    ]);
    expect(result).toEqual(['a/foo', 'b/bar', 'c/baz']);
  });

  it('trims whitespace from entries', () => {
    expect(normalizeFavoriteModels(['  a/foo  ', 'b/bar'])).toEqual(['a/foo', 'b/bar']);
  });

  it('accepts valid composite ids', () => {
    const input = [
      'sam-desktop/qwen3.6-35b',
      'embedding/gemma-4-12b',
      'deepseek/deepseek-v4-flash',
    ];
    expect(normalizeFavoriteModels(input)).toEqual(input);
  });

  it('handles empty array', () => {
    expect(normalizeFavoriteModels([])).toEqual([]);
  });

  it('preserves insertion order after dedup', () => {
    const input = ['b/bar', 'a/foo', 'c/baz', 'a/foo', 'b/bar'];
    expect(normalizeFavoriteModels(input)).toEqual(['b/bar', 'a/foo', 'c/baz']);
  });
});
