@@ -67,117 +67,121 @@ describe("gateway-hosted exec approvals", () => {
6767 clearSessionStoreCacheForTest ( ) ;
6868 } ) ;
6969
70- it ( "lets OpenClaw-style gateway tool calls request and wait for approval over separate connections" , async ( ) => {
71- const envSnapshot = captureEnv ( TEST_ENV_KEYS ) ;
72- cleanup . push ( ( ) => envSnapshot . restore ( ) ) ;
73-
74- const tempHome = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-exec-approval-e2e-" ) ) ;
75- cleanup . push ( ( ) => fs . rm ( tempHome , { recursive : true , force : true , maxRetries : 5 } ) ) ;
76-
77- const stateDir = path . join ( tempHome , ".openclaw" ) ;
78- const workspaceDir = path . join ( tempHome , "workspace" ) ;
79- await fs . mkdir ( workspaceDir , { recursive : true } ) ;
80-
81- const port = await getFreeGatewayPort ( ) ;
82- const token = "exec-approval-e2e-token" ;
83- const configPath = path . join ( stateDir , "openclaw.json" ) ;
84- await fs . mkdir ( stateDir , { recursive : true } ) ;
85- await fs . writeFile (
86- configPath ,
87- `${ JSON . stringify (
88- {
89- gateway : {
90- port,
91- auth : { mode : "token" , token } ,
92- } ,
93- tools : {
94- exec : {
95- host : "gateway" ,
96- security : "allowlist" ,
97- ask : "always" ,
70+ it (
71+ "lets OpenClaw-style gateway tool calls request and wait for approval over separate connections" ,
72+ async ( ) => {
73+ const envSnapshot = captureEnv ( TEST_ENV_KEYS ) ;
74+ cleanup . push ( ( ) => envSnapshot . restore ( ) ) ;
75+
76+ const tempHome = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-exec-approval-e2e-" ) ) ;
77+ cleanup . push ( ( ) => fs . rm ( tempHome , { recursive : true , force : true , maxRetries : 5 } ) ) ;
78+
79+ const stateDir = path . join ( tempHome , ".openclaw" ) ;
80+ const workspaceDir = path . join ( tempHome , "workspace" ) ;
81+ await fs . mkdir ( workspaceDir , { recursive : true } ) ;
82+
83+ const port = await getFreeGatewayPort ( ) ;
84+ const token = "exec-approval-e2e-token" ;
85+ const configPath = path . join ( stateDir , "openclaw.json" ) ;
86+ await fs . mkdir ( stateDir , { recursive : true } ) ;
87+ await fs . writeFile (
88+ configPath ,
89+ `${ JSON . stringify (
90+ {
91+ gateway : {
92+ port,
93+ auth : { mode : "token" , token } ,
94+ } ,
95+ tools : {
96+ exec : {
97+ host : "gateway" ,
98+ security : "allowlist" ,
99+ ask : "always" ,
100+ } ,
98101 } ,
99102 } ,
103+ null ,
104+ 2 ,
105+ ) } \n`,
106+ "utf8" ,
107+ ) ;
108+
109+ process . env . HOME = tempHome ;
110+ process . env . OPENCLAW_STATE_DIR = stateDir ;
111+ process . env . OPENCLAW_CONFIG_PATH = configPath ;
112+ process . env . OPENCLAW_GATEWAY_TOKEN = token ;
113+ process . env . OPENCLAW_GATEWAY_PORT = String ( port ) ;
114+ process . env . OPENCLAW_SKIP_CHANNELS = "1" ;
115+ process . env . OPENCLAW_SKIP_GMAIL_WATCHER = "1" ;
116+ process . env . OPENCLAW_SKIP_CRON = "1" ;
117+ process . env . OPENCLAW_SKIP_CANVAS_HOST = "1" ;
118+ process . env . OPENCLAW_SKIP_BROWSER_CONTROL_SERVER = "1" ;
119+ process . env . OPENCLAW_SKIP_PROVIDERS = "1" ;
120+ process . env . OPENCLAW_TEST_MINIMAL_GATEWAY = "1" ;
121+ clearRuntimeConfigSnapshot ( ) ;
122+ clearConfigCache ( ) ;
123+ clearSessionStoreCacheForTest ( ) ;
124+
125+ const server = await startGatewayServer ( port , {
126+ bind : "loopback" ,
127+ auth : { mode : "token" , token } ,
128+ controlUiEnabled : false ,
129+ deferStartupSidecars : true ,
130+ } ) ;
131+ cleanup . push ( ( ) => server . close ( ) ) ;
132+
133+ const operator = await connectGatewayClient ( {
134+ url : `ws://127.0.0.1:${ port } ` ,
135+ token,
136+ clientName : GATEWAY_CLIENT_NAMES . TEST ,
137+ clientDisplayName : "approval operator" ,
138+ mode : GATEWAY_CLIENT_MODES . TEST ,
139+ scopes : [ ADMIN_SCOPE ] ,
140+ requestTimeoutMs : GATEWAY_CONNECT_TIMEOUT_MS ,
141+ timeoutMs : GATEWAY_CONNECT_TIMEOUT_MS ,
142+ } ) ;
143+ cleanup . push ( ( ) => disconnectGatewayClient ( operator ) ) ;
144+
145+ let resolveOutcome : ( outcome : ExecApprovalFollowupOutcome ) => void = ( ) => { } ;
146+ const outcomePromise = new Promise < ExecApprovalFollowupOutcome > ( ( resolve ) => {
147+ resolveOutcome = resolve ;
148+ } ) ;
149+
150+ const tool = createExecTool ( {
151+ host : "gateway" ,
152+ security : "allowlist" ,
153+ ask : "always" ,
154+ cwd : workspaceDir ,
155+ approvalRunningNoticeMs : 0 ,
156+ approvalFollowupMode : "direct" ,
157+ approvalFollowup : ( { outcome } ) => {
158+ resolveOutcome ( outcome ) ;
159+ return undefined ;
100160 } ,
101- null ,
102- 2 ,
103- ) } \n`,
104- "utf8" ,
105- ) ;
106-
107- process . env . HOME = tempHome ;
108- process . env . OPENCLAW_STATE_DIR = stateDir ;
109- process . env . OPENCLAW_CONFIG_PATH = configPath ;
110- process . env . OPENCLAW_GATEWAY_TOKEN = token ;
111- process . env . OPENCLAW_GATEWAY_PORT = String ( port ) ;
112- process . env . OPENCLAW_SKIP_CHANNELS = "1" ;
113- process . env . OPENCLAW_SKIP_GMAIL_WATCHER = "1" ;
114- process . env . OPENCLAW_SKIP_CRON = "1" ;
115- process . env . OPENCLAW_SKIP_CANVAS_HOST = "1" ;
116- process . env . OPENCLAW_SKIP_BROWSER_CONTROL_SERVER = "1" ;
117- process . env . OPENCLAW_SKIP_PROVIDERS = "1" ;
118- process . env . OPENCLAW_TEST_MINIMAL_GATEWAY = "1" ;
119- clearRuntimeConfigSnapshot ( ) ;
120- clearConfigCache ( ) ;
121- clearSessionStoreCacheForTest ( ) ;
122-
123- const server = await startGatewayServer ( port , {
124- bind : "loopback" ,
125- auth : { mode : "token" , token } ,
126- controlUiEnabled : false ,
127- deferStartupSidecars : true ,
128- } ) ;
129- cleanup . push ( ( ) => server . close ( ) ) ;
130-
131- const operator = await connectGatewayClient ( {
132- url : `ws://127.0.0.1:${ port } ` ,
133- token,
134- clientName : GATEWAY_CLIENT_NAMES . TEST ,
135- clientDisplayName : "approval operator" ,
136- mode : GATEWAY_CLIENT_MODES . TEST ,
137- scopes : [ ADMIN_SCOPE ] ,
138- requestTimeoutMs : GATEWAY_CONNECT_TIMEOUT_MS ,
139- timeoutMs : GATEWAY_CONNECT_TIMEOUT_MS ,
140- } ) ;
141- cleanup . push ( ( ) => disconnectGatewayClient ( operator ) ) ;
142-
143- let resolveOutcome : ( outcome : ExecApprovalFollowupOutcome ) => void = ( ) => { } ;
144- const outcomePromise = new Promise < ExecApprovalFollowupOutcome > ( ( resolve ) => {
145- resolveOutcome = resolve ;
146- } ) ;
147-
148- const tool = createExecTool ( {
149- host : "gateway" ,
150- security : "allowlist" ,
151- ask : "always" ,
152- cwd : workspaceDir ,
153- approvalRunningNoticeMs : 0 ,
154- approvalFollowupMode : "direct" ,
155- approvalFollowup : ( { outcome } ) => {
156- resolveOutcome ( outcome ) ;
157- return undefined ;
158- } ,
159- } ) ;
160-
161- const pending = await tool . execute ( "exec-approval-e2e" , {
162- command : "printf 'smoke\\n'" ,
163- workdir : workspaceDir ,
164- timeout : 5 ,
165- } ) ;
166-
167- expect ( pending . details . status ) . toBe ( "approval-pending" ) ;
168- if ( pending . details . status !== "approval-pending" ) {
169- throw new Error ( "expected approval-pending exec result" ) ;
170- }
171-
172- await operator . request (
173- "exec.approval.resolve" ,
174- { id : pending . details . approvalId , decision : "allow-once" } ,
175- { timeoutMs : 10_000 } ,
176- ) ;
177-
178- const outcome = await withTimeout ( outcomePromise , 15_000 , "approved exec outcome" ) ;
179- expect ( outcome . status ) . toBe ( "completed" ) ;
180- expect ( outcome . exitCode ) . toBe ( 0 ) ;
181- expect ( outcome . aggregated ) . toBe ( "smoke" ) ;
182- } , EXEC_APPROVAL_E2E_TIMEOUT_MS ) ;
161+ } ) ;
162+
163+ const pending = await tool . execute ( "exec-approval-e2e" , {
164+ command : "printf 'smoke\\n'" ,
165+ workdir : workspaceDir ,
166+ timeout : 5 ,
167+ } ) ;
168+
169+ expect ( pending . details . status ) . toBe ( "approval-pending" ) ;
170+ if ( pending . details . status !== "approval-pending" ) {
171+ throw new Error ( "expected approval-pending exec result" ) ;
172+ }
173+
174+ await operator . request (
175+ "exec.approval.resolve" ,
176+ { id : pending . details . approvalId , decision : "allow-once" } ,
177+ { timeoutMs : 10_000 } ,
178+ ) ;
179+
180+ const outcome = await withTimeout ( outcomePromise , 15_000 , "approved exec outcome" ) ;
181+ expect ( outcome . status ) . toBe ( "completed" ) ;
182+ expect ( outcome . exitCode ) . toBe ( 0 ) ;
183+ expect ( outcome . aggregated ) . toBe ( "smoke" ) ;
184+ } ,
185+ EXEC_APPROVAL_E2E_TIMEOUT_MS ,
186+ ) ;
183187} ) ;
0 commit comments