File size: 7,507 Bytes
9332ccf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
275
276
277
278
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, it, expect } from 'vitest';
import {
  extensionUpdatesReducer,
  type ExtensionUpdatesState,
  ExtensionUpdateState,
  initialExtensionUpdatesState,
} from './extensions.js';

describe('extensionUpdatesReducer', () => {
  describe('SET_STATE', () => {
    it.each([
      ExtensionUpdateState.UPDATE_AVAILABLE,
      ExtensionUpdateState.UPDATED,
      ExtensionUpdateState.ERROR,
    ])('should handle SET_STATE action for state: %s', (state) => {
      const action = {
        type: 'SET_STATE' as const,
        payload: { name: 'ext1', state },
      };

      const newState = extensionUpdatesReducer(
        initialExtensionUpdatesState,
        action,
      );

      expect(newState.extensionStatuses.get('ext1')).toEqual({
        status: state,
        notified: false,
      });
    });

    it('should not update state if SET_STATE payload is identical to existing state', () => {
      const initialState: ExtensionUpdatesState = {
        ...initialExtensionUpdatesState,
        extensionStatuses: new Map([
          [
            'ext1',
            {
              status: ExtensionUpdateState.UPDATE_AVAILABLE,
              notified: false,
            },
          ],
        ]),
      };

      const action = {
        type: 'SET_STATE' as const,
        payload: { name: 'ext1', state: ExtensionUpdateState.UPDATE_AVAILABLE },
      };

      const newState = extensionUpdatesReducer(initialState, action);

      expect(newState).toBe(initialState);
    });
  });

  describe('SET_NOTIFIED', () => {
    it.each([true, false])(
      'should handle SET_NOTIFIED action with notified: %s',
      (notified) => {
        const initialState: ExtensionUpdatesState = {
          ...initialExtensionUpdatesState,
          extensionStatuses: new Map([
            [
              'ext1',
              {
                status: ExtensionUpdateState.UPDATE_AVAILABLE,
                notified: !notified,
              },
            ],
          ]),
        };

        const action = {
          type: 'SET_NOTIFIED' as const,
          payload: { name: 'ext1', notified },
        };

        const newState = extensionUpdatesReducer(initialState, action);

        expect(newState.extensionStatuses.get('ext1')).toEqual({
          status: ExtensionUpdateState.UPDATE_AVAILABLE,
          notified,
        });
      },
    );

    it('should not update state if SET_NOTIFIED payload is identical to existing state', () => {
      const initialState: ExtensionUpdatesState = {
        ...initialExtensionUpdatesState,
        extensionStatuses: new Map([
          [
            'ext1',
            {
              status: ExtensionUpdateState.UPDATE_AVAILABLE,
              notified: true,
            },
          ],
        ]),
      };

      const action = {
        type: 'SET_NOTIFIED' as const,
        payload: { name: 'ext1', notified: true },
      };

      const newState = extensionUpdatesReducer(initialState, action);

      expect(newState).toBe(initialState);
    });

    it('should ignore SET_NOTIFIED if extension does not exist', () => {
      const action = {
        type: 'SET_NOTIFIED' as const,
        payload: { name: 'non-existent', notified: true },
      };

      const newState = extensionUpdatesReducer(
        initialExtensionUpdatesState,
        action,
      );

      expect(newState).toBe(initialExtensionUpdatesState);
    });
  });

  describe('Batch Checks', () => {
    it('should handle BATCH_CHECK_START action', () => {
      const action = { type: 'BATCH_CHECK_START' as const };
      const newState = extensionUpdatesReducer(
        initialExtensionUpdatesState,
        action,
      );
      expect(newState.batchChecksInProgress).toBe(1);
    });

    it('should handle BATCH_CHECK_END action', () => {
      const initialState = {
        ...initialExtensionUpdatesState,
        batchChecksInProgress: 1,
      };
      const action = { type: 'BATCH_CHECK_END' as const };
      const newState = extensionUpdatesReducer(initialState, action);
      expect(newState.batchChecksInProgress).toBe(0);
    });
  });

  describe('Scheduled Updates', () => {
    it('should handle SCHEDULE_UPDATE action', () => {
      const callback = () => {};
      const action = {
        type: 'SCHEDULE_UPDATE' as const,
        payload: {
          names: ['ext1'],
          all: false,
          onComplete: callback,
        },
      };

      const newState = extensionUpdatesReducer(
        initialExtensionUpdatesState,
        action,
      );

      expect(newState.scheduledUpdate).toEqual({
        names: ['ext1'],
        all: false,
        onCompleteCallbacks: [callback],
      });
    });

    it('should merge SCHEDULE_UPDATE with existing scheduled update', () => {
      const callback1 = () => {};
      const callback2 = () => {};
      const initialState: ExtensionUpdatesState = {
        ...initialExtensionUpdatesState,
        scheduledUpdate: {
          names: ['ext1'],
          all: false,
          onCompleteCallbacks: [callback1],
        },
      };

      const action = {
        type: 'SCHEDULE_UPDATE' as const,
        payload: {
          names: ['ext2'],
          all: true,
          onComplete: callback2,
        },
      };

      const newState = extensionUpdatesReducer(initialState, action);

      expect(newState.scheduledUpdate).toEqual({
        names: ['ext1', 'ext2'],
        all: true, // Should be true if any update is all: true
        onCompleteCallbacks: [callback1, callback2],
      });
    });

    it('should handle CLEAR_SCHEDULED_UPDATE action', () => {
      const initialState: ExtensionUpdatesState = {
        ...initialExtensionUpdatesState,
        scheduledUpdate: {
          names: ['ext1'],
          all: false,
          onCompleteCallbacks: [],
        },
      };

      const action = { type: 'CLEAR_SCHEDULED_UPDATE' as const };
      const newState = extensionUpdatesReducer(initialState, action);

      expect(newState.scheduledUpdate).toBeNull();
    });
  });

  describe('RESTARTED', () => {
    it('should handle RESTARTED action', () => {
      const initialState: ExtensionUpdatesState = {
        ...initialExtensionUpdatesState,
        extensionStatuses: new Map([
          [
            'ext1',
            {
              status: ExtensionUpdateState.UPDATED_NEEDS_RESTART,
              notified: true,
            },
          ],
        ]),
      };

      const action = {
        type: 'RESTARTED' as const,
        payload: { name: 'ext1' },
      };

      const newState = extensionUpdatesReducer(initialState, action);

      expect(newState.extensionStatuses.get('ext1')).toEqual({
        status: ExtensionUpdateState.UPDATED,
        notified: true,
      });
    });

    it('should not change state for RESTARTED action if status is not UPDATED_NEEDS_RESTART', () => {
      const initialState: ExtensionUpdatesState = {
        ...initialExtensionUpdatesState,
        extensionStatuses: new Map([
          [
            'ext1',
            {
              status: ExtensionUpdateState.UPDATED,
              notified: true,
            },
          ],
        ]),
      };

      const action = {
        type: 'RESTARTED' as const,
        payload: { name: 'ext1' },
      };

      const newState = extensionUpdatesReducer(initialState, action);

      expect(newState).toBe(initialState);
    });
  });
});