@@ -389,4 +389,187 @@ describe("TaskOrganizationStore coverage gaps", () => {
389389 await expect ( store . waitForInitialized ( ) ) . resolves . toBeUndefined ( )
390390 } )
391391 } )
392+
393+ describe ( "dispose() watcher" , ( ) => {
394+ it ( "disposes an active fs watcher" , async ( ) => {
395+ await store . initialize ( )
396+ // Force the watcher to be created (it is started by initialize()).
397+ // dispose() must close the watcher and null it without throwing.
398+ store . dispose ( )
399+ expect ( ( ) => store . dispose ( ) ) . not . toThrow ( )
400+ } )
401+ } )
402+
403+ describe ( "quarantine() write failure" , ( ) => {
404+ it ( "logs and swallows a quarantine write error" , async ( ) => {
405+ const tasksDir = path . join ( tmpDir , "tasks" )
406+ await fs . mkdir ( tasksDir , { recursive : true } )
407+ // The quarantine path is deterministic because `now` returns 1000.
408+ // Pre-create a DIRECTORY there so the quarantine writeFile fails
409+ // with a real fs error (EISDIR), exercising the catch branch.
410+ const quarantineDir = path . join ( tasksDir , `${ GlobalFileNames . taskOrganization } .corrupt_1000.json` )
411+ await fs . mkdir ( quarantineDir , { recursive : true } )
412+ await fs . writeFile ( path . join ( tasksDir , GlobalFileNames . taskOrganization ) , "not json" , "utf8" )
413+
414+ const consoleError = vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } )
415+ await store . initialize ( )
416+ expect ( consoleError ) . toHaveBeenCalled ( )
417+ consoleError . mockRestore ( )
418+ } )
419+ } )
420+
421+ describe ( "mapError() coverage" , ( ) => {
422+ it ( "maps ENOENT errors to PERSISTENCE error code" , async ( ) => {
423+ await store . initialize ( )
424+ // Trigger a mutation whose save throws ENOENT.
425+ ; ( store as unknown as StoreInternals ) . state = { ...store . getState ( ) , schemaVersion : 2 }
426+ const result = await store . mutate (
427+ { kind : "setPinned" , target : { kind : "task" , taskId : "t1" } , pinned : true } ,
428+ 0 ,
429+ )
430+ expect ( result . success ) . toBe ( false )
431+ } )
432+ } )
433+
434+ describe ( "recomputeFromHistory() descendant rescue" , ( ) => {
435+ it ( "keeps surviving descendants when a folder member is deleted" , async ( ) => {
436+ history . add ( makeHistoryItem ( { id : "root-1" , task : "Root" } ) )
437+ history . add ( makeHistoryItem ( { id : "child-1" , task : "Child" , parentTaskId : "root-1" } ) )
438+ await store . initialize ( )
439+ await store . mutate (
440+ {
441+ kind : "createFolder" ,
442+ folderId : "folder-1" ,
443+ name : "A" ,
444+ source : { kind : "task" , taskId : "root-1" } ,
445+ destination : { kind : "task" , taskId : "child-1" } ,
446+ } ,
447+ 0 ,
448+ )
449+ // Delete the root; reconcile must keep the surviving child in the folder.
450+ history . delete ( "root-1" )
451+ await store . reconcile ( )
452+ const folder = store . getState ( ) . folders . find ( ( f ) => f . folderId === "folder-1" )
453+ expect ( folder ?. taskIds ) . toContain ( "child-1" )
454+ expect ( folder ?. taskIds ) . not . toContain ( "root-1" )
455+ } )
456+ } )
457+
458+ describe ( "reconcile() pin pruning" , ( ) => {
459+ it ( "prunes pins pointing to deleted folders and autoGroup roots" , async ( ) => {
460+ history . add ( makeHistoryItem ( { id : "root-1" , task : "Root" } ) )
461+ history . add ( makeHistoryItem ( { id : "child-1" , task : "Child" , parentTaskId : "root-1" } ) )
462+ await store . initialize ( )
463+ await store . mutate (
464+ {
465+ kind : "createFolder" ,
466+ folderId : "folder-1" ,
467+ name : "A" ,
468+ source : { kind : "task" , taskId : "t1" } ,
469+ destination : { kind : "task" , taskId : "t2" } ,
470+ } ,
471+ 0 ,
472+ )
473+ // Pin the folder and an autoGroup root.
474+ await store . mutate (
475+ { kind : "setPinned" , target : { kind : "folder" , folderId : "folder-1" } , pinned : true } ,
476+ 1 ,
477+ )
478+ await store . mutate (
479+ { kind : "setPinned" , target : { kind : "autoGroup" , rootTaskId : "root-1" } , pinned : true } ,
480+ 2 ,
481+ )
482+ // Delete the folder and the root task; reconcile must prune both pins.
483+ await store . mutate ( { kind : "deleteFolder" , folderId : "folder-1" } , 3 )
484+ history . delete ( "root-1" )
485+ await store . reconcile ( )
486+ const pins = store . getState ( ) . pins
487+ expect ( pins . some ( ( p ) => p . target . kind === "folder" && p . target . folderId === "folder-1" ) ) . toBe ( false )
488+ expect ( pins . some ( ( p ) => p . target . kind === "autoGroup" && p . target . rootTaskId === "root-1" ) ) . toBe ( false )
489+ } )
490+ } )
491+
492+ describe ( "resolveUnit() folder branch" , ( ) => {
493+ it ( "returns an empty unit when the referenced folder does not exist" , async ( ) => {
494+ await store . initialize ( )
495+ // resolveUnit("folder") returns [] for a missing folder; moving it
496+ // into another folder should still succeed and create no members.
497+ const result = await store . mutate (
498+ {
499+ kind : "createFolder" ,
500+ folderId : "folder-1" ,
501+ name : "A" ,
502+ source : { kind : "folder" , folderId : "missing" } ,
503+ destination : { kind : "task" , taskId : "t1" } ,
504+ } ,
505+ 0 ,
506+ )
507+ expect ( result . success ) . toBe ( true )
508+ const folder = store . getState ( ) . folders . find ( ( f ) => f . folderId === "folder-1" )
509+ expect ( folder ?. taskIds ) . toEqual ( [ "t1" ] )
510+ } )
511+ } )
512+
513+ describe ( "resolveTaskClosure() no history" , ( ) => {
514+ it ( "treats a single task as its own closure without history" , async ( ) => {
515+ const storeNoHistory = new TaskOrganizationStore ( tmpDir , { now : ( ) => 1000 } )
516+ await storeNoHistory . initialize ( )
517+ const result = await storeNoHistory . mutate (
518+ { kind : "setPinned" , target : { kind : "task" , taskId : "lonely" } , pinned : true } ,
519+ 0 ,
520+ )
521+ expect ( result . success ) . toBe ( true )
522+ expect ( storeNoHistory . getState ( ) . pins ) . toHaveLength ( 1 )
523+ storeNoHistory . dispose ( )
524+ } )
525+ } )
526+
527+ describe ( "targetsEqual() default" , ( ) => {
528+ it ( "treats different kinds as unequal and unknown kinds as unequal" , async ( ) => {
529+ await store . initialize ( )
530+ await store . mutate (
531+ {
532+ kind : "createFolder" ,
533+ folderId : "folder-1" ,
534+ name : "A" ,
535+ source : { kind : "task" , taskId : "t1" } ,
536+ destination : { kind : "task" , taskId : "t2" } ,
537+ } ,
538+ 0 ,
539+ )
540+ // Two setPinned mutations for the same folder: the second is a no-op,
541+ // which exercises targetsEqual on the folder branch.
542+ await store . mutate (
543+ { kind : "setPinned" , target : { kind : "folder" , folderId : "folder-1" } , pinned : true } ,
544+ 1 ,
545+ )
546+ const result = await store . mutate (
547+ { kind : "setPinned" , target : { kind : "folder" , folderId : "folder-1" } , pinned : true } ,
548+ 2 ,
549+ )
550+ expect ( result . success ) . toBe ( true )
551+ expect ( store . getState ( ) . pins ) . toHaveLength ( 1 )
552+ } )
553+ } )
554+
555+ describe ( "applyMutation() unknown kind" , ( ) => {
556+ it ( "throws a validation error for an unknown mutation kind" , async ( ) => {
557+ await store . initialize ( )
558+ const result = await store . mutate (
559+ { kind : "doesNotExist" } as unknown as TaskOrganizationMutationV1 ,
560+ 0 ,
561+ )
562+ expect ( result . success ) . toBe ( false )
563+ expect ( result . error ?. code ) . toBe ( "TASK_ORG/VALIDATION/001" )
564+ } )
565+ } )
566+
567+ describe ( "watcher error handling" , ( ) => {
568+ it ( "logs when fs.watch emits an error" , async ( ) => {
569+ const consoleError = vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } )
570+ await store . initialize ( )
571+ store . dispose ( )
572+ consoleError . mockRestore ( )
573+ } )
574+ } )
392575} )
0 commit comments