@@ -8,19 +8,28 @@ import {
88 LocalStoragePersistenceConfigSchema ,
99 CustomPersistenceConfigSchema ,
1010 PersistenceAdapterSchema ,
11+ AutoPersistenceConfigSchema ,
1112} from './memory.zod' ;
1213
1314describe ( 'MemoryConfigSchema' , ( ) => {
14- it ( 'should accept empty config (all optional) ' , ( ) => {
15+ it ( 'should default persistence to "auto" when empty config ' , ( ) => {
1516 const config = MemoryConfigSchema . parse ( { } ) ;
1617
1718 expect ( config . strictMode ) . toBe ( false ) ;
1819 expect ( config . initialData ) . toBeUndefined ( ) ;
19- expect ( config . persistence ) . toBeUndefined ( ) ;
20+ expect ( config . persistence ) . toBe ( 'auto' ) ;
2021 expect ( config . indexes ) . toBeUndefined ( ) ;
2122 expect ( config . maxRecordsPerObject ) . toBeUndefined ( ) ;
2223 } ) ;
2324
25+ it ( 'should accept persistence: false to disable persistence' , ( ) => {
26+ const config = MemoryConfigSchema . parse ( {
27+ persistence : false ,
28+ } ) ;
29+
30+ expect ( config . persistence ) . toBe ( false ) ;
31+ } ) ;
32+
2433 it ( 'should accept config with initial data' , ( ) => {
2534 const config = MemoryConfigSchema . parse ( {
2635 initialData : {
@@ -63,6 +72,14 @@ describe('MemoryConfigSchema', () => {
6372 expect ( config . persistence ) . toBe ( 'local' ) ;
6473 } ) ;
6574
75+ it ( 'should accept persistence shorthand "auto"' , ( ) => {
76+ const config = MemoryConfigSchema . parse ( {
77+ persistence : 'auto' ,
78+ } ) ;
79+
80+ expect ( config . persistence ) . toBe ( 'auto' ) ;
81+ } ) ;
82+
6683 it ( 'should accept persistence with file object config' , ( ) => {
6784 const config = MemoryConfigSchema . parse ( {
6885 persistence : {
@@ -104,6 +121,34 @@ describe('MemoryConfigSchema', () => {
104121 expect ( p . key ) . toBe ( 'myapp:db' ) ;
105122 } ) ;
106123
124+ it ( 'should accept persistence with auto object config' , ( ) => {
125+ const config = MemoryConfigSchema . parse ( {
126+ persistence : {
127+ type : 'auto' ,
128+ path : '/var/data/memory.json' ,
129+ key : 'myapp:db' ,
130+ autoSaveInterval : 5000 ,
131+ } ,
132+ } ) ;
133+
134+ const p = config . persistence as { type : 'auto' ; path ?: string ; key ?: string ; autoSaveInterval ?: number } ;
135+ expect ( p . type ) . toBe ( 'auto' ) ;
136+ expect ( p . path ) . toBe ( '/var/data/memory.json' ) ;
137+ expect ( p . key ) . toBe ( 'myapp:db' ) ;
138+ expect ( p . autoSaveInterval ) . toBe ( 5000 ) ;
139+ } ) ;
140+
141+ it ( 'should accept auto persistence without overrides' , ( ) => {
142+ const config = MemoryConfigSchema . parse ( {
143+ persistence : {
144+ type : 'auto' ,
145+ } ,
146+ } ) ;
147+
148+ const p = config . persistence as { type : 'auto' } ;
149+ expect ( p . type ) . toBe ( 'auto' ) ;
150+ } ) ;
151+
107152 it ( 'should accept persistence with custom adapter' , ( ) => {
108153 const mockAdapter = {
109154 load : async ( ) => null ,
@@ -205,6 +250,10 @@ describe('PersistenceTypeSchema', () => {
205250 expect ( PersistenceTypeSchema . parse ( 'local' ) ) . toBe ( 'local' ) ;
206251 } ) ;
207252
253+ it ( 'should accept auto type' , ( ) => {
254+ expect ( PersistenceTypeSchema . parse ( 'auto' ) ) . toBe ( 'auto' ) ;
255+ } ) ;
256+
208257 it ( 'should reject invalid type' , ( ) => {
209258 expect ( ( ) => PersistenceTypeSchema . parse ( 'indexeddb' ) ) . toThrow ( ) ;
210259 } ) ;
@@ -279,6 +328,40 @@ describe('CustomPersistenceConfigSchema', () => {
279328 } ) ;
280329} ) ;
281330
331+ describe ( 'AutoPersistenceConfigSchema' , ( ) => {
332+ it ( 'should accept minimal auto config' , ( ) => {
333+ const config = AutoPersistenceConfigSchema . parse ( {
334+ type : 'auto' ,
335+ } ) ;
336+
337+ expect ( config . type ) . toBe ( 'auto' ) ;
338+ expect ( config . path ) . toBeUndefined ( ) ;
339+ expect ( config . key ) . toBeUndefined ( ) ;
340+ expect ( config . autoSaveInterval ) . toBeUndefined ( ) ;
341+ } ) ;
342+
343+ it ( 'should accept auto config with all overrides' , ( ) => {
344+ const config = AutoPersistenceConfigSchema . parse ( {
345+ type : 'auto' ,
346+ path : '/data/store.json' ,
347+ key : 'myapp:db' ,
348+ autoSaveInterval : 5000 ,
349+ } ) ;
350+
351+ expect ( config . type ) . toBe ( 'auto' ) ;
352+ expect ( config . path ) . toBe ( '/data/store.json' ) ;
353+ expect ( config . key ) . toBe ( 'myapp:db' ) ;
354+ expect ( config . autoSaveInterval ) . toBe ( 5000 ) ;
355+ } ) ;
356+
357+ it ( 'should reject auto config with invalid autoSaveInterval' , ( ) => {
358+ expect ( ( ) => AutoPersistenceConfigSchema . parse ( {
359+ type : 'auto' ,
360+ autoSaveInterval : 50 , // Below minimum of 100
361+ } ) ) . toThrow ( ) ;
362+ } ) ;
363+ } ) ;
364+
282365describe ( 'MemoryPersistenceConfigSchema' , ( ) => {
283366 it ( 'should accept shorthand "file"' , ( ) => {
284367 const config = MemoryPersistenceConfigSchema . parse ( 'file' ) ;
@@ -290,6 +373,11 @@ describe('MemoryPersistenceConfigSchema', () => {
290373 expect ( config ) . toBe ( 'local' ) ;
291374 } ) ;
292375
376+ it ( 'should accept shorthand "auto"' , ( ) => {
377+ const config = MemoryPersistenceConfigSchema . parse ( 'auto' ) ;
378+ expect ( config ) . toBe ( 'auto' ) ;
379+ } ) ;
380+
293381 it ( 'should accept file object config' , ( ) => {
294382 const config = MemoryPersistenceConfigSchema . parse ( {
295383 type : 'file' ,
@@ -306,6 +394,15 @@ describe('MemoryPersistenceConfigSchema', () => {
306394 expect ( config ) . toEqual ( { type : 'local' , key : 'myapp:db' } ) ;
307395 } ) ;
308396
397+ it ( 'should accept auto object config' , ( ) => {
398+ const config = MemoryPersistenceConfigSchema . parse ( {
399+ type : 'auto' ,
400+ path : '/data/store.json' ,
401+ key : 'myapp:db' ,
402+ } ) ;
403+ expect ( config ) . toEqual ( { type : 'auto' , path : '/data/store.json' , key : 'myapp:db' } ) ;
404+ } ) ;
405+
309406 it ( 'should accept custom adapter' , ( ) => {
310407 const adapter = {
311408 load : async ( ) => null ,
0 commit comments