@@ -26,6 +26,7 @@ class Harness {
2626 restoreCommandCenter : vi . fn ( ) ,
2727 getFocusedWorktreePath : vi . fn ( ) . mockReturnValue ( null ) ,
2828 disableFocus : vi . fn ( ) . mockResolvedValue ( undefined ) ,
29+ stopCloudRun : vi . fn ( ) . mockResolvedValue ( true ) ,
2930 disconnectFromTask : vi . fn ( ) . mockResolvedValue ( undefined ) ,
3031 archive : vi . fn ( ) . mockResolvedValue ( undefined ) ,
3132 logError : vi . fn ( ) ,
@@ -122,6 +123,43 @@ describe("archiveTask", () => {
122123
123124 expect ( harness . deps . clearTerminalStates ) . not . toHaveBeenCalled ( ) ;
124125 } ) ;
126+
127+ it ( "stops a running cloud task before archiving it" , async ( ) => {
128+ await archiveTask ( TASK_ID , harness . deps ) ;
129+
130+ expect ( harness . deps . stopCloudRun ) . toHaveBeenCalledWith ( TASK_ID ) ;
131+ expect (
132+ vi . mocked ( harness . deps . stopCloudRun ) . mock . invocationCallOrder [ 0 ] ,
133+ ) . toBeLessThan (
134+ vi . mocked ( harness . deps . archive ) . mock . invocationCallOrder [ 0 ] ?? Infinity ,
135+ ) ;
136+ } ) ;
137+
138+ it ( "does not archive when a running cloud task cannot be stopped" , async ( ) => {
139+ harness . deps . stopCloudRun = vi . fn ( ) . mockResolvedValue ( false ) ;
140+
141+ await expect ( archiveTask ( TASK_ID , harness . deps ) ) . rejects . toThrow (
142+ "Couldn't stop the task" ,
143+ ) ;
144+
145+ expect ( harness . deps . archive ) . not . toHaveBeenCalled ( ) ;
146+ expect ( harness . ids ) . not . toContain ( TASK_ID ) ;
147+ } ) ;
148+
149+ it . each ( [
150+ [ "local workspace" , { mode : "local" } ] ,
151+ [ "task without workspace state" , null ] ,
152+ ] ) (
153+ "checks a %s for a cloud run before archiving" ,
154+ async ( _name , workspace ) => {
155+ harness . deps . getWorkspace = vi . fn ( ) . mockResolvedValue ( workspace ) ;
156+
157+ await archiveTask ( TASK_ID , harness . deps ) ;
158+
159+ expect ( harness . deps . stopCloudRun ) . toHaveBeenCalledWith ( TASK_ID ) ;
160+ expect ( harness . deps . archive ) . toHaveBeenCalledWith ( TASK_ID ) ;
161+ } ,
162+ ) ;
125163} ) ;
126164
127165describe ( "archiveTasks" , ( ) => {
0 commit comments