aboutsummaryrefslogtreecommitdiffstats
path: root/packages/main/src/controllers/__tests__/config.spec.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/main/src/controllers/__tests__/config.spec.ts')
-rw-r--r--packages/main/src/controllers/__tests__/config.spec.ts184
1 files changed, 184 insertions, 0 deletions
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});