@@ -27,7 +27,9 @@ vi.mock("../lib/runtime", async () => {
2727} ) ;
2828
2929import { createMobilePreferencesState } from "./preferences" ;
30+ import * as MobileDatabase from "../persistence/mobile-database" ;
3031import * as MobilePreferences from "../persistence/mobile-preferences" ;
32+ import * as MobileSecureStorage from "../persistence/mobile-secure-storage" ;
3133
3234function deferred < A > ( ) {
3335 let resolve ! : ( value : A ) => void ;
@@ -60,6 +62,53 @@ function makePreferencesState(
6062 ) ;
6163}
6264
65+ function makePreferencesStore ( initialPayload : MobilePreferences . Preferences ) {
66+ const row = { payload : JSON . stringify ( initialPayload ) , updatedAt : 1 } ;
67+ const unused = Effect . die ( "unused in preferences tests" ) ;
68+ return MobilePreferences . make ( ) . pipe (
69+ Effect . provideService (
70+ MobileDatabase . MobileDatabase ,
71+ MobileDatabase . MobileDatabase . of ( {
72+ loadCache : ( ) => unused ,
73+ listCache : ( ) => unused ,
74+ saveCache : ( ) => unused ,
75+ removeCache : ( ) => unused ,
76+ clearCacheKind : ( ) => unused ,
77+ clearEnvironmentCache : ( ) => unused ,
78+ clearAllCaches : unused ,
79+ inspectCaches : unused ,
80+ loadPreferencesJson : Effect . sync ( ( ) => Option . some ( { ...row } ) ) ,
81+ savePreferencesJson : ( payload , updatedAt ) =>
82+ Effect . sync ( ( ) => {
83+ row . payload = payload ;
84+ row . updatedAt = updatedAt ;
85+ } ) ,
86+ } ) ,
87+ ) ,
88+ Effect . provideService (
89+ MobileSecureStorage . MobileSecureStorage ,
90+ MobileSecureStorage . MobileSecureStorage . of ( {
91+ getItem : ( ) => Effect . succeed ( null ) ,
92+ setItem : ( ) => Effect . void ,
93+ removeItem : ( ) => Effect . void ,
94+ } ) ,
95+ ) ,
96+ Effect . map ( ( store ) => ( { store, storedPayload : ( ) => JSON . parse ( row . payload ) as unknown } ) ) ,
97+ ) ;
98+ }
99+
100+ describe ( "mobile preferences store" , ( ) => {
101+ it . effect ( "keeps a null dictation language and drops an unknown one" , ( ) =>
102+ Effect . gen ( function * ( ) {
103+ const cleared = yield * makePreferencesStore ( { voiceInputLocale : null } ) ;
104+ expect ( yield * cleared . store . load ) . toEqual ( { voiceInputLocale : null } ) ;
105+
106+ const unknown = yield * makePreferencesStore ( { voiceInputLocale : "xx-XX" } ) ;
107+ expect ( yield * unknown . store . load ) . toEqual ( { } ) ;
108+ } ) ,
109+ ) ;
110+ } ) ;
111+
63112describe ( "mobile preferences state" , ( ) => {
64113 it . effect ( "shares one preference load across consumers" , ( ) =>
65114 Effect . gen ( function * ( ) {
@@ -355,4 +404,35 @@ describe("mobile preferences state", () => {
355404 registry . dispose ( ) ;
356405 } ) ,
357406 ) ;
407+
408+ it . effect ( "keeps the device dictation language after another preference saves" , ( ) =>
409+ Effect . gen ( function * ( ) {
410+ const { store, storedPayload } = yield * makePreferencesStore ( { voiceInputLocale : "pt-BR" } ) ;
411+ const state = createMobilePreferencesState (
412+ Atom . runtime ( Layer . succeed ( MobilePreferences . MobilePreferencesStore , store ) ) ,
413+ ) ;
414+ const registry = AtomRegistry . make ( ) ;
415+ const unmountPreferences = registry . mount ( state . preferencesAtom ) ;
416+ const unmountUpdate = registry . mount ( state . updatePreferencesAtom ) ;
417+ const save = ( patch : Partial < MobilePreferences . Preferences > ) => {
418+ registry . set ( state . updatePreferencesAtom , patch ) ;
419+ return AtomRegistry . getResult ( registry , state . updatePreferencesAtom , {
420+ suspendOnWaiting : true ,
421+ } ) ;
422+ } ;
423+
424+ yield * AtomRegistry . getResult ( registry , state . preferencesAtom , { suspendOnWaiting : true } ) ;
425+ yield * save ( { voiceInputLocale : null } ) ;
426+ yield * save ( { baseFontSize : 18 } ) ;
427+
428+ expect (
429+ yield * AtomRegistry . getResult ( registry , state . preferencesAtom , { suspendOnWaiting : true } ) ,
430+ ) . toEqual ( { voiceInputLocale : null , baseFontSize : 18 } ) ;
431+ expect ( storedPayload ( ) ) . toEqual ( { voiceInputLocale : null , baseFontSize : 18 } ) ;
432+
433+ unmountUpdate ( ) ;
434+ unmountPreferences ( ) ;
435+ registry . dispose ( ) ;
436+ } ) ,
437+ ) ;
358438} ) ;
0 commit comments