-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathconfig.test.js
More file actions
293 lines (250 loc) · 10.2 KB
/
config.test.js
File metadata and controls
293 lines (250 loc) · 10.2 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
const path = require('path');
// Mock nedb before requiring Config
jest.mock('nedb', () => {
return jest.fn().mockImplementation((opts) => ({
_filename: opts?.filename,
findOne: jest.fn(),
insert: jest.fn(),
update: jest.fn(),
}));
});
// Mock fs
jest.mock('fs', () => ({
existsSync: jest.fn(() => false),
readFileSync: jest.fn(() => '{}'),
writeFileSync: jest.fn(),
mkdirSync: jest.fn(),
readdirSync: jest.fn(() => []),
}));
const fs = require('fs');
const Datastore = require('nedb');
let Config;
beforeEach(() => {
// Reset singleton before each test
jest.isolateModules(() => {
Config = require('./config');
});
// Also clear the static instance in case it leaked
if (Config.instance) {
Config.instance = null;
}
jest.clearAllMocks();
// Default: no savePath.json found
fs.existsSync.mockReturnValue(false);
fs.readFileSync.mockReturnValue('{}');
});
describe('Config singleton', () => {
it('getInstance() returns a Config instance', () => {
const instance = Config.getInstance();
expect(instance).toBeInstanceOf(Config);
});
it('getInstance() always returns the same instance', () => {
const a = Config.getInstance();
const b = Config.getInstance();
expect(a).toBe(b);
});
it('new Config() returns existing instance if already created', () => {
const a = new Config();
const b = new Config();
expect(a).toBe(b);
});
});
describe('getIsBuild()', () => {
it('returns the IS_BUILD value', () => {
const instance = Config.getInstance();
const val = instance.getIsBuild();
expect(typeof val).toBe('boolean');
});
});
describe('getAssetsPath()', () => {
it('returns a string path', () => {
const instance = Config.getInstance();
const p = instance.getAssetsPath();
expect(typeof p).toBe('string');
expect(p).toContain('assets');
});
});
describe('getDefaultExecPath()', () => {
it('returns a path string', () => {
const instance = Config.getInstance();
const p = instance.getDefaultExecPath();
expect(typeof p).toBe('string');
});
});
describe('getSavePath() / setSavePath()', () => {
it('getSavePath() returns no path when none configured', () => {
const instance = Config.getInstance();
const result = instance.getSavePath();
expect(result.path).toBeFalsy();
});
it('setSavePath() updates savePath and creates databases', async () => {
const instance = Config.getInstance();
instance.loadDefaultTask = jest.fn();
instance.refreshData = jest.fn().mockResolvedValue({ success: true });
const callsBefore = Datastore.mock.calls.length;
await instance.setSavePath('/test/save/path');
expect(instance.getSavePath()).toEqual({ success: true, path: '/test/save/path' });
// Should have created 3 new Datastore instances (wallet, task, fingerprint)
const newCalls = Datastore.mock.calls.slice(callsBefore);
expect(newCalls.length).toBe(3);
const filenames = newCalls.map(c => c[0]?.filename);
expect(filenames.some(f => f.includes('walletData.db'))).toBe(true);
expect(filenames.some(f => f.includes('task.db'))).toBe(true);
expect(filenames.some(f => f.includes('fingerPrint.db'))).toBe(true);
});
it('setSavePath() persists path to JSON file', async () => {
const instance = Config.getInstance();
instance.loadDefaultTask = jest.fn();
instance.refreshData = jest.fn().mockResolvedValue({ success: true });
await instance.setSavePath('/my/path');
expect(fs.writeFileSync).toHaveBeenCalled();
const writeCall = fs.writeFileSync.mock.calls.find(call =>
call[1]?.includes('/my/path')
);
expect(writeCall).toBeTruthy();
});
});
describe('getChromePath() / setChromePath()', () => {
it('getChromePath() returns no path when none configured', () => {
const instance = Config.getInstance();
const result = instance.getChromePath();
expect(result.path).toBeFalsy();
});
it('setChromePath() stores and retrieves chromePath', () => {
const instance = Config.getInstance();
instance.setChromePath('/usr/bin/chrome');
const result = instance.getChromePath();
expect(result).toEqual({ success: true, path: '/usr/bin/chrome' });
});
it('setChromePath() returns success', () => {
const instance = Config.getInstance();
const result = instance.setChromePath('/usr/bin/chrome');
expect(result).toEqual({ success: true });
});
});
describe('database getters', () => {
it('getWalletDb() returns undefined when no savePath set', () => {
const instance = Config.getInstance();
expect(instance.getWalletDb()).toBeUndefined();
});
it('getTaskDb() returns undefined when no savePath set', () => {
const instance = Config.getInstance();
expect(instance.getTaskDb()).toBeUndefined();
});
it('getFingerPrintDb() returns undefined when no savePath set', () => {
const instance = Config.getInstance();
expect(instance.getFingerPrintDb()).toBeUndefined();
});
it('database getters return Datastore instances after setSavePath()', async () => {
const instance = Config.getInstance();
instance.loadDefaultTask = jest.fn();
instance.refreshData = jest.fn().mockResolvedValue({ success: true });
await instance.setSavePath('/test/db/path');
expect(instance.getWalletDb()).toBeDefined();
expect(instance.getTaskDb()).toBeDefined();
expect(instance.getFingerPrintDb()).toBeDefined();
});
});
describe('getConfigDir()', () => {
it('returns APP_USER_DATA env var when set', () => {
const orig = process.env.APP_USER_DATA;
process.env.APP_USER_DATA = '/custom/config/dir';
const instance = Config.getInstance();
expect(instance.getConfigDir()).toBe('/custom/config/dir');
if (orig !== undefined) {
process.env.APP_USER_DATA = orig;
} else {
delete process.env.APP_USER_DATA;
}
});
it('falls back to assetsPath when APP_USER_DATA is not set', () => {
const orig = process.env.APP_USER_DATA;
delete process.env.APP_USER_DATA;
const instance = Config.getInstance();
expect(instance.getConfigDir()).toBe(instance.getAssetsPath());
if (orig !== undefined) {
process.env.APP_USER_DATA = orig;
}
});
});
describe('_loadAllPathsFromJson()', () => {
it('loads paths from savePath.json when it exists', () => {
fs.existsSync.mockImplementation((p) => {
if (typeof p === 'string' && p.endsWith('savePath.json')) return true;
return false;
});
fs.readFileSync.mockReturnValue(JSON.stringify({ path: '/loaded/path', chromePath: '/loaded/chrome' }));
const instance = Config.getInstance();
const result = instance.getSavePath();
expect(result.path).toBe('/loaded/path');
});
it('handles corrupted JSON gracefully', () => {
fs.existsSync.mockImplementation((p) => {
if (typeof p === 'string' && p.endsWith('savePath.json')) return true;
return false;
});
fs.readFileSync.mockReturnValue('not-valid-json');
// Should not throw
expect(() => Config.getInstance()).not.toThrow();
});
});
describe('_saveAllPathsToJson()', () => {
it('creates config directory if it does not exist', () => {
fs.existsSync.mockReturnValue(false);
const instance = Config.getInstance();
// Trigger a save
instance.setChromePath('/test/chrome');
// Should have tried to create directory
expect(fs.mkdirSync).toHaveBeenCalled();
});
it('writes JSON to savePath.json', () => {
const instance = Config.getInstance();
instance.setChromePath('/test/chrome');
const writeCall = fs.writeFileSync.mock.calls.find(call =>
typeof call[0] === 'string' && call[0].endsWith('savePath.json')
);
expect(writeCall).toBeTruthy();
const written = JSON.parse(writeCall[1]);
expect(written.chromePath).toBe('/test/chrome');
});
});
describe('getPlatform()', () => {
it('returns the current platform', () => {
const instance = Config.getInstance();
const platform = instance.getPlatform();
expect(['win32', 'darwin']).toContain(platform);
});
});
describe('getDefaultScriptPath()', () => {
it('joins relative path with default script directory', () => {
const instance = Config.getInstance();
const result = instance.getDefaultScriptPath('myScript.js');
expect(result).toContain('scripts');
expect(result).toContain('myScript.js');
});
});
describe('getInstallerPath()', () => {
it('returns failure when no installer configured', () => {
fs.existsSync.mockReturnValue(false);
const instance = Config.getInstance();
const result = instance.getInstallerPath();
expect(result.success).toBe(false);
});
it('returns assets installer when it exists', () => {
const instance = Config.getInstance();
fs.existsSync.mockImplementation((p) => {
if (typeof p === 'string' && p.includes('mini_installer.exe')) return true;
return false;
});
const result = instance.getInstallerPath();
expect(result.success).toBe(true);
expect(result.path).toContain('mini_installer.exe');
});
it('returns user-set installer path when configured', () => {
const instance = Config.getInstance();
instance.setInstallerPath('/custom/installer.exe');
const result = instance.getInstallerPath();
expect(result.success).toBe(true);
expect(result.path).toBe('/custom/installer.exe');
});
});