import { jsx as _jsx } from "react/jsx-runtime";
/** @vitest-environment jsdom */
import React from 'react';
import { describe, it, expect, vi } from 'vitest';
import { render, act } from '@testing-library/react';
import { CredentialSyncProvider, useCredentialSync } from '../CredentialContext';
const openAiModel = {
    id: 'openai-model-1',
    provider: 'openai',
    label: 'OpenAI Test Model',
    model: 'gpt-4o',
    connection: {
        apiKey: '  sk-test-openai  '
    }
};
const anthropicModel = {
    id: 'anthropic-model-1',
    provider: 'anthropic',
    label: 'Anthropic Test Model',
    model: 'claude-3-opus',
    connection: {
        apiKey: 'sk-anthropic'
    }
};
describe('CredentialSyncProvider', () => {
    it('emits sanitized payloads with trimmed connection values', async () => {
        const onSubmit = vi.fn();
        let submitCredentials;
        const Harness = () => {
            const context = useCredentialSync();
            React.useEffect(() => {
                submitCredentials = context.submitCredentials;
            }, [context.submitCredentials]);
            return null;
        };
        render(_jsx(CredentialSyncProvider, { onSubmit: onSubmit, children: _jsx(Harness, {}) }));
        const payload = {
            action: 'upsert',
            providerId: 'openai',
            modelId: openAiModel.id,
            model: openAiModel,
            allModels: [openAiModel, anthropicModel]
        };
        await act(async () => {
            await submitCredentials?.(payload);
        });
        expect(onSubmit).toHaveBeenCalledTimes(1);
        const submittedPayload = onSubmit.mock.calls[0][0];
        expect(submittedPayload.action).toBe('upsert');
        expect(submittedPayload.providerId).toBe('openai');
        expect(submittedPayload.modelId).toBe('openai-model-1');
        expect(submittedPayload.model?.connection?.apiKey).toBe('sk-test-openai');
        expect(Array.isArray(submittedPayload.allModels)).toBe(true);
        expect(submittedPayload.allModels).toHaveLength(2);
        const matching = submittedPayload.allModels.find((model) => model.id === 'openai-model-1');
        expect(matching?.connection?.apiKey).toBe('sk-test-openai');
    });
    it('emits sanitized payloads for deletes', async () => {
        const onSubmit = vi.fn();
        let submitCredentials;
        const Harness = () => {
            const context = useCredentialSync();
            React.useEffect(() => {
                submitCredentials = context.submitCredentials;
            }, [context.submitCredentials]);
            return null;
        };
        render(_jsx(CredentialSyncProvider, { onSubmit: onSubmit, children: _jsx(Harness, {}) }));
        const payload = {
            action: 'delete',
            providerId: 'openai',
            modelId: openAiModel.id,
            model: null,
            allModels: []
        };
        await act(async () => {
            await submitCredentials?.(payload);
        });
        expect(onSubmit).toHaveBeenCalledTimes(1);
        const submittedPayload = onSubmit.mock.calls[0][0];
        expect(submittedPayload.action).toBe('delete');
        expect(submittedPayload.providerId).toBe('openai');
        expect(submittedPayload.model).toBeNull();
        expect(submittedPayload.allModels).toEqual([]);
    });
});
