11// Hook command tests cover metadata config keys and missing-hook exit status.
22import { Command } from "commander" ;
3- import { beforeEach , describe , expect , it , vi } from "vitest" ;
3+ import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
4+ import { GatewayClientRequestError } from "../../packages/gateway-client/src/request-error.js" ;
45import type { OpenClawConfig } from "../config/types.openclaw.js" ;
6+ import { GatewayTransportError } from "../gateway/transport-error.js" ;
57import { resolveConfiguredInternalHookNames } from "../hooks/configured.js" ;
68import type { HookStatusEntry , HookStatusReport } from "../hooks/hooks-status.js" ;
79import { ExpectedCliError } from "./failure-output.js" ;
@@ -45,6 +47,12 @@ vi.mock("../config/config.js", () => ({
4547
4648vi . mock ( "../gateway/call.js" , ( ) => ( {
4749 callGateway : mocks . callGateway ,
50+ isGatewayClientRequestError : ( error : unknown ) =>
51+ error instanceof Error && error . name === "GatewayClientRequestError" ,
52+ isGatewayCredentialsRequiredError : ( error : unknown ) =>
53+ error instanceof Error && error . name === "GatewayCredentialsRequiredError" ,
54+ isImplicitLocalGatewayTarget : async ( { config } : { config ?: OpenClawConfig } ) =>
55+ ! process . env . OPENCLAW_GATEWAY_URL && config ?. gateway ?. mode !== "remote" ,
4856} ) ) ;
4957
5058vi . mock ( "../hooks/hooks-status.js" , ( ) => ( {
@@ -135,6 +143,16 @@ function createHooksProgram(): Command {
135143 return program ;
136144}
137145
146+ function createGatewayTransportError ( kind : "closed" | "timeout" , code = 1006 ) {
147+ return new GatewayTransportError ( {
148+ kind,
149+ message :
150+ kind === "closed" ? `gateway closed (${ code } ): unavailable` : "gateway timeout after 1500ms" ,
151+ connectionDetails : { url : "ws://127.0.0.1:18789" , urlSource : "local loopback" , message : "" } ,
152+ ...( kind === "closed" ? { code, reason : "unavailable" } : { timeoutMs : 1_500 } ) ,
153+ } ) ;
154+ }
155+
138156function configureExplicitFleet ( ) {
139157 const config = {
140158 ...sourceConfig ,
@@ -159,10 +177,14 @@ function configureExplicitFleet() {
159177}
160178
161179describe ( "hooks CLI metadata config keys" , ( ) => {
180+ afterEach ( ( ) => {
181+ vi . unstubAllEnvs ( ) ;
182+ } ) ;
183+
162184 beforeEach ( ( ) => {
163185 vi . clearAllMocks ( ) ;
164186 capture . resetRuntimeCapture ( ) ;
165- mocks . callGateway . mockRejectedValue ( new Error ( "gateway unavailable ") ) ;
187+ mocks . callGateway . mockRejectedValue ( createGatewayTransportError ( "closed ") ) ;
166188 mocks . buildWorkspaceHookStatus . mockReturnValue ( report ) ;
167189 mocks . getRuntimeConfig . mockReturnValue ( sourceConfig ) ;
168190 mocks . listAgentIds . mockReturnValue ( [ "main" ] ) ;
@@ -452,10 +474,7 @@ describe("hooks CLI metadata config keys", () => {
452474 }
453475 if ( testCase . phase === "gateway" ) {
454476 mocks . callGateway . mockRejectedValue (
455- Object . assign ( new Error ( testCase . message ) , {
456- name : "GatewayClientRequestError" ,
457- gatewayCode : "INVALID_REQUEST" ,
458- } ) ,
477+ new GatewayClientRequestError ( { code : "INVALID_REQUEST" , message : testCase . message } ) ,
459478 ) ;
460479 }
461480 if ( testCase . phase === "report" ) {
@@ -542,6 +561,149 @@ describe("hooks CLI metadata config keys", () => {
542561 expect ( mocks . buildWorkspaceHookStatus ) . not . toHaveBeenCalled ( ) ;
543562 } ) ;
544563
564+ const explicitGatewayHookFailures = [
565+ {
566+ label : "configured remote missing URL" ,
567+ config : { ...sourceConfig , gateway : { mode : "remote" as const } } ,
568+ message : "gateway remote mode misconfigured: gateway.remote.url missing" ,
569+ } ,
570+ {
571+ label : "configured remote transport failure" ,
572+ config : {
573+ ...sourceConfig ,
574+ gateway : { mode : "remote" as const , remote : { url : "ws://127.0.0.1:9" } } ,
575+ } ,
576+ message : "Gateway not reachable: ws://127.0.0.1:9" ,
577+ } ,
578+ {
579+ label : "configured remote auth failure" ,
580+ config : { ...sourceConfig , gateway : { mode : "remote" as const } } ,
581+ message : "gateway authentication failed" ,
582+ } ,
583+ {
584+ label : "configured remote unsupported method" ,
585+ config : { ...sourceConfig , gateway : { mode : "remote" as const } } ,
586+ message : "unknown method: hooks.status" ,
587+ unsupported : true ,
588+ } ,
589+ {
590+ label : "environment-selected transport failure" ,
591+ config : sourceConfig ,
592+ url : "ws://127.0.0.1:9" ,
593+ message : "Gateway not reachable: ws://127.0.0.1:9" ,
594+ } ,
595+ {
596+ label : "environment-selected auth failure" ,
597+ config : sourceConfig ,
598+ url : "ws://127.0.0.1:9" ,
599+ message : "gateway authentication failed" ,
600+ } ,
601+ {
602+ label : "environment-selected unsupported method" ,
603+ config : sourceConfig ,
604+ url : "ws://127.0.0.1:9" ,
605+ message : "unknown method: hooks.status" ,
606+ unsupported : true ,
607+ } ,
608+ ] ;
609+ const hookReadCommands = [
610+ { label : "default" , argv : [ "hooks" ] } ,
611+ { label : "list" , argv : [ "hooks" , "list" ] } ,
612+ { label : "info" , argv : [ "hooks" , "info" , "display-name" ] } ,
613+ { label : "check" , argv : [ "hooks" , "check" ] } ,
614+ ] ;
615+
616+ it . each (
617+ explicitGatewayHookFailures . flatMap ( ( target ) =>
618+ hookReadCommands . flatMap ( ( command ) =>
619+ [ false , true ] . map ( ( json ) => ( {
620+ target,
621+ command,
622+ json,
623+ label : `${ command . label } ${ json ? "JSON" : "human" } : ${ target . label } ` ,
624+ } ) ) ,
625+ ) ,
626+ ) ,
627+ ) ( "does not substitute local hooks after $label" , async ( { target, command, json } ) => {
628+ mocks . getRuntimeConfig . mockReturnValue ( target . config ) ;
629+ if ( target . url ) {
630+ vi . stubEnv ( "OPENCLAW_GATEWAY_URL" , target . url ) ;
631+ }
632+ const error = target . unsupported
633+ ? new GatewayClientRequestError ( { code : "INVALID_REQUEST" , message : target . message } )
634+ : new Error ( target . message ) ;
635+ mocks . callGateway . mockRejectedValue ( error ) ;
636+
637+ const failure = await createHooksProgram ( )
638+ . parseAsync ( [ ...command . argv , ...( json ? [ "--json" ] : [ ] ) ] , { from : "user" } )
639+ . then (
640+ ( ) => undefined ,
641+ ( caughtError : unknown ) => caughtError ,
642+ ) ;
643+ expect ( failure ) . toMatchObject ( {
644+ name : "ExpectedCliError" ,
645+ message : target . message ,
646+ } ) ;
647+
648+ expect ( mocks . buildWorkspaceHookStatus ) . not . toHaveBeenCalled ( ) ;
649+ expect ( capture . defaultRuntime . writeStdout ) . not . toHaveBeenCalled ( ) ;
650+ expect ( mocks . requestExitAfterOneShotOutput ) . not . toHaveBeenCalled ( ) ;
651+ } ) ;
652+
653+ it . each ( [
654+ {
655+ label : "request validation" ,
656+ error : new GatewayClientRequestError ( {
657+ code : "INVALID_REQUEST" ,
658+ message : 'invalid hooks.status params: unknown agent id "retired"' ,
659+ } ) ,
660+ } ,
661+ {
662+ label : "internal server failure" ,
663+ error : new GatewayClientRequestError ( {
664+ code : "INTERNAL_ERROR" ,
665+ message : "hook inventory crashed" ,
666+ } ) ,
667+ } ,
668+ { label : "pairing close" , error : createGatewayTransportError ( "closed" , 1008 ) } ,
669+ { label : "authentication rotation" , error : createGatewayTransportError ( "closed" , 4001 ) } ,
670+ { label : "plain pairing close" , error : new Error ( "gateway closed (1008): pairing required" ) } ,
671+ {
672+ label : "plain authentication close" ,
673+ error : new Error ( "gateway closed (4001): auth changed" ) ,
674+ } ,
675+ { label : "unknown failure" , error : new Error ( "gateway unavailable" ) } ,
676+ ] ) ( "does not substitute implicit-local hooks after $label" , async ( { error } ) => {
677+ mocks . callGateway . mockRejectedValue ( error ) ;
678+
679+ await expect (
680+ createHooksProgram ( ) . parseAsync ( [ "hooks" , "list" , "--json" ] , { from : "user" } ) ,
681+ ) . rejects . toMatchObject ( { message : error . message } ) ;
682+
683+ expect ( mocks . buildWorkspaceHookStatus ) . not . toHaveBeenCalled ( ) ;
684+ expect ( mocks . requestExitAfterOneShotOutput ) . not . toHaveBeenCalled ( ) ;
685+ } ) ;
686+
687+ it . each ( [
688+ {
689+ label : "missing credentials before connecting" ,
690+ error : Object . assign ( new Error ( "gateway requires credentials" ) , {
691+ name : "GatewayCredentialsRequiredError" ,
692+ method : "hooks.status" ,
693+ configPath : "/tmp/openclaw.json" ,
694+ } ) ,
695+ } ,
696+ { label : "typed timeout" , error : createGatewayTransportError ( "timeout" ) } ,
697+ { label : "pending-request close" , error : new Error ( "gateway closed (1006): abnormal closure" ) } ,
698+ { label : "pending-request timeout" , error : new Error ( "gateway timeout after 1500ms" ) } ,
699+ ] ) ( "retains implicit-local hook discovery after $label" , async ( { error } ) => {
700+ mocks . callGateway . mockRejectedValue ( error ) ;
701+
702+ await createHooksProgram ( ) . parseAsync ( [ "hooks" , "list" , "--json" ] , { from : "user" } ) ;
703+
704+ expect ( mocks . buildWorkspaceHookStatus ) . toHaveBeenCalledOnce ( ) ;
705+ } ) ;
706+
545707 it . each ( [
546708 [ "list" , [ "hooks" , "list" , "--agent" , "research" , "--json" ] ] ,
547709 [ "info" , [ "hooks" , "info" , "display-name" , "--agent" , "research" , "--json" ] ] ,
@@ -652,9 +814,9 @@ describe("hooks CLI metadata config keys", () => {
652814 } ) ;
653815
654816 it ( "does not replace an authoritative Gateway ownership error with a local report" , async ( ) => {
655- const error = Object . assign ( new Error ( 'unknown agent id "retired"' ) , {
656- name : "GatewayClientRequestError " ,
657- gatewayCode : "INVALID_REQUEST" ,
817+ const error = new GatewayClientRequestError ( {
818+ code : "INVALID_REQUEST " ,
819+ message : 'unknown agent id "retired"' ,
658820 } ) ;
659821 mocks . callGateway . mockRejectedValue ( error ) ;
660822
@@ -672,12 +834,10 @@ describe("hooks CLI metadata config keys", () => {
672834 it . each ( [
673835 "unknown method: hooks.status" ,
674836 "invalid hooks.status params: unexpected property agentId" ,
837+ "invalid hooks.status params: at root: unexpected property 'agentId'" ,
675838 ] ) ( "uses the selected local fallback for an older Gateway: %s" , async ( message ) => {
676839 mocks . callGateway . mockRejectedValue (
677- Object . assign ( new Error ( message ) , {
678- name : "GatewayClientRequestError" ,
679- gatewayCode : "INVALID_REQUEST" ,
680- } ) ,
840+ new GatewayClientRequestError ( { code : "INVALID_REQUEST" , message } ) ,
681841 ) ;
682842
683843 await createHooksProgram ( ) . parseAsync ( [ "hooks" , "list" , "--agent" , "main" , "--json" ] , {
0 commit comments