@@ -786,6 +786,58 @@ describe("flagged storage extracted helpers", () => {
786786 }
787787 } ) ;
788788
789+ it ( "returns empty and does not log successful backup recovery when persist returns false" , async ( ) => {
790+ const { loadFlaggedAccountsState } = await import (
791+ "../lib/storage/flagged-storage-io.js"
792+ ) ;
793+ const fixtureRoot = join (
794+ tmpdir ( ) ,
795+ `codex-flagged-io-${ Math . random ( ) . toString ( 36 ) . slice ( 2 ) } ` ,
796+ ) ;
797+ const flaggedPath = join ( fixtureRoot , "flagged.json" ) ;
798+ const resetMarkerPath = `${ flaggedPath } .reset` ;
799+ const logError = vi . fn ( ) ;
800+ const logInfo = vi . fn ( ) ;
801+ const persistRecoveredBackup = vi . fn ( async ( ) => false ) ;
802+
803+ try {
804+ await fs . mkdir ( fixtureRoot , { recursive : true } ) ;
805+ await fs . writeFile (
806+ `${ flaggedPath } .bak` ,
807+ JSON . stringify ( {
808+ version : 1 ,
809+ accounts : [
810+ {
811+ refreshToken : "backup-token" ,
812+ flaggedAt : 1 ,
813+ addedAt : 1 ,
814+ lastUsed : 1 ,
815+ } ,
816+ ] ,
817+ } ) ,
818+ "utf8" ,
819+ ) ;
820+
821+ await expect (
822+ loadFlaggedAccountsState ( {
823+ path : flaggedPath ,
824+ legacyPath : `${ flaggedPath } .legacy` ,
825+ resetMarkerPath,
826+ normalizeFlaggedStorage : ( data ) => data as never ,
827+ persistRecoveredBackup,
828+ saveFlaggedAccounts : vi . fn ( async ( ) => { } ) ,
829+ logError,
830+ logInfo,
831+ } ) ,
832+ ) . resolves . toEqual ( { version : 1 , accounts : [ ] } ) ;
833+ expect ( persistRecoveredBackup ) . toHaveBeenCalledTimes ( 1 ) ;
834+ expect ( logInfo ) . not . toHaveBeenCalled ( ) ;
835+ expect ( logError ) . not . toHaveBeenCalled ( ) ;
836+ } finally {
837+ await removeWithRetry ( fixtureRoot , { recursive : true , force : true } ) ;
838+ }
839+ } ) ;
840+
789841 it ( "retries transient backup read locks before recovering flagged storage" , async ( ) => {
790842 const { loadFlaggedAccountsState } = await import (
791843 "../lib/storage/flagged-storage-io.js"
@@ -802,6 +854,7 @@ describe("flagged storage extracted helpers", () => {
802854 const logInfo = vi . fn ( ) ;
803855 const persistRecoveredBackup = vi . fn ( async ( ) => true ) ;
804856 let backupReadAttempts = 0 ;
857+ let readSpy : ReturnType < typeof vi . spyOn > | undefined ;
805858
806859 try {
807860 await fs . mkdir ( fixtureRoot , { recursive : true } ) ;
@@ -821,7 +874,7 @@ describe("flagged storage extracted helpers", () => {
821874 "utf8" ,
822875 ) ;
823876
824- const readSpy = vi
877+ readSpy = vi
825878 . spyOn ( fs , "readFile" )
826879 . mockImplementation ( async ( ...args ) => {
827880 const [ targetPath ] = args ;
@@ -865,9 +918,8 @@ describe("flagged storage extracted helpers", () => {
865918 expect . objectContaining ( { from : backupPath , to : flaggedPath , accounts : 1 } ) ,
866919 ) ;
867920 expect ( logError ) . not . toHaveBeenCalled ( ) ;
868-
869- readSpy . mockRestore ( ) ;
870921 } finally {
922+ readSpy ?. mockRestore ( ) ;
871923 await removeWithRetry ( fixtureRoot , { recursive : true , force : true } ) ;
872924 }
873925 } ) ;
@@ -888,6 +940,7 @@ describe("flagged storage extracted helpers", () => {
888940 const logInfo = vi . fn ( ) ;
889941 const saveFlaggedAccounts = vi . fn ( async ( ) => { } ) ;
890942 let legacyReadAttempts = 0 ;
943+ let readSpy : ReturnType < typeof vi . spyOn > | undefined ;
891944
892945 try {
893946 await fs . mkdir ( fixtureRoot , { recursive : true } ) ;
@@ -907,7 +960,7 @@ describe("flagged storage extracted helpers", () => {
907960 "utf8" ,
908961 ) ;
909962
910- const readSpy = vi
963+ readSpy = vi
911964 . spyOn ( fs , "readFile" )
912965 . mockImplementation ( async ( ...args ) => {
913966 const [ targetPath ] = args ;
@@ -961,9 +1014,8 @@ describe("flagged storage extracted helpers", () => {
9611014 expect . objectContaining ( { from : legacyPath , to : flaggedPath , accounts : 1 } ) ,
9621015 ) ;
9631016 expect ( logError ) . not . toHaveBeenCalled ( ) ;
964-
965- readSpy . mockRestore ( ) ;
9661017 } finally {
1018+ readSpy ?. mockRestore ( ) ;
9671019 await removeWithRetry ( fixtureRoot , { recursive : true , force : true } ) ;
9681020 }
9691021 } ) ;
0 commit comments