|
1 | 1 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
2 | | -import { |
3 | | - getStoredApiToken, |
4 | | - setStoredApiToken, |
5 | | - setUnauthorizedHandler, |
6 | | - retryPendingAfterAuth, |
7 | | - api, |
8 | | -} from './api.service.js'; |
9 | | - |
10 | | -// jsdom provides localStorage, but we replace it with a controlled stub so |
11 | | -// tests are isolated from each other's stored tokens. |
12 | | -const localStorageMock = (() => { |
13 | | - let store: Record<string, string> = {}; |
14 | | - return { |
15 | | - getItem: vi.fn((key: string) => store[key] ?? null), |
16 | | - setItem: vi.fn((key: string, value: string) => { store[key] = value; }), |
17 | | - removeItem: vi.fn((key: string) => { delete store[key]; }), |
18 | | - clear: () => { store = {}; }, |
19 | | - }; |
20 | | -})(); |
| 2 | +import { api } from './api.service.js'; |
21 | 3 |
|
22 | 4 | /** |
23 | 5 | * Builds a fresh `window.gsd` IPC-bridge double. Every namespace method is a |
@@ -77,56 +59,15 @@ function makeGsdMock() { |
77 | 59 | let gsd: ReturnType<typeof makeGsdMock>; |
78 | 60 |
|
79 | 61 | beforeEach(() => { |
80 | | - localStorageMock.clear(); |
81 | | - localStorageMock.getItem.mockClear(); |
82 | | - localStorageMock.setItem.mockClear(); |
83 | | - localStorageMock.removeItem.mockClear(); |
84 | | - vi.stubGlobal('localStorage', localStorageMock); |
85 | 62 | gsd = makeGsdMock(); |
86 | 63 | vi.stubGlobal('gsd', gsd); |
87 | | - setStoredApiToken(''); |
88 | | - setUnauthorizedHandler(null); |
89 | 64 | }); |
90 | 65 |
|
91 | 66 | afterEach(() => { |
92 | 67 | vi.unstubAllGlobals(); |
93 | 68 | vi.restoreAllMocks(); |
94 | 69 | }); |
95 | 70 |
|
96 | | -describe('getStoredApiToken / setStoredApiToken', () => { |
97 | | - it('should return an empty string when no token has been stored', () => { |
98 | | - expect(getStoredApiToken()).toBe(''); |
99 | | - }); |
100 | | - |
101 | | - it('should persist and retrieve a non-empty token', () => { |
102 | | - setStoredApiToken('my-api-token'); |
103 | | - expect(getStoredApiToken()).toBe('my-api-token'); |
104 | | - }); |
105 | | - |
106 | | - it('should remove the stored token when called with an empty string', () => { |
107 | | - setStoredApiToken('tok'); |
108 | | - setStoredApiToken(''); |
109 | | - expect(getStoredApiToken()).toBe(''); |
110 | | - }); |
111 | | - |
112 | | - it('should return empty string when localStorage.getItem throws', () => { |
113 | | - vi.stubGlobal('localStorage', { |
114 | | - getItem: () => { throw new Error('unavailable'); }, |
115 | | - }); |
116 | | - expect(getStoredApiToken()).toBe(''); |
117 | | - }); |
118 | | - |
119 | | - it('should silently ignore setItem errors (e.g. private browsing quota)', () => { |
120 | | - vi.stubGlobal('localStorage', { |
121 | | - getItem: () => null, |
122 | | - setItem: () => { throw new Error('QuotaExceeded'); }, |
123 | | - removeItem: () => { throw new Error('unavailable'); }, |
124 | | - }); |
125 | | - expect(() => setStoredApiToken('tok')).not.toThrow(); |
126 | | - expect(() => setStoredApiToken('')).not.toThrow(); |
127 | | - }); |
128 | | -}); |
129 | | - |
130 | 71 | describe('IPC bridge delegation', () => { |
131 | 72 | it('should delegate api.env() to window.gsd.env.get()', async () => { |
132 | 73 | await api.env(); |
@@ -264,14 +205,3 @@ describe('missing IPC bridge', () => { |
264 | 205 | await expect(api.env()).rejects.toThrow('window.gsd IPC bridge is unavailable'); |
265 | 206 | }); |
266 | 207 | }); |
267 | | - |
268 | | -describe('inert auth stubs (retained for #162)', () => { |
269 | | - it('should treat setUnauthorizedHandler as a no-op that never throws', () => { |
270 | | - expect(() => setUnauthorizedHandler(vi.fn())).not.toThrow(); |
271 | | - expect(() => setUnauthorizedHandler(null)).not.toThrow(); |
272 | | - }); |
273 | | - |
274 | | - it('should resolve retryPendingAfterAuth to true since nothing is ever queued', async () => { |
275 | | - expect(await retryPendingAfterAuth()).toBe(true); |
276 | | - }); |
277 | | -}); |
0 commit comments