@@ -718,6 +718,8 @@ describe("storage", () => {
718718
719719 const stalePersistPayload = {
720720 ...current ,
721+ activeIndex : 1 ,
722+ activeIndexByFamily : { codex : 1 } ,
721723 accounts : [
722724 ...current . accounts ,
723725 {
@@ -751,6 +753,18 @@ describe("storage", () => {
751753 } ) ,
752754 ] ) ,
753755 ) ;
756+ expect ( loaded ?. accounts [ loaded . activeIndex ] ) . toEqual (
757+ expect . objectContaining ( {
758+ accountId : "acct-appended" ,
759+ refreshToken : "refresh-appended" ,
760+ } ) ,
761+ ) ;
762+ expect ( loaded ?. accounts [ loaded . activeIndexByFamily ?. codex ?? - 1 ] ) . toEqual (
763+ expect . objectContaining ( {
764+ accountId : "acct-appended" ,
765+ refreshToken : "refresh-appended" ,
766+ } ) ,
767+ ) ;
754768 } ) ;
755769
756770 it ( "allows a live transaction payload to intentionally remove imported accounts" , async ( ) => {
@@ -798,13 +812,10 @@ describe("storage", () => {
798812 if ( ! liveCurrent ) {
799813 throw new Error ( "expected live transaction snapshot" ) ;
800814 }
801-
802- await persist ( {
803- ...liveCurrent ,
804- accounts : liveCurrent . accounts . filter (
805- ( account ) => account . accountId !== "acct-imported" ,
806- ) ,
807- } ) ;
815+ liveCurrent . accounts = liveCurrent . accounts . filter (
816+ ( account ) => account . accountId !== "acct-imported" ,
817+ ) ;
818+ await persist ( liveCurrent ) ;
808819 } ) ;
809820
810821 const loaded = await loadAccounts ( ) ;
@@ -859,13 +870,10 @@ describe("storage", () => {
859870 if ( ! liveCurrent ) {
860871 throw new Error ( "expected live transaction snapshot" ) ;
861872 }
862-
863- await persist ( {
864- ...liveCurrent ,
865- accounts : liveCurrent . accounts . filter (
866- ( account ) => account . accountId !== "acct-imported" ,
867- ) ,
868- } ) ;
873+ liveCurrent . accounts = liveCurrent . accounts . filter (
874+ ( account ) => account . accountId !== "acct-imported" ,
875+ ) ;
876+ await persist ( liveCurrent ) ;
869877 } ) ;
870878
871879 const loaded = await loadAccounts ( ) ;
@@ -999,6 +1007,118 @@ describe("storage", () => {
9991007 ) ;
10001008 } ) ;
10011009
1010+ it ( "getCurrent exposes the live snapshot after persist inside a flagged transaction" , async ( ) => {
1011+ const now = Date . now ( ) ;
1012+ let liveSnapshot : Awaited < ReturnType < typeof loadAccounts > > = null ;
1013+
1014+ await saveAccounts ( {
1015+ version : 3 ,
1016+ activeIndex : 0 ,
1017+ activeIndexByFamily : { codex : 0 } ,
1018+ accounts : [
1019+ {
1020+ accountId : "acct-existing" ,
1021+ email : "existing@example.com" ,
1022+ refreshToken : "refresh-existing" ,
1023+ addedAt : now - 1_000 ,
1024+ lastUsed : now - 1_000 ,
1025+ } ,
1026+ ] ,
1027+ } ) ;
1028+
1029+ await withAccountAndFlaggedStorageTransaction (
1030+ async ( current , persist , getCurrent ) => {
1031+ if ( ! current ) {
1032+ throw new Error ( "expected existing account storage" ) ;
1033+ }
1034+ expect ( getCurrent ( ) ) . toBe ( current ) ;
1035+
1036+ await persist (
1037+ {
1038+ ...current ,
1039+ accounts : [
1040+ ...current . accounts ,
1041+ {
1042+ accountId : "acct-added" ,
1043+ email : "added@example.com" ,
1044+ refreshToken : "refresh-added" ,
1045+ addedAt : now ,
1046+ lastUsed : now ,
1047+ } ,
1048+ ] ,
1049+ } ,
1050+ {
1051+ version : 1 ,
1052+ accounts : [ ] ,
1053+ } ,
1054+ ) ;
1055+
1056+ liveSnapshot = getCurrent ( ) ;
1057+ } ,
1058+ ) ;
1059+
1060+ expect ( liveSnapshot ?. accounts ) . toEqual (
1061+ expect . arrayContaining ( [
1062+ expect . objectContaining ( {
1063+ accountId : "acct-existing" ,
1064+ refreshToken : "refresh-existing" ,
1065+ } ) ,
1066+ expect . objectContaining ( {
1067+ accountId : "acct-added" ,
1068+ refreshToken : "refresh-added" ,
1069+ } ) ,
1070+ ] ) ,
1071+ ) ;
1072+ } ) ;
1073+
1074+ it ( "getCurrent starts null and becomes live after persist inside a flagged transaction" , async ( ) => {
1075+ const storagePath = getStoragePath ( ) ;
1076+ const now = Date . now ( ) ;
1077+ let liveSnapshot : Awaited < ReturnType < typeof loadAccounts > > = null ;
1078+
1079+ await fs . mkdir ( dirname ( storagePath ) , { recursive : true } ) ;
1080+ await fs . writeFile ( storagePath , "{invalid-json" , "utf-8" ) ;
1081+
1082+ await withAccountAndFlaggedStorageTransaction (
1083+ async ( current , persist , getCurrent ) => {
1084+ expect ( current ) . toBeNull ( ) ;
1085+ expect ( getCurrent ( ) ) . toBeNull ( ) ;
1086+
1087+ await persist (
1088+ {
1089+ version : 3 ,
1090+ activeIndex : 0 ,
1091+ activeIndexByFamily : { codex : 0 } ,
1092+ accounts : [
1093+ {
1094+ accountId : "acct-created" ,
1095+ email : "created@example.com" ,
1096+ refreshToken : "refresh-created" ,
1097+ addedAt : now ,
1098+ lastUsed : now ,
1099+ } ,
1100+ ] ,
1101+ } ,
1102+ {
1103+ version : 1 ,
1104+ accounts : [ ] ,
1105+ } ,
1106+ ) ;
1107+
1108+ liveSnapshot = getCurrent ( ) ;
1109+ } ,
1110+ ) ;
1111+
1112+ expect ( liveSnapshot ?. accounts ) . toEqual (
1113+ expect . arrayContaining ( [
1114+ expect . objectContaining ( {
1115+ accountId : "acct-created" ,
1116+ refreshToken : "refresh-created" ,
1117+ } ) ,
1118+ ] ) ,
1119+ ) ;
1120+ } ) ;
1121+
10021122 it ( "should preserve distinct shared-accountId imports when the imported row has no email" , async ( ) => {
10031123 const existing = {
10041124 version : 3 ,
0 commit comments