/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { describe, it, expect, vi } from 'vitest';
import { handleInstall, installCommand } from './install.js';
import yargs from 'yargs';
const mockInstallOrUpdateExtension = vi.hoisted(() => vi.fn());
const mockLoadExtensionByName = vi.hoisted(() => vi.fn());
const mockRequestConsentNonInteractive = vi.hoisted(() => vi.fn());
const mockStat = vi.hoisted(() => vi.fn());
vi.mock('../../config/extension.js', async (importOriginal) => {
    const actual = await importOriginal();
    return {
        ...actual,
        installOrUpdateExtension: mockInstallOrUpdateExtension,
        loadExtensionByName: mockLoadExtensionByName,
        requestConsentNonInteractive: mockRequestConsentNonInteractive,
    };
});
vi.mock('../../utils/errors.js', () => ({
    getErrorMessage: vi.fn((error) => error.message),
}));
vi.mock('node:fs/promises', () => ({
    stat: mockStat,
    default: {
        stat: mockStat,
    },
}));
describe('extensions install command', () => {
    it('should fail if no source is provided', () => {
        const validationParser = yargs([]).command(installCommand).fail(false);
        expect(() => validationParser.parse('install')).toThrow('Either --source or --path must be provided.');
    });
});
describe('handleInstall', () => {
    let consoleLogSpy;
    let consoleErrorSpy;
    let processSpy;
    beforeEach(() => {
        consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => { });
        consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => { });
        processSpy = vi
            .spyOn(process, 'exit')
            .mockImplementation(() => undefined);
    });
    afterEach(() => {
        mockInstallOrUpdateExtension.mockClear();
        mockLoadExtensionByName.mockClear();
        mockRequestConsentNonInteractive.mockClear();
        mockStat.mockClear();
        vi.clearAllMocks();
    });
    it.each([
        {
            source: 'http://google.com',
            name: 'http-extension',
            type: 'http source',
        },
        {
            source: 'https://google.com',
            name: 'https-extension',
            type: 'https source',
        },
        { source: 'git@some-url', name: 'git-extension', type: 'git source' },
        { source: 'sso://google.com', name: 'sso-extension', type: 'sso source' },
        {
            source: '/some/path',
            name: 'local-extension',
            type: 'local path',
            needsStat: true,
        },
    ])('should install an extension from a $type', async ({ source, name, needsStat }) => {
        if (needsStat) {
            mockStat.mockResolvedValue({});
        }
        mockInstallOrUpdateExtension.mockResolvedValue(name);
        mockLoadExtensionByName.mockReturnValue({
            name,
        });
        await handleInstall({ source });
        expect(consoleLogSpy).toHaveBeenCalledWith(`Extension "${name}" installed successfully and enabled.`);
    });
    it('throws an error from an unknown source', async () => {
        mockStat.mockRejectedValue(new Error('ENOENT: no such file or directory'));
        await handleInstall({
            source: 'test://google.com',
        });
        expect(consoleErrorSpy).toHaveBeenCalledWith('Install source not found.');
        expect(processSpy).toHaveBeenCalledWith(1);
    });
    it('should throw an error if install extension fails', async () => {
        mockInstallOrUpdateExtension.mockRejectedValue(new Error('Install extension failed'));
        await handleInstall({ source: 'git@some-url' });
        expect(consoleErrorSpy).toHaveBeenCalledWith('Install extension failed');
        expect(processSpy).toHaveBeenCalledWith(1);
    });
});
//# sourceMappingURL=install.test.js.map