diff options
author | 2022-04-26 16:55:57 +0200 | |
---|---|---|
committer | 2022-05-16 00:55:02 +0200 | |
commit | b971de0717a66ae6085670fe5d3a469e236a9446 (patch) | |
tree | c64352908648512bad713fc1e91dd5f07a66968c /packages/main/src/reactions/__tests__/synchronizeConfig.spec.ts | |
parent | refactor: remove json5 dependency (diff) | |
download | sophie-b971de0717a66ae6085670fe5d3a469e236a9446.tar.gz sophie-b971de0717a66ae6085670fe5d3a469e236a9446.tar.zst sophie-b971de0717a66ae6085670fe5d3a469e236a9446.zip |
refactor: config file saving and debugging
Reduce the number of dependencies and the amount of code running in a
security sensitive context.
Instead of a deep comparison, we just compare the serialized versions
of the config files.
Signed-off-by: Kristóf Marussy <kristof@marussy.com>
Diffstat (limited to 'packages/main/src/reactions/__tests__/synchronizeConfig.spec.ts')
-rw-r--r-- | packages/main/src/reactions/__tests__/synchronizeConfig.spec.ts | 56 |
1 files changed, 32 insertions, 24 deletions
diff --git a/packages/main/src/reactions/__tests__/synchronizeConfig.spec.ts b/packages/main/src/reactions/__tests__/synchronizeConfig.spec.ts index b5013ea..d3338d0 100644 --- a/packages/main/src/reactions/__tests__/synchronizeConfig.spec.ts +++ b/packages/main/src/reactions/__tests__/synchronizeConfig.spec.ts | |||
@@ -25,7 +25,7 @@ import type ConfigRepository from '../../infrastructure/config/ConfigRepository' | |||
25 | import SharedStore from '../../stores/SharedStore'; | 25 | import SharedStore from '../../stores/SharedStore'; |
26 | import type Disposer from '../../utils/Disposer'; | 26 | import type Disposer from '../../utils/Disposer'; |
27 | import { silenceLogger } from '../../utils/log'; | 27 | import { silenceLogger } from '../../utils/log'; |
28 | import synchronizeConfig from '../synchronizeConfig'; | 28 | import synchronizeConfig, { serializeConfig } from '../synchronizeConfig'; |
29 | 29 | ||
30 | let store: SharedStore; | 30 | let store: SharedStore; |
31 | const repository: ConfigRepository = { | 31 | const repository: ConfigRepository = { |
@@ -70,11 +70,11 @@ describe('when synchronizeializing', () => { | |||
70 | beforeEach(() => { | 70 | beforeEach(() => { |
71 | mocked(repository.readConfig).mockResolvedValueOnce({ | 71 | mocked(repository.readConfig).mockResolvedValueOnce({ |
72 | found: true, | 72 | found: true, |
73 | data: { | 73 | contents: serializeConfig({ |
74 | // Use a default empty config file to not trigger config rewrite. | 74 | // Use a default empty config file to not trigger config rewrite. |
75 | ...store.config, | 75 | ...store.config, |
76 | themeSource: 'dark', | 76 | themeSource: 'dark', |
77 | }, | 77 | }), |
78 | }); | 78 | }); |
79 | }); | 79 | }); |
80 | 80 | ||
@@ -97,12 +97,15 @@ describe('when synchronizeializing', () => { | |||
97 | it('should update the config file if new details are added during read', async () => { | 97 | it('should update the config file if new details are added during read', async () => { |
98 | mocked(repository.readConfig).mockResolvedValueOnce({ | 98 | mocked(repository.readConfig).mockResolvedValueOnce({ |
99 | found: true, | 99 | found: true, |
100 | data: { | 100 | contents: `{ |
101 | themeSource: 'light', | 101 | "themeSource": "light", |
102 | profile: { | 102 | "profiles": [ |
103 | name: 'Test profile', | 103 | { |
104 | }, | 104 | "name": "Test profile" |
105 | }, | 105 | } |
106 | ] | ||
107 | } | ||
108 | `, | ||
106 | }); | 109 | }); |
107 | await synchronizeConfig(store, repository); | 110 | await synchronizeConfig(store, repository); |
108 | expect(repository.writeConfig).toHaveBeenCalledTimes(1); | 111 | expect(repository.writeConfig).toHaveBeenCalledTimes(1); |
@@ -111,9 +114,10 @@ describe('when synchronizeializing', () => { | |||
111 | it('should not apply an invalid config file but should not overwrite it', async () => { | 114 | it('should not apply an invalid config file but should not overwrite it', async () => { |
112 | mocked(repository.readConfig).mockResolvedValueOnce({ | 115 | mocked(repository.readConfig).mockResolvedValueOnce({ |
113 | found: true, | 116 | found: true, |
114 | data: { | 117 | contents: `{ |
115 | themeSource: -1, | 118 | "themeSource": -1 |
116 | }, | 119 | } |
120 | `, | ||
117 | }); | 121 | }); |
118 | await synchronizeConfig(store, repository); | 122 | await synchronizeConfig(store, repository); |
119 | expect(store.settings.themeSource).not.toBe(-1); | 123 | expect(store.settings.themeSource).not.toBe(-1); |
@@ -136,7 +140,7 @@ describe('when it has loaded the config', () => { | |||
136 | beforeEach(async () => { | 140 | beforeEach(async () => { |
137 | mocked(repository.readConfig).mockResolvedValueOnce({ | 141 | mocked(repository.readConfig).mockResolvedValueOnce({ |
138 | found: true, | 142 | found: true, |
139 | data: store.config, | 143 | contents: serializeConfig(store.config), |
140 | }); | 144 | }); |
141 | mocked(repository.watchConfig).mockReturnValueOnce(watcherDisposer); | 145 | mocked(repository.watchConfig).mockReturnValueOnce(watcherDisposer); |
142 | sutDisposer = await synchronizeConfig(store, repository, throttleMs); | 146 | sutDisposer = await synchronizeConfig(store, repository, throttleMs); |
@@ -163,11 +167,11 @@ describe('when it has loaded the config', () => { | |||
163 | it('should read the config file when it has changed', async () => { | 167 | it('should read the config file when it has changed', async () => { |
164 | mocked(repository.readConfig).mockResolvedValueOnce({ | 168 | mocked(repository.readConfig).mockResolvedValueOnce({ |
165 | found: true, | 169 | found: true, |
166 | data: { | 170 | contents: serializeConfig({ |
167 | // Use a default empty config file to not trigger config rewrite. | 171 | // Use a default empty config file to not trigger config rewrite. |
168 | ...store.config, | 172 | ...store.config, |
169 | themeSource: 'dark', | 173 | themeSource: 'dark', |
170 | }, | 174 | }), |
171 | }); | 175 | }); |
172 | await configChangedCallback(); | 176 | await configChangedCallback(); |
173 | // Do not write back the changes we have just read. | 177 | // Do not write back the changes we have just read. |
@@ -178,12 +182,15 @@ describe('when it has loaded the config', () => { | |||
178 | it('should update the config file if new details are added', async () => { | 182 | it('should update the config file if new details are added', async () => { |
179 | mocked(repository.readConfig).mockResolvedValueOnce({ | 183 | mocked(repository.readConfig).mockResolvedValueOnce({ |
180 | found: true, | 184 | found: true, |
181 | data: { | 185 | contents: `{ |
182 | themeSource: 'light', | 186 | "themeSource": "light", |
183 | profile: { | 187 | "profiles": [ |
184 | name: 'Test profile', | 188 | { |
185 | }, | 189 | "name": "Test profile" |
186 | }, | 190 | } |
191 | ] | ||
192 | } | ||
193 | `, | ||
187 | }); | 194 | }); |
188 | await configChangedCallback(); | 195 | await configChangedCallback(); |
189 | expect(repository.writeConfig).toHaveBeenCalledTimes(1); | 196 | expect(repository.writeConfig).toHaveBeenCalledTimes(1); |
@@ -192,9 +199,10 @@ describe('when it has loaded the config', () => { | |||
192 | it('should not apply an invalid config file when it has changed but should not overwrite it', async () => { | 199 | it('should not apply an invalid config file when it has changed but should not overwrite it', async () => { |
193 | mocked(repository.readConfig).mockResolvedValueOnce({ | 200 | mocked(repository.readConfig).mockResolvedValueOnce({ |
194 | found: true, | 201 | found: true, |
195 | data: { | 202 | contents: `{ |
196 | themeSource: -1, | 203 | "themeSource": -1 |
197 | }, | 204 | } |
205 | `, | ||
198 | }); | 206 | }); |
199 | await configChangedCallback(); | 207 | await configChangedCallback(); |
200 | expect(store.settings.themeSource).not.toBe(-1); | 208 | expect(store.settings.themeSource).not.toBe(-1); |