|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import * as fs from 'node:fs'; |
| 8 | +import { describe, it, expect, vi, type MockInstance } from 'vitest'; |
| 9 | +import { handleValidate, validateCommand } from './validate.js'; |
| 10 | +import yargs from 'yargs'; |
| 11 | +import { createExtension } from '../../test-utils/createExtension.js'; |
| 12 | +import path from 'node:path'; |
| 13 | +import * as os from 'node:os'; |
| 14 | +import { debugLogger } from '@google/gemini-cli-core'; |
| 15 | + |
| 16 | +describe('extensions validate command', () => { |
| 17 | + it('should fail if no path is provided', () => { |
| 18 | + const validationParser = yargs([]).command(validateCommand).fail(false); |
| 19 | + expect(() => validationParser.parse('validate')).toThrow( |
| 20 | + 'Not enough non-option arguments: got 0, need at least 1', |
| 21 | + ); |
| 22 | + }); |
| 23 | +}); |
| 24 | + |
| 25 | +describe('handleValidate', () => { |
| 26 | + let debugLoggerLogSpy: MockInstance; |
| 27 | + let debugLoggerWarnSpy: MockInstance; |
| 28 | + let debugLoggerErrorSpy: MockInstance; |
| 29 | + let processSpy: MockInstance; |
| 30 | + let tempHomeDir: string; |
| 31 | + let tempWorkspaceDir: string; |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + debugLoggerLogSpy = vi.spyOn(debugLogger, 'log'); |
| 35 | + debugLoggerWarnSpy = vi.spyOn(debugLogger, 'warn'); |
| 36 | + debugLoggerErrorSpy = vi.spyOn(debugLogger, 'error'); |
| 37 | + processSpy = vi |
| 38 | + .spyOn(process, 'exit') |
| 39 | + .mockImplementation(() => undefined as never); |
| 40 | + tempHomeDir = fs.mkdtempSync(path.join(os.tmpdir(), 'test-home')); |
| 41 | + tempWorkspaceDir = fs.mkdtempSync(path.join(tempHomeDir, 'test-workspace')); |
| 42 | + vi.spyOn(process, 'cwd').mockReturnValue(tempWorkspaceDir); |
| 43 | + }); |
| 44 | + |
| 45 | + afterEach(() => { |
| 46 | + vi.restoreAllMocks(); |
| 47 | + fs.rmSync(tempHomeDir, { recursive: true, force: true }); |
| 48 | + fs.rmSync(tempWorkspaceDir, { recursive: true, force: true }); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should validate an extension from a local dir', async () => { |
| 52 | + createExtension({ |
| 53 | + extensionsDir: tempWorkspaceDir, |
| 54 | + name: 'local-ext-name', |
| 55 | + version: '1.0.0', |
| 56 | + }); |
| 57 | + |
| 58 | + await handleValidate({ |
| 59 | + path: 'local-ext-name', |
| 60 | + }); |
| 61 | + expect(debugLoggerLogSpy).toHaveBeenCalledWith( |
| 62 | + 'Extension local-ext-name has been successfully validated.', |
| 63 | + ); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should throw an error if the extension name is invalid', async () => { |
| 67 | + createExtension({ |
| 68 | + extensionsDir: tempWorkspaceDir, |
| 69 | + name: 'INVALID_NAME', |
| 70 | + version: '1.0.0', |
| 71 | + }); |
| 72 | + |
| 73 | + await handleValidate({ |
| 74 | + path: 'INVALID_NAME', |
| 75 | + }); |
| 76 | + expect(debugLoggerErrorSpy).toHaveBeenCalledWith( |
| 77 | + expect.stringContaining( |
| 78 | + 'Invalid extension name: "INVALID_NAME". Only letters (a-z, A-Z), numbers (0-9), and dashes (-) are allowed.', |
| 79 | + ), |
| 80 | + ); |
| 81 | + expect(processSpy).toHaveBeenCalledWith(1); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should warn if version is not formatted with semver', async () => { |
| 85 | + createExtension({ |
| 86 | + extensionsDir: tempWorkspaceDir, |
| 87 | + name: 'valid-name', |
| 88 | + version: '1', |
| 89 | + }); |
| 90 | + |
| 91 | + await handleValidate({ |
| 92 | + path: 'valid-name', |
| 93 | + }); |
| 94 | + expect(debugLoggerWarnSpy).toHaveBeenCalledWith( |
| 95 | + expect.stringContaining( |
| 96 | + "Version '1' does not appear to be standard semver (e.g., 1.0.0).", |
| 97 | + ), |
| 98 | + ); |
| 99 | + expect(debugLoggerLogSpy).toHaveBeenCalledWith( |
| 100 | + 'Extension valid-name has been successfully validated.', |
| 101 | + ); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should throw an error if context files are missing', async () => { |
| 105 | + createExtension({ |
| 106 | + extensionsDir: tempWorkspaceDir, |
| 107 | + name: 'valid-name', |
| 108 | + version: '1.0.0', |
| 109 | + contextFileName: 'contextFile.md', |
| 110 | + }); |
| 111 | + fs.rmSync(path.join(tempWorkspaceDir, 'valid-name/contextFile.md')); |
| 112 | + await handleValidate({ |
| 113 | + path: 'valid-name', |
| 114 | + }); |
| 115 | + expect(debugLoggerErrorSpy).toHaveBeenCalledWith( |
| 116 | + expect.stringContaining( |
| 117 | + 'The following context files referenced in gemini-extension.json are missing: contextFile.md', |
| 118 | + ), |
| 119 | + ); |
| 120 | + expect(processSpy).toHaveBeenCalledWith(1); |
| 121 | + }); |
| 122 | +}); |
0 commit comments