@@ -4,6 +4,7 @@ import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
44import { tmpdir } from 'node:os'
55import { join } from 'node:path'
66import process from 'node:process'
7+ import { exec } from 'tinyexec'
78import { describe , expect , it , vi } from 'vitest'
89import { hasNative } from 'zigpty'
910import { DevframeTerminalsHost } from '../host-terminals'
@@ -14,6 +15,8 @@ const zigptyModuleMock = vi.hoisted(() => ({
1415 spawn : vi . fn ( ) ,
1516} ) )
1617
18+ vi . mock ( 'tinyexec' , { spy : true } )
19+
1720vi . mock ( 'zigpty' , async ( importOriginal ) => {
1821 const originalModule = await importOriginal < typeof import ( 'zigpty' ) > ( )
1922 zigptyModuleMock . spawn . mockImplementation ( originalModule . spawn )
@@ -337,6 +340,37 @@ describe('devframeTerminalHost stream lifecycle', () => {
337340 }
338341 } )
339342
343+ it . skipIf ( process . platform === 'win32' ) ( 'keeps streaming when the old command exits during restart' , async ( ) => {
344+ const { host, sinks } = createTerminalHost ( )
345+ const session = await host . startChildProcess ( {
346+ command : NODE ,
347+ args : [ '-e' , 'console.log(String(process.pid)); process.stdin.resume()' ] ,
348+ } , { id : 'child' , title : 'Child' } )
349+ const previous = session . getResult ( )
350+ const child = session . getChildProcess ( ) !
351+ const snapshot = vi . mocked ( exec )
352+ try {
353+ snapshot . mockImplementationOnce ( ( command ) => {
354+ expect ( command ) . toBe ( 'ps' )
355+ // Hold the snapshot open until the old command closes.
356+ const ps = exec ( NODE , [ '-e' , 'process.stdin.resume()' ] )
357+ child . once ( 'close' , ( ) => ps . process ?. stdin ?. end ( ) )
358+ child . stdin ! . end ( )
359+ return ps
360+ } )
361+ await session . restart ( )
362+ const pid = session . getResult ( ) . pid
363+ await waitUntil ( ( ) => expect ( sinks . get ( 'child' ) ?. write ) . toHaveBeenCalledWith ( `${ pid } \n` ) , 5000 )
364+ expect ( session . status ) . toBe ( 'running' )
365+ expect ( ( await previous ) . exitCode ) . toBeUndefined ( )
366+ await session . restart ( )
367+ }
368+ finally {
369+ snapshot . mockReset ( )
370+ await session . terminate ( )
371+ }
372+ } )
373+
340374 it ( 'getResult() tracks the new run after restart()' , async ( ) => {
341375 const { host } = createTerminalHost ( )
342376
0 commit comments