diff options
author | Kristóf Marussy <kristof@marussy.com> | 2021-12-31 01:52:28 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2021-12-31 01:56:30 +0100 |
commit | 7108c642f4ff6dc5f0c4d30b8a8960064ff8e90f (patch) | |
tree | f8c0450a6e1b62f7e7f8470efd375b3659b91b2b | |
parent | refactor: Install devtools extensions earlier (diff) | |
download | sophie-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.
35 files changed, 529 insertions, 215 deletions
@@ -6,6 +6,7 @@ | |||
6 | !.yarn/plugins | 6 | !.yarn/plugins |
7 | !.yarn/releases | 7 | !.yarn/releases |
8 | !.yarn/versions | 8 | !.yarn/versions |
9 | coverage/ | ||
9 | dist/ | 10 | dist/ |
10 | node_modules/ | 11 | node_modules/ |
11 | *.tsbuildinfo | 12 | *.tsbuildinfo |
diff --git a/config/build-common.js b/config/buildConstants.js index ff5a218..4952907 100644 --- a/config/build-common.js +++ b/config/buildConstants.js | |||
@@ -1,6 +1,7 @@ | |||
1 | import { readFileSync } from 'fs'; | 1 | import { readFileSync } from 'fs'; |
2 | import { dirname, join } from 'path'; | 2 | import { join } from 'path'; |
3 | import { fileURLToPath } from 'url'; | 3 | |
4 | import { fileURLToDirname } from './utils.js'; | ||
4 | 5 | ||
5 | const thisDir = fileURLToDirname(import.meta.url); | 6 | const thisDir = fileURLToDirname(import.meta.url); |
6 | 7 | ||
@@ -37,11 +38,3 @@ export const chrome = `chrome${chromeVersion}`; | |||
37 | 38 | ||
38 | /** @type {string} */ | 39 | /** @type {string} */ |
39 | export const node = `node${nodeVersion}`; | 40 | export const node = `node${nodeVersion}`; |
40 | |||
41 | /** | ||
42 | * @param {string} url | ||
43 | * @returns {string} | ||
44 | */ | ||
45 | export function fileURLToDirname(url) { | ||
46 | return dirname(fileURLToPath(url)); | ||
47 | } | ||
diff --git a/config/esbuild-config.js b/config/esbuildConfig.js index 5cef85f..05386b1 100644 --- a/config/esbuild-config.js +++ b/config/esbuildConfig.js | |||
@@ -1,4 +1,4 @@ | |||
1 | import { banner } from './build-common.js'; | 1 | import { banner } from './buildConstants.js'; |
2 | 2 | ||
3 | /** @type {string} */ | 3 | /** @type {string} */ |
4 | const mode = process.env.MODE || 'development'; | 4 | const mode = process.env.MODE || 'development'; |
diff --git a/config/jest.config.base.js b/config/jest.config.base.js new file mode 100644 index 0000000..f265c1c --- /dev/null +++ b/config/jest.config.base.js | |||
@@ -0,0 +1,26 @@ | |||
1 | import { join } from 'path'; | ||
2 | |||
3 | import { fileURLToDirname } from './utils.js'; | ||
4 | |||
5 | const dirname = fileURLToDirname(import.meta.url); | ||
6 | |||
7 | /** @type {import('ts-jest').InitialOptionsTsJest} */ | ||
8 | export default { | ||
9 | preset: 'ts-jest/presets/default-esm', | ||
10 | globals: { | ||
11 | 'ts-jest': { | ||
12 | useESM: true, | ||
13 | }, | ||
14 | }, | ||
15 | moduleNameMapper: { | ||
16 | '@sophie/(.+)': join(dirname, '../packages/$1/src/index.ts'), | ||
17 | '^(\\.{1,2}/.*)\\.js$': '$1', | ||
18 | }, | ||
19 | resetMocks: true, | ||
20 | restoreMocks: true, | ||
21 | testEnvironment: 'node', | ||
22 | testPathIgnorePatterns: [ | ||
23 | '/dist/', | ||
24 | '/node_modules/', | ||
25 | ], | ||
26 | }; | ||
diff --git a/config/utils.js b/config/utils.js new file mode 100644 index 0000000..d3e13d9 --- /dev/null +++ b/config/utils.js | |||
@@ -0,0 +1,10 @@ | |||
1 | import { dirname } from 'path'; | ||
2 | import { fileURLToPath } from 'url'; | ||
3 | |||
4 | /** | ||
5 | * @param {string} url | ||
6 | * @returns {string} | ||
7 | */ | ||
8 | export function fileURLToDirname(url) { | ||
9 | return dirname(fileURLToPath(url)); | ||
10 | } | ||
diff --git a/jest.config.js b/jest.config.js index 1aa0cbb..414e49f 100644 --- a/jest.config.js +++ b/jest.config.js | |||
@@ -1,27 +1,6 @@ | |||
1 | import { join } from 'path'; | ||
2 | |||
3 | import { fileURLToDirname } from './config/build-common.js'; | ||
4 | |||
5 | const dirname = fileURLToDirname(import.meta.url); | ||
6 | |||
7 | /** @type {import('ts-jest').InitialOptionsTsJest} */ | 1 | /** @type {import('ts-jest').InitialOptionsTsJest} */ |
8 | export default { | 2 | export default { |
9 | preset: 'ts-jest/presets/default-esm', | 3 | projects: [ |
10 | globals: { | 4 | '<rootDir>/packages/*', |
11 | 'ts-jest': { | ||
12 | isolatedModules: true, | ||
13 | useESM: true, | ||
14 | }, | ||
15 | }, | ||
16 | moduleNameMapper: { | ||
17 | '@sophie/(.+)': join(dirname, 'packages/$1/src/index.ts'), | ||
18 | '^(\\.{1,2}/.*)\\.js$': '$1', | ||
19 | }, | ||
20 | resetMocks: true, | ||
21 | restoreMocks: true, | ||
22 | testEnvironment: 'node', | ||
23 | testPathIgnorePatterns: [ | ||
24 | '/dist/', | ||
25 | '/node_modules/', | ||
26 | ], | 5 | ], |
27 | }; | 6 | }; |
diff --git a/package.json b/package.json index 22748f7..ab2fc9f 100644 --- a/package.json +++ b/package.json | |||
@@ -17,7 +17,7 @@ | |||
17 | "main": "packages/main/dist/index.cjs", | 17 | "main": "packages/main/dist/index.cjs", |
18 | "scripts": { | 18 | "scripts": { |
19 | "clean": "rimraf dist packages/*/dist packages/*/tsconfig.tsbuildinfo .vite", | 19 | "clean": "rimraf dist packages/*/dist packages/*/tsconfig.tsbuildinfo .vite", |
20 | "test": "yarn workspaces foreach -vpt run test", | 20 | "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js", |
21 | "build": "node scripts/build.js", | 21 | "build": "node scripts/build.js", |
22 | "precompile": "cross-env MODE=production yarn run build", | 22 | "precompile": "cross-env MODE=production yarn run build", |
23 | "compile": "yarn precompile && yarn compile:electron-builder", | 23 | "compile": "yarn precompile && yarn compile:electron-builder", |
@@ -38,7 +38,7 @@ | |||
38 | ], | 38 | ], |
39 | "devDependencies": { | 39 | "devDependencies": { |
40 | "@electron/fuses": "^1.5.0", | 40 | "@electron/fuses": "^1.5.0", |
41 | "@types/jest": "^27.0.3", | 41 | "@types/jest": "^27.4.0", |
42 | "@vitejs/plugin-react": "^1.1.3", | 42 | "@vitejs/plugin-react": "^1.1.3", |
43 | "chokidar": "^3.5.2", | 43 | "chokidar": "^3.5.2", |
44 | "cross-env": "^7.0.3", | 44 | "cross-env": "^7.0.3", |
@@ -51,7 +51,7 @@ | |||
51 | "rollup": "^2.62.0", | 51 | "rollup": "^2.62.0", |
52 | "ts-jest": "^27.1.2", | 52 | "ts-jest": "^27.1.2", |
53 | "typescript": "^4.5.4", | 53 | "typescript": "^4.5.4", |
54 | "vite": "^2.7.9" | 54 | "vite": "^2.7.10" |
55 | }, | 55 | }, |
56 | "packageManager": "yarn@3.1.1", | 56 | "packageManager": "yarn@3.1.1", |
57 | "dependencies": { | 57 | "dependencies": { |
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 @@ | |||
1 | import { node, fileURLToDirname } from '../../config/build-common.js'; | 1 | import { node } from '../../config/buildConstants.js'; |
2 | import { getConfig } from '../../config/esbuild-config.js'; | 2 | import { getConfig } from '../../config/esbuildConfig.js'; |
3 | import { fileURLToDirname } from '../../config/utils.js'; | ||
3 | 4 | ||
4 | const externalPackages = ['electron']; | 5 | const 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 @@ | |||
1 | import rootConfig from '../../config/jest.config.base.js'; | ||
2 | |||
3 | export 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 | ||
23 | import { initConfig } from './controllers/config'; | 23 | import { initConfig } from './controllers/config'; |
24 | import { initNativeTheme } from './controllers/nativeTheme'; | 24 | import { initNativeTheme } from './controllers/nativeTheme'; |
25 | import { ConfigPersistenceService } from './services/ConfigPersistenceService'; | 25 | import { ConfigPersistenceServiceImpl } from './services/impl/ConfigPersistenceServiceImpl'; |
26 | import { MainStore } from './stores/MainStore'; | 26 | import { MainStore } from './stores/MainStore'; |
27 | import { Disposer } from './utils'; | 27 | import { Disposer } from './utils'; |
28 | 28 | ||
29 | export async function init(store: MainStore): Promise<Disposer> { | 29 | export 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 | |||
21 | import { jest } from '@jest/globals'; | ||
22 | import { mocked } from 'jest-mock'; | ||
23 | import ms from 'ms'; | ||
24 | |||
25 | import { initConfig } from '../config'; | ||
26 | import type { ConfigPersistenceService } from '../../services/ConfigPersistenceService'; | ||
27 | import { Config, config as configModel } from '../../stores/Config'; | ||
28 | import { Disposer, silenceLogger } from '../../utils'; | ||
29 | |||
30 | let config: Config; | ||
31 | let persistenceService: ConfigPersistenceService = { | ||
32 | readConfig: jest.fn(), | ||
33 | writeConfig: jest.fn(), | ||
34 | watchConfig: jest.fn(), | ||
35 | }; | ||
36 | let lessThanThrottleMs = ms('0.1s'); | ||
37 | let throttleMs = ms('1s'); | ||
38 | |||
39 | beforeAll(() => { | ||
40 | jest.useFakeTimers(); | ||
41 | silenceLogger(); | ||
42 | }); | ||
43 | |||
44 | beforeEach(() => { | ||
45 | config = configModel.create(); | ||
46 | }); | ||
47 | |||
48 | describe('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 | |||
108 | describe('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 | |||
21 | import { jest } from '@jest/globals'; | ||
22 | import { mocked } from 'jest-mock'; | ||
23 | |||
24 | import { createMainStore, MainStore } from '../../stores/MainStore'; | ||
25 | import { Disposer } from '../../utils'; | ||
26 | |||
27 | let shouldUseDarkColors = false; | ||
28 | |||
29 | jest.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 | |||
40 | const { nativeTheme } = await import('electron'); | ||
41 | const { initNativeTheme } = await import('../nativeTheme'); | ||
42 | |||
43 | let store: MainStore; | ||
44 | let disposeSut: Disposer; | ||
45 | |||
46 | beforeEach(() => { | ||
47 | store = createMainStore(); | ||
48 | disposeSut = initNativeTheme(store); | ||
49 | }); | ||
50 | |||
51 | it('should register a nativeTheme updated listener', () => { | ||
52 | expect(nativeTheme.on).toBeCalledWith('updated', expect.anything()); | ||
53 | }); | ||
54 | |||
55 | it('should synchronize themeSource changes to the nativeTheme', () => { | ||
56 | store.config.setThemeSource('dark'); | ||
57 | expect(nativeTheme.themeSource).toBe('dark'); | ||
58 | }); | ||
59 | |||
60 | it('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 | |||
67 | it('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 | */ |
20 | import { watch } from 'fs'; | ||
21 | import { readFile, stat, writeFile } from 'fs/promises'; | ||
22 | import JSON5 from 'json5'; | ||
23 | import throttle from 'lodash-es/throttle'; | ||
24 | import { join } from 'path'; | ||
25 | 20 | ||
26 | import type { ConfigSnapshotOut } from '../stores/Config'; | 21 | import type { ConfigSnapshotOut } from '../stores/Config'; |
27 | import { Disposer, getLogger } from '../utils'; | 22 | import { Disposer } from '../utils'; |
28 | |||
29 | const log = getLogger('configPersistence'); | ||
30 | 23 | ||
31 | export type ReadConfigResult = { found: true; data: unknown; } | { found: false; }; | 24 | export type ReadConfigResult = { found: true; data: unknown; } | { found: false; }; |
32 | 25 | ||
33 | export class ConfigPersistenceService { | 26 | export 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 | */ | ||
21 | import { watch } from 'fs'; | ||
22 | import { readFile, stat, writeFile } from 'fs/promises'; | ||
23 | import JSON5 from 'json5'; | ||
24 | import throttle from 'lodash-es/throttle'; | ||
25 | import { join } from 'path'; | ||
26 | |||
27 | import type { ConfigPersistenceService, ReadConfigResult } from '../ConfigPersistenceService'; | ||
28 | import type { ConfigSnapshotOut } from '../../stores/Config'; | ||
29 | import { Disposer, getLogger } from '../../utils'; | ||
30 | |||
31 | const log = getLogger('configPersistence'); | ||
32 | |||
33 | export 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 | ||
23 | export type Disposer = IDisposer; | 23 | export type Disposer = IDisposer; |
24 | 24 | ||
25 | export { getLogger } from './logging'; | 25 | export { 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 | ||
21 | import chalk, { ChalkInstance } from 'chalk'; | 21 | import chalk, { Chalk } from 'chalk'; |
22 | import loglevel, { Logger } from 'loglevel'; | 22 | import loglevel, { Logger } from 'loglevel'; |
23 | import prefix from 'loglevel-plugin-prefix'; | 23 | import prefix from 'loglevel-plugin-prefix'; |
24 | 24 | ||
25 | if (import.meta.env.DEV) { | 25 | if (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 | ||
31 | const COLORS: Partial<Record<string, ChalkInstance>> = { | 31 | const 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 | ||
40 | function getColor(level: string): ChalkInstance { | 40 | function 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, { | |||
52 | export function getLogger(loggerName: string): Logger { | 52 | export function getLogger(loggerName: string): Logger { |
53 | return loglevel.getLogger(loggerName); | 53 | return loglevel.getLogger(loggerName); |
54 | } | 54 | } |
55 | |||
56 | export 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 @@ | |||
1 | import { chrome, fileURLToDirname } from '../../config/build-common.js'; | 1 | import { chrome } from '../../config/buildConstants.js'; |
2 | import { getConfig } from '../../config/esbuild-config.js'; | 2 | import { getConfig } from '../../config/esbuildConfig.js'; |
3 | import { fileURLToDirname } from '../../config/utils.js'; | ||
3 | 4 | ||
4 | export default getConfig({ | 5 | export 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 @@ | |||
1 | import rootConfig from '../../jest.config.js'; | 1 | import rootConfig from '../../config/jest.config.base.js'; |
2 | 2 | ||
3 | /** @type {import('ts-jest').InitialOptionsTsJest} */ | 3 | /** @type {import('ts-jest').InitialOptionsTsJest} */ |
4 | export default { | 4 | export 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 | ||
21 | import { describe, it, jest } from '@jest/globals'; | 21 | import { jest } from '@jest/globals'; |
22 | import { mocked } from 'jest-mock'; | 22 | import { mocked } from 'jest-mock'; |
23 | import log from 'loglevel'; | 23 | import log from 'loglevel'; |
24 | import type { IJsonPatch } from 'mobx-state-tree'; | 24 | import 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 | ||
70 | log.disableAll(); | 70 | beforeAll(() => { |
71 | log.disableAll(); | ||
72 | }); | ||
71 | 73 | ||
72 | describe('createSophieRenderer', () => { | 74 | describe('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'; | |||
4 | import { join } from 'path'; | 4 | import { join } from 'path'; |
5 | import react from '@vitejs/plugin-react'; | 5 | import react from '@vitejs/plugin-react'; |
6 | 6 | ||
7 | import { banner, chrome, fileURLToDirname } from '../../config/build-common.js'; | 7 | import { banner, chrome } from '../../config/buildConstants.js'; |
8 | import { fileURLToDirname } from '../../config/utils.js'; | ||
8 | 9 | ||
9 | const thisDir = fileURLToDirname(import.meta.url); | 10 | const 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 @@ | |||
1 | import { chrome, fileURLToDirname } from '../../config/build-common.js'; | 1 | import { chrome } from '../../config/buildConstants.js'; |
2 | import { getConfig } from '../../config/esbuild-config.js'; | 2 | import { getConfig } from '../../config/esbuildConfig.js'; |
3 | import { fileURLToDirname } from '../../config/utils.js'; | ||
3 | 4 | ||
4 | export default getConfig({ | 5 | export 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 @@ | |||
1 | import { chrome, fileURLToDirname } from "../../config/build-common.js"; | 1 | import { chrome } from '../../config/buildConstants.js'; |
2 | import { getConfig } from '../../config/esbuild-config.js'; | 2 | import { getConfig } from '../../config/esbuildConfig.js'; |
3 | import { fileURLToDirname } from '../../config/utils.js'; | ||
3 | 4 | ||
4 | export default getConfig({ | 5 | export 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 @@ | |||
1 | import { chrome, fileURLToDirname } from '../../config/build-common.js'; | 1 | import { chrome } from '../../config/buildConstants.js'; |
2 | import { getConfig } from '../../config/esbuild-config.js'; | 2 | import { getConfig } from '../../config/esbuildConfig.js'; |
3 | import { fileURLToDirname } from '../../config/utils.js'; | ||
3 | 4 | ||
4 | export default getConfig({ | 5 | export 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 @@ | |||
1 | import { chrome, fileURLToDirname } from '../../config/build-common.js'; | 1 | import { chrome } from '../../config/buildConstants.js'; |
2 | import { getConfig } from '../../config/esbuild-config.js'; | 2 | import { getConfig } from '../../config/esbuildConfig.js'; |
3 | import { fileURLToDirname } from '../../config/utils.js'; | ||
3 | 4 | ||
4 | export default getConfig({ | 5 | export default getConfig({ |
5 | absWorkingDir: fileURLToDirname(import.meta.url), | 6 | absWorkingDir: fileURLToDirname(import.meta.url), |
diff --git a/scripts/build.js b/scripts/build.js index 9b9b26e..ef2b998 100644 --- a/scripts/build.js +++ b/scripts/build.js | |||
@@ -2,7 +2,7 @@ import { build as esbuildBuild } from 'esbuild'; | |||
2 | import { join } from 'path'; | 2 | import { join } from 'path'; |
3 | import { build as viteBuild } from 'vite'; | 3 | import { build as viteBuild } from 'vite'; |
4 | 4 | ||
5 | import { fileURLToDirname } from '../config/build-common.js'; | 5 | import { fileURLToDirname } from '../config/utils.js'; |
6 | 6 | ||
7 | const thisDir = fileURLToDirname(import.meta.url); | 7 | const thisDir = fileURLToDirname(import.meta.url); |
8 | 8 | ||
diff --git a/scripts/update-electron-vendors.js b/scripts/update-electron-vendors.js index fea8b9d..b2f4ca4 100644 --- a/scripts/update-electron-vendors.js +++ b/scripts/update-electron-vendors.js | |||
@@ -3,7 +3,7 @@ import electronPath from 'electron'; | |||
3 | import { writeFile } from 'fs/promises'; | 3 | import { writeFile } from 'fs/promises'; |
4 | import { join, resolve } from 'path'; | 4 | import { join, resolve } from 'path'; |
5 | 5 | ||
6 | import { fileURLToDirname } from '../config/build-common.js'; | 6 | import { fileURLToDirname } from '../config/utils.js'; |
7 | 7 | ||
8 | /** | 8 | /** |
9 | * Returns versions of electron vendors | 9 | * Returns versions of electron vendors |
diff --git a/scripts/watch.js b/scripts/watch.js index 82a8feb..b635fac 100644 --- a/scripts/watch.js +++ b/scripts/watch.js | |||
@@ -5,7 +5,7 @@ import electronPath from 'electron'; | |||
5 | import { join } from 'path'; | 5 | import { join } from 'path'; |
6 | import { createServer } from 'vite'; | 6 | import { createServer } from 'vite'; |
7 | 7 | ||
8 | import { fileURLToDirname } from '../config/build-common.js'; | 8 | import { fileURLToDirname } from '../config/utils.js'; |
9 | 9 | ||
10 | /** @type {string} */ | 10 | /** @type {string} */ |
11 | const thisDir = fileURLToDirname(import.meta.url); | 11 | const thisDir = fileURLToDirname(import.meta.url); |
@@ -127,7 +127,7 @@ function setupMainPackageWatcher(viteDevServer) { | |||
127 | const path = '/'; | 127 | const path = '/'; |
128 | process.env.VITE_DEV_SERVER_URL = `${protocol}//${host}:${port}${path}`; | 128 | process.env.VITE_DEV_SERVER_URL = `${protocol}//${host}:${port}${path}`; |
129 | 129 | ||
130 | /** @type {import('child_process').ChildProcessWithoutNullStreams | null} */ | 130 | /** @type {import('child_process').ChildProcessByStdio<null, null, import('stream').Readable> | null} */ |
131 | let spawnProcess = null; | 131 | let spawnProcess = null; |
132 | 132 | ||
133 | return setupEsbuildWatcher( | 133 | return setupEsbuildWatcher( |
diff --git a/tsconfig.json b/tsconfig.json index dfffc9d..255f334 100644 --- a/tsconfig.json +++ b/tsconfig.json | |||
@@ -1,7 +1,7 @@ | |||
1 | { | 1 | { |
2 | "compilerOptions": { | 2 | "compilerOptions": { |
3 | "module": "es2022", | 3 | "module": "esnext", |
4 | "target": "es2021", | 4 | "target": "esnext", |
5 | "moduleResolution": "node", | 5 | "moduleResolution": "node", |
6 | "esModuleInterop": true, | 6 | "esModuleInterop": true, |
7 | "allowSyntheticDefaultImports": true, | 7 | "allowSyntheticDefaultImports": true, |
@@ -1203,10 +1203,11 @@ __metadata: | |||
1203 | "@types/lodash-es": ^4.17.5 | 1203 | "@types/lodash-es": ^4.17.5 |
1204 | "@types/ms": ^0.7.31 | 1204 | "@types/ms": ^0.7.31 |
1205 | "@types/node": ^17.0.5 | 1205 | "@types/node": ^17.0.5 |
1206 | chalk: ^5.0.0 | 1206 | chalk: ^4.1.2 |
1207 | electron: 16.0.5 | 1207 | electron: 16.0.5 |
1208 | electron-devtools-installer: ^3.2.0 | 1208 | electron-devtools-installer: ^3.2.0 |
1209 | esbuild: ^0.14.9 | 1209 | esbuild: ^0.14.9 |
1210 | jest: ^27.4.5 | ||
1210 | json5: ^2.2.0 | 1211 | json5: ^2.2.0 |
1211 | lodash-es: ^4.17.21 | 1212 | lodash-es: ^4.17.21 |
1212 | loglevel: ^1.8.0 | 1213 | loglevel: ^1.8.0 |
@@ -1224,7 +1225,7 @@ __metadata: | |||
1224 | resolution: "@sophie/preload@workspace:packages/preload" | 1225 | resolution: "@sophie/preload@workspace:packages/preload" |
1225 | dependencies: | 1226 | dependencies: |
1226 | "@sophie/shared": "workspace:*" | 1227 | "@sophie/shared": "workspace:*" |
1227 | "@types/jest": ^27.0.3 | 1228 | "@types/jest": ^27.4.0 |
1228 | electron: 16.0.5 | 1229 | electron: 16.0.5 |
1229 | jest: ^27.4.5 | 1230 | jest: ^27.4.5 |
1230 | jest-mock: ^27.4.2 | 1231 | jest-mock: ^27.4.2 |
@@ -1261,7 +1262,7 @@ __metadata: | |||
1261 | remotedev: ^0.2.9 | 1262 | remotedev: ^0.2.9 |
1262 | rimraf: ^3.0.2 | 1263 | rimraf: ^3.0.2 |
1263 | typescript: ^4.5.4 | 1264 | typescript: ^4.5.4 |
1264 | vite: ^2.7.9 | 1265 | vite: ^2.7.10 |
1265 | languageName: unknown | 1266 | languageName: unknown |
1266 | linkType: soft | 1267 | linkType: soft |
1267 | 1268 | ||
@@ -1441,13 +1442,13 @@ __metadata: | |||
1441 | languageName: node | 1442 | languageName: node |
1442 | linkType: hard | 1443 | linkType: hard |
1443 | 1444 | ||
1444 | "@types/jest@npm:^27.0.3": | 1445 | "@types/jest@npm:^27.4.0": |
1445 | version: 27.0.3 | 1446 | version: 27.4.0 |
1446 | resolution: "@types/jest@npm:27.0.3" | 1447 | resolution: "@types/jest@npm:27.4.0" |
1447 | dependencies: | 1448 | dependencies: |
1448 | jest-diff: ^27.0.0 | 1449 | jest-diff: ^27.0.0 |
1449 | pretty-format: ^27.0.0 | 1450 | pretty-format: ^27.0.0 |
1450 | checksum: 3683a9945821966f6dccddf337219a5d682633687c9d30df859223db553589f63e9b2c34e69f0cc845c86ffcf115742f25c12ea03c8d33d2244890fdc0af61e2 | 1451 | checksum: d2350267f954f9a2e4a15e5f02fbf19a77abfb9fd9e57a954de1fb0e9a0d3d5f8d3646ac7d9c42aeb4b4d828d2e70624ec149c85bb50a48634a54eed8429e1f8 |
1451 | languageName: node | 1452 | languageName: node |
1452 | linkType: hard | 1453 | linkType: hard |
1453 | 1454 | ||
@@ -2344,7 +2345,7 @@ __metadata: | |||
2344 | languageName: node | 2345 | languageName: node |
2345 | linkType: hard | 2346 | linkType: hard |
2346 | 2347 | ||
2347 | "chalk@npm:^4.0.0, chalk@npm:^4.1.0, chalk@npm:^4.1.1": | 2348 | "chalk@npm:^4.0.0, chalk@npm:^4.1.0, chalk@npm:^4.1.1, chalk@npm:^4.1.2": |
2348 | version: 4.1.2 | 2349 | version: 4.1.2 |
2349 | resolution: "chalk@npm:4.1.2" | 2350 | resolution: "chalk@npm:4.1.2" |
2350 | dependencies: | 2351 | dependencies: |
@@ -2354,13 +2355,6 @@ __metadata: | |||
2354 | languageName: node | 2355 | languageName: node |
2355 | linkType: hard | 2356 | linkType: hard |
2356 | 2357 | ||
2357 | "chalk@npm:^5.0.0": | ||
2358 | version: 5.0.0 | ||
2359 | resolution: "chalk@npm:5.0.0" | ||
2360 | checksum: 6eba7c518b9aa5fe882ae6d14a1ffa58c418d72a3faa7f72af56641f1bbef51b645fca1d6e05d42357b7d3c846cd504c0b7b64d12309cdd07867e3b4411e0d01 | ||
2361 | languageName: node | ||
2362 | linkType: hard | ||
2363 | |||
2364 | "char-regex@npm:^1.0.2": | 2358 | "char-regex@npm:^1.0.2": |
2365 | version: 1.0.2 | 2359 | version: 1.0.2 |
2366 | resolution: "char-regex@npm:1.0.2" | 2360 | resolution: "char-regex@npm:1.0.2" |
@@ -7001,7 +6995,7 @@ __metadata: | |||
7001 | resolution: "sophie@workspace:." | 6995 | resolution: "sophie@workspace:." |
7002 | dependencies: | 6996 | dependencies: |
7003 | "@electron/fuses": ^1.5.0 | 6997 | "@electron/fuses": ^1.5.0 |
7004 | "@types/jest": ^27.0.3 | 6998 | "@types/jest": ^27.4.0 |
7005 | "@vitejs/plugin-react": ^1.1.3 | 6999 | "@vitejs/plugin-react": ^1.1.3 |
7006 | chokidar: ^3.5.2 | 7000 | chokidar: ^3.5.2 |
7007 | cross-env: ^7.0.3 | 7001 | cross-env: ^7.0.3 |
@@ -7016,7 +7010,7 @@ __metadata: | |||
7016 | rollup: ^2.62.0 | 7010 | rollup: ^2.62.0 |
7017 | ts-jest: ^27.1.2 | 7011 | ts-jest: ^27.1.2 |
7018 | typescript: ^4.5.4 | 7012 | typescript: ^4.5.4 |
7019 | vite: ^2.7.9 | 7013 | vite: ^2.7.10 |
7020 | languageName: unknown | 7014 | languageName: unknown |
7021 | linkType: soft | 7015 | linkType: soft |
7022 | 7016 | ||
@@ -7673,9 +7667,9 @@ __metadata: | |||
7673 | languageName: node | 7667 | languageName: node |
7674 | linkType: hard | 7668 | linkType: hard |
7675 | 7669 | ||
7676 | "vite@npm:^2.7.9": | 7670 | "vite@npm:^2.7.10": |
7677 | version: 2.7.9 | 7671 | version: 2.7.10 |
7678 | resolution: "vite@npm:2.7.9" | 7672 | resolution: "vite@npm:2.7.10" |
7679 | dependencies: | 7673 | dependencies: |
7680 | esbuild: ^0.13.12 | 7674 | esbuild: ^0.13.12 |
7681 | fsevents: ~2.3.2 | 7675 | fsevents: ~2.3.2 |
@@ -7698,7 +7692,7 @@ __metadata: | |||
7698 | optional: true | 7692 | optional: true |
7699 | bin: | 7693 | bin: |
7700 | vite: bin/vite.js | 7694 | vite: bin/vite.js |
7701 | checksum: 6a13e0678fec4d5811d4e1bc796a98644856e4d7a56b5e56dbe513834c871d2313a065e4b82469de910568c0d6c762133517c6b7bccff4514f9acf5ed47f1f61 | 7695 | checksum: 1757108547ca34bd01a8ee3973cb04b484492c9e5690fc75f54bbcbde5965ab6e0a5b8355c336adf0a9117aa0fdae3053af3aafa9e5eb783282fffa948cfbe11 |
7702 | languageName: node | 7696 | languageName: node |
7703 | linkType: hard | 7697 | linkType: hard |
7704 | 7698 | ||