aboutsummaryrefslogtreecommitdiffstats
path: root/packages
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2021-12-31 01:52:28 +0100
committerLibravatar Kristóf Marussy <kristof@marussy.com>2021-12-31 01:56:30 +0100
commit7108c642f4ff6dc5f0c4d30b8a8960064ff8e90f (patch)
treef8c0450a6e1b62f7e7f8470efd375b3659b91b2b /packages
parentrefactor: Install devtools extensions earlier (diff)
downloadsophie-7108c642f4ff6dc5f0c4d30b8a8960064ff8e90f.tar.gz
sophie-7108c642f4ff6dc5f0c4d30b8a8960064ff8e90f.tar.zst
sophie-7108c642f4ff6dc5f0c4d30b8a8960064ff8e90f.zip
test: Add tests for main package
- Changed jest to run from the root package and reference the packages as projects. This required moving the base jest config file away from the project root. - Module isolation seems to prevent ts-jest from loading the shared package, so we disabled it for now. - To better facilitate mocking, services should be split into interfaces and implementation - Had to downgrade to chald 4.1.2 as per https://github.com/chalk/chalk/releases/tag/v5.0.0 at least until https://github.com/microsoft/TypeScript/issues/46452 is resolved.
Diffstat (limited to 'packages')
-rw-r--r--packages/main/esbuild.config.js5
-rw-r--r--packages/main/jest.config.js3
-rw-r--r--packages/main/package.json3
-rw-r--r--packages/main/src/compositionRoot.ts4
-rw-r--r--packages/main/src/controllers/__tests__/config.spec.ts184
-rw-r--r--packages/main/src/controllers/__tests__/nativeTheme.spec.ts71
-rw-r--r--packages/main/src/controllers/config.ts16
-rw-r--r--packages/main/src/services/ConfigPersistenceService.ts106
-rw-r--r--packages/main/src/services/impl/ConfigPersistenceServiceImpl.ts128
-rw-r--r--packages/main/src/utils/index.ts2
-rw-r--r--packages/main/src/utils/logging.ts16
-rw-r--r--packages/main/tsconfig.json1
-rw-r--r--packages/preload/esbuild.config.js5
-rw-r--r--packages/preload/jest.config.js2
-rw-r--r--packages/preload/package.json3
-rw-r--r--packages/preload/src/contextBridge/__tests__/SophieRendererImpl.spec.ts36
-rw-r--r--packages/preload/tsconfig.json3
-rw-r--r--packages/renderer/package.json2
-rw-r--r--packages/renderer/vite.config.js3
-rw-r--r--packages/service-inject/esbuild.config.js5
-rw-r--r--packages/service-preload/esbuild.config.js5
-rw-r--r--packages/service-shared/esbuild.config.js5
-rw-r--r--packages/shared/esbuild.config.js5
23 files changed, 462 insertions, 151 deletions
diff --git a/packages/main/esbuild.config.js b/packages/main/esbuild.config.js
index a39534d..af52f27 100644
--- a/packages/main/esbuild.config.js
+++ b/packages/main/esbuild.config.js
@@ -1,5 +1,6 @@
1import { node, fileURLToDirname } from '../../config/build-common.js'; 1import { node } from '../../config/buildConstants.js';
2import { getConfig } from '../../config/esbuild-config.js'; 2import { getConfig } from '../../config/esbuildConfig.js';
3import { fileURLToDirname } from '../../config/utils.js';
3 4
4const externalPackages = ['electron']; 5const externalPackages = ['electron'];
5 6
diff --git a/packages/main/jest.config.js b/packages/main/jest.config.js
new file mode 100644
index 0000000..b86463c
--- /dev/null
+++ b/packages/main/jest.config.js
@@ -0,0 +1,3 @@
1import rootConfig from '../../config/jest.config.base.js';
2
3export default rootConfig;
diff --git a/packages/main/package.json b/packages/main/package.json
index 99451bd..eb2ecf6 100644
--- a/packages/main/package.json
+++ b/packages/main/package.json
@@ -10,7 +10,7 @@
10 "dependencies": { 10 "dependencies": {
11 "@sophie/service-shared": "workspace:*", 11 "@sophie/service-shared": "workspace:*",
12 "@sophie/shared": "workspace:*", 12 "@sophie/shared": "workspace:*",
13 "chalk": "^5.0.0", 13 "chalk": "^4.1.2",
14 "electron": "16.0.5", 14 "electron": "16.0.5",
15 "json5": "^2.2.0", 15 "json5": "^2.2.0",
16 "lodash-es": "^4.17.21", 16 "lodash-es": "^4.17.21",
@@ -27,6 +27,7 @@
27 "@types/node": "^17.0.5", 27 "@types/node": "^17.0.5",
28 "electron-devtools-installer": "^3.2.0", 28 "electron-devtools-installer": "^3.2.0",
29 "esbuild": "^0.14.9", 29 "esbuild": "^0.14.9",
30 "jest": "^27.4.5",
30 "rimraf": "^3.0.2", 31 "rimraf": "^3.0.2",
31 "typescript": "^4.5.4" 32 "typescript": "^4.5.4"
32 } 33 }
diff --git a/packages/main/src/compositionRoot.ts b/packages/main/src/compositionRoot.ts
index eb6f50f..d420bd6 100644
--- a/packages/main/src/compositionRoot.ts
+++ b/packages/main/src/compositionRoot.ts
@@ -22,12 +22,12 @@ import { app } from 'electron';
22 22
23import { initConfig } from './controllers/config'; 23import { initConfig } from './controllers/config';
24import { initNativeTheme } from './controllers/nativeTheme'; 24import { initNativeTheme } from './controllers/nativeTheme';
25import { ConfigPersistenceService } from './services/ConfigPersistenceService'; 25import { ConfigPersistenceServiceImpl } from './services/impl/ConfigPersistenceServiceImpl';
26import { MainStore } from './stores/MainStore'; 26import { MainStore } from './stores/MainStore';
27import { Disposer } from './utils'; 27import { Disposer } from './utils';
28 28
29export async function init(store: MainStore): Promise<Disposer> { 29export async function init(store: MainStore): Promise<Disposer> {
30 const configPersistenceService = new ConfigPersistenceService(app.getPath('userData')); 30 const configPersistenceService = new ConfigPersistenceServiceImpl(app.getPath('userData'));
31 const disposeConfigController = await initConfig(store.config, configPersistenceService); 31 const disposeConfigController = await initConfig(store.config, configPersistenceService);
32 const disposeNativeThemeController = initNativeTheme(store); 32 const disposeNativeThemeController = initNativeTheme(store);
33 33
diff --git a/packages/main/src/controllers/__tests__/config.spec.ts b/packages/main/src/controllers/__tests__/config.spec.ts
new file mode 100644
index 0000000..9471ca9
--- /dev/null
+++ b/packages/main/src/controllers/__tests__/config.spec.ts
@@ -0,0 +1,184 @@
1/*
2 * Copyright (C) 2021-2022 Kristóf Marussy <kristof@marussy.com>
3 *
4 * This file is part of Sophie.
5 *
6 * Sophie is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, version 3.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: AGPL-3.0-only
19 */
20
21import { jest } from '@jest/globals';
22import { mocked } from 'jest-mock';
23import ms from 'ms';
24
25import { initConfig } from '../config';
26import type { ConfigPersistenceService } from '../../services/ConfigPersistenceService';
27import { Config, config as configModel } from '../../stores/Config';
28import { Disposer, silenceLogger } from '../../utils';
29
30let config: Config;
31let persistenceService: ConfigPersistenceService = {
32 readConfig: jest.fn(),
33 writeConfig: jest.fn(),
34 watchConfig: jest.fn(),
35};
36let lessThanThrottleMs = ms('0.1s');
37let throttleMs = ms('1s');
38
39beforeAll(() => {
40 jest.useFakeTimers();
41 silenceLogger();
42});
43
44beforeEach(() => {
45 config = configModel.create();
46});
47
48describe('when initializing', () => {
49 describe('when there is no config file', () => {
50 beforeEach(() => {
51 mocked(persistenceService.readConfig).mockResolvedValueOnce({
52 found: false,
53 });
54 });
55
56 it('should create a new config file', async () => {
57 await initConfig(config, persistenceService);
58 expect(persistenceService.writeConfig).toBeCalledTimes(1);
59 });
60
61 it('should bail if there is an an error creating the config file', async () => {
62 mocked(persistenceService.writeConfig).mockRejectedValue(new Error('boo'));
63 await expect(() => initConfig(config, persistenceService)).rejects.toBeInstanceOf(Error);
64 });
65 });
66
67 describe('when there is a valid config file', () => {
68 beforeEach(() => {
69 mocked(persistenceService.readConfig).mockResolvedValueOnce({
70 found: true,
71 data: {
72 themeSource: 'dark',
73 },
74 });
75 });
76
77 it('should read the existing config file is there is one', async () => {
78 await initConfig(config, persistenceService);
79 expect(persistenceService.writeConfig).not.toBeCalled();
80 expect(config.themeSource).toBe('dark');
81 });
82
83 it('should bail if it cannot set up a watcher', async () => {
84 mocked(persistenceService.watchConfig).mockImplementationOnce(() => {
85 throw new Error('boo');
86 });
87 await expect(() => initConfig(config, persistenceService)).rejects.toBeInstanceOf(Error);
88 });
89 });
90
91 it('should not apply an invalid config file', async () => {
92 mocked(persistenceService.readConfig).mockResolvedValueOnce({
93 found: true,
94 data: {
95 themeSource: -1,
96 },
97 });
98 await initConfig(config, persistenceService);
99 expect(config.themeSource).not.toBe(-1);
100 });
101
102 it('should bail if it cannot determine whether there is a config file', async () => {
103 mocked(persistenceService.readConfig).mockRejectedValue(new Error('boo'));
104 await expect(() => initConfig(config, persistenceService)).rejects.toBeInstanceOf(Error);
105 });
106});
107
108describe('when it has loaded the config', () => {
109 let sutDisposer: Disposer;
110 let watcherDisposer: Disposer = jest.fn();
111 let configChangedCallback: () => Promise<void>;
112
113 beforeEach(async () => {
114 mocked(persistenceService.readConfig).mockResolvedValueOnce({
115 found: true,
116 data: {},
117 });
118 mocked(persistenceService.watchConfig).mockReturnValueOnce(watcherDisposer);
119 sutDisposer = await initConfig(config, persistenceService, throttleMs);
120 configChangedCallback = mocked(persistenceService.watchConfig).mock.calls[0][0];
121 jest.resetAllMocks();
122 });
123
124 it('should throttle saving changes to the config file', () => {
125 mocked(persistenceService.writeConfig).mockResolvedValue(undefined);
126 config.setThemeSource('dark');
127 jest.advanceTimersByTime(lessThanThrottleMs);
128 config.setThemeSource('light');
129 jest.advanceTimersByTime(throttleMs);
130 expect(persistenceService.writeConfig).toBeCalledTimes(1);
131 });
132
133 it('should handle config writing errors gracefully', () => {
134 mocked(persistenceService.writeConfig).mockRejectedValue(new Error('boo'));
135 config.setThemeSource('dark');
136 jest.advanceTimersByTime(throttleMs);
137 expect(persistenceService.writeConfig).toBeCalledTimes(1);
138 });
139
140 it('should read the config file when it has changed', async () => {
141 mocked(persistenceService.readConfig).mockResolvedValueOnce({
142 found: true,
143 data: {
144 themeSource: 'dark',
145 },
146 });
147 await configChangedCallback();
148 // Do not write back the changes we have just read.
149 expect(persistenceService.writeConfig).not.toBeCalled();
150 expect(config.themeSource).toBe('dark');
151 });
152
153 it('should not apply an invalid config file when it has changed', async () => {
154 mocked(persistenceService.readConfig).mockResolvedValueOnce({
155 found: true,
156 data: {
157 themeSource: -1,
158 },
159 });
160 await configChangedCallback();
161 expect(config.themeSource).not.toBe(-1);
162 });
163
164 it('should handle config writing errors gracefully', async () => {
165 mocked(persistenceService.readConfig).mockRejectedValue(new Error('boo'));
166 await configChangedCallback();
167 });
168
169 describe('when it was disposed', () => {
170 beforeEach(() => {
171 sutDisposer();
172 });
173
174 it('should dispose the watcher', () => {
175 expect(watcherDisposer).toBeCalled();
176 });
177
178 it('should not listen to store changes any more', () => {
179 config.setThemeSource('dark');
180 jest.advanceTimersByTime(2 * throttleMs);
181 expect(persistenceService.writeConfig).not.toBeCalled();
182 });
183 });
184});
diff --git a/packages/main/src/controllers/__tests__/nativeTheme.spec.ts b/packages/main/src/controllers/__tests__/nativeTheme.spec.ts
new file mode 100644
index 0000000..cfb557c
--- /dev/null
+++ b/packages/main/src/controllers/__tests__/nativeTheme.spec.ts
@@ -0,0 +1,71 @@
1/*
2 * Copyright (C) 2021-2022 Kristóf Marussy <kristof@marussy.com>
3 *
4 * This file is part of Sophie.
5 *
6 * Sophie is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, version 3.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: AGPL-3.0-only
19 */
20
21import { jest } from '@jest/globals';
22import { mocked } from 'jest-mock';
23
24import { createMainStore, MainStore } from '../../stores/MainStore';
25import { Disposer } from '../../utils';
26
27let shouldUseDarkColors = false;
28
29jest.unstable_mockModule('electron', () => ({
30 nativeTheme: {
31 themeSource: 'system',
32 get shouldUseDarkColors() {
33 return shouldUseDarkColors;
34 },
35 on: jest.fn(),
36 off: jest.fn(),
37 },
38}));
39
40const { nativeTheme } = await import('electron');
41const { initNativeTheme } = await import('../nativeTheme');
42
43let store: MainStore;
44let disposeSut: Disposer;
45
46beforeEach(() => {
47 store = createMainStore();
48 disposeSut = initNativeTheme(store);
49});
50
51it('should register a nativeTheme updated listener', () => {
52 expect(nativeTheme.on).toBeCalledWith('updated', expect.anything());
53});
54
55it('should synchronize themeSource changes to the nativeTheme', () => {
56 store.config.setThemeSource('dark');
57 expect(nativeTheme.themeSource).toBe('dark');
58});
59
60it('should synchronize shouldUseDarkColors changes to the store', () => {
61 const listener = mocked(nativeTheme.on).mock.calls.find(([event]) => event === 'updated')![1];
62 shouldUseDarkColors = true;
63 listener();
64 expect(store.shared.shouldUseDarkColors).toBe(true);
65});
66
67it('should remove the listener on dispose', () => {
68 const listener = mocked(nativeTheme.on).mock.calls.find(([event]) => event === 'updated')![1];
69 disposeSut();
70 expect(nativeTheme.off).toBeCalledWith('updated', listener);
71});
diff --git a/packages/main/src/controllers/config.ts b/packages/main/src/controllers/config.ts
index 600a142..d3559c8 100644
--- a/packages/main/src/controllers/config.ts
+++ b/packages/main/src/controllers/config.ts
@@ -60,12 +60,8 @@ export async function initConfig(
60 60
61 if (!await readConfig()) { 61 if (!await readConfig()) {
62 log.info('Config file was not found'); 62 log.info('Config file was not found');
63 try { 63 await writeConfig();
64 await writeConfig(); 64 log.info('Created config file');
65 log.info('Created config file');
66 } catch (err) {
67 log.error('Failed to initialize config', err);
68 }
69 } 65 }
70 66
71 const disposeOnSnapshot = onSnapshot(config, debounce((snapshot) => { 67 const disposeOnSnapshot = onSnapshot(config, debounce((snapshot) => {
@@ -73,12 +69,16 @@ export async function initConfig(
73 if (lastSnapshotOnDisk !== snapshot) { 69 if (lastSnapshotOnDisk !== snapshot) {
74 writeConfig().catch((err) => { 70 writeConfig().catch((err) => {
75 log.error('Failed to write config on config change', err); 71 log.error('Failed to write config on config change', err);
76 }) 72 });
77 } 73 }
78 }, debounceTime)); 74 }, debounceTime));
79 75
80 const disposeWatcher = persistenceService.watchConfig(async () => { 76 const disposeWatcher = persistenceService.watchConfig(async () => {
81 await readConfig(); 77 try {
78 await readConfig();
79 } catch (err) {
80 log.error('Failed to read config', err);
81 }
82 }, debounceTime); 82 }, debounceTime);
83 83
84 return () => { 84 return () => {
diff --git a/packages/main/src/services/ConfigPersistenceService.ts b/packages/main/src/services/ConfigPersistenceService.ts
index b2109f6..b3ad162 100644
--- a/packages/main/src/services/ConfigPersistenceService.ts
+++ b/packages/main/src/services/ConfigPersistenceService.ts
@@ -17,112 +17,16 @@
17 * 17 *
18 * SPDX-License-Identifier: AGPL-3.0-only 18 * SPDX-License-Identifier: AGPL-3.0-only
19 */ 19 */
20import { watch } from 'fs';
21import { readFile, stat, writeFile } from 'fs/promises';
22import JSON5 from 'json5';
23import throttle from 'lodash-es/throttle';
24import { join } from 'path';
25 20
26import type { ConfigSnapshotOut } from '../stores/Config'; 21import type { ConfigSnapshotOut } from '../stores/Config';
27import { Disposer, getLogger } from '../utils'; 22import { Disposer } from '../utils';
28
29const log = getLogger('configPersistence');
30 23
31export type ReadConfigResult = { found: true; data: unknown; } | { found: false; }; 24export type ReadConfigResult = { found: true; data: unknown; } | { found: false; };
32 25
33export class ConfigPersistenceService { 26export interface ConfigPersistenceService {
34 private readonly configFilePath: string; 27 readConfig(): Promise<ReadConfigResult>;
35
36 private writingConfig = false;
37
38 private timeLastWritten: Date | null = null;
39
40 constructor(
41 private readonly userDataDir: string,
42 private readonly configFileName: string = 'config.json5',
43 ) {
44 this.configFileName = configFileName;
45 this.configFilePath = join(this.userDataDir, this.configFileName);
46 }
47
48 async readConfig(): Promise<ReadConfigResult> {
49 let configStr;
50 try {
51 configStr = await readFile(this.configFilePath, 'utf8');
52 } catch (err) {
53 if ((err as NodeJS.ErrnoException).code === 'ENOENT') {
54 log.debug('Config file', this.configFilePath, 'was not found');
55 return { found: false };
56 }
57 throw err;
58 }
59 log.info('Read config file', this.configFilePath);
60 return {
61 found: true,
62 data: JSON5.parse(configStr),
63 };
64 }
65
66 async writeConfig(configSnapshot: ConfigSnapshotOut): Promise<void> {
67 const configJson = JSON5.stringify(configSnapshot, {
68 space: 2,
69 });
70 this.writingConfig = true;
71 try {
72 await writeFile(this.configFilePath, configJson, 'utf8');
73 const { mtime } = await stat(this.configFilePath);
74 log.trace('Config file', this.configFilePath, 'last written at', mtime);
75 this.timeLastWritten = mtime;
76 } finally {
77 this.writingConfig = false;
78 }
79 log.info('Wrote config file', this.configFilePath);
80 }
81
82 watchConfig(callback: () => Promise<void>, throttleMs: number): Disposer {
83 log.debug('Installing watcher for', this.userDataDir);
84
85 const configChanged = throttle(async () => {
86 let mtime: Date;
87 try {
88 const stats = await stat(this.configFilePath);
89 mtime = stats.mtime;
90 log.trace('Config file last modified at', mtime);
91 } catch (err) {
92 if ((err as NodeJS.ErrnoException).code === 'ENOENT') {
93 log.debug('Config file', this.configFilePath, 'was deleted after being changed');
94 return;
95 }
96 throw err;
97 }
98 if (!this.writingConfig
99 && (this.timeLastWritten === null || mtime > this.timeLastWritten)) {
100 log.debug(
101 'Found a config file modified at',
102 mtime,
103 'whish is newer than last written',
104 this.timeLastWritten,
105 );
106 return callback();
107 }
108 }, throttleMs);
109
110 const watcher = watch(this.userDataDir, {
111 persistent: false,
112 });
113 28
114 watcher.on('change', (eventType, filename) => { 29 writeConfig(configSnapshot: ConfigSnapshotOut): Promise<void>;
115 if (eventType === 'change'
116 && (filename === this.configFileName || filename === null)) {
117 configChanged()?.catch((err) => {
118 console.log('Unhandled error while listening for config changes', err);
119 });
120 }
121 });
122 30
123 return () => { 31 watchConfig(callback: () => Promise<void>, throttleMs: number): Disposer;
124 log.trace('Removing watcher for', this.configFilePath);
125 watcher.close();
126 };
127 }
128} 32}
diff --git a/packages/main/src/services/impl/ConfigPersistenceServiceImpl.ts b/packages/main/src/services/impl/ConfigPersistenceServiceImpl.ts
new file mode 100644
index 0000000..bffc38c
--- /dev/null
+++ b/packages/main/src/services/impl/ConfigPersistenceServiceImpl.ts
@@ -0,0 +1,128 @@
1
2/*
3 * Copyright (C) 2021-2022 Kristóf Marussy <kristof@marussy.com>
4 *
5 * This file is part of Sophie.
6 *
7 * Sophie is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, version 3.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: AGPL-3.0-only
20 */
21import { watch } from 'fs';
22import { readFile, stat, writeFile } from 'fs/promises';
23import JSON5 from 'json5';
24import throttle from 'lodash-es/throttle';
25import { join } from 'path';
26
27import type { ConfigPersistenceService, ReadConfigResult } from '../ConfigPersistenceService';
28import type { ConfigSnapshotOut } from '../../stores/Config';
29import { Disposer, getLogger } from '../../utils';
30
31const log = getLogger('configPersistence');
32
33export class ConfigPersistenceServiceImpl implements ConfigPersistenceService {
34 private readonly configFilePath: string;
35
36 private writingConfig = false;
37
38 private timeLastWritten: Date | null = null;
39
40 constructor(
41 private readonly userDataDir: string,
42 private readonly configFileName: string = 'config.json5',
43 ) {
44 this.configFileName = configFileName;
45 this.configFilePath = join(this.userDataDir, this.configFileName);
46 }
47
48 async readConfig(): Promise<ReadConfigResult> {
49 let configStr;
50 try {
51 configStr = await readFile(this.configFilePath, 'utf8');
52 } catch (err) {
53 if ((err as NodeJS.ErrnoException).code === 'ENOENT') {
54 log.debug('Config file', this.configFilePath, 'was not found');
55 return { found: false };
56 }
57 throw err;
58 }
59 log.info('Read config file', this.configFilePath);
60 return {
61 found: true,
62 data: JSON5.parse(configStr),
63 };
64 }
65
66 async writeConfig(configSnapshot: ConfigSnapshotOut): Promise<void> {
67 const configJson = JSON5.stringify(configSnapshot, {
68 space: 2,
69 });
70 this.writingConfig = true;
71 try {
72 await writeFile(this.configFilePath, configJson, 'utf8');
73 const { mtime } = await stat(this.configFilePath);
74 log.trace('Config file', this.configFilePath, 'last written at', mtime);
75 this.timeLastWritten = mtime;
76 } finally {
77 this.writingConfig = false;
78 }
79 log.info('Wrote config file', this.configFilePath);
80 }
81
82 watchConfig(callback: () => Promise<void>, throttleMs: number): Disposer {
83 log.debug('Installing watcher for', this.userDataDir);
84
85 const configChanged = throttle(async () => {
86 let mtime: Date;
87 try {
88 const stats = await stat(this.configFilePath);
89 mtime = stats.mtime;
90 log.trace('Config file last modified at', mtime);
91 } catch (err) {
92 if ((err as NodeJS.ErrnoException).code === 'ENOENT') {
93 log.debug('Config file', this.configFilePath, 'was deleted after being changed');
94 return;
95 }
96 throw err;
97 }
98 if (!this.writingConfig
99 && (this.timeLastWritten === null || mtime > this.timeLastWritten)) {
100 log.debug(
101 'Found a config file modified at',
102 mtime,
103 'whish is newer than last written',
104 this.timeLastWritten,
105 );
106 return callback();
107 }
108 }, throttleMs);
109
110 const watcher = watch(this.userDataDir, {
111 persistent: false,
112 });
113
114 watcher.on('change', (eventType, filename) => {
115 if (eventType === 'change'
116 && (filename === this.configFileName || filename === null)) {
117 configChanged()?.catch((err) => {
118 console.log('Unhandled error while listening for config changes', err);
119 });
120 }
121 });
122
123 return () => {
124 log.trace('Removing watcher for', this.configFilePath);
125 watcher.close();
126 };
127 }
128}
diff --git a/packages/main/src/utils/index.ts b/packages/main/src/utils/index.ts
index 9a57e02..2b85989 100644
--- a/packages/main/src/utils/index.ts
+++ b/packages/main/src/utils/index.ts
@@ -22,4 +22,4 @@ import { IDisposer } from 'mobx-state-tree';
22 22
23export type Disposer = IDisposer; 23export type Disposer = IDisposer;
24 24
25export { getLogger } from './logging'; 25export { getLogger, silenceLogger } from './logging';
diff --git a/packages/main/src/utils/logging.ts b/packages/main/src/utils/logging.ts
index 6c4cba2..ed40365 100644
--- a/packages/main/src/utils/logging.ts
+++ b/packages/main/src/utils/logging.ts
@@ -18,17 +18,17 @@
18 * SPDX-License-Identifier: AGPL-3.0-only 18 * SPDX-License-Identifier: AGPL-3.0-only
19 */ 19 */
20 20
21import chalk, { ChalkInstance } from 'chalk'; 21import chalk, { Chalk } from 'chalk';
22import loglevel, { Logger } from 'loglevel'; 22import loglevel, { Logger } from 'loglevel';
23import prefix from 'loglevel-plugin-prefix'; 23import prefix from 'loglevel-plugin-prefix';
24 24
25if (import.meta.env.DEV) { 25if (import.meta.env?.DEV) {
26 loglevel.setLevel('debug'); 26 loglevel.setLevel('debug');
27} else { 27} else {
28 loglevel.setLevel('info'); 28 loglevel.setLevel('info');
29} 29}
30 30
31const COLORS: Partial<Record<string, ChalkInstance>> = { 31const COLORS: Partial<Record<string, Chalk>> = {
32 TRACE: chalk.magenta, 32 TRACE: chalk.magenta,
33 DEBUG: chalk.cyan, 33 DEBUG: chalk.cyan,
34 INFO: chalk.blue, 34 INFO: chalk.blue,
@@ -37,7 +37,7 @@ const COLORS: Partial<Record<string, ChalkInstance>> = {
37 CRITICAL: chalk.red, 37 CRITICAL: chalk.red,
38}; 38};
39 39
40function getColor(level: string): ChalkInstance { 40function getColor(level: string): Chalk {
41 return COLORS[level] ?? chalk.gray; 41 return COLORS[level] ?? chalk.gray;
42} 42}
43 43
@@ -52,3 +52,11 @@ prefix.apply(loglevel, {
52export function getLogger(loggerName: string): Logger { 52export function getLogger(loggerName: string): Logger {
53 return loglevel.getLogger(loggerName); 53 return loglevel.getLogger(loggerName);
54} 54}
55
56export function silenceLogger(): void {
57 loglevel.disableAll();
58 const loggers = loglevel.getLoggers();
59 for (const loggerName of Object.keys(loggers)) {
60 loggers[loggerName].disableAll();
61 }
62}
diff --git a/packages/main/tsconfig.json b/packages/main/tsconfig.json
index 10f5765..1401445 100644
--- a/packages/main/tsconfig.json
+++ b/packages/main/tsconfig.json
@@ -3,6 +3,7 @@
3 "compilerOptions": { 3 "compilerOptions": {
4 "noEmit": true, 4 "noEmit": true,
5 "types": [ 5 "types": [
6 "@types/jest",
6 "node" 7 "node"
7 ] 8 ]
8 }, 9 },
diff --git a/packages/preload/esbuild.config.js b/packages/preload/esbuild.config.js
index de51fc5..b73a071 100644
--- a/packages/preload/esbuild.config.js
+++ b/packages/preload/esbuild.config.js
@@ -1,5 +1,6 @@
1import { chrome, fileURLToDirname } from '../../config/build-common.js'; 1import { chrome } from '../../config/buildConstants.js';
2import { getConfig } from '../../config/esbuild-config.js'; 2import { getConfig } from '../../config/esbuildConfig.js';
3import { fileURLToDirname } from '../../config/utils.js';
3 4
4export default getConfig({ 5export default getConfig({
5 absWorkingDir: fileURLToDirname(import.meta.url), 6 absWorkingDir: fileURLToDirname(import.meta.url),
diff --git a/packages/preload/jest.config.js b/packages/preload/jest.config.js
index faecf9a..e474c4c 100644
--- a/packages/preload/jest.config.js
+++ b/packages/preload/jest.config.js
@@ -1,4 +1,4 @@
1import rootConfig from '../../jest.config.js'; 1import rootConfig from '../../config/jest.config.base.js';
2 2
3/** @type {import('ts-jest').InitialOptionsTsJest} */ 3/** @type {import('ts-jest').InitialOptionsTsJest} */
4export default { 4export default {
diff --git a/packages/preload/package.json b/packages/preload/package.json
index e6dd0ee..9818ba8 100644
--- a/packages/preload/package.json
+++ b/packages/preload/package.json
@@ -6,7 +6,6 @@
6 "type": "module", 6 "type": "module",
7 "types": "dist-types/index.d.ts", 7 "types": "dist-types/index.d.ts",
8 "scripts": { 8 "scripts": {
9 "test": "node --experimental-vm-modules ../../node_modules/jest/bin/jest.js",
10 "typecheck": "tsc" 9 "typecheck": "tsc"
11 }, 10 },
12 "dependencies": { 11 "dependencies": {
@@ -16,7 +15,7 @@
16 "mobx-state-tree": "^5.1.0" 15 "mobx-state-tree": "^5.1.0"
17 }, 16 },
18 "devDependencies": { 17 "devDependencies": {
19 "@types/jest": "^27.0.3", 18 "@types/jest": "^27.4.0",
20 "jest": "^27.4.5", 19 "jest": "^27.4.5",
21 "jest-mock": "^27.4.2", 20 "jest-mock": "^27.4.2",
22 "jsdom": "^19.0.0", 21 "jsdom": "^19.0.0",
diff --git a/packages/preload/src/contextBridge/__tests__/SophieRendererImpl.spec.ts b/packages/preload/src/contextBridge/__tests__/SophieRendererImpl.spec.ts
index 41937c2..fdd1cc5 100644
--- a/packages/preload/src/contextBridge/__tests__/SophieRendererImpl.spec.ts
+++ b/packages/preload/src/contextBridge/__tests__/SophieRendererImpl.spec.ts
@@ -18,7 +18,7 @@
18 * SPDX-License-Identifier: AGPL-3.0-only 18 * SPDX-License-Identifier: AGPL-3.0-only
19 */ 19 */
20 20
21import { describe, it, jest } from '@jest/globals'; 21import { jest } from '@jest/globals';
22import { mocked } from 'jest-mock'; 22import { mocked } from 'jest-mock';
23import log from 'loglevel'; 23import log from 'loglevel';
24import type { IJsonPatch } from 'mobx-state-tree'; 24import type { IJsonPatch } from 'mobx-state-tree';
@@ -67,7 +67,9 @@ const invalidAction = {
67 action: 'not-a-valid-action', 67 action: 'not-a-valid-action',
68} as unknown as Action; 68} as unknown as Action;
69 69
70log.disableAll(); 70beforeAll(() => {
71 log.disableAll();
72});
71 73
72describe('createSophieRenderer', () => { 74describe('createSophieRenderer', () => {
73 it('registers a shared store patch listener', () => { 75 it('registers a shared store patch listener', () => {
@@ -95,59 +97,59 @@ describe('SophieRendererImpl', () => {
95 }); 97 });
96 98
97 describe('onSharedStoreChange', () => { 99 describe('onSharedStoreChange', () => {
98 it('requests a snapshot from the main process', async () => { 100 it('should request a snapshot from the main process', async () => {
99 mocked(ipcRenderer.invoke).mockResolvedValueOnce(snapshot); 101 mocked(ipcRenderer.invoke).mockResolvedValueOnce(snapshot);
100 await sut.onSharedStoreChange(listener); 102 await sut.onSharedStoreChange(listener);
101 expect(ipcRenderer.invoke).toBeCalledWith(RendererToMainIpcMessage.GetSharedStoreSnapshot); 103 expect(ipcRenderer.invoke).toBeCalledWith(RendererToMainIpcMessage.GetSharedStoreSnapshot);
102 expect(listener.onSnapshot).toBeCalledWith(snapshot); 104 expect(listener.onSnapshot).toBeCalledWith(snapshot);
103 }); 105 });
104 106
105 it('catches IPC errors without exposing them', async () => { 107 it('should catch IPC errors without exposing them', async () => {
106 mocked(ipcRenderer.invoke).mockRejectedValue(new Error('s3cr3t')); 108 mocked(ipcRenderer.invoke).mockRejectedValue(new Error('s3cr3t'));
107 await expect(sut.onSharedStoreChange(listener)).rejects.not.toHaveProperty( 109 await expect(sut.onSharedStoreChange(listener)).rejects.not.toHaveProperty(
108 'message', 110 'message',
109 expect.stringMatching(/s3cr3t/), 111 expect.stringMatching(/s3cr3t/),
110 ); 112 );
111 expect(listener.onSnapshot).toBeCalledTimes(0); 113 expect(listener.onSnapshot).not.toBeCalled();
112 }); 114 });
113 115
114 it('does not pass on invalid snapshots', async () => { 116 it('should not pass on invalid snapshots', async () => {
115 mocked(ipcRenderer.invoke).mockResolvedValueOnce(invalidSnapshot); 117 mocked(ipcRenderer.invoke).mockResolvedValueOnce(invalidSnapshot);
116 await expect(sut.onSharedStoreChange(listener)).rejects.toBeInstanceOf(Error); 118 await expect(sut.onSharedStoreChange(listener)).rejects.toBeInstanceOf(Error);
117 expect(listener.onSnapshot).toBeCalledTimes(0); 119 expect(listener.onSnapshot).not.toBeCalled();
118 }); 120 });
119 }); 121 });
120 122
121 describe('dispatchAction', () => { 123 describe('dispatchAction', () => {
122 it('dispatched valid actions', () => { 124 it('should dispatch valid actions', () => {
123 sut.dispatchAction(action); 125 sut.dispatchAction(action);
124 expect(ipcRenderer.send).toBeCalledWith(RendererToMainIpcMessage.DispatchAction, action); 126 expect(ipcRenderer.send).toBeCalledWith(RendererToMainIpcMessage.DispatchAction, action);
125 }); 127 });
126 128
127 it('does not dispatch invalid actions', () => { 129 it('should not dispatch invalid actions', () => {
128 expect(() => sut.dispatchAction(invalidAction)).toThrowError(); 130 expect(() => sut.dispatchAction(invalidAction)).toThrowError();
129 expect(ipcRenderer.send).toBeCalledTimes(0); 131 expect(ipcRenderer.send).not.toBeCalled();
130 }); 132 });
131 }); 133 });
132 134
133 describe('when no listener is registered', () => { 135 describe('when no listener is registered', () => {
134 it('discards the received patch without any error', () => { 136 it('should discard the received patch without any error', () => {
135 onSharedStorePatch(event, patch); 137 onSharedStorePatch(event, patch);
136 }); 138 });
137 }); 139 });
138 140
139 function itRefusesToRegisterAnotherListener() { 141 function itRefusesToRegisterAnotherListener() {
140 it('refuses to register another listener', async () => { 142 it('should refuse to register another listener', async () => {
141 await expect(sut.onSharedStoreChange(listener)).rejects.toBeInstanceOf(Error); 143 await expect(sut.onSharedStoreChange(listener)).rejects.toBeInstanceOf(Error);
142 }); 144 });
143 } 145 }
144 146
145 function itDoesNotPassPatchesToTheListener( 147 function itDoesNotPassPatchesToTheListener(
146 name: string = 'does not pass patches to the listener', 148 name: string = 'should not pass patches to the listener',
147 ) { 149 ) {
148 it(name, () => { 150 it(name, () => {
149 onSharedStorePatch(event, patch); 151 onSharedStorePatch(event, patch);
150 expect(listener.onPatch).toBeCalledTimes(0); 152 expect(listener.onPatch).not.toBeCalled();
151 }); 153 });
152 } 154 }
153 155
@@ -157,12 +159,12 @@ describe('SophieRendererImpl', () => {
157 await sut.onSharedStoreChange(listener); 159 await sut.onSharedStoreChange(listener);
158 }); 160 });
159 161
160 it('passes patches to the listener', () => { 162 it('should pass patches to the listener', () => {
161 onSharedStorePatch(event, patch); 163 onSharedStorePatch(event, patch);
162 expect(listener.onPatch).toBeCalledWith(patch); 164 expect(listener.onPatch).toBeCalledWith(patch);
163 }); 165 });
164 166
165 it('catches listener errors', () => { 167 it('should catch listener errors', () => {
166 mocked(listener.onPatch).mockImplementation(() => { throw new Error(); }); 168 mocked(listener.onPatch).mockImplementation(() => { throw new Error(); });
167 onSharedStorePatch(event, patch); 169 onSharedStorePatch(event, patch);
168 }); 170 });
@@ -176,7 +178,7 @@ describe('SophieRendererImpl', () => {
176 listener.onPatch.mockRestore(); 178 listener.onPatch.mockRestore();
177 }); 179 });
178 180
179 itDoesNotPassPatchesToTheListener('does not pass on patches any more'); 181 itDoesNotPassPatchesToTheListener('should not pass on patches any more');
180 }); 182 });
181 }); 183 });
182 184
diff --git a/packages/preload/tsconfig.json b/packages/preload/tsconfig.json
index 0f27305..741d435 100644
--- a/packages/preload/tsconfig.json
+++ b/packages/preload/tsconfig.json
@@ -6,6 +6,9 @@
6 "dom", 6 "dom",
7 "dom.iterable", 7 "dom.iterable",
8 "esnext" 8 "esnext"
9 ],
10 "types": [
11 "@types/jest"
9 ] 12 ]
10 }, 13 },
11 "references": [ 14 "references": [
diff --git a/packages/renderer/package.json b/packages/renderer/package.json
index 2a26b2b..0a9f4ef 100644
--- a/packages/renderer/package.json
+++ b/packages/renderer/package.json
@@ -30,6 +30,6 @@
30 "remotedev": "^0.2.9", 30 "remotedev": "^0.2.9",
31 "rimraf": "^3.0.2", 31 "rimraf": "^3.0.2",
32 "typescript": "^4.5.4", 32 "typescript": "^4.5.4",
33 "vite": "^2.7.9" 33 "vite": "^2.7.10"
34 } 34 }
35} 35}
diff --git a/packages/renderer/vite.config.js b/packages/renderer/vite.config.js
index 6f3d351..82ce33d 100644
--- a/packages/renderer/vite.config.js
+++ b/packages/renderer/vite.config.js
@@ -4,7 +4,8 @@ import { builtinModules } from 'module';
4import { join } from 'path'; 4import { join } from 'path';
5import react from '@vitejs/plugin-react'; 5import react from '@vitejs/plugin-react';
6 6
7import { banner, chrome, fileURLToDirname } from '../../config/build-common.js'; 7import { banner, chrome } from '../../config/buildConstants.js';
8import { fileURLToDirname } from '../../config/utils.js';
8 9
9const thisDir = fileURLToDirname(import.meta.url); 10const thisDir = fileURLToDirname(import.meta.url);
10 11
diff --git a/packages/service-inject/esbuild.config.js b/packages/service-inject/esbuild.config.js
index 3f1d6d0..2169c8e 100644
--- a/packages/service-inject/esbuild.config.js
+++ b/packages/service-inject/esbuild.config.js
@@ -1,5 +1,6 @@
1import { chrome, fileURLToDirname } from '../../config/build-common.js'; 1import { chrome } from '../../config/buildConstants.js';
2import { getConfig } from '../../config/esbuild-config.js'; 2import { getConfig } from '../../config/esbuildConfig.js';
3import { fileURLToDirname } from '../../config/utils.js';
3 4
4export default getConfig({ 5export default getConfig({
5 absWorkingDir: fileURLToDirname(import.meta.url), 6 absWorkingDir: fileURLToDirname(import.meta.url),
diff --git a/packages/service-preload/esbuild.config.js b/packages/service-preload/esbuild.config.js
index 3c67b31..b73a071 100644
--- a/packages/service-preload/esbuild.config.js
+++ b/packages/service-preload/esbuild.config.js
@@ -1,5 +1,6 @@
1import { chrome, fileURLToDirname } from "../../config/build-common.js"; 1import { chrome } from '../../config/buildConstants.js';
2import { getConfig } from '../../config/esbuild-config.js'; 2import { getConfig } from '../../config/esbuildConfig.js';
3import { fileURLToDirname } from '../../config/utils.js';
3 4
4export default getConfig({ 5export default getConfig({
5 absWorkingDir: fileURLToDirname(import.meta.url), 6 absWorkingDir: fileURLToDirname(import.meta.url),
diff --git a/packages/service-shared/esbuild.config.js b/packages/service-shared/esbuild.config.js
index 8df2edf..071ca7d 100644
--- a/packages/service-shared/esbuild.config.js
+++ b/packages/service-shared/esbuild.config.js
@@ -1,5 +1,6 @@
1import { chrome, fileURLToDirname } from '../../config/build-common.js'; 1import { chrome } from '../../config/buildConstants.js';
2import { getConfig } from '../../config/esbuild-config.js'; 2import { getConfig } from '../../config/esbuildConfig.js';
3import { fileURLToDirname } from '../../config/utils.js';
3 4
4export default getConfig({ 5export default getConfig({
5 absWorkingDir: fileURLToDirname(import.meta.url), 6 absWorkingDir: fileURLToDirname(import.meta.url),
diff --git a/packages/shared/esbuild.config.js b/packages/shared/esbuild.config.js
index fbaa6f1..b134931 100644
--- a/packages/shared/esbuild.config.js
+++ b/packages/shared/esbuild.config.js
@@ -1,5 +1,6 @@
1import { chrome, fileURLToDirname } from '../../config/build-common.js'; 1import { chrome } from '../../config/buildConstants.js';
2import { getConfig } from '../../config/esbuild-config.js'; 2import { getConfig } from '../../config/esbuildConfig.js';
3import { fileURLToDirname } from '../../config/utils.js';
3 4
4export default getConfig({ 5export default getConfig({
5 absWorkingDir: fileURLToDirname(import.meta.url), 6 absWorkingDir: fileURLToDirname(import.meta.url),