11import assert from "node:assert/strict"
2+ import fs from "node:fs"
23import { createServer } from "node:http"
4+ import os from "node:os"
5+ import path from "node:path"
36import { test } from "node:test"
47
58async function loadPlugin ( env : Record < string , string | undefined > = { } ) {
@@ -335,7 +338,7 @@ test("plugin executes non-scaffold requester tools through requester-local gatew
335338 try {
336339 await runPayload ( captureWrapper ( plugin ) , { messages : [ ] } , { baseUrl : `http://127.0.0.1:${ taasAddress . port } ` } )
337340 await new Promise ( ( resolve ) => setTimeout ( resolve , 150 ) )
338- assert . deepEqual ( gatewayBody , { tool : "prd_list" , args : { query : "requester bridge" } } )
341+ assert . deepEqual ( gatewayBody , { name : "prd_list" , arguments : { query : "requester bridge" } , tool : "prd_list" , args : { query : "requester bridge" } } )
339342 assert . equal ( resultBody . operation_id , "bro_tool" )
340343 assert . equal ( resultBody . ok , true )
341344 assert . deepEqual ( resultBody . result , { rows : [ { title : "PRD" } ] } )
@@ -346,6 +349,85 @@ test("plugin executes non-scaffold requester tools through requester-local gatew
346349 }
347350} )
348351
352+
353+ test ( "plugin falls back to local OpenClaw config token for requester-local gateway" , async ( ) => {
354+ let resultBody : any
355+ let gatewayAuth : string | string [ ] | undefined
356+ let pollCount = 0
357+ const tempState = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "taas-bridge-config-token-" ) )
358+ fs . writeFileSync ( path . join ( tempState , "openclaw.json" ) , JSON . stringify ( { gateway : { auth : { token : "config-token" } } } ) )
359+
360+ const gateway = createServer ( ( req , res ) => {
361+ let body = ""
362+ req . on ( "data" , ( chunk ) => { body += chunk } )
363+ req . on ( "end" , ( ) => {
364+ void body
365+ gatewayAuth = req . headers . authorization
366+ res . setHeader ( "Content-Type" , "application/json" )
367+ res . end ( JSON . stringify ( { ok : true , result : { status : "from-config-token" } } ) )
368+ } )
369+ } )
370+ await new Promise < void > ( ( resolve ) => gateway . listen ( 0 , "127.0.0.1" , resolve ) )
371+ const gatewayAddress = gateway . address ( )
372+ assert ( gatewayAddress && typeof gatewayAddress === "object" )
373+
374+ const taas = createServer ( ( req , res ) => {
375+ let body = ""
376+ req . on ( "data" , ( chunk ) => { body += chunk } )
377+ req . on ( "end" , ( ) => {
378+ res . setHeader ( "Content-Type" , "application/json" )
379+ if ( req . url === "/internal/requester-bridges/leases" ) {
380+ res . end ( JSON . stringify ( { ok : true , descriptor : {
381+ name : "requester-workspace" , version : "2026-05-23" , status : "verified" ,
382+ bridge_id : "br_config" , lease_id : "brl_config" , capabilities : [ "requester.tool.invoke" ] ,
383+ endpoint_ref : "epref_config" , auth_context_id : "authctx_config" , expires_at : "2026-05-23T19:00:00Z" ,
384+ } } ) )
385+ return
386+ }
387+ if ( req . url === "/internal/requester-bridges/poll" ) {
388+ pollCount += 1
389+ res . end ( JSON . stringify ( { ok : true , operations : pollCount === 1 ? [ {
390+ operation_id : "bro_config" , audit_id : "bra_config" , lease_id : "brl_config" , bridge_id : "br_config" ,
391+ operation : "requester.tool.invoke" , arguments : { tool : "session_status" , arguments : { } } ,
392+ } ] : [ ] } ) )
393+ return
394+ }
395+ if ( req . url === "/internal/requester-bridges/results" ) {
396+ resultBody = JSON . parse ( body )
397+ res . end ( JSON . stringify ( { ok : true } ) )
398+ return
399+ }
400+ res . statusCode = 404
401+ res . end ( JSON . stringify ( { ok : false } ) )
402+ } )
403+ } )
404+ await new Promise < void > ( ( resolve ) => taas . listen ( 0 , "127.0.0.1" , resolve ) )
405+ const taasAddress = taas . address ( )
406+ assert ( taasAddress && typeof taasAddress === "object" )
407+
408+ const { plugin, restore } = await loadPlugin ( {
409+ TAAS_REQUESTER_BRIDGE_POLL_INTERVAL_MS : "50" ,
410+ TAAS_REQUESTER_LOCAL_GATEWAY_URL : `http://127.0.0.1:${ gatewayAddress . port } ` ,
411+ TAAS_REQUESTER_LOCAL_GATEWAY_TOKEN : undefined ,
412+ OPENCLAW_GATEWAY_TOKEN : undefined ,
413+ OPENCLAW_GATEWAY_PASSWORD : undefined ,
414+ OPENCLAW_STATE_DIR : tempState ,
415+ } )
416+ try {
417+ await runPayload ( captureWrapper ( plugin ) , { messages : [ ] } , { baseUrl : `http://127.0.0.1:${ taasAddress . port } ` } )
418+ await new Promise ( ( resolve ) => setTimeout ( resolve , 150 ) )
419+ assert . equal ( gatewayAuth , "Bearer config-token" )
420+ assert . equal ( resultBody . operation_id , "bro_config" )
421+ assert . equal ( resultBody . ok , true )
422+ assert . deepEqual ( resultBody . result , { status : "from-config-token" } )
423+ } finally {
424+ restore ( )
425+ gateway . close ( )
426+ taas . close ( )
427+ fs . rmSync ( tempState , { recursive : true , force : true } )
428+ }
429+ } )
430+
349431test ( "plugin includes claim_id in result submission when poll returns one" , async ( ) => {
350432 let pollCount = 0
351433 let resultBody : any
0 commit comments