@@ -164,6 +164,123 @@ describe("cli json stdout contract", () => {
164164 ) ;
165165 } ) ;
166166
167+ it . each ( [
168+ {
169+ name : "invalid call params in JSON mode" ,
170+ args : [ "gateway" , "call" , "system-presence" , "--params" , "not-json" , "--json" ] ,
171+ message : "--params must be valid JSON." ,
172+ } ,
173+ {
174+ name : "invalid call params in human mode" ,
175+ args : [ "gateway" , "call" , "system-presence" , "--params" , "not-json" ] ,
176+ message : "--params must be valid JSON." ,
177+ human : true ,
178+ } ,
179+ {
180+ name : "invalid call params through forced Commander" ,
181+ args : [ "gateway" , "call" , "system-presence" , "--params" , "not-json" , "--json" ] ,
182+ message : "--params must be valid JSON." ,
183+ commander : true ,
184+ } ,
185+ {
186+ name : "invalid call params with dual TTYs" ,
187+ args : [ "gateway" , "call" , "system-presence" , "--params" , "not-json" , "--json" ] ,
188+ message : "--params must be valid JSON." ,
189+ tty : true ,
190+ } ,
191+ {
192+ name : "contradictory usage options in JSON mode" ,
193+ args : [ "gateway" , "usage-cost" , "--agent" , "alpha" , "--all-agents" , "--json" ] ,
194+ message : "Use --agent or --all-agents, not both" ,
195+ } ,
196+ {
197+ name : "contradictory usage options in human mode" ,
198+ args : [ "gateway" , "usage-cost" , "--agent" , "alpha" , "--all-agents" ] ,
199+ message : "Use --agent or --all-agents, not both" ,
200+ human : true ,
201+ } ,
202+ {
203+ name : "contradictory usage options through forced Commander" ,
204+ args : [ "gateway" , "usage-cost" , "--agent" , "alpha" , "--all-agents" , "--json" ] ,
205+ message : "Use --agent or --all-agents, not both" ,
206+ commander : true ,
207+ } ,
208+ {
209+ name : "contradictory usage options with dual TTYs" ,
210+ args : [ "gateway" , "usage-cost" , "--agent" , "alpha" , "--all-agents" , "--json" ] ,
211+ message : "Use --agent or --all-agents, not both" ,
212+ tty : true ,
213+ } ,
214+ {
215+ name : "specialized explicit Gateway authentication failure" ,
216+ args : [ "gateway" , "call" , "system-presence" , "--url" , "ws://127.0.0.1:29793" , "--json" ] ,
217+ specializedAuth : true ,
218+ } ,
219+ ] ) ( "renders Gateway query failures through the Gateway owner for $name" , async ( testCase ) => {
220+ await withTempHome (
221+ async ( tempHome ) => {
222+ const configPath = path . join ( tempHome , "missing-openclaw.json" ) ;
223+ const stateDir = path . join ( tempHome , "isolated-state" ) ;
224+ const gatewayError = "AUTOQA_INJECTED_GATEWAY_FAILURE" ;
225+ const preload = Buffer . from (
226+ [
227+ 'import net from "node:net";' ,
228+ `net.Socket.prototype.connect = function () { throw new Error(${ JSON . stringify ( gatewayError ) } ); };` ,
229+ ...( "tty" in testCase
230+ ? [
231+ 'Object.defineProperty(process.stdout, "isTTY", { value: true, configurable: true });' ,
232+ 'Object.defineProperty(process.stderr, "isTTY", { value: true, configurable: true });' ,
233+ ]
234+ : [ ] ) ,
235+ ] . join ( "\n" ) ,
236+ ) . toString ( "base64" ) ;
237+ const result = runBuiltCli ( tempHome , testCase . args , {
238+ NODE_OPTIONS : `--import=data:text/javascript;base64,${ preload } ` ,
239+ OPENCLAW_CONFIG_PATH : configPath ,
240+ OPENCLAW_DISABLE_BUNDLED_PLUGINS : "1" ,
241+ OPENCLAW_STATE_DIR : stateDir ,
242+ ...( "commander" in testCase ? { OPENCLAW_DISABLE_ROUTE_FIRST : "1" } : { } ) ,
243+ ...( "tty" in testCase ? { FORCE_COLOR : "1" , NO_COLOR : undefined } : { } ) ,
244+ } ) ;
245+
246+ expect ( result . status , result . stderr ) . toBe ( 1 ) ;
247+ expect ( result . stdout , result . stderr ) . not . toMatch ( / [ \u001B \u0007 ] / u) ;
248+ expect ( result . stdout ) . not . toContain ( gatewayError ) ;
249+ expect ( result . stderr ) . not . toContain ( gatewayError ) ;
250+ if ( "human" in testCase ) {
251+ expect ( result . stdout ) . toBe ( "" ) ;
252+ expect ( result . stderr ) . toContain ( testCase . message ) ;
253+ } else if ( "specializedAuth" in testCase ) {
254+ expect ( JSON . parse ( result . stdout ) ) . toEqual ( {
255+ ok : false ,
256+ error : {
257+ type : "gateway_credentials_required" ,
258+ message : [
259+ "gateway url override requires explicit credentials" ,
260+ "Fix: pass --token or --password with --url (or gatewayToken in tools)." ,
261+ "For the default local or SSH-tunneled Gateway, remove --url to use the configured target." ,
262+ `Config: ${ configPath } ` ,
263+ ] . join ( "\n" ) ,
264+ } ,
265+ } ) ;
266+ expect ( result . stderr ) . toBe ( "" ) ;
267+ } else {
268+ expect ( JSON . parse ( result . stdout ) ) . toEqual ( {
269+ ok : false ,
270+ error : { type : "cli_error" , message : testCase . message } ,
271+ } ) ;
272+ expect ( result . stderr ) . not . toContain ( testCase . message ) ;
273+ }
274+ if ( "tty" in testCase ) {
275+ expect ( result . stderr ) . toContain ( "\u001B[?25h" ) ;
276+ }
277+ await expect ( fs . stat ( stateDir ) ) . rejects . toMatchObject ( { code : "ENOENT" } ) ;
278+ await expect ( fs . stat ( configPath ) ) . rejects . toMatchObject ( { code : "ENOENT" } ) ;
279+ } ,
280+ { prefix : "openclaw-gateway-query-json-failure-e2e-" } ,
281+ ) ;
282+ } ) ;
283+
167284 it . each ( [
168285 {
169286 name : "bare report with parent JSON" ,
0 commit comments