@@ -512,6 +512,68 @@ describe("native main token refresh", () => {
512512 expect ( existsSync ( join ( home , stagedBasename ) ) ) . toBe ( true ) ;
513513 } ) ;
514514
515+ test ( "restores an external writer displaced by an interrupted prepared exchange" , ( ) => {
516+ const authPath = join ( home , "auth.json" ) ;
517+ const transactionId = "11111111-1111-4111-8111-111111111111" ;
518+ const stagedBasename = `.opencodex-native-main-refresh.${ transactionId } .new` ;
519+ const previousBasename = `.opencodex-native-main-refresh.${ transactionId } .previous` ;
520+ const stagedPath = join ( home , stagedBasename ) ;
521+ const previousPath = join ( home , previousBasename ) ;
522+ const journalPath = join ( home , ".opencodex-native-main-refresh.json" ) ;
523+ const original = JSON . stringify ( { tokens : { access_token : "old-access" , refresh_token : "old-refresh" } } ) ;
524+ const replacement = JSON . stringify ( { tokens : { access_token : "new-access" , refresh_token : "new-refresh" } } ) ;
525+ const external = JSON . stringify ( { tokens : { access_token : "external-access" , refresh_token : "external-refresh" } } ) ;
526+ const digest = ( value : string ) => createHash ( "sha256" ) . update ( value ) . digest ( "hex" ) ;
527+ writeFileSync ( authPath , replacement ) ;
528+ writeFileSync ( process . platform === "win32" ? previousPath : stagedPath , external ) ;
529+ writeFileSync ( journalPath , JSON . stringify ( {
530+ version : 1 ,
531+ transactionId,
532+ targetPath : authPath ,
533+ stagedBasename,
534+ previousBasename,
535+ phase : "prepared" ,
536+ expectedSha256 : digest ( original ) ,
537+ replacementSha256 : digest ( replacement ) ,
538+ } ) ) ;
539+
540+ recoverNativeMainRefreshPublication ( resolveNativeProfileContext ( ) ) ;
541+
542+ expect ( readFileSync ( authPath , "utf8" ) ) . toBe ( external ) ;
543+ expect ( existsSync ( journalPath ) ) . toBe ( false ) ;
544+ expect ( existsSync ( stagedPath ) ) . toBe ( false ) ;
545+ expect ( existsSync ( previousPath ) ) . toBe ( false ) ;
546+ } ) ;
547+
548+ test ( "retains an unprovable prepared replacement without a displaced artifact" , ( ) => {
549+ const authPath = join ( home , "auth.json" ) ;
550+ const transactionId = "11111111-1111-4111-8111-111111111111" ;
551+ const stagedBasename = `.opencodex-native-main-refresh.${ transactionId } .new` ;
552+ const previousBasename = `.opencodex-native-main-refresh.${ transactionId } .previous` ;
553+ const journalPath = join ( home , ".opencodex-native-main-refresh.json" ) ;
554+ const original = JSON . stringify ( { tokens : { access_token : "old-access" , refresh_token : "old-refresh" } } ) ;
555+ const replacement = JSON . stringify ( { tokens : { access_token : "new-access" , refresh_token : "new-refresh" } } ) ;
556+ const digest = ( value : string ) => createHash ( "sha256" ) . update ( value ) . digest ( "hex" ) ;
557+ writeFileSync ( authPath , replacement ) ;
558+ writeFileSync ( journalPath , JSON . stringify ( {
559+ version : 1 ,
560+ transactionId,
561+ targetPath : authPath ,
562+ stagedBasename,
563+ previousBasename,
564+ phase : "prepared" ,
565+ expectedSha256 : digest ( original ) ,
566+ replacementSha256 : digest ( replacement ) ,
567+ } ) ) ;
568+
569+ expect ( ( ) => recoverNativeMainRefreshPublication ( resolveNativeProfileContext ( ) ) ) . toThrow (
570+ "Native credential refresh could not be published" ,
571+ ) ;
572+
573+ expect ( readFileSync ( authPath , "utf8" ) ) . toBe ( replacement ) ;
574+ expect ( existsSync ( journalPath ) ) . toBe ( true ) ;
575+ } ) ;
576+
515577 test ( "cleans committed recovery journals with zero, one, and multiple exact remnants" , ( ) => {
516578 const original = JSON . stringify ( { tokens : { refresh_token : "old-refresh" , account_id : "account-main" } } ) ;
517579 const replacement = JSON . stringify ( { tokens : { access_token : "access-b" , refresh_token : "refresh-b" , account_id : "account-main" } } ) ;
0 commit comments