File size: 4,643 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
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { render } from '../../../test-utils/render.js';
import { vi, describe, beforeEach, it, expect } from 'vitest';
import { useUIState } from '../../contexts/UIStateContext.js';
import { ExtensionUpdateState } from '../../state/extensions.js';
import { ExtensionsList } from './ExtensionsList.js';

vi.mock('../../contexts/UIStateContext.js');

const mockUseUIState = vi.mocked(useUIState);

const mockExtensions = [
  {
    name: 'ext-one',
    version: '1.0.0',
    isActive: true,
    path: '/path/to/ext-one',
    contextFiles: [],
    id: '',
  },
  {
    name: 'ext-two',
    version: '2.1.0',
    isActive: true,
    path: '/path/to/ext-two',
    contextFiles: [],
    id: '',
  },
  {
    name: 'ext-disabled',
    version: '3.0.0',
    isActive: false,
    path: '/path/to/ext-disabled',
    contextFiles: [],
    id: '',
  },
];

describe('<ExtensionsList />', () => {
  beforeEach(() => {
    vi.resetAllMocks();
  });

  const mockUIState = (
    extensionsUpdateState: Map<string, ExtensionUpdateState>,
  ) => {
    mockUseUIState.mockReturnValue({
      extensionsUpdateState,
      // Add other required properties from UIState if needed by the component
    } as never);
  };

  it('should render "No extensions installed." if there are no extensions', async () => {
    mockUIState(new Map());
    const { lastFrame, unmount } = await render(
      <ExtensionsList extensions={[]} />,
    );
    expect(lastFrame()).toContain('No extensions installed.');
    unmount();
  });

  it('should render a list of extensions with their version and status', async () => {
    mockUIState(new Map());
    const { lastFrame, unmount } = await render(
      <ExtensionsList extensions={mockExtensions} />,
    );
    const output = lastFrame();
    expect(output).toContain('ext-one (v1.0.0) - active');
    expect(output).toContain('ext-two (v2.1.0) - active');
    expect(output).toContain('ext-disabled (v3.0.0) - disabled');
    unmount();
  });

  it('should display "unknown state" if an extension has no update state', async () => {
    mockUIState(new Map());
    const { lastFrame, unmount } = await render(
      <ExtensionsList extensions={[mockExtensions[0]]} />,
    );
    expect(lastFrame()).toContain('(unknown state)');
    unmount();
  });

  it.each([
    {
      state: ExtensionUpdateState.CHECKING_FOR_UPDATES,
      expectedText: '(checking for updates)',
    },
    {
      state: ExtensionUpdateState.UPDATING,
      expectedText: '(updating)',
    },
    {
      state: ExtensionUpdateState.UPDATE_AVAILABLE,
      expectedText: '(update available)',
    },
    {
      state: ExtensionUpdateState.UPDATED_NEEDS_RESTART,
      expectedText: '(updated, needs restart)',
    },
    {
      state: ExtensionUpdateState.UPDATED,
      expectedText: '(updated)',
    },
    {
      state: ExtensionUpdateState.ERROR,
      expectedText: '(error)',
    },
    {
      state: ExtensionUpdateState.UP_TO_DATE,
      expectedText: '(up to date)',
    },
  ])(
    'should correctly display the state: $state',
    async ({ state, expectedText }) => {
      const updateState = new Map([[mockExtensions[0].name, state]]);
      mockUIState(updateState);
      const { lastFrame, unmount } = await render(
        <ExtensionsList extensions={[mockExtensions[0]]} />,
      );
      expect(lastFrame()).toContain(expectedText);
      unmount();
    },
  );

  it('should render resolved settings for an extension', async () => {
    mockUIState(new Map());
    const extensionWithSettings = {
      ...mockExtensions[0],
      resolvedSettings: [
        {
          name: 'sensitiveApiKey',
          value: '***',
          envVar: 'API_KEY',
          sensitive: true,
        },
        {
          name: 'maxTokens',
          value: '1000',
          envVar: 'MAX_TOKENS',
          sensitive: false,
          scope: 'user' as const,
          source: '/path/to/.env',
        },
        {
          name: 'model',
          value: 'gemini-pro',
          envVar: 'MODEL',
          sensitive: false,
          scope: 'workspace' as const,
          source: 'Keychain',
        },
      ],
    };
    const { lastFrame, unmount } = await render(
      <ExtensionsList extensions={[extensionWithSettings]} />,
    );
    const output = lastFrame();
    expect(output).toContain('settings:');
    expect(output).toContain('- sensitiveApiKey: ***');
    expect(output).toContain('- maxTokens: 1000 (User - /path/to/.env)');
    expect(output).toContain('- model: gemini-pro (Workspace - Keychain)');
    unmount();
  });
});