-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathfirst-run.test.ts
More file actions
126 lines (96 loc) · 3.74 KB
/
first-run.test.ts
File metadata and controls
126 lines (96 loc) · 3.74 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
import path from 'node:path';
const existsSyncMock = vi.fn();
const mkdirSyncMock = vi.fn();
const writeFileSyncMock = vi.fn();
vi.mock('node:fs', () => ({
__esModule: true,
default: {
existsSync: (...a: unknown[]) => existsSyncMock(...a),
mkdirSync: (...a: unknown[]) => mkdirSyncMock(...a),
writeFileSync: (...a: unknown[]) => writeFileSyncMock(...a),
},
existsSync: (...a: unknown[]) => existsSyncMock(...a),
mkdirSync: (...a: unknown[]) => mkdirSyncMock(...a),
writeFileSync: (...a: unknown[]) => writeFileSyncMock(...a),
}));
const moveToApplicationsFolderMock = vi.fn();
const isInApplicationsFolderMock = vi.fn(() => false);
const getPathMock = vi.fn(() => '/User/Data');
const showMessageBoxMock = vi.fn(async () => ({ response: 0 }));
vi.mock('electron', () => ({
app: {
getPath: () => getPathMock(),
isInApplicationsFolder: () => isInApplicationsFolderMock(),
moveToApplicationsFolder: () => moveToApplicationsFolderMock(),
},
dialog: { showMessageBox: () => showMessageBoxMock() },
}));
// Ensure the module under test thinks we're not in dev mode
vi.mock('../utils', () => ({ isDevMode: () => false }));
const logErrorMock = vi.fn();
vi.mock('../../shared/logger', async (importOriginal) => {
const actual = await importOriginal<typeof import('../../shared/logger')>();
return {
...actual,
logError: (...a: unknown[]) => logErrorMock(...a),
};
});
let mac = true;
vi.mock('../../shared/platform', () => ({ isMacOS: () => mac }));
import { APPLICATION } from '../../shared/constants';
import { onFirstRunMaybe } from './first-run';
function configPath() {
return path.join('/User/Data', 'FirstRun', APPLICATION.FIRST_RUN_FOLDER);
}
describe('main/lifecycle/first-run', () => {
beforeEach(() => {
mac = true;
});
it('creates first-run marker when not existing and returns true', async () => {
existsSyncMock.mockReturnValueOnce(false); // marker absent
existsSyncMock.mockReturnValueOnce(false); // folder absent
await onFirstRunMaybe();
expect(mkdirSyncMock).toHaveBeenCalledWith(path.dirname(configPath()));
expect(writeFileSyncMock).toHaveBeenCalledWith(configPath(), '');
});
it('skips writing when marker exists', async () => {
existsSyncMock.mockReturnValueOnce(true); // marker present
await onFirstRunMaybe();
expect(writeFileSyncMock).not.toHaveBeenCalled();
expect(mkdirSyncMock).not.toHaveBeenCalled();
});
it('handles fs write error gracefully', async () => {
existsSyncMock.mockReturnValueOnce(false); // marker absent
existsSyncMock.mockReturnValueOnce(true); // folder exists
writeFileSyncMock.mockImplementation(() => {
throw new Error('fail');
});
await onFirstRunMaybe();
expect(logErrorMock).toHaveBeenCalledWith(
'checkAndMarkFirstRun',
'Unable to write firstRun file',
expect.any(Error),
);
});
it('prompts and moves app on macOS when user accepts', async () => {
existsSyncMock.mockReturnValueOnce(false); // marker
existsSyncMock.mockReturnValueOnce(false); // folder
showMessageBoxMock.mockResolvedValueOnce({ response: 0 });
await onFirstRunMaybe();
expect(moveToApplicationsFolderMock).toHaveBeenCalled();
});
it('does not move when user declines', async () => {
existsSyncMock.mockReturnValueOnce(false);
existsSyncMock.mockReturnValueOnce(false);
showMessageBoxMock.mockResolvedValueOnce({ response: 1 });
await onFirstRunMaybe();
expect(moveToApplicationsFolderMock).not.toHaveBeenCalled();
});
it('does not prompt on non-macOS', async () => {
mac = false;
existsSyncMock.mockReturnValueOnce(false);
existsSyncMock.mockReturnValueOnce(false);
await onFirstRunMaybe();
expect(showMessageBoxMock).not.toHaveBeenCalled();
});
});