|
4 | 4 | * SPDX-License-Identifier: Apache-2.0
|
5 | 5 | */
|
6 | 6 |
|
7 |
| -import { describe, it, expect } from 'vitest'; |
8 |
| -import { extensionsCommand } from './extensionsCommand.js'; |
9 |
| -import { type CommandContext } from './types.js'; |
| 7 | +import { |
| 8 | + updateAllUpdatableExtensions, |
| 9 | + updateExtensionByName, |
| 10 | +} from '../../config/extension.js'; |
10 | 11 | import { createMockCommandContext } from '../../test-utils/mockCommandContext.js';
|
11 | 12 | import { MessageType } from '../types.js';
|
| 13 | +import { extensionsCommand } from './extensionsCommand.js'; |
| 14 | +import { type CommandContext } from './types.js'; |
| 15 | +import { |
| 16 | + describe, |
| 17 | + it, |
| 18 | + expect, |
| 19 | + vi, |
| 20 | + beforeEach, |
| 21 | + type MockedFunction, |
| 22 | +} from 'vitest'; |
| 23 | + |
| 24 | +vi.mock('../../config/extension.js', () => ({ |
| 25 | + updateExtensionByName: vi.fn(), |
| 26 | + updateAllUpdatableExtensions: vi.fn(), |
| 27 | +})); |
| 28 | + |
| 29 | +const mockUpdateExtensionByName = updateExtensionByName as MockedFunction< |
| 30 | + typeof updateExtensionByName |
| 31 | +>; |
| 32 | + |
| 33 | +const mockUpdateAllUpdatableExtensions = |
| 34 | + updateAllUpdatableExtensions as MockedFunction< |
| 35 | + typeof updateAllUpdatableExtensions |
| 36 | + >; |
12 | 37 |
|
13 | 38 | describe('extensionsCommand', () => {
|
14 | 39 | let mockContext: CommandContext;
|
15 | 40 |
|
16 |
| - it('should display "No active extensions." when none are found', async () => { |
| 41 | + beforeEach(() => { |
| 42 | + vi.resetAllMocks(); |
17 | 43 | mockContext = createMockCommandContext({
|
18 | 44 | services: {
|
19 | 45 | config: {
|
20 | 46 | getExtensions: () => [],
|
21 | 47 | },
|
22 | 48 | },
|
23 | 49 | });
|
| 50 | + }); |
24 | 51 |
|
25 |
| - if (!extensionsCommand.action) throw new Error('Action not defined'); |
26 |
| - await extensionsCommand.action(mockContext, ''); |
| 52 | + describe('list', () => { |
| 53 | + it('should display "No active extensions." when none are found', async () => { |
| 54 | + if (!extensionsCommand.action) throw new Error('Action not defined'); |
| 55 | + await extensionsCommand.action(mockContext, ''); |
27 | 56 |
|
28 |
| - expect(mockContext.ui.addItem).toHaveBeenCalledWith( |
29 |
| - { |
30 |
| - type: MessageType.INFO, |
31 |
| - text: 'No active extensions.', |
32 |
| - }, |
33 |
| - expect.any(Number), |
34 |
| - ); |
| 57 | + expect(mockContext.ui.addItem).toHaveBeenCalledWith( |
| 58 | + { |
| 59 | + type: MessageType.INFO, |
| 60 | + text: 'No active extensions.', |
| 61 | + }, |
| 62 | + expect.any(Number), |
| 63 | + ); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should list active extensions when they are found', async () => { |
| 67 | + const mockExtensions = [ |
| 68 | + { name: 'ext-one', version: '1.0.0', isActive: true }, |
| 69 | + { name: 'ext-two', version: '2.1.0', isActive: true }, |
| 70 | + { name: 'ext-three', version: '3.0.0', isActive: false }, |
| 71 | + ]; |
| 72 | + mockContext = createMockCommandContext({ |
| 73 | + services: { |
| 74 | + config: { |
| 75 | + getExtensions: () => mockExtensions, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }); |
| 79 | + |
| 80 | + if (!extensionsCommand.action) throw new Error('Action not defined'); |
| 81 | + await extensionsCommand.action(mockContext, ''); |
| 82 | + |
| 83 | + const expectedMessage = |
| 84 | + 'Active extensions:\n\n' + |
| 85 | + ` - \u001b[36mext-one (v1.0.0)\u001b[0m\n` + |
| 86 | + ` - \u001b[36mext-two (v2.1.0)\u001b[0m\n`; |
| 87 | + |
| 88 | + expect(mockContext.ui.addItem).toHaveBeenCalledWith( |
| 89 | + { |
| 90 | + type: MessageType.INFO, |
| 91 | + text: expectedMessage, |
| 92 | + }, |
| 93 | + expect.any(Number), |
| 94 | + ); |
| 95 | + }); |
35 | 96 | });
|
36 | 97 |
|
37 |
| - it('should list active extensions when they are found', async () => { |
38 |
| - const mockExtensions = [ |
39 |
| - { name: 'ext-one', version: '1.0.0', isActive: true }, |
40 |
| - { name: 'ext-two', version: '2.1.0', isActive: true }, |
41 |
| - { name: 'ext-three', version: '3.0.0', isActive: false }, |
42 |
| - ]; |
43 |
| - mockContext = createMockCommandContext({ |
44 |
| - services: { |
45 |
| - config: { |
46 |
| - getExtensions: () => mockExtensions, |
| 98 | + describe('update', () => { |
| 99 | + const updateAction = extensionsCommand.subCommands?.find( |
| 100 | + (cmd) => cmd.name === 'update', |
| 101 | + )?.action; |
| 102 | + |
| 103 | + if (!updateAction) { |
| 104 | + throw new Error('Update action not found'); |
| 105 | + } |
| 106 | + |
| 107 | + it('should show usage if no args are provided', async () => { |
| 108 | + await updateAction(mockContext, ''); |
| 109 | + expect(mockContext.ui.addItem).toHaveBeenCalledWith( |
| 110 | + { |
| 111 | + type: MessageType.ERROR, |
| 112 | + text: 'Usage: /extensions update <extension-names>|--all', |
47 | 113 | },
|
48 |
| - }, |
| 114 | + expect.any(Number), |
| 115 | + ); |
49 | 116 | });
|
50 | 117 |
|
51 |
| - if (!extensionsCommand.action) throw new Error('Action not defined'); |
52 |
| - await extensionsCommand.action(mockContext, ''); |
| 118 | + it('should inform user if there are no extensions to update with --all', async () => { |
| 119 | + mockUpdateAllUpdatableExtensions.mockResolvedValue([]); |
| 120 | + await updateAction(mockContext, '--all'); |
| 121 | + expect(mockContext.ui.addItem).toHaveBeenCalledWith( |
| 122 | + { |
| 123 | + type: MessageType.INFO, |
| 124 | + text: 'No extensions to update.', |
| 125 | + }, |
| 126 | + expect.any(Number), |
| 127 | + ); |
| 128 | + }); |
53 | 129 |
|
54 |
| - const expectedMessage = |
55 |
| - 'Active extensions:\n\n' + |
56 |
| - ` - \u001b[36mext-one (v1.0.0)\u001b[0m\n` + |
57 |
| - ` - \u001b[36mext-two (v2.1.0)\u001b[0m\n`; |
| 130 | + it('should update all extensions with --all', async () => { |
| 131 | + mockUpdateAllUpdatableExtensions.mockResolvedValue([ |
| 132 | + { |
| 133 | + name: 'ext-one', |
| 134 | + originalVersion: '1.0.0', |
| 135 | + updatedVersion: '1.0.1', |
| 136 | + }, |
| 137 | + { |
| 138 | + name: 'ext-two', |
| 139 | + originalVersion: '2.0.0', |
| 140 | + updatedVersion: '2.0.1', |
| 141 | + }, |
| 142 | + ]); |
| 143 | + await updateAction(mockContext, '--all'); |
| 144 | + expect(mockContext.ui.addItem).toHaveBeenCalledWith( |
| 145 | + { |
| 146 | + type: MessageType.INFO, |
| 147 | + text: |
| 148 | + 'Extension "ext-one" successfully updated: 1.0.0 → 1.0.1.\n' + |
| 149 | + 'Extension "ext-two" successfully updated: 2.0.0 → 2.0.1.\n' + |
| 150 | + 'Restart gemini-cli to see the changes.', |
| 151 | + }, |
| 152 | + expect.any(Number), |
| 153 | + ); |
| 154 | + }); |
58 | 155 |
|
59 |
| - expect(mockContext.ui.addItem).toHaveBeenCalledWith( |
60 |
| - { |
61 |
| - type: MessageType.INFO, |
62 |
| - text: expectedMessage, |
63 |
| - }, |
64 |
| - expect.any(Number), |
65 |
| - ); |
| 156 | + it('should handle errors when updating all extensions', async () => { |
| 157 | + mockUpdateAllUpdatableExtensions.mockRejectedValue( |
| 158 | + new Error('Something went wrong'), |
| 159 | + ); |
| 160 | + await updateAction(mockContext, '--all'); |
| 161 | + expect(mockContext.ui.addItem).toHaveBeenCalledWith( |
| 162 | + { |
| 163 | + type: MessageType.ERROR, |
| 164 | + text: 'Something went wrong', |
| 165 | + }, |
| 166 | + expect.any(Number), |
| 167 | + ); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should update a single extension by name', async () => { |
| 171 | + mockUpdateExtensionByName.mockResolvedValue({ |
| 172 | + name: 'ext-one', |
| 173 | + originalVersion: '1.0.0', |
| 174 | + updatedVersion: '1.0.1', |
| 175 | + }); |
| 176 | + await updateAction(mockContext, 'ext-one'); |
| 177 | + expect(mockContext.ui.addItem).toHaveBeenCalledWith( |
| 178 | + { |
| 179 | + type: MessageType.INFO, |
| 180 | + text: |
| 181 | + 'Extension "ext-one" successfully updated: 1.0.0 → 1.0.1.\n' + |
| 182 | + 'Restart gemini-cli to see the changes.', |
| 183 | + }, |
| 184 | + expect.any(Number), |
| 185 | + ); |
| 186 | + }); |
| 187 | + |
| 188 | + it('should handle errors when updating a single extension', async () => { |
| 189 | + mockUpdateExtensionByName.mockRejectedValue( |
| 190 | + new Error('Extension not found'), |
| 191 | + ); |
| 192 | + await updateAction(mockContext, 'ext-one'); |
| 193 | + expect(mockContext.ui.addItem).toHaveBeenCalledWith( |
| 194 | + { |
| 195 | + type: MessageType.ERROR, |
| 196 | + text: 'Extension not found', |
| 197 | + }, |
| 198 | + expect.any(Number), |
| 199 | + ); |
| 200 | + }); |
| 201 | + |
| 202 | + it('should update multiple extensions by name', async () => { |
| 203 | + mockUpdateExtensionByName |
| 204 | + .mockResolvedValueOnce({ |
| 205 | + name: 'ext-one', |
| 206 | + originalVersion: '1.0.0', |
| 207 | + updatedVersion: '1.0.1', |
| 208 | + }) |
| 209 | + .mockResolvedValueOnce({ |
| 210 | + name: 'ext-two', |
| 211 | + originalVersion: '2.0.0', |
| 212 | + updatedVersion: '2.0.1', |
| 213 | + }); |
| 214 | + await updateAction(mockContext, 'ext-one ext-two'); |
| 215 | + expect(mockUpdateExtensionByName).toHaveBeenCalledTimes(2); |
| 216 | + expect(mockContext.ui.addItem).toHaveBeenCalledWith( |
| 217 | + { |
| 218 | + type: MessageType.INFO, |
| 219 | + text: |
| 220 | + 'Extension "ext-one" successfully updated: 1.0.0 → 1.0.1.\n' + |
| 221 | + 'Extension "ext-two" successfully updated: 2.0.0 → 2.0.1.\n' + |
| 222 | + 'Restart gemini-cli to see the changes.', |
| 223 | + }, |
| 224 | + expect.any(Number), |
| 225 | + ); |
| 226 | + }); |
66 | 227 | });
|
67 | 228 | });
|
0 commit comments