@@ -9,13 +9,16 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
99const {
1010 executionStoreState,
1111 mockCancel,
12+ mockAdoptScopedExecution,
13+ mockBeginScopedExecution,
14+ mockEndScopedExecution,
1215 mockExecute,
1316 mockExecuteFromBlock,
1417 mockFetch,
1518 mockHandleExecutionCancelledConsole,
1619 mockHandleExecutionErrorConsole,
17- mockPersistenceExecutionEnded ,
18- mockPersistenceExecutionStarted ,
20+ mockLoadExecutionPointer ,
21+ mockReconnect ,
1922 mockRequestJson,
2023 mockResolveStartCandidates,
2124 mockSelectBestTrigger,
@@ -86,13 +89,16 @@ const {
8689 return {
8790 executionStoreState,
8891 mockCancel : vi . fn ( ) ,
92+ mockAdoptScopedExecution : vi . fn ( ) ,
93+ mockBeginScopedExecution : vi . fn ( ( ) => ( { } ) ) ,
94+ mockEndScopedExecution : vi . fn ( ( ) => true ) ,
8995 mockExecute : vi . fn ( ) ,
9096 mockExecuteFromBlock : vi . fn ( ) ,
9197 mockFetch : vi . fn ( ) ,
9298 mockHandleExecutionCancelledConsole : vi . fn ( ) ,
9399 mockHandleExecutionErrorConsole : vi . fn ( ) ,
94- mockPersistenceExecutionEnded : vi . fn ( ) ,
95- mockPersistenceExecutionStarted : vi . fn ( ( ) => ( { } ) ) ,
100+ mockLoadExecutionPointer : vi . fn ( ) ,
101+ mockReconnect : vi . fn ( ) ,
96102 mockRequestJson : vi . fn ( ) ,
97103 mockResolveStartCandidates : vi . fn ( ) ,
98104 mockSelectBestTrigger : vi . fn ( ) ,
@@ -210,7 +216,7 @@ vi.mock('@/hooks/use-execution-stream', () => {
210216 useExecutionStream : ( ) => ( {
211217 execute : mockExecute ,
212218 executeFromBlock : mockExecuteFromBlock ,
213- reconnect : vi . fn ( ) ,
219+ reconnect : mockReconnect ,
214220 cancel : mockCancel ,
215221 cancelExecute : vi . fn ( ) ,
216222 cancelReconnect : vi . fn ( ) ,
@@ -241,11 +247,12 @@ vi.mock('@/stores/execution', () => ({
241247vi . mock ( '@/stores/terminal' , ( ) => ( {
242248 clearExecutionPointer : vi . fn ( ) ,
243249 consolePersistence : {
244- executionStarted : mockPersistenceExecutionStarted ,
245- executionEnded : mockPersistenceExecutionEnded ,
250+ adoptScopedExecution : mockAdoptScopedExecution ,
251+ beginScopedExecution : mockBeginScopedExecution ,
252+ endScopedExecution : mockEndScopedExecution ,
246253 persist : vi . fn ( ) ,
247254 } ,
248- loadExecutionPointer : vi . fn ( ) ,
255+ loadExecutionPointer : mockLoadExecutionPointer ,
249256 saveExecutionPointer : vi . fn ( ) ,
250257 useTerminalConsoleStore : Object . assign (
251258 ( selector : ( state : typeof terminalStoreState ) => unknown ) => selector ( terminalStoreState ) ,
@@ -406,10 +413,14 @@ describe('useWorkflowExecution cancellation', () => {
406413describe ( 'useWorkflowExecution attachment uploads' , ( ) => {
407414 beforeEach ( ( ) => {
408415 vi . clearAllMocks ( )
416+ terminalStoreState . _hasHydrated = false
409417 executionStoreState . getWorkflowExecution . mockReturnValue (
410418 executionStoreState . workflowExecutions . get ( 'workflow-1' ) !
411419 )
412420 executionStoreState . getCurrentExecutionId . mockReturnValue ( null )
421+ mockAdoptScopedExecution . mockReturnValue ( undefined )
422+ mockLoadExecutionPointer . mockResolvedValue ( null )
423+ mockReconnect . mockResolvedValue ( undefined )
413424 mockResolveStartCandidates . mockReturnValue ( [ ] )
414425 mockSelectBestTrigger . mockReturnValue ( [ ] )
415426 vi . stubGlobal ( 'fetch' , mockFetch )
@@ -548,7 +559,7 @@ describe('useWorkflowExecution attachment uploads', () => {
548559 it ( 'does not let an overlapping run without lifecycle ownership end the active run' , async ( ) => {
549560 const persistenceExecution = { }
550561 let resolveActiveRun : ( ( ) => void ) | undefined
551- mockPersistenceExecutionStarted . mockReturnValueOnce ( persistenceExecution )
562+ mockBeginScopedExecution . mockReturnValueOnce ( persistenceExecution )
552563 mockExecute . mockImplementationOnce (
553564 ( ) =>
554565 new Promise < void > ( ( resolve ) => {
@@ -571,16 +582,49 @@ describe('useWorkflowExecution attachment uploads', () => {
571582 await result ( ) . handleRunWorkflow ( )
572583 } )
573584
574- expect ( mockPersistenceExecutionStarted ) . toHaveBeenCalledTimes ( 1 )
575- expect ( mockPersistenceExecutionEnded ) . not . toHaveBeenCalled ( )
585+ expect ( mockBeginScopedExecution ) . toHaveBeenCalledTimes ( 1 )
586+ expect ( mockEndScopedExecution ) . not . toHaveBeenCalled ( )
576587
577588 await act ( async ( ) => {
578589 resolveActiveRun ?.( )
579590 await drainStream ( activeRun )
580591 } )
581592
582- expect ( mockPersistenceExecutionEnded ) . toHaveBeenCalledOnce ( )
583- expect ( mockPersistenceExecutionEnded ) . toHaveBeenCalledWith ( persistenceExecution )
593+ expect ( mockEndScopedExecution ) . toHaveBeenCalledOnce ( )
594+ expect ( mockEndScopedExecution ) . toHaveBeenCalledWith ( 'workflow-1' , persistenceExecution )
595+
596+ unmount ( )
597+ } )
598+
599+ it ( 'adopts and finishes persistence ownership created before the hook mounted' , async ( ) => {
600+ const persistenceExecution = { }
601+ terminalStoreState . _hasHydrated = true
602+ executionStoreState . getWorkflowExecution . mockReturnValue ( {
603+ ...executionStoreState . getWorkflowExecution ( ) ,
604+ status : 'running' ,
605+ isExecuting : true ,
606+ currentExecutionId : 'execution-1' ,
607+ } )
608+ executionStoreState . getCurrentExecutionId . mockReturnValue ( 'execution-1' )
609+ mockLoadExecutionPointer . mockResolvedValue ( {
610+ workflowId : 'workflow-1' ,
611+ executionId : 'execution-1' ,
612+ lastEventId : 0 ,
613+ } )
614+ mockAdoptScopedExecution . mockReturnValue ( persistenceExecution )
615+ mockReconnect . mockImplementationOnce ( async ( { callbacks } ) => {
616+ callbacks . onExecutionCompleted ( { finalBlockLogs : [ ] } )
617+ } )
618+
619+ const { unmount } = renderWorkflowExecutionHook ( )
620+ await act ( async ( ) => {
621+ await Promise . resolve ( )
622+ await Promise . resolve ( )
623+ } )
624+
625+ expect ( mockBeginScopedExecution ) . not . toHaveBeenCalled ( )
626+ expect ( mockAdoptScopedExecution ) . toHaveBeenCalledWith ( 'workflow-1' )
627+ expect ( mockEndScopedExecution ) . toHaveBeenCalledWith ( 'workflow-1' , persistenceExecution )
584628
585629 unmount ( )
586630 } )
0 commit comments