From dbcfcd45f5912b292315cd5b43020326d469b1f5 Mon Sep 17 00:00:00 2001 From: shishu314 Date: Tue, 16 Sep 2025 14:23:49 -0400 Subject: [PATCH] cleanup(extension) - Add tests for the install command (#8371) Co-authored-by: Shi Shu --- .../src/commands/extensions/install.test.ts | 110 +++++++++++++++++- 1 file changed, 107 insertions(+), 3 deletions(-) diff --git a/packages/cli/src/commands/extensions/install.test.ts b/packages/cli/src/commands/extensions/install.test.ts index 441855da2c..722ffd49d5 100644 --- a/packages/cli/src/commands/extensions/install.test.ts +++ b/packages/cli/src/commands/extensions/install.test.ts @@ -4,12 +4,18 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { describe, it, expect } from 'vitest'; -import { installCommand } from './install.js'; +import { describe, it, expect, type MockInstance } from 'vitest'; +import { handleInstall, installCommand } from './install.js'; import yargs from 'yargs'; +const mockInstallExtension = vi.hoisted(() => vi.fn()); + vi.mock('../../config/extension.js', () => ({ - installExtension: vi.fn(), + installExtension: mockInstallExtension, +})); + +vi.mock('../../utils/errors.js', () => ({ + getErrorMessage: vi.fn((error: Error) => error.message), })); describe('extensions install command', () => { @@ -27,3 +33,101 @@ describe('extensions install command', () => { ).toThrow('Arguments source and path are mutually exclusive'); }); }); + +describe('handleInstall', () => { + let consoleLogSpy: MockInstance; + let consoleErrorSpy: MockInstance; + let processSpy: MockInstance; + + beforeEach(() => { + consoleLogSpy = vi.spyOn(console, 'log'); + consoleErrorSpy = vi.spyOn(console, 'error'); + processSpy = vi + .spyOn(process, 'exit') + .mockImplementation(() => undefined as never); + }); + + afterEach(() => { + mockInstallExtension.mockClear(); + vi.resetAllMocks(); + }); + + it('should install an extension from a http source', async () => { + mockInstallExtension.mockResolvedValue('http-extension'); + + await handleInstall({ + source: 'http://google.com', + }); + + expect(consoleLogSpy).toHaveBeenCalledWith( + 'Extension "http-extension" installed successfully and enabled.', + ); + }); + + it('should install an extension from a https source', async () => { + mockInstallExtension.mockResolvedValue('https-extension'); + + await handleInstall({ + source: 'https://google.com', + }); + + expect(consoleLogSpy).toHaveBeenCalledWith( + 'Extension "https-extension" installed successfully and enabled.', + ); + }); + + it('should install an extension from a git source', async () => { + mockInstallExtension.mockResolvedValue('git-extension'); + + await handleInstall({ + source: 'git@some-url', + }); + + expect(consoleLogSpy).toHaveBeenCalledWith( + 'Extension "git-extension" installed successfully and enabled.', + ); + }); + + it('throws an error from an unknown source', async () => { + await handleInstall({ + source: 'test://google.com', + }); + + expect(consoleErrorSpy).toHaveBeenCalledWith( + 'The source "test://google.com" is not a valid URL format.', + ); + expect(processSpy).toHaveBeenCalledWith(1); + }); + + it('should install an extension from a local path', async () => { + mockInstallExtension.mockResolvedValue('local-extension'); + + await handleInstall({ + path: '/some/path', + }); + + expect(consoleLogSpy).toHaveBeenCalledWith( + 'Extension "local-extension" installed successfully and enabled.', + ); + }); + + it('should throw an error if no source or path is provided', async () => { + await handleInstall({}); + + expect(consoleErrorSpy).toHaveBeenCalledWith( + 'Either --source or --path must be provided.', + ); + expect(processSpy).toHaveBeenCalledWith(1); + }); + + it('should throw an error if install extension fails', async () => { + mockInstallExtension.mockRejectedValue( + new Error('Install extension failed'), + ); + + await handleInstall({ source: 'git@some-url' }); + + expect(consoleErrorSpy).toHaveBeenCalledWith('Install extension failed'); + expect(processSpy).toHaveBeenCalledWith(1); + }); +});