/**
 * Tests for utils/provider-options.js
 *
 * resolveProviderOptions backs the settings-page model-provider dropdown.
 * It must prefer the backend's auto-discovered list so the dropdown can't
 * drift from the provider registry (#4622), while falling back safely when
 * the API list is unavailable (offline / fetch failure).
 */

import '@js/utils/provider-options.js';

const { resolveProviderOptions } = window.LdrProviderOptions;

const STATIC = [
    { value: 'OLLAMA', label: 'Ollama (Local)' },
    { value: 'OPENAI', label: 'OpenAI (Cloud)' },
];

describe('resolveProviderOptions', () => {
    it('prefers the auto-discovered list when it has entries', () => {
        const discovered = [
            { value: 'OPENAI', label: 'OpenAI ☁️ Cloud' },
            { value: 'XAI', label: 'xAI ☁️ Cloud' },
        ];
        // Returned as-is (same reference) so the full discovered set is shown.
        expect(resolveProviderOptions(discovered, [], STATIC)).toBe(discovered);
    });

    it('falls back to the llm.provider settings options when discovery is empty', () => {
        const allSettings = [
            { key: 'something.else', options: [{ value: 'X', label: 'X' }] },
            {
                key: 'llm.provider',
                options: [
                    { value: 'GOOGLE', label: 'Google', extra: 'ignored' },
                ],
            },
        ];
        const result = resolveProviderOptions([], allSettings, STATIC);
        // Projected to {value, label} only (drops arbitrary backend props).
        expect(result).toEqual([{ value: 'GOOGLE', label: 'Google' }]);
    });

    it('falls back to the static list when neither discovery nor settings have options', () => {
        expect(resolveProviderOptions([], [], STATIC)).toBe(STATIC);
        expect(
            resolveProviderOptions([], [{ key: 'llm.provider', options: [] }], STATIC),
        ).toBe(STATIC);
        expect(
            resolveProviderOptions([], [{ key: 'other', options: [{ value: 'a', label: 'a' }] }], STATIC),
        ).toBe(STATIC);
    });

    it('is defensive against non-array discovered / settings input', () => {
        expect(resolveProviderOptions(undefined, undefined, STATIC)).toBe(STATIC);
        expect(resolveProviderOptions(null, null, STATIC)).toBe(STATIC);
        expect(resolveProviderOptions('nope', 42, STATIC)).toBe(STATIC);
    });

    it('does not mutate its inputs', () => {
        const discovered = [{ value: 'A', label: 'A' }];
        const snapshot = JSON.parse(JSON.stringify(discovered));
        resolveProviderOptions(discovered, [], STATIC);
        expect(discovered).toEqual(snapshot);
    });
});
