@@ -26,26 +26,17 @@ function makeService() {
2626 const log : string [ ] = [ ] ;
2727 const activated : Array < PromiseWithResolvers < void > > = [ ] ;
2828 const cleanedUp : Array < PromiseWithResolvers < void > > = [ ] ;
29- const gates = new Map <
30- string ,
31- { readonly started : PromiseWithResolvers < void > ; readonly release : PromiseWithResolvers < void > }
32- > ( ) ;
29+ const cleanUpGates = new Map < string , PromiseWithResolvers < void > > ( ) ;
3330 const service = createCloudAccountService < CloudAccountSource > ( {
3431 deactivate : ( ) => log . push ( "deactivate" ) ,
3532 detach : ( ) => log . push ( "detach" ) ,
36- onSignOutTransition : ( ) => log . push ( "signOutTransition" ) ,
37- cleanUp : async ( { accountId, wasActive } ) => {
38- log . push ( `cleanUp: ${ accountId } : ${ wasActive } ` ) ;
33+ cleanUp : async ( { accountId } ) => {
34+ log . push ( `cleanUp: ${ accountId } ` ) ;
35+ await cleanUpGates . get ( accountId ?? "" ) ?. promise ;
3936 cleanedUp . shift ( ) ?. resolve ( ) ;
4037 } ,
41- prepare : async ( { accountId } ) => {
42- log . push ( `prepare:${ accountId } ` ) ;
43- const gate = gates . get ( accountId ) ;
44- gate ?. started . resolve ( ) ;
45- await gate ?. release . promise ;
46- } ,
47- activate : ( { accountId, isAccountTransition } ) => {
48- log . push ( `activate:${ accountId } :${ isAccountTransition } ` ) ;
38+ activate : ( { accountId } ) => {
39+ log . push ( `activate:${ accountId } ` ) ;
4940 activated . shift ( ) ?. resolve ( ) ;
5041 } ,
5142 } ) ;
@@ -59,29 +50,24 @@ function makeService() {
5950 cleanedUp . push ( deferred ) ;
6051 return deferred . promise ;
6152 } ;
62- const gate = ( accountId : string ) => {
63- const deferred = {
64- started : Promise . withResolvers < void > ( ) ,
65- release : Promise . withResolvers < void > ( ) ,
66- } ;
67- gates . set ( accountId , deferred ) ;
53+ const holdCleanUp = ( accountId : string ) => {
54+ const deferred = Promise . withResolvers < void > ( ) ;
55+ cleanUpGates . set ( accountId , deferred ) ;
6856 return deferred ;
6957 } ;
70- return { service, log, nextActivation, nextCleanUp, gate } ;
58+ return { service, log, nextActivation, nextCleanUp, holdCleanUp } ;
7159}
7260
7361describe ( "planCloudAccountStep" , ( ) => {
74- it ( "treats the first observation as a non-transition " , ( ) => {
62+ it ( "plans the first observation from no previous account " , ( ) => {
7563 expect ( planCloudAccountStep ( undefined , "a" ) ) . toEqual ( {
7664 _tag : "SignIn" ,
7765 accountId : "a" ,
7866 switchFromAccountId : null ,
79- isAccountTransition : false ,
8067 } ) ;
8168 expect ( planCloudAccountStep ( undefined , null ) ) . toEqual ( {
8269 _tag : "SignOut" ,
8370 previousAccountId : null ,
84- isAccountTransition : false ,
8571 } ) ;
8672 } ) ;
8773
@@ -92,7 +78,6 @@ describe("planCloudAccountStep", () => {
9278 _tag : "SignIn" ,
9379 accountId : "b" ,
9480 switchFromAccountId : "a" ,
95- isAccountTransition : true ,
9681 } ) ;
9782 } ) ;
9883} ) ;
@@ -104,15 +89,14 @@ describe("createCloudAccountService", () => {
10489 service . attach ( source ) ;
10590
10691 emit ( undefined ) ;
107- expect ( service . getState ( ) ) . toEqual ( { _tag : "Unobserved" } ) ;
92+ expect ( log ) . toEqual ( [ ] ) ;
10893 const activation = nextActivation ( ) ;
10994 emit ( { id : "a" } ) ;
11095 emit ( { id : "a" } ) ;
111- expect ( service . getState ( ) ) . toEqual ( { _tag : "Activating" , accountId : "a" } ) ;
11296 await activation ;
97+ emit ( { id : "a" } ) ;
11398
114- expect ( service . getState ( ) ) . toEqual ( { _tag : "Active" , accountId : "a" } ) ;
115- expect ( log ) . toEqual ( [ "prepare:a" , "activate:a:false" ] ) ;
99+ expect ( log ) . toEqual ( [ "activate:a" ] ) ;
116100 } ) ;
117101
118102 it ( "drops credentials synchronously and cleans up before activating the next account" , async ( ) => {
@@ -129,7 +113,7 @@ describe("createCloudAccountService", () => {
129113 expect ( log ) . toEqual ( [ "deactivate" ] ) ;
130114 await second ;
131115
132- expect ( log ) . toEqual ( [ "deactivate" , "cleanUp:a:true " , "prepare:b" , " activate:b:true "] ) ;
116+ expect ( log ) . toEqual ( [ "deactivate" , "cleanUp:a" , "activate:b" ] ) ;
133117 } ) ;
134118
135119 it ( "cleans up on sign-out, including a signed-out first observation" , async ( ) => {
@@ -140,7 +124,7 @@ describe("createCloudAccountService", () => {
140124 const initialCleanUp = nextCleanUp ( ) ;
141125 emit ( null ) ;
142126 await initialCleanUp ;
143- expect ( log ) . toEqual ( [ "deactivate" , "cleanUp:null:false " ] ) ;
127+ expect ( log ) . toEqual ( [ "deactivate" , "cleanUp:null" ] ) ;
144128
145129 const activation = nextActivation ( ) ;
146130 emit ( { id : "a" } ) ;
@@ -150,70 +134,27 @@ describe("createCloudAccountService", () => {
150134 const cleanUp = nextCleanUp ( ) ;
151135 emit ( null ) ;
152136 await cleanUp ;
153- expect ( service . getState ( ) ) . toEqual ( { _tag : "SignedOut" } ) ;
154- expect ( log ) . toEqual ( [ "signOutTransition" , "deactivate" , "cleanUp:a:true" ] ) ;
137+ expect ( log ) . toEqual ( [ "deactivate" , "cleanUp:a" ] ) ;
155138 } ) ;
156139
157140 it ( "never activates an account that signed out while it was activating" , async ( ) => {
158- const { service, log, nextCleanUp, gate } = makeService ( ) ;
141+ const { service, log, nextActivation , nextCleanUp, holdCleanUp } = makeService ( ) ;
159142 const { source, emit } = makeSource ( ) ;
160143 service . attach ( source ) ;
161- const prepared = gate ( "a" ) ;
144+ const first = nextActivation ( ) ;
162145 emit ( { id : "a" } ) ;
163- await prepared . started . promise ;
164- const cleanUp = nextCleanUp ( ) ;
165- emit ( null ) ;
166- prepared . release . resolve ( ) ;
167- await cleanUp ;
168-
169- expect ( service . getState ( ) ) . toEqual ( { _tag : "SignedOut" } ) ;
170- expect ( log ) . toEqual ( [ "prepare:a" , "signOutTransition" , "deactivate" , "cleanUp:a:false" ] ) ;
171- } ) ;
146+ await first ;
147+ log . length = 0 ;
172148
173- it ( "retries a rejected activation on the next emission for the same account" , async ( ) => {
174- const log : string [ ] = [ ] ;
175- const failed = Promise . withResolvers < void > ( ) ;
176- const activated = Promise . withResolvers < void > ( ) ;
177- let attempts = 0 ;
178- const service = createCloudAccountService < CloudAccountSource > ( {
179- deactivate : ( ) => log . push ( "deactivate" ) ,
180- detach : ( ) => log . push ( "detach" ) ,
181- cleanUp : async ( { accountId, wasActive } ) => {
182- log . push ( `cleanUp:${ accountId } :${ wasActive } ` ) ;
183- } ,
184- prepare : async ( { accountId } ) => {
185- attempts += 1 ;
186- log . push ( `prepare:${ accountId } ` ) ;
187- if ( attempts === 1 ) {
188- // Queued before the rejection, so the service's catch runs before
189- // the test's `await failed.promise` continuation.
190- queueMicrotask ( failed . resolve ) ;
191- throw new Error ( "restore failed" ) ;
192- }
193- } ,
194- activate : ( { accountId, isAccountTransition } ) => {
195- log . push ( `activate:${ accountId } :${ isAccountTransition } ` ) ;
196- activated . resolve ( ) ;
197- } ,
198- } ) ;
199- const { source, emit } = makeSource ( ) ;
200- service . attach ( source ) ;
149+ const heldCleanUp = holdCleanUp ( "a" ) ;
150+ emit ( { id : "b" } ) ;
201151 emit ( null ) ;
202- emit ( { id : "a" } ) ;
203- await failed . promise ;
204- expect ( service . getState ( ) ) . toMatchObject ( { _tag : "Failed" , accountId : "a" } ) ;
152+ void nextCleanUp ( ) ;
153+ const signOutCleanUp = nextCleanUp ( ) ;
154+ heldCleanUp . resolve ( ) ;
155+ await signOutCleanUp ;
205156
206- emit ( { id : "a" } ) ;
207- await activated . promise ;
208-
209- expect ( service . getState ( ) ) . toEqual ( { _tag : "Active" , accountId : "a" } ) ;
210- expect ( log ) . toEqual ( [
211- "deactivate" ,
212- "cleanUp:null:false" ,
213- "prepare:a" ,
214- "prepare:a" ,
215- "activate:a:true" ,
216- ] ) ;
157+ expect ( log ) . toEqual ( [ "deactivate" , "deactivate" , "cleanUp:a" , "cleanUp:b" ] ) ;
217158 } ) ;
218159
219160 it ( "detaches from a replaced Clerk instance" , ( ) => {
@@ -238,7 +179,6 @@ describe("createCloudAccountService", () => {
238179
239180 detach ( ) ;
240181 expect ( isSubscribed ( ) ) . toBe ( false ) ;
241- expect ( service . getState ( ) ) . toEqual ( { _tag : "Unobserved" } ) ;
242- expect ( log ) . toEqual ( [ "prepare:a" , "activate:a:false" , "detach" ] ) ;
182+ expect ( log ) . toEqual ( [ "activate:a" , "detach" ] ) ;
243183 } ) ;
244184} ) ;
0 commit comments