@@ -714,5 +714,138 @@ describe("TaskOrganizationStore", () => {
714714 expect ( successful ) . toHaveLength ( 5 )
715715 expect ( successful . map ( ( result ) => result . committedRevision ) ) . toEqual ( [ 1 , 2 , 3 , 4 , 5 ] )
716716 } )
717+
718+ describe ( "cross-process writes" , ( ) => {
719+ it ( "rejects a same-revision write from another instance (lost update)" , async ( ) => {
720+ await store . initialize ( )
721+ // A second instance sharing the same backing file.
722+ const other = new TaskOrganizationStore ( tmpDir , { taskHistory : history , now : ( ) => 1000 } )
723+ await other . initialize ( )
724+
725+ const first = await store . mutate (
726+ {
727+ kind : "createFolder" ,
728+ folderId : "folder-a" ,
729+ name : "A" ,
730+ source : { kind : "task" , taskId : "t1" } ,
731+ destination : { kind : "task" , taskId : "t2" } ,
732+ } ,
733+ 0 ,
734+ )
735+ expect ( first . success ) . toBe ( true )
736+
737+ // `other` still holds revision 0 in memory and computes next = 1,
738+ // the same revision the first instance just committed.
739+ const second = await other . mutate (
740+ {
741+ kind : "createFolder" ,
742+ folderId : "folder-b" ,
743+ name : "B" ,
744+ source : { kind : "task" , taskId : "t3" } ,
745+ destination : { kind : "task" , taskId : "t4" } ,
746+ } ,
747+ 0 ,
748+ )
749+ expect ( second . success ) . toBe ( false )
750+ expect ( second . error ?. code ) . toBe ( "TASK_ORG/PERSISTENCE/005" )
751+
752+ // The first instance's write must survive on disk.
753+ const raw = JSON . parse (
754+ await fs . readFile ( path . join ( tmpDir , "tasks" , GlobalFileNames . taskOrganization ) , "utf8" ) ,
755+ )
756+ expect ( raw . folders . map ( ( f : { folderId : string } ) => f . folderId ) ) . toEqual ( [ "folder-a" ] )
757+
758+ other . dispose ( )
759+ } )
760+ } )
761+
762+ describe ( "watcher reload resilience" , ( ) => {
763+ it ( "keeps in-memory state on transient read errors" , async ( ) => {
764+ await store . initialize ( )
765+ await store . mutate (
766+ {
767+ kind : "createFolder" ,
768+ folderId : "folder-1" ,
769+ name : "A" ,
770+ source : { kind : "task" , taskId : "t1" } ,
771+ destination : { kind : "task" , taskId : "t2" } ,
772+ } ,
773+ 0 ,
774+ )
775+ expect ( store . getState ( ) . revision ) . toBe ( 1 )
776+
777+ // Simulate a transient read failure (e.g. the watcher firing while a
778+ // temp+rename write replaces the file): swap the file for a
779+ // directory so readFile rejects with a non-ENOENT error.
780+ const filePath = path . join ( tmpDir , "tasks" , GlobalFileNames . taskOrganization )
781+ await fs . rm ( filePath )
782+ await fs . mkdir ( filePath )
783+ const errorSpy = vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } )
784+ try {
785+ await store [ "reloadFromWatcher" ] ( )
786+ } finally {
787+ errorSpy . mockRestore ( )
788+ await fs . rmdir ( filePath )
789+ }
790+
791+ // The loaded state must survive; the next mutation computes from it.
792+ expect ( store . getState ( ) . revision ) . toBe ( 1 )
793+ expect ( store . getState ( ) . folders ) . toHaveLength ( 1 )
794+ const result = await store . mutate (
795+ { kind : "setPinned" , target : { kind : "task" , taskId : "t9" } , pinned : true } ,
796+ 1 ,
797+ )
798+ expect ( result . success ) . toBe ( true )
799+ expect ( result . committedRevision ) . toBe ( 2 )
800+ } )
801+
802+ it ( "fires onChange when reloaded content differs at the same revision" , async ( ) => {
803+ const onChange = vi . fn ( )
804+ const watched = new TaskOrganizationStore ( tmpDir , { taskHistory : history , now : ( ) => 1000 , onChange } )
805+ await watched . initialize ( )
806+ await watched . mutate (
807+ {
808+ kind : "createFolder" ,
809+ folderId : "folder-1" ,
810+ name : "A" ,
811+ source : { kind : "task" , taskId : "t1" } ,
812+ destination : { kind : "task" , taskId : "t2" } ,
813+ } ,
814+ 0 ,
815+ )
816+ onChange . mockClear ( )
817+
818+ // Simulate another process overwriting the file with different
819+ // content at the SAME revision (a lost update).
820+ const filePath = path . join ( tmpDir , "tasks" , GlobalFileNames . taskOrganization )
821+ const diverged = watched . getState ( )
822+ diverged . folders = [
823+ { folderId : "folder-other" , name : "Other" , taskIds : [ "t9" ] , createdAt : 1000 , updatedAt : 1000 } ,
824+ ]
825+ await fs . writeFile ( filePath , JSON . stringify ( diverged ) , "utf8" )
826+
827+ await watched [ "reloadFromWatcher" ] ( )
828+
829+ expect ( onChange ) . toHaveBeenCalledTimes ( 1 )
830+ expect ( onChange ) . toHaveBeenCalledWith ( expect . objectContaining ( { revision : 1 } ) )
831+ expect ( watched . getState ( ) . folders [ 0 ] . folderId ) . toBe ( "folder-other" )
832+ watched . dispose ( )
833+ } )
834+
835+ it ( "does not fire onChange when the reloaded content is identical" , async ( ) => {
836+ const onChange = vi . fn ( )
837+ const watched = new TaskOrganizationStore ( tmpDir , { taskHistory : history , now : ( ) => 1000 , onChange } )
838+ await watched . initialize ( )
839+ await watched . mutate ( { kind : "setPinned" , target : { kind : "task" , taskId : "t1" } , pinned : true } , 0 )
840+ onChange . mockClear ( )
841+
842+ // A watcher reload of unchanged content (e.g. our own write's event)
843+ // must not notify again.
844+ await watched [ "reloadFromWatcher" ] ( )
845+
846+ expect ( onChange ) . not . toHaveBeenCalled ( )
847+ watched . dispose ( )
848+ } )
849+ } )
717850 } )
718851} )
0 commit comments