@@ -108,7 +108,7 @@ const scratchProject = (environmentId: EnvironmentId) => ({
108108} ) ;
109109
110110describe ( "useDraftEnvironmentRouting" , ( ) => {
111- it ( "ignores a late scratch open for an old Auto pick and retries when Auto is re-armed " , async ( ) => {
111+ it ( "shows Preparing machine only while the current Auto scratch open runs " , async ( ) => {
112112 balancer . setup ( machineA ) ;
113113 scratchOpens . setup ( ) ;
114114 useComposerDraftStore
@@ -120,27 +120,35 @@ describe("useDraftEnvironmentRouting", () => {
120120 { environmentSelection : "auto" } ,
121121 ) ;
122122 await using screen = await render ( < Harness /> ) ;
123+ const opened = ( ) => scratchOpens . opens . map ( ( open ) => open . environmentId ) ;
123124 await expect . element ( screen . getByText ( "Preparing machine" ) ) . toBeInTheDocument ( ) ;
124125
126+ // A late open for an old pick neither moves the draft nor leaves it preparing.
125127 await act ( ( ) => balancer . set ( machineB ) ) ;
126- expect ( scratchOpens . opens . map ( ( open ) => open . environmentId ) ) . toEqual ( [ machineA , machineB ] ) ;
127128 await act ( ( ) => scratchOpens . opens [ 1 ] ! . resolve ( null ) ) ;
128129 await act ( ( ) => scratchOpens . opens [ 0 ] ! . resolve ( scratchProject ( machineA ) ) ) ;
129130 await expect . element ( screen . getByText ( "Ready" ) ) . toBeInTheDocument ( ) ;
130131 expect ( useComposerDraftStore . getState ( ) . getDraftSession ( draftId ) ?. environmentId ) . toBe ( local ) ;
131- expect ( scratchOpens . opens ) . toHaveLength ( 2 ) ;
132+ expect ( opened ( ) ) . toEqual ( [ machineA , machineB ] ) ;
132133
134+ // Re-arming Auto retries the failed open.
133135 await userEvent . click ( screen . getByRole ( "button" ) ) ;
134- expect ( scratchOpens . opens . map ( ( open ) => open . environmentId ) ) . toEqual ( [
135- machineA ,
136- machineB ,
137- machineB ,
138- ] ) ;
139- await act ( ( ) => scratchOpens . opens [ 2 ] ! . resolve ( scratchProject ( machineB ) ) ) ;
136+ expect ( opened ( ) ) . toEqual ( [ machineA , machineB , machineB ] ) ;
137+ await act ( ( ) => scratchOpens . opens [ 2 ] ! . resolve ( null ) ) ;
138+
139+ // Returning to a target whose open failed opens it again, and shows it.
140+ await act ( ( ) => balancer . set ( machineA ) ) ;
141+ await act ( ( ) => scratchOpens . opens [ 3 ] ! . resolve ( null ) ) ;
142+ await expect . element ( screen . getByText ( "Ready" ) ) . toBeInTheDocument ( ) ;
143+ await act ( ( ) => balancer . set ( machineB ) ) ;
144+ await act ( ( ) => balancer . set ( machineA ) ) ;
145+ expect ( opened ( ) ) . toEqual ( [ machineA , machineB , machineB , machineA , machineB , machineA ] ) ;
146+ await expect . element ( screen . getByText ( "Preparing machine" ) ) . toBeInTheDocument ( ) ;
147+ await act ( ( ) => scratchOpens . opens [ 5 ] ! . resolve ( scratchProject ( machineA ) ) ) ;
140148 await expect . element ( screen . getByText ( "Ready" ) ) . toBeInTheDocument ( ) ;
141149 expect ( useComposerDraftStore . getState ( ) . getDraftSession ( draftId ) ) . toMatchObject ( {
142- environmentId : machineB ,
143- loadBalancedEnvironmentId : machineB ,
150+ environmentId : machineA ,
151+ loadBalancedEnvironmentId : machineA ,
144152 } ) ;
145153 } ) ;
146154} ) ;
0 commit comments