File size: 8,594 Bytes
52efc7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import React from 'react';
import { renderWithProviders } from '../../../test-utils/render.js';
import { waitFor } from '../../../test-utils/async.js';
import { makeFakeConfig } from '@google/gemini-cli-core';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { ExtensionRegistryView } from './ExtensionRegistryView.js';
import { type ExtensionManager } from '../../../config/extension-manager.js';
import { useExtensionRegistry } from '../../hooks/useExtensionRegistry.js';
import { useExtensionUpdates } from '../../hooks/useExtensionUpdates.js';
import { useRegistrySearch } from '../../hooks/useRegistrySearch.js';
import { type RegistryExtension } from '../../../config/extensionRegistryClient.js';
import { type UIState } from '../../contexts/UIStateContext.js';
import {
  type SearchListState,
  type GenericListItem,
} from '../shared/SearchableList.js';
import { type TextBuffer } from '../shared/text-buffer.js';
import { type UseHistoryManagerReturn } from '../../hooks/useHistoryManager.js';
import { ExtensionUpdateState } from '../../state/extensions.js';

// Mocks
vi.mock('../../hooks/useExtensionRegistry.js');
vi.mock('../../hooks/useExtensionUpdates.js');
vi.mock('../../hooks/useRegistrySearch.js');
vi.mock('../../../config/extension-manager.js');

const mockExtensions: RegistryExtension[] = [
  {
    id: 'ext1',
    extensionName: 'Test Extension 1',
    extensionDescription: 'Description 1',
    fullName: 'author/ext1',
    extensionVersion: '1.0.0',
    rank: 1,
    stars: 10,
    url: 'http://example.com',
    repoDescription: 'Repo Desc 1',
    avatarUrl: 'http://avatar.com',
    lastUpdated: '2023-01-01',
    hasMCP: false,
    hasContext: false,
    hasHooks: false,
    hasSkills: false,
    hasCustomCommands: false,
    isGoogleOwned: false,
    licenseKey: 'mit',
  },
  {
    id: 'ext2',
    extensionName: 'Test Extension 2',
    extensionDescription: 'Description 2',
    fullName: 'author/ext2',
    extensionVersion: '2.0.0',
    rank: 2,
    stars: 20,
    url: 'http://example.com/2',
    repoDescription: 'Repo Desc 2',
    avatarUrl: 'http://avatar.com/2',
    lastUpdated: '2023-01-02',
    hasMCP: true,
    hasContext: true,
    hasHooks: true,
    hasSkills: true,
    hasCustomCommands: true,
    isGoogleOwned: true,
    licenseKey: 'apache-2.0',
  },
];

describe('ExtensionRegistryView', () => {
  let mockExtensionManager: ExtensionManager;
  let mockOnSelect: ReturnType<typeof vi.fn>;
  let mockOnClose: ReturnType<typeof vi.fn>;
  let mockSearch: ReturnType<typeof vi.fn>;

  beforeEach(() => {
    vi.clearAllMocks();

    mockExtensionManager = {
      getExtensions: vi.fn().mockReturnValue([]),
    } as unknown as ExtensionManager;

    mockOnSelect = vi.fn();
    mockOnClose = vi.fn();
    mockSearch = vi.fn();

    vi.mocked(useExtensionRegistry).mockReturnValue({
      extensions: mockExtensions,
      loading: false,
      error: null,
      search: mockSearch,
    });

    vi.mocked(useExtensionUpdates).mockReturnValue({
      extensionsUpdateState: new Map(),
      dispatchExtensionStateUpdate: vi.fn(),
    } as unknown as ReturnType<typeof useExtensionUpdates>);

    // Mock useRegistrySearch implementation
    vi.mocked(useRegistrySearch).mockImplementation(
      (props: { items: GenericListItem[]; onSearch?: (q: string) => void }) =>
        ({
          filteredItems: props.items, // Pass through items
          searchBuffer: {
            text: '',
            cursorOffset: 0,
            viewport: { width: 10, height: 1 },
            visualCursor: [0, 0] as [number, number],
            viewportVisualLines: [{ text: '', visualRowIndex: 0 }],
            visualScrollRow: 0,
            lines: [''],
            cursor: [0, 0] as [number, number],
            selectionAnchor: undefined,
          } as unknown as TextBuffer,
          searchQuery: '',
          setSearchQuery: vi.fn(),
          maxLabelWidth: 10,
        }) as unknown as SearchListState<GenericListItem>,
    );
  });

  const renderView = async () =>
    renderWithProviders(
      <ExtensionRegistryView
        extensionManager={mockExtensionManager}
        onSelect={mockOnSelect}
        onClose={mockOnClose}
      />,
      {
        config: makeFakeConfig(),
        uiState: {
          staticExtraHeight: 5,
          terminalHeight: 40,
          historyManager: {
            addItem: vi.fn(),
          } as unknown as UseHistoryManagerReturn,
        } as Partial<UIState>,
      },
    );

  it('should render extensions', async () => {
    const { lastFrame } = await renderView();

    await waitFor(() => {
      expect(lastFrame()).toContain('Test Extension 1');
      expect(lastFrame()).toContain('Test Extension 2');
    });
  });

  it('should use useRegistrySearch hook', async () => {
    await renderView();
    expect(useRegistrySearch).toHaveBeenCalled();
  });

  it('should call search function when typing', async () => {
    // Mock useRegistrySearch to trigger onSearch
    vi.mocked(useRegistrySearch).mockImplementation(
      (props: {
        items: GenericListItem[];
        onSearch?: (q: string) => void;
      }): SearchListState<GenericListItem> => {
        const { onSearch } = props;
        // Simulate typing
        React.useEffect(() => {
          if (onSearch) {
            onSearch('test query');
          }
        }, [onSearch]);
        return {
          filteredItems: props.items,
          searchBuffer: {
            text: 'test query',
            cursorOffset: 10,
            viewport: { width: 10, height: 1 },
            visualCursor: [0, 10] as [number, number],
            viewportVisualLines: [{ text: 'test query', visualRowIndex: 0 }],
            visualScrollRow: 0,
            lines: ['test query'],
            cursor: [0, 10] as [number, number],
            selectionAnchor: undefined,
          } as unknown as TextBuffer,
          searchQuery: 'test query',
          setSearchQuery: vi.fn(),
          maxLabelWidth: 10,
        } as unknown as SearchListState<GenericListItem>;
      },
    );

    await renderView();

    await waitFor(() => {
      expect(useRegistrySearch).toHaveBeenCalledWith(
        expect.objectContaining({
          onSearch: mockSearch,
        }),
      );
    });
  });

  it('should call onSelect when extension is selected and Enter is pressed in details', async () => {
    const { stdin, lastFrame } = await renderView();

    // Select the first extension in the list (Enter opens details)
    await React.act(async () => {
      stdin.write('\r');
    });

    // Verify we are in details view
    await waitFor(() => {
      expect(lastFrame()).toContain('author/ext1');
      expect(lastFrame()).toContain('[Enter] Install');
    });

    // Ensure onSelect hasn't been called yet
    expect(mockOnSelect).not.toHaveBeenCalled();

    // Press Enter again in the details view to trigger install
    await React.act(async () => {
      stdin.write('\r');
    });

    await waitFor(() => {
      expect(mockOnSelect).toHaveBeenCalledWith(
        mockExtensions[0],
        expect.any(Function),
      );
    });
  });

  it('should show [Update available] and hide [Installed] when update is available', async () => {
    mockExtensionManager.getExtensions = vi
      .fn()
      .mockReturnValue([{ name: 'Test Extension 1' }]);
    vi.mocked(useExtensionUpdates).mockReturnValue({
      extensionsUpdateState: new Map([
        ['Test Extension 1', ExtensionUpdateState.UPDATE_AVAILABLE],
      ]),
      dispatchExtensionStateUpdate: vi.fn(),
    } as unknown as ReturnType<typeof useExtensionUpdates>);

    const { lastFrame } = await renderView();

    await waitFor(() => {
      expect(lastFrame()).toContain('[Update available]');
      expect(lastFrame()).not.toContain('[Installed]');
    });
  });

  it('should show [Updating...] and hide [Installed] when update is in progress', async () => {
    mockExtensionManager.getExtensions = vi
      .fn()
      .mockReturnValue([{ name: 'Test Extension 1' }]);
    vi.mocked(useExtensionUpdates).mockReturnValue({
      extensionsUpdateState: new Map([
        ['Test Extension 1', ExtensionUpdateState.UPDATING],
      ]),
      dispatchExtensionStateUpdate: vi.fn(),
    } as unknown as ReturnType<typeof useExtensionUpdates>);

    const { lastFrame } = await renderView();

    await waitFor(() => {
      expect(lastFrame()).toContain('[Updating...]');
      expect(lastFrame()).not.toContain('[Installed]');
    });
  });
});