-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElectronStoreService.test.ts
More file actions
233 lines (183 loc) · 7.81 KB
/
ElectronStoreService.test.ts
File metadata and controls
233 lines (183 loc) · 7.81 KB
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
/**
* Unit tests for ElectronStoreService.
*
* `electron-store` is mocked at the module level so no real disk I/O or
* Electron native modules are ever touched. Protected methods
* (`readIsElectron`, `createStore`) are stubbed via `vi.spyOn` on the
* prototype before each Electron-path construction so the constructor takes
* the right branch.
*/
import 'reflect-metadata';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import type Store from 'electron-store';
vi.mock('../logger.js', () => ({
logger: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() },
}));
vi.mock('electron-store', () => {
const MockStore = vi.fn().mockImplementation(() => ({
get: vi.fn(),
set: vi.fn(),
}));
return { default: MockStore };
});
import { ElectronStoreService, type AppStoreSchema } from './ElectronStoreService.js';
import { SafeStorageService } from './SafeStorageService.js';
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/**
* Creates a `SafeStorageService` whose `encrypt` / `decrypt` methods are
* identity functions by default (outside-Electron degraded path).
*/
function makeSafeStorage(): SafeStorageService {
return new SafeStorageService();
}
/**
* Builds a minimal mock `Store<AppStoreSchema>` compatible with what
* `ElectronStoreService` calls on it.
*/
function makeMockStore(): Store<AppStoreSchema> {
return {
get: vi.fn(),
set: vi.fn(),
} as unknown as Store<AppStoreSchema>;
}
// ---------------------------------------------------------------------------
// Non-Electron path (Map fallback)
// ---------------------------------------------------------------------------
describe('ElectronStoreService — non-Electron path (Map fallback)', () => {
let service: ElectronStoreService;
let safeStorage: SafeStorageService;
beforeEach(() => {
safeStorage = makeSafeStorage();
// process.versions['electron'] is not set in Vitest/Node, so the Map
// fallback is used automatically — no spy needed.
service = new ElectronStoreService(safeStorage);
vi.clearAllMocks();
});
it('should use Map fallback when not running in Electron', () => {
expect(service.isElectron()).toBe(false);
expect(service.get('wizardCompleted')).toBeUndefined();
});
it('should store and retrieve a value in Map fallback', () => {
service.set('wizardCompleted', true);
expect(service.get('wizardCompleted')).toBe(true);
});
it('should store and retrieve a nested object in Map fallback', () => {
const awsValue: AppStoreSchema['aws'] = { region: 'us-east-1', profile: 'default' };
service.set('aws', awsValue);
expect(service.get('aws')).toEqual(awsValue);
});
});
// ---------------------------------------------------------------------------
// Electron path (mocked Store)
// ---------------------------------------------------------------------------
describe('ElectronStoreService — Electron path (mocked Store)', () => {
let service: ElectronStoreService;
let safeStorage: SafeStorageService;
let mockStore: Store<AppStoreSchema>;
beforeEach(() => {
safeStorage = makeSafeStorage();
mockStore = makeMockStore();
// Stub prototype BEFORE construction so the constructor takes the Electron branch.
vi.spyOn(
ElectronStoreService.prototype as unknown as { readIsElectron(): boolean },
'readIsElectron',
).mockReturnValue(true);
vi.spyOn(
ElectronStoreService.prototype as unknown as { createStore(): Store<AppStoreSchema> },
'createStore',
).mockReturnValue(mockStore);
service = new ElectronStoreService(safeStorage);
vi.clearAllMocks();
});
it('should call store.get when running in Electron', () => {
(mockStore.get as ReturnType<typeof vi.fn>).mockReturnValue(true);
const result = service.get('wizardCompleted');
expect(mockStore.get).toHaveBeenCalledWith('wizardCompleted');
expect(result).toBe(true);
});
it('should call store.set when running in Electron', () => {
service.set('wizardCompleted', true);
expect(mockStore.set).toHaveBeenCalledWith('wizardCompleted', true);
});
});
// ---------------------------------------------------------------------------
// Secret field — setSecretAccessKeyId / getSecretAccessKeyId
// ---------------------------------------------------------------------------
describe('ElectronStoreService — setSecretAccessKeyId / getSecretAccessKeyId', () => {
let service: ElectronStoreService;
let safeStorage: SafeStorageService;
beforeEach(() => {
safeStorage = makeSafeStorage();
service = new ElectronStoreService(safeStorage);
vi.clearAllMocks();
});
it('should encrypt accessKeyId before storing', () => {
vi.spyOn(safeStorage, 'encrypt').mockReturnValue('enc-key-id');
service.setSecretAccessKeyId('AKID123');
expect(safeStorage.encrypt).toHaveBeenCalledWith('AKID123');
const stored = service.get('aws');
expect(stored?.accessKeyId).toBe('enc-key-id');
});
it('should decrypt accessKeyId when reading', () => {
service.set('aws', { region: 'us-east-1', profile: 'default', accessKeyId: 'enc-key-id' });
vi.spyOn(safeStorage, 'decrypt').mockReturnValue('AKID123');
const result = service.getSecretAccessKeyId();
expect(safeStorage.decrypt).toHaveBeenCalledWith('enc-key-id');
expect(result).toBe('AKID123');
});
it('should return undefined for accessKeyId when not stored', () => {
expect(service.getSecretAccessKeyId()).toBeUndefined();
});
});
// ---------------------------------------------------------------------------
// Secret field — setSecretAccessKey / getSecretAccessKey
// ---------------------------------------------------------------------------
describe('ElectronStoreService — setSecretAccessKey / getSecretAccessKey', () => {
let service: ElectronStoreService;
let safeStorage: SafeStorageService;
beforeEach(() => {
safeStorage = makeSafeStorage();
service = new ElectronStoreService(safeStorage);
vi.clearAllMocks();
});
it('should encrypt secretAccessKey before storing', () => {
vi.spyOn(safeStorage, 'encrypt').mockReturnValue('enc-secret-key');
service.setSecretAccessKey('MY_SECRET');
expect(safeStorage.encrypt).toHaveBeenCalledWith('MY_SECRET');
const stored = service.get('aws');
expect(stored?.secretAccessKey).toBe('enc-secret-key');
});
it('should decrypt secretAccessKey when reading', () => {
service.set('aws', { region: 'us-east-1', profile: 'default', secretAccessKey: 'enc-secret-key' });
vi.spyOn(safeStorage, 'decrypt').mockReturnValue('MY_SECRET');
const result = service.getSecretAccessKey();
expect(safeStorage.decrypt).toHaveBeenCalledWith('enc-secret-key');
expect(result).toBe('MY_SECRET');
});
it('should return undefined for secretAccessKey when not stored', () => {
expect(service.getSecretAccessKey()).toBeUndefined();
});
});
// ---------------------------------------------------------------------------
// Round-trip
// ---------------------------------------------------------------------------
describe('ElectronStoreService — round-trip', () => {
let service: ElectronStoreService;
let safeStorage: SafeStorageService;
beforeEach(() => {
safeStorage = makeSafeStorage();
service = new ElectronStoreService(safeStorage);
vi.clearAllMocks();
});
it('should encrypt and decrypt accessKeyId in a round-trip', () => {
vi.spyOn(safeStorage, 'encrypt').mockImplementation((plaintext: string) => `enc-${plaintext}`);
vi.spyOn(safeStorage, 'decrypt').mockImplementation((ciphertext: string) =>
ciphertext.startsWith('enc-') ? ciphertext.slice(4) : ciphertext,
);
service.setSecretAccessKeyId('AKID123');
const result = service.getSecretAccessKeyId();
expect(result).toBe('AKID123');
});
});