/** @vitest-environment node */
import { describe, expect, it } from 'vitest';
import { sanitizeStoredModel, coerceStoredModels } from '../credentialUtils';
const buildModel = (overrides = {}) => ({
    id: overrides.id ?? 'test-model',
    provider: overrides.provider ?? 'openai',
    label: overrides.label ?? 'Test Model',
    model: overrides.model ?? 'gpt-4.1',
    description: overrides.description,
    baseUrl: overrides.baseUrl,
    connection: overrides.connection ?? {
        apiKey: ' sk-secret ',
        extra: 123,
        empty: '',
        nested: { foo: 'bar' }
    },
    settings: overrides.settings ?? {
        temperature: 0.9,
        nested: { flag: true }
    },
    clientHeaders: overrides.clientHeaders ?? {
        'X-Custom-Header': ' value '
    }
});
describe('credentialUtils', () => {
    it('sanitizes stored models by trimming connection values and stringifying supported primitives', () => {
        const model = buildModel();
        const sanitized = sanitizeStoredModel(model);
        expect(sanitized.connection).toEqual({
            apiKey: 'sk-secret',
            extra: '123',
            nested: '{"foo":"bar"}'
        });
        expect(sanitized.clientHeaders).toEqual({
            'X-Custom-Header': 'value'
        });
        expect(sanitized.settings).toEqual({
            temperature: 0.9,
            nested: { flag: true }
        });
    });
    it('coerces lists of models and filters invalid entries', () => {
        const models = coerceStoredModels([
            buildModel({ id: 'one' }),
            null,
            buildModel({ id: 'two', connection: { apiKey: 'other' } })
        ]);
        expect(models).toHaveLength(2);
        expect(models[0].id).toBe('one');
        expect(models[1].connection).toEqual({ apiKey: 'other' });
    });
});
