@@ -424,4 +424,62 @@ describe('CollaborationActor', () => {
424424 expect ( client . state . users [ 'user-2' ] . profile . tags ) . toContain ( 'tag2' ) ;
425425 } ) ;
426426 } ) ;
427+
428+ describe ( 'Offline-First Support' , ( ) => {
429+ it ( 'should allow setState before any peers connect' , async ( ) => {
430+ const client = system . getClient ( TodosToken ) ! ;
431+
432+ // No connection - completely offline
433+ client . actions . addTodo ( 'Offline todo' ) ;
434+ await flushMicrotask ( ) ;
435+
436+ expect ( client . state . todos . length ) . toBe ( 1 ) ;
437+ expect ( client . state . todos [ 0 ] . text ) . toBe ( 'Offline todo' ) ;
438+ } ) ;
439+
440+ it ( 'should accumulate multiple setState calls before connection' , async ( ) => {
441+ const client = system . getClient ( TodosToken ) ! ;
442+
443+ // Multiple changes while offline
444+ client . actions . addTodo ( 'Todo 1' ) ;
445+ await new Promise ( resolve => setTimeout ( resolve , 1 ) ) ; // Ensure unique timestamp
446+ client . actions . addTodo ( 'Todo 2' ) ;
447+ await new Promise ( resolve => setTimeout ( resolve , 1 ) ) ; // Ensure unique timestamp
448+ client . actions . addTodo ( 'Todo 3' ) ;
449+ await flushMicrotask ( ) ;
450+
451+ expect ( client . state . todos . length ) . toBe ( 3 ) ;
452+
453+ // Toggle one todo
454+ const todoId = client . state . todos [ 1 ] . id ;
455+ client . actions . toggleTodo ( todoId ) ;
456+ await flushMicrotask ( ) ;
457+
458+ expect ( client . state . todos [ 1 ] . done ) . toBe ( true ) ;
459+ } ) ;
460+
461+ it ( 'should maintain state consistency across offline operations' , async ( ) => {
462+ const client = system . getClient ( TodosToken ) ! ;
463+
464+ // Complex offline workflow
465+ client . actions . addTodo ( 'Task 1' ) ;
466+ await flushMicrotask ( ) ;
467+ const id1 = client . state . todos [ 0 ] . id ;
468+
469+ client . actions . addTodo ( 'Task 2' ) ;
470+ await flushMicrotask ( ) ;
471+
472+ client . actions . toggleTodo ( id1 ) ;
473+ await flushMicrotask ( ) ;
474+
475+ client . actions . addTodo ( 'Task 3' ) ;
476+ await flushMicrotask ( ) ;
477+
478+ // Verify final state
479+ expect ( client . state . todos . length ) . toBe ( 3 ) ;
480+ expect ( client . state . todos [ 0 ] . done ) . toBe ( true ) ;
481+ expect ( client . state . todos [ 1 ] . done ) . toBe ( false ) ;
482+ expect ( client . state . todos [ 2 ] . done ) . toBe ( false ) ;
483+ } ) ;
484+ } ) ;
427485} ) ;
0 commit comments