/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { describe, expect, it, vi } from 'vitest';
import { parseZedAuthMethodId } from './zedIntegration.js';
// Mock runtimeSettings to test credential cache clearing logic
const mockGetActiveProfileName = vi.fn();
const mockLoadProfileByName = vi.fn();
vi.mock('../runtime/runtimeSettings.js', () => ({
    clearActiveModelParam: vi.fn(),
    getActiveModelParams: vi.fn(),
    getActiveProfileName: (...args) => mockGetActiveProfileName(...args),
    loadProfileByName: (...args) => mockLoadProfileByName(...args),
}));
const mockClearCachedCredentialFile = vi.fn();
vi.mock('@vybestack/llxprt-code-core', async (importOriginal) => {
    const actual = await importOriginal();
    return {
        ...actual,
        clearCachedCredentialFile: (...args) => mockClearCachedCredentialFile(...args),
    };
});
describe('zedIntegration auth method validation', () => {
    it('accepts known profile names', () => {
        expect(parseZedAuthMethodId('alpha', ['alpha', 'beta'])).toBe('alpha');
        expect(parseZedAuthMethodId('beta', ['alpha', 'beta'])).toBe('beta');
    });
    it('rejects unknown profile names', () => {
        expect(() => parseZedAuthMethodId('gamma', ['alpha', 'beta'])).toThrow(/Invalid enum value/);
    });
    it('rejects selection when no profiles exist', () => {
        expect(() => parseZedAuthMethodId('alpha', [])).toThrow(/No profiles available for selection/);
    });
});
describe('GeminiAgent.authenticate credential cache', () => {
    // Import dynamically after mocks are set up
    let GeminiAgent;
    beforeAll(async () => {
        const mod = await import('./zedIntegration.js');
        GeminiAgent = mod.GeminiAgent;
    });
    beforeEach(() => {
        vi.clearAllMocks();
        mockLoadProfileByName.mockResolvedValue(undefined);
    });
    function createAgent() {
        const mockConfig = {
            getProfileManager: () => ({
                listProfiles: async () => ['alpha', 'beta'],
            }),
            getEphemeralSetting: () => undefined,
        };
        const agent = new GeminiAgent(mockConfig, { debug: () => { } }, undefined);
        // Stub applyRuntimeProviderOverrides to avoid config dependencies
        vi.spyOn(agent, 'applyRuntimeProviderOverrides').mockResolvedValue(undefined);
        return agent;
    }
    it('clears credential cache when switching to a different profile', async () => {
        mockGetActiveProfileName.mockReturnValue('alpha');
        mockClearCachedCredentialFile.mockResolvedValue(undefined);
        const agent = createAgent();
        await agent.authenticate({ methodId: 'beta' });
        expect(mockClearCachedCredentialFile).toHaveBeenCalledOnce();
        expect(mockLoadProfileByName).toHaveBeenCalledWith('beta');
    });
    it('does NOT clear credential cache when re-authenticating same profile', async () => {
        mockGetActiveProfileName.mockReturnValue('alpha');
        mockClearCachedCredentialFile.mockResolvedValue(undefined);
        const agent = createAgent();
        await agent.authenticate({ methodId: 'alpha' });
        expect(mockClearCachedCredentialFile).not.toHaveBeenCalled();
        expect(mockLoadProfileByName).toHaveBeenCalledWith('alpha');
    });
    it('clears credential cache when no active profile exists', async () => {
        mockGetActiveProfileName.mockReturnValue(null);
        mockClearCachedCredentialFile.mockResolvedValue(undefined);
        const agent = createAgent();
        await agent.authenticate({ methodId: 'alpha' });
        expect(mockClearCachedCredentialFile).toHaveBeenCalledOnce();
        expect(mockLoadProfileByName).toHaveBeenCalledWith('alpha');
    });
});
//# sourceMappingURL=zedIntegration.test.js.map