@baukit/preferences-core 0.7.2 → 0.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -1
- package/src/controller.test.ts +600 -0
- package/src/controller.ts +362 -0
- package/src/definitions.ts +78 -0
- package/src/index.ts +4 -0
- package/src/raw.d.ts +4 -0
- package/src/repository-store.test.ts +173 -0
- package/src/store.test.ts +27 -0
- package/src/store.ts +97 -0
- package/src/vitest.ts +51 -0
- package/src/wire.test.ts +76 -0
- package/src/wire.ts +33 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@baukit/preferences-core",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.4",
|
|
4
4
|
"description": "Typed preferences and their update behavior, with storage and effects supplied by the product.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Patrick Koss",
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
},
|
|
35
35
|
"files": [
|
|
36
36
|
"dist",
|
|
37
|
+
"src",
|
|
37
38
|
"README.md",
|
|
38
39
|
"LICENSE"
|
|
39
40
|
],
|
|
@@ -0,0 +1,600 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createPreferenceController,
|
|
5
|
+
type PreferenceRegistry,
|
|
6
|
+
type PreferenceSideEffect,
|
|
7
|
+
} from './index.js';
|
|
8
|
+
import { InMemoryPreferenceStore, type PreferenceStore } from './store.js';
|
|
9
|
+
|
|
10
|
+
interface TestPreferences {
|
|
11
|
+
readonly language: 'system' | 'de' | 'en';
|
|
12
|
+
readonly theme: 'system' | 'dark' | 'light';
|
|
13
|
+
readonly gameLayerEnabled: boolean;
|
|
14
|
+
readonly customColor: string | null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type GameLayerEffect = PreferenceSideEffect<'gameLayerEnabled', boolean, TestPreferences>;
|
|
18
|
+
|
|
19
|
+
function definitions(gameLayerEffect?: GameLayerEffect): PreferenceRegistry<TestPreferences> {
|
|
20
|
+
return {
|
|
21
|
+
language: {
|
|
22
|
+
key: 'language',
|
|
23
|
+
defaultValue: 'system',
|
|
24
|
+
normalize: (value) => (value === 'de' || value === 'en' ? value : 'system'),
|
|
25
|
+
scope: 'synced',
|
|
26
|
+
},
|
|
27
|
+
theme: {
|
|
28
|
+
key: 'theme',
|
|
29
|
+
defaultValue: 'system',
|
|
30
|
+
normalize: (value) =>
|
|
31
|
+
value === 'dark' || value === 'light' || value === 'system' ? value : 'system',
|
|
32
|
+
scope: 'identity',
|
|
33
|
+
},
|
|
34
|
+
gameLayerEnabled: {
|
|
35
|
+
key: 'gameLayerEnabled',
|
|
36
|
+
defaultValue: false,
|
|
37
|
+
normalize: (value) => value === true,
|
|
38
|
+
scope: 'synced',
|
|
39
|
+
...(gameLayerEffect ? { sideEffect: gameLayerEffect } : {}),
|
|
40
|
+
},
|
|
41
|
+
customColor: {
|
|
42
|
+
key: 'customColor',
|
|
43
|
+
defaultValue: null,
|
|
44
|
+
normalize: (value) => (typeof value === 'string' ? value : null),
|
|
45
|
+
scope: 'synced',
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const defaults: TestPreferences = {
|
|
51
|
+
language: 'system',
|
|
52
|
+
theme: 'system',
|
|
53
|
+
gameLayerEnabled: false,
|
|
54
|
+
customColor: null,
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
function deferred<T>(): {
|
|
58
|
+
readonly promise: Promise<T>;
|
|
59
|
+
resolve(value: T): void;
|
|
60
|
+
reject(error: unknown): void;
|
|
61
|
+
} {
|
|
62
|
+
let resolvePromise!: (value: T) => void;
|
|
63
|
+
let rejectPromise!: (error: unknown) => void;
|
|
64
|
+
const promise = new Promise<T>((resolve, reject) => {
|
|
65
|
+
resolvePromise = resolve;
|
|
66
|
+
rejectPromise = reject;
|
|
67
|
+
});
|
|
68
|
+
return { promise, resolve: resolvePromise, reject: rejectPromise };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function gatedStore(initialValues: TestPreferences): {
|
|
72
|
+
readonly store: PreferenceStore<TestPreferences>;
|
|
73
|
+
readonly writes: TestPreferences[];
|
|
74
|
+
resolve(index: number): void;
|
|
75
|
+
reject(index: number, error: Error): void;
|
|
76
|
+
} {
|
|
77
|
+
let stored = initialValues;
|
|
78
|
+
const writes: TestPreferences[] = [];
|
|
79
|
+
const gates: ReturnType<typeof deferred<undefined>>[] = [];
|
|
80
|
+
return {
|
|
81
|
+
store: {
|
|
82
|
+
read: () => Promise.resolve(stored),
|
|
83
|
+
patch: (patch) => {
|
|
84
|
+
const next = { ...stored, ...patch };
|
|
85
|
+
const gate = deferred<undefined>();
|
|
86
|
+
writes.push(next);
|
|
87
|
+
gates.push(gate);
|
|
88
|
+
return gate.promise.then(() => {
|
|
89
|
+
stored = next;
|
|
90
|
+
return stored;
|
|
91
|
+
});
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
writes,
|
|
95
|
+
resolve: (index) => {
|
|
96
|
+
gates[index]?.resolve(undefined);
|
|
97
|
+
},
|
|
98
|
+
reject: (index, error) => {
|
|
99
|
+
gates[index]?.reject(error);
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
describe('preference controller hydration', () => {
|
|
105
|
+
it('normalizes stored values and supplies defaults for omitted values', async () => {
|
|
106
|
+
const store: PreferenceStore<TestPreferences> = {
|
|
107
|
+
read: () =>
|
|
108
|
+
Promise.resolve({ language: 'invalid', theme: 'dark' } as unknown as TestPreferences),
|
|
109
|
+
patch: () => Promise.reject(new Error('unused')),
|
|
110
|
+
};
|
|
111
|
+
const visible = vi.fn();
|
|
112
|
+
const controller = createPreferenceController({
|
|
113
|
+
definitions: definitions(),
|
|
114
|
+
store,
|
|
115
|
+
onVisibleChange: visible,
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
await expect(controller.hydrate()).resolves.toEqual({ ...defaults, theme: 'dark' });
|
|
119
|
+
expect(controller.getState()).toEqual({
|
|
120
|
+
values: { ...defaults, theme: 'dark' },
|
|
121
|
+
status: 'ready',
|
|
122
|
+
error: null,
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
describe('preference controller updates', () => {
|
|
128
|
+
it('shows an optimistic value and restores the previous value after a failed write', async () => {
|
|
129
|
+
const failure = new Error('local write failed');
|
|
130
|
+
const write = deferred<TestPreferences>();
|
|
131
|
+
const store: PreferenceStore<TestPreferences> = {
|
|
132
|
+
read: () => Promise.resolve(defaults),
|
|
133
|
+
patch: () => write.promise,
|
|
134
|
+
};
|
|
135
|
+
const visible: TestPreferences[] = [];
|
|
136
|
+
const controller = createPreferenceController({
|
|
137
|
+
definitions: definitions(),
|
|
138
|
+
store,
|
|
139
|
+
onVisibleChange: (values) => visible.push(values),
|
|
140
|
+
});
|
|
141
|
+
await controller.hydrate();
|
|
142
|
+
|
|
143
|
+
const update = controller.update({ theme: 'dark' });
|
|
144
|
+
expect(controller.getState().values.theme).toBe('dark');
|
|
145
|
+
write.reject(failure);
|
|
146
|
+
|
|
147
|
+
await expect(update).rejects.toBe(failure);
|
|
148
|
+
expect(controller.getState()).toEqual({ values: defaults, status: 'ready', error: failure });
|
|
149
|
+
expect(visible.at(-1)).toEqual(defaults);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('keeps optimistic updates as the default compatibility behavior', async () => {
|
|
153
|
+
const firstWrite = deferred<TestPreferences>();
|
|
154
|
+
const secondWrite = deferred<TestPreferences>();
|
|
155
|
+
const patch = vi
|
|
156
|
+
.fn<(value: Partial<TestPreferences>) => Promise<TestPreferences>>()
|
|
157
|
+
.mockReturnValueOnce(firstWrite.promise)
|
|
158
|
+
.mockReturnValueOnce(secondWrite.promise);
|
|
159
|
+
const controller = createPreferenceController({
|
|
160
|
+
definitions: definitions(),
|
|
161
|
+
store: { read: () => Promise.resolve(defaults), patch },
|
|
162
|
+
onVisibleChange: () => undefined,
|
|
163
|
+
});
|
|
164
|
+
await controller.hydrate();
|
|
165
|
+
|
|
166
|
+
const first = controller.update({ theme: 'dark' });
|
|
167
|
+
const second = controller.update({ language: 'de' });
|
|
168
|
+
|
|
169
|
+
expect(patch).toHaveBeenCalledTimes(2);
|
|
170
|
+
expect(controller.getState().values).toEqual({ ...defaults, theme: 'dark', language: 'de' });
|
|
171
|
+
expect('pendingCount' in controller.getState()).toBe(false);
|
|
172
|
+
|
|
173
|
+
secondWrite.resolve({ ...defaults, language: 'de' });
|
|
174
|
+
await expect(second).resolves.toEqual({ ...defaults, language: 'de' });
|
|
175
|
+
firstWrite.resolve({ ...defaults, theme: 'dark' });
|
|
176
|
+
await expect(first).resolves.toEqual({ ...defaults, theme: 'dark' });
|
|
177
|
+
|
|
178
|
+
expect(controller.getState().values).toEqual({ ...defaults, theme: 'dark' });
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('does not patch storage when an old payload omits a preference', async () => {
|
|
182
|
+
const stored = { ...defaults, language: 'de' as const };
|
|
183
|
+
const patch = vi.fn<(value: Partial<TestPreferences>) => Promise<TestPreferences>>();
|
|
184
|
+
patch.mockResolvedValue(stored);
|
|
185
|
+
const controller = createPreferenceController({
|
|
186
|
+
definitions: definitions(),
|
|
187
|
+
store: { read: () => Promise.resolve(stored), patch },
|
|
188
|
+
onVisibleChange: () => undefined,
|
|
189
|
+
});
|
|
190
|
+
await controller.hydrate();
|
|
191
|
+
|
|
192
|
+
await expect(controller.applyWireValue('language', { state: 'absent' })).resolves.toEqual(
|
|
193
|
+
stored,
|
|
194
|
+
);
|
|
195
|
+
expect(patch).not.toHaveBeenCalled();
|
|
196
|
+
expect(controller.getState().values.language).toBe('de');
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('applies explicit wire null without turning it into omission', async () => {
|
|
200
|
+
const stored = { ...defaults, customColor: '#336699' };
|
|
201
|
+
const store = new InMemoryPreferenceStore<TestPreferences>(defaults, stored);
|
|
202
|
+
const controller = createPreferenceController({
|
|
203
|
+
definitions: definitions(),
|
|
204
|
+
store,
|
|
205
|
+
onVisibleChange: () => undefined,
|
|
206
|
+
});
|
|
207
|
+
await controller.hydrate();
|
|
208
|
+
|
|
209
|
+
await controller.applyWireValue('customColor', { state: 'null' });
|
|
210
|
+
await expect(store.read()).resolves.toEqual({ ...stored, customColor: null });
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
describe('serialized preference controller updates', () => {
|
|
215
|
+
it('serializes rapid writes against the latest committed values', async () => {
|
|
216
|
+
const gated = gatedStore(defaults);
|
|
217
|
+
const controller = createPreferenceController({
|
|
218
|
+
definitions: definitions(),
|
|
219
|
+
store: gated.store,
|
|
220
|
+
onVisibleChange: () => undefined,
|
|
221
|
+
updateMode: 'serialized',
|
|
222
|
+
});
|
|
223
|
+
await controller.hydrate();
|
|
224
|
+
|
|
225
|
+
const theme = controller.update({ theme: 'dark' });
|
|
226
|
+
const language = controller.update({ language: 'de' });
|
|
227
|
+
|
|
228
|
+
expect(gated.writes).toEqual([{ ...defaults, theme: 'dark' }]);
|
|
229
|
+
expect(controller.getState()).toMatchObject({ values: defaults, pendingCount: 2 });
|
|
230
|
+
|
|
231
|
+
gated.resolve(0);
|
|
232
|
+
await expect(theme).resolves.toEqual({ ...defaults, theme: 'dark' });
|
|
233
|
+
await vi.waitFor(() => {
|
|
234
|
+
expect(gated.writes).toHaveLength(2);
|
|
235
|
+
});
|
|
236
|
+
expect(gated.writes[1]).toEqual({ ...defaults, theme: 'dark', language: 'de' });
|
|
237
|
+
expect(controller.getState()).toMatchObject({
|
|
238
|
+
values: { ...defaults, theme: 'dark' },
|
|
239
|
+
pendingCount: 1,
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
gated.resolve(1);
|
|
243
|
+
await expect(language).resolves.toEqual({ ...defaults, theme: 'dark', language: 'de' });
|
|
244
|
+
expect(controller.getState()).toEqual({
|
|
245
|
+
values: { ...defaults, theme: 'dark', language: 'de' },
|
|
246
|
+
status: 'ready',
|
|
247
|
+
pendingCount: 0,
|
|
248
|
+
error: null,
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it('continues after a failed first write without leaking its value', async () => {
|
|
253
|
+
const gated = gatedStore(defaults);
|
|
254
|
+
const controller = createPreferenceController({
|
|
255
|
+
definitions: definitions(),
|
|
256
|
+
store: gated.store,
|
|
257
|
+
onVisibleChange: () => undefined,
|
|
258
|
+
updateMode: 'serialized',
|
|
259
|
+
});
|
|
260
|
+
await controller.hydrate();
|
|
261
|
+
|
|
262
|
+
const failed = controller.update({ theme: 'dark' });
|
|
263
|
+
const next = controller.update({ language: 'de' });
|
|
264
|
+
const failure = new Error('disk full');
|
|
265
|
+
gated.reject(0, failure);
|
|
266
|
+
|
|
267
|
+
await expect(failed).rejects.toBe(failure);
|
|
268
|
+
await vi.waitFor(() => {
|
|
269
|
+
expect(gated.writes).toHaveLength(2);
|
|
270
|
+
});
|
|
271
|
+
expect(gated.writes[1]).toEqual({ ...defaults, language: 'de' });
|
|
272
|
+
gated.resolve(1);
|
|
273
|
+
|
|
274
|
+
await expect(next).resolves.toEqual({ ...defaults, language: 'de' });
|
|
275
|
+
expect(controller.getState()).toMatchObject({
|
|
276
|
+
values: { ...defaults, language: 'de' },
|
|
277
|
+
pendingCount: 0,
|
|
278
|
+
error: null,
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it('rolls back a preview after a failed serialized write', async () => {
|
|
283
|
+
const events: string[] = [];
|
|
284
|
+
const failure = new Error('write failed');
|
|
285
|
+
const write = deferred<TestPreferences>();
|
|
286
|
+
const controller = createPreferenceController({
|
|
287
|
+
definitions: definitions({
|
|
288
|
+
mode: 'preview-with-rollback',
|
|
289
|
+
onError: 'report',
|
|
290
|
+
preview: () => {
|
|
291
|
+
events.push('preview');
|
|
292
|
+
},
|
|
293
|
+
rollback: () => {
|
|
294
|
+
events.push('rollback');
|
|
295
|
+
},
|
|
296
|
+
afterPersistence: () => {
|
|
297
|
+
events.push('persisted');
|
|
298
|
+
},
|
|
299
|
+
}),
|
|
300
|
+
store: { read: () => Promise.resolve(defaults), patch: () => write.promise },
|
|
301
|
+
onVisibleChange: () => undefined,
|
|
302
|
+
updateMode: 'serialized',
|
|
303
|
+
});
|
|
304
|
+
await controller.hydrate();
|
|
305
|
+
|
|
306
|
+
const update = controller.update({ gameLayerEnabled: true });
|
|
307
|
+
await vi.waitFor(() => {
|
|
308
|
+
expect(events).toEqual(['preview']);
|
|
309
|
+
});
|
|
310
|
+
expect(controller.getState()).toMatchObject({ values: defaults, pendingCount: 1 });
|
|
311
|
+
write.reject(failure);
|
|
312
|
+
|
|
313
|
+
await expect(update).rejects.toBe(failure);
|
|
314
|
+
expect(events).toEqual(['preview', 'rollback']);
|
|
315
|
+
expect(controller.getState()).toMatchObject({
|
|
316
|
+
values: defaults,
|
|
317
|
+
pendingCount: 0,
|
|
318
|
+
error: failure,
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
it('invalidates in-flight publication and queued writes after an identity switch', async () => {
|
|
323
|
+
const old = gatedStore({ ...defaults, theme: 'dark' });
|
|
324
|
+
const nextValues = { ...defaults, language: 'de' as const };
|
|
325
|
+
const nextPatch = vi.fn<(patch: Partial<TestPreferences>) => Promise<TestPreferences>>();
|
|
326
|
+
const visible: TestPreferences[] = [];
|
|
327
|
+
const controller = createPreferenceController({
|
|
328
|
+
definitions: definitions(),
|
|
329
|
+
store: old.store,
|
|
330
|
+
onVisibleChange: (values) => visible.push(values),
|
|
331
|
+
updateMode: 'serialized',
|
|
332
|
+
});
|
|
333
|
+
await controller.hydrate();
|
|
334
|
+
|
|
335
|
+
const inFlight = controller.update({ theme: 'light' });
|
|
336
|
+
const queued = controller.update({ language: 'en' });
|
|
337
|
+
await controller.switchIdentity({
|
|
338
|
+
read: () => Promise.resolve(nextValues),
|
|
339
|
+
patch: nextPatch,
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
old.resolve(0);
|
|
343
|
+
await expect(inFlight).resolves.toEqual(nextValues);
|
|
344
|
+
await expect(queued).resolves.toEqual(nextValues);
|
|
345
|
+
|
|
346
|
+
expect(old.writes).toHaveLength(1);
|
|
347
|
+
expect(nextPatch).not.toHaveBeenCalled();
|
|
348
|
+
expect(visible.at(-1)).toEqual(nextValues);
|
|
349
|
+
expect(controller.getState()).toEqual({
|
|
350
|
+
values: nextValues,
|
|
351
|
+
status: 'ready',
|
|
352
|
+
pendingCount: 0,
|
|
353
|
+
error: null,
|
|
354
|
+
});
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
it('stops publishing pending and committed values during an in-flight write', async () => {
|
|
358
|
+
const gated = gatedStore(defaults);
|
|
359
|
+
const visible: TestPreferences[] = [];
|
|
360
|
+
const controller = createPreferenceController({
|
|
361
|
+
definitions: definitions(),
|
|
362
|
+
store: gated.store,
|
|
363
|
+
onVisibleChange: (values) => visible.push(values),
|
|
364
|
+
updateMode: 'serialized',
|
|
365
|
+
});
|
|
366
|
+
await controller.hydrate();
|
|
367
|
+
|
|
368
|
+
const update = controller.update({ theme: 'dark' });
|
|
369
|
+
const publishedBeforeStop = visible.length;
|
|
370
|
+
controller.stop();
|
|
371
|
+
gated.resolve(0);
|
|
372
|
+
|
|
373
|
+
await expect(update).resolves.toEqual({ ...defaults, theme: 'dark' });
|
|
374
|
+
expect(visible).toHaveLength(publishedBeforeStop);
|
|
375
|
+
expect(controller.getState()).toEqual({
|
|
376
|
+
values: { ...defaults, theme: 'dark' },
|
|
377
|
+
status: 'ready',
|
|
378
|
+
pendingCount: 0,
|
|
379
|
+
error: null,
|
|
380
|
+
});
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
it('rejects unknown keys without adding them to the queue', async () => {
|
|
384
|
+
const patch = vi.fn<(value: Partial<TestPreferences>) => Promise<TestPreferences>>();
|
|
385
|
+
const controller = createPreferenceController({
|
|
386
|
+
definitions: definitions(),
|
|
387
|
+
store: { read: () => Promise.resolve(defaults), patch },
|
|
388
|
+
onVisibleChange: () => undefined,
|
|
389
|
+
updateMode: 'serialized',
|
|
390
|
+
});
|
|
391
|
+
await controller.hydrate();
|
|
392
|
+
|
|
393
|
+
await expect(
|
|
394
|
+
controller.update({ removedPreference: true } as unknown as Partial<TestPreferences>),
|
|
395
|
+
).rejects.toThrow('Unknown preference key: removedPreference');
|
|
396
|
+
expect(patch).not.toHaveBeenCalled();
|
|
397
|
+
expect(controller.getState()).toMatchObject({ pendingCount: 0, error: null });
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
it('reports pending-count transitions while committed values stay visible', async () => {
|
|
401
|
+
const gated = gatedStore(defaults);
|
|
402
|
+
const transitions: { pendingCount: number; theme: TestPreferences['theme'] }[] = [];
|
|
403
|
+
const controllerRef: { current?: ReturnType<typeof createSerializedController> } = {};
|
|
404
|
+
const controller = createSerializedController(gated.store, () => {
|
|
405
|
+
const state = controllerRef.current?.getState();
|
|
406
|
+
if (!state) {
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
transitions.push({ pendingCount: state.pendingCount, theme: state.values.theme });
|
|
410
|
+
});
|
|
411
|
+
controllerRef.current = controller;
|
|
412
|
+
await controller.hydrate();
|
|
413
|
+
|
|
414
|
+
const first = controller.update({ theme: 'dark' });
|
|
415
|
+
const second = controller.update({ language: 'de' });
|
|
416
|
+
expect(controller.getState().pendingCount).toBe(2);
|
|
417
|
+
expect(controller.getState().values).toEqual(defaults);
|
|
418
|
+
|
|
419
|
+
gated.resolve(0);
|
|
420
|
+
await first;
|
|
421
|
+
expect(controller.getState()).toMatchObject({
|
|
422
|
+
values: { ...defaults, theme: 'dark' },
|
|
423
|
+
pendingCount: 1,
|
|
424
|
+
});
|
|
425
|
+
await vi.waitFor(() => {
|
|
426
|
+
expect(gated.writes).toHaveLength(2);
|
|
427
|
+
});
|
|
428
|
+
gated.resolve(1);
|
|
429
|
+
await second;
|
|
430
|
+
expect(controller.getState().pendingCount).toBe(0);
|
|
431
|
+
expect(transitions).toContainEqual({ pendingCount: 2, theme: 'system' });
|
|
432
|
+
expect(transitions.at(-1)).toEqual({ pendingCount: 0, theme: 'dark' });
|
|
433
|
+
});
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
function createSerializedController(
|
|
437
|
+
store: PreferenceStore<TestPreferences>,
|
|
438
|
+
onVisibleChange: (values: TestPreferences) => void,
|
|
439
|
+
) {
|
|
440
|
+
return createPreferenceController({
|
|
441
|
+
definitions: definitions(),
|
|
442
|
+
store,
|
|
443
|
+
onVisibleChange,
|
|
444
|
+
updateMode: 'serialized',
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
describe('preference controller identity changes', () => {
|
|
449
|
+
it('clears the previous identity before reading the next identity', async () => {
|
|
450
|
+
const previous = { ...defaults, language: 'de' as const, theme: 'dark' as const };
|
|
451
|
+
const next = { ...defaults, language: 'en' as const, theme: 'light' as const };
|
|
452
|
+
const nextRead = deferred<TestPreferences | undefined>();
|
|
453
|
+
const visible: TestPreferences[] = [];
|
|
454
|
+
const controller = createPreferenceController({
|
|
455
|
+
definitions: definitions(),
|
|
456
|
+
store: new InMemoryPreferenceStore(defaults, previous),
|
|
457
|
+
onVisibleChange: (values) => visible.push(values),
|
|
458
|
+
});
|
|
459
|
+
await controller.hydrate();
|
|
460
|
+
|
|
461
|
+
const switched = controller.switchIdentity({
|
|
462
|
+
read: () => nextRead.promise,
|
|
463
|
+
patch: () => Promise.reject(new Error('unused')),
|
|
464
|
+
});
|
|
465
|
+
expect(controller.getState().values).toEqual(defaults);
|
|
466
|
+
expect(visible.at(-1)).toEqual(defaults);
|
|
467
|
+
nextRead.resolve(next);
|
|
468
|
+
|
|
469
|
+
await expect(switched).resolves.toEqual(next);
|
|
470
|
+
expect(visible.slice(-2)).toEqual([defaults, next]);
|
|
471
|
+
});
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
describe('preference side effects', () => {
|
|
475
|
+
it('runs the default side effect only after persistence succeeds', async () => {
|
|
476
|
+
const write = deferred<TestPreferences>();
|
|
477
|
+
const run = vi.fn();
|
|
478
|
+
const controller = createPreferenceController({
|
|
479
|
+
definitions: definitions({ mode: 'after-persistence', onError: 'report', run }),
|
|
480
|
+
store: { read: () => Promise.resolve(defaults), patch: () => write.promise },
|
|
481
|
+
onVisibleChange: () => undefined,
|
|
482
|
+
});
|
|
483
|
+
await controller.hydrate();
|
|
484
|
+
|
|
485
|
+
const update = controller.update({ gameLayerEnabled: true });
|
|
486
|
+
expect(run).not.toHaveBeenCalled();
|
|
487
|
+
write.resolve({ ...defaults, gameLayerEnabled: true });
|
|
488
|
+
await update;
|
|
489
|
+
|
|
490
|
+
expect(run).toHaveBeenCalledOnce();
|
|
491
|
+
expect(run).toHaveBeenCalledWith(
|
|
492
|
+
expect.objectContaining({ previousValue: false, value: true }),
|
|
493
|
+
);
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
it('allows an explicit preview and rolls it back after persistence fails', async () => {
|
|
497
|
+
const events: string[] = [];
|
|
498
|
+
const failure = new Error('write failed');
|
|
499
|
+
const controller = createPreferenceController({
|
|
500
|
+
definitions: definitions({
|
|
501
|
+
mode: 'preview-with-rollback',
|
|
502
|
+
onError: 'report',
|
|
503
|
+
preview: () => {
|
|
504
|
+
events.push('preview');
|
|
505
|
+
},
|
|
506
|
+
rollback: () => {
|
|
507
|
+
events.push('rollback');
|
|
508
|
+
},
|
|
509
|
+
afterPersistence: () => {
|
|
510
|
+
events.push('persisted');
|
|
511
|
+
},
|
|
512
|
+
}),
|
|
513
|
+
store: {
|
|
514
|
+
read: () => Promise.resolve(defaults),
|
|
515
|
+
patch: () => Promise.reject(failure),
|
|
516
|
+
},
|
|
517
|
+
onVisibleChange: () => undefined,
|
|
518
|
+
});
|
|
519
|
+
await controller.hydrate();
|
|
520
|
+
|
|
521
|
+
await expect(controller.update({ gameLayerEnabled: true })).rejects.toBe(failure);
|
|
522
|
+
expect(events).toEqual(['preview', 'rollback']);
|
|
523
|
+
expect(controller.getState().values.gameLayerEnabled).toBe(false);
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
it('reports a post-persistence side-effect failure without undoing persisted state', async () => {
|
|
527
|
+
const failure = new Error('query toggle failed');
|
|
528
|
+
const store = new InMemoryPreferenceStore<TestPreferences>(defaults, defaults);
|
|
529
|
+
const controller = createPreferenceController({
|
|
530
|
+
definitions: definitions({
|
|
531
|
+
mode: 'after-persistence',
|
|
532
|
+
onError: 'report',
|
|
533
|
+
run: () => Promise.reject(failure),
|
|
534
|
+
}),
|
|
535
|
+
store,
|
|
536
|
+
onVisibleChange: () => undefined,
|
|
537
|
+
});
|
|
538
|
+
await controller.hydrate();
|
|
539
|
+
|
|
540
|
+
await expect(controller.update({ gameLayerEnabled: true })).rejects.toBe(failure);
|
|
541
|
+
expect(controller.getState()).toEqual({
|
|
542
|
+
values: { ...defaults, gameLayerEnabled: true },
|
|
543
|
+
status: 'ready',
|
|
544
|
+
error: failure,
|
|
545
|
+
});
|
|
546
|
+
await expect(store.read()).resolves.toEqual({ ...defaults, gameLayerEnabled: true });
|
|
547
|
+
});
|
|
548
|
+
});
|
|
549
|
+
|
|
550
|
+
describe('preference controller visibility guard', () => {
|
|
551
|
+
it('does not report a superseded identity side-effect failure on the new identity', async () => {
|
|
552
|
+
const effect = deferred<undefined>();
|
|
553
|
+
const write = deferred<TestPreferences>();
|
|
554
|
+
const controller = createPreferenceController({
|
|
555
|
+
definitions: definitions({
|
|
556
|
+
mode: 'after-persistence',
|
|
557
|
+
onError: 'report',
|
|
558
|
+
run: () => effect.promise,
|
|
559
|
+
}),
|
|
560
|
+
store: { read: () => Promise.resolve(defaults), patch: () => write.promise },
|
|
561
|
+
onVisibleChange: () => undefined,
|
|
562
|
+
});
|
|
563
|
+
await controller.hydrate();
|
|
564
|
+
|
|
565
|
+
const update = controller.update({ gameLayerEnabled: true }).catch(() => undefined);
|
|
566
|
+
write.resolve({ ...defaults, gameLayerEnabled: true });
|
|
567
|
+
await Promise.resolve();
|
|
568
|
+
await Promise.resolve();
|
|
569
|
+
|
|
570
|
+
const next = { ...defaults, theme: 'light' as const };
|
|
571
|
+
await controller.switchIdentity({
|
|
572
|
+
read: () => Promise.resolve(next),
|
|
573
|
+
patch: () => Promise.reject(new Error('unused')),
|
|
574
|
+
});
|
|
575
|
+
|
|
576
|
+
effect.reject(new Error('stale reminder rescheduling failed'));
|
|
577
|
+
await update;
|
|
578
|
+
|
|
579
|
+
expect(controller.getState()).toEqual({ values: next, status: 'ready', error: null });
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
it('stops publishing to the consumer after stop, while state still settles', async () => {
|
|
583
|
+
const read = deferred<TestPreferences | undefined>();
|
|
584
|
+
const visible: TestPreferences[] = [];
|
|
585
|
+
const stored = { ...defaults, theme: 'dark' as const };
|
|
586
|
+
const controller = createPreferenceController({
|
|
587
|
+
definitions: definitions(),
|
|
588
|
+
store: { read: () => read.promise, patch: () => Promise.reject(new Error('unused')) },
|
|
589
|
+
onVisibleChange: (values) => visible.push(values),
|
|
590
|
+
});
|
|
591
|
+
|
|
592
|
+
const hydrated = controller.hydrate();
|
|
593
|
+
controller.stop();
|
|
594
|
+
read.resolve(stored);
|
|
595
|
+
|
|
596
|
+
await expect(hydrated).resolves.toEqual(stored);
|
|
597
|
+
expect(visible).toEqual([]);
|
|
598
|
+
expect(controller.getState().values).toEqual(stored);
|
|
599
|
+
});
|
|
600
|
+
});
|