@@ -83,7 +83,7 @@ const loadPluginCliDescriptorsMock = vi.hoisted(() =>
8383const resolveManifestCommandAliasOwnerMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
8484const resolveManifestToolOwnerMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
8585const resolveManifestCliCommandSurfaceOwnerMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
86- const restoreTerminalStateMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
86+ const restoreRuntimeTerminalStateMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
8787const hasEnvHttpProxyAgentConfiguredMock = vi . hoisted ( ( ) => vi . fn ( ( ) => false ) ) ;
8888const ensureGlobalUndiciEnvProxyDispatcherMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
8989const readConfigFileSnapshotMock = vi . hoisted ( ( ) =>
@@ -350,9 +350,13 @@ vi.mock("../plugins/manifest-command-aliases.runtime.js", () => ({
350350 resolveManifestToolOwner : resolveManifestToolOwnerMock ,
351351} ) ) ;
352352
353- vi . mock ( "../../packages/terminal-core/src/restore.js" , ( ) => ( {
354- restoreTerminalState : restoreTerminalStateMock ,
355- } ) ) ;
353+ vi . mock ( "../runtime.js" , async ( importOriginal ) => {
354+ const actual = await importOriginal < typeof import ( "../runtime.js" ) > ( ) ;
355+ return {
356+ ...actual ,
357+ restoreRuntimeTerminalState : restoreRuntimeTerminalStateMock ,
358+ } ;
359+ } ) ;
356360
357361vi . mock ( "../infra/net/proxy-env.js" , ( ) => ( {
358362 hasEnvHttpProxyAgentConfigured : hasEnvHttpProxyAgentConfiguredMock ,
@@ -4071,43 +4075,48 @@ describe("runCli exit behavior", () => {
40714075 ] ) ;
40724076 } ) ;
40734077
4074- it ( "restores terminal state before uncaught CLI exits" , async ( ) => {
4075- buildProgramMock . mockReturnValueOnce ( {
4076- commands : [ { name : ( ) => "status" } ] ,
4077- parseAsync : vi . fn ( ) . mockResolvedValueOnce ( undefined ) ,
4078- } ) ;
4078+ it . each ( [ false , true ] ) (
4079+ "restores terminal state before uncaught CLI exits (machine output: %s)" ,
4080+ async ( machineOutput ) => {
4081+ buildProgramMock . mockReturnValueOnce ( {
4082+ commands : [ { name : ( ) => "status" } ] ,
4083+ parseAsync : vi . fn ( ) . mockResolvedValueOnce ( undefined ) ,
4084+ } ) ;
40794085
4080- const processOnSpy = vi . spyOn ( process , "on" ) ;
4081- const consoleErrorSpy = vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
4082- const exitSpy = vi . spyOn ( process , "exit" ) . mockImplementation ( ( ( code ?: number ) => {
4083- throw new Error ( `process.exit(${ String ( code ) } )` ) ;
4084- } ) as typeof process . exit ) ;
4086+ const processOnSpy = vi . spyOn ( process , "on" ) ;
4087+ const consoleErrorSpy = vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
4088+ const exitSpy = vi . spyOn ( process , "exit" ) . mockImplementation ( ( ( code ?: number ) => {
4089+ throw new Error ( `process.exit(${ String ( code ) } )` ) ;
4090+ } ) as typeof process . exit ) ;
40854091
4086- await runCli ( [ "node" , "openclaw" , "status" ] ) ;
4092+ await runCli ( [ "node" , "openclaw" , "status" ] ) ;
40874093
4088- const handler = processOnSpy . mock . calls . find ( ( [ event ] ) => event === "uncaughtException" ) ?. [ 1 ] ;
4089- if ( typeof handler !== "function" ) {
4090- throw new Error ( "uncaughtException handler was not registered" ) ;
4091- }
4094+ const handler = processOnSpy . mock . calls . find ( ( [ event ] ) => event === "uncaughtException" ) ?. [ 1 ] ;
4095+ if ( typeof handler !== "function" ) {
4096+ throw new Error ( "uncaughtException handler was not registered" ) ;
4097+ }
40924098
4093- try {
4094- expect ( ( ) => handler ( new Error ( "boom" ) ) ) . toThrow ( "process.exit(1)" ) ;
4095- expect ( consoleErrorSpy ) . toHaveBeenCalledWith (
4096- "[openclaw] OpenClaw hit an unexpected runtime error." ,
4097- ) ;
4098- expect ( consoleErrorSpy ) . toHaveBeenCalledWith ( "[openclaw] Reason: boom" ) ;
4099- expect ( restoreTerminalStateMock ) . toHaveBeenCalledWith ( "uncaught exception" , {
4100- resumeStdinIfPaused : false ,
4101- } ) ;
4102- } finally {
4103- if ( typeof handler === "function" ) {
4104- process . off ( "uncaughtException" , handler ) ;
4099+ try {
4100+ loggingState . forceConsoleToStderr = machineOutput ;
4101+ expect ( ( ) => handler ( new Error ( "boom" ) ) ) . toThrow ( "process.exit(1)" ) ;
4102+ expect ( consoleErrorSpy ) . toHaveBeenCalledWith (
4103+ "[openclaw] OpenClaw hit an unexpected runtime error." ,
4104+ ) ;
4105+ expect ( consoleErrorSpy ) . toHaveBeenCalledWith ( "[openclaw] Reason: boom" ) ;
4106+ expect ( restoreRuntimeTerminalStateMock ) . toHaveBeenCalledWith ( "uncaught exception" , {
4107+ resumeStdinIfPaused : false ,
4108+ } ) ;
4109+ } finally {
4110+ loggingState . forceConsoleToStderr = false ;
4111+ if ( typeof handler === "function" ) {
4112+ process . off ( "uncaughtException" , handler ) ;
4113+ }
4114+ consoleErrorSpy . mockRestore ( ) ;
4115+ exitSpy . mockRestore ( ) ;
4116+ processOnSpy . mockRestore ( ) ;
41054117 }
4106- consoleErrorSpy . mockRestore ( ) ;
4107- exitSpy . mockRestore ( ) ;
4108- processOnSpy . mockRestore ( ) ;
4109- }
4110- } ) ;
4118+ } ,
4119+ ) ;
41114120
41124121 it ( "does not exit for transient uncaught CLI exceptions" , async ( ) => {
41134122 buildProgramMock . mockReturnValueOnce ( {
@@ -4136,7 +4145,7 @@ describe("runCli exit behavior", () => {
41364145 expect ( consoleWarnSpy . mock . calls ) . toEqual ( [
41374146 [ "[openclaw] Non-fatal uncaught exception (continuing):" , hostUnreachable . stack ] ,
41384147 ] ) ;
4139- expect ( restoreTerminalStateMock ) . not . toHaveBeenCalled ( ) ;
4148+ expect ( restoreRuntimeTerminalStateMock ) . not . toHaveBeenCalled ( ) ;
41404149 expect ( exitSpy ) . not . toHaveBeenCalled ( ) ;
41414150 } finally {
41424151 if ( typeof handler === "function" ) {
0 commit comments