@@ -59,14 +59,15 @@ function createGatewayDiscoveryBeacon(): GatewayBonjourBeacon {
5959}
6060
6161describe ( "promptRemoteGatewayConfig" , ( ) => {
62- const envSnapshot = captureEnv ( [ "OPENCLAW_ALLOW_INSECURE_PRIVATE_WS" ] ) ;
62+ const envSnapshot = captureEnv ( [ "OPENCLAW_ALLOW_INSECURE_PRIVATE_WS" , "OPENCLAW_GATEWAY_TOKEN" ] ) ;
6363
6464 async function runRemotePrompt ( params : {
65+ cfg ?: OpenClawConfig ;
6566 text : WizardPrompter [ "text" ] ;
6667 selectResponses : Partial < Record < string , string > > ;
6768 confirm : boolean ;
6869 } ) {
69- const cfg = { } as OpenClawConfig ;
70+ const cfg = params . cfg ?? { } ;
7071 const prompter = createPrompter ( {
7172 confirm : vi . fn ( async ( ) => params . confirm ) ,
7273 select : createSelectPrompter ( params . selectResponses ) ,
@@ -87,7 +88,71 @@ describe("promptRemoteGatewayConfig", () => {
8788
8889 afterEach ( ( ) => {
8990 envSnapshot . restore ( ) ;
90- delete process . env . OPENCLAW_ALLOW_INSECURE_PRIVATE_WS ;
91+ } ) ;
92+
93+ it . each ( [
94+ { name : "unchanged URL" , auth : "token" , url : "wss://gateway.example/rpc" } ,
95+ { name : "trimmed URL" , auth : "token" , url : " wss://gateway.example/rpc " } ,
96+ { name : "changed host" , auth : "token" , url : "wss://other.example/rpc" } ,
97+ { name : "changed path" , auth : "token" , url : "wss://gateway.example/other" } ,
98+ { name : "password selection" , auth : "password" , url : "wss://gateway.example/rpc" } ,
99+ { name : "auth disabled" , auth : "off" , url : "wss://gateway.example/rpc" } ,
100+ {
101+ name : "changed URL seeded by onboarding" ,
102+ auth : "token" ,
103+ url : "wss://other.example/rpc" ,
104+ seededUrl : "wss://other.example/rpc" ,
105+ } ,
106+ {
107+ name : "seeded URL edited back to the original endpoint" ,
108+ auth : "token" ,
109+ url : "wss://gateway.example/rpc" ,
110+ seededUrl : "wss://other.example/rpc" ,
111+ } ,
112+ {
113+ name : "seeded URL without a previously configured endpoint" ,
114+ auth : "token" ,
115+ url : "wss://other.example/rpc" ,
116+ seededUrl : "wss://other.example/rpc" ,
117+ noOriginalUrl : true ,
118+ } ,
119+ ] ) ( "scopes saved remote settings for $name" , async ( { auth, url, seededUrl, noOriginalUrl } ) => {
120+ const remote = {
121+ url : noOriginalUrl ? undefined : " wss://gateway.example/rpc " ,
122+ transport : "direct" as const ,
123+ remotePort : 19443 ,
124+ token : "existing-token" ,
125+ password : "existing-password" ,
126+ edgeAuth : { "X-Edge-Auth" : "test-secret" } ,
127+ tlsFingerprint : "ab" . repeat ( 32 ) ,
128+ 129+ sshIdentity : "/tmp/test-identity" ,
130+ sshHostKeyPolicy : "strict" as const ,
131+ } ;
132+ const cfg : OpenClawConfig = {
133+ gateway : { mode : "remote" , remote : { ...remote , url : seededUrl ?? remote . url } } ,
134+ } ;
135+ detectBinary . mockResolvedValue ( true ) ;
136+ const prompter = createPrompter ( {
137+ confirm : vi . fn ( async ( { message } ) => message . startsWith ( "Use existing gateway" ) ) ,
138+ select : createSelectPrompter ( { "Gateway auth" : auth } ) ,
139+ text : vi . fn ( async ( ) => url ) ,
140+ } ) ;
141+
142+ const next = await promptRemoteGatewayConfig ( cfg , prompter , {
143+ secretInputMode : "plaintext" ,
144+ ...( seededUrl ? { remoteOriginUrl : remote . url } : { } ) ,
145+ } ) ;
146+
147+ const unchanged = url . trim ( ) === remote . url ?. trim ( ) ;
148+ expect ( next . gateway ?. remote ) . toEqual ( {
149+ ...( unchanged ? remote : { } ) ,
150+ url : url . trim ( ) ,
151+ token : auth === "token" ? remote . token : undefined ,
152+ password : auth === "password" ? remote . password : undefined ,
153+ } ) ;
154+ expect ( cfg . gateway ?. remote ) . toEqual ( { ...remote , url : seededUrl ?? remote . url } ) ;
155+ expect ( discoverGatewayBeacons ) . not . toHaveBeenCalled ( ) ;
91156 } ) ;
92157
93158 it . each ( [
@@ -116,44 +181,90 @@ describe("promptRemoteGatewayConfig", () => {
116181 expect ( next . gateway ?. remote ?. edgeAuth ) . toEqual ( expected ) ;
117182 } ) ;
118183
119- it ( "defaults discovered direct remote URLs to wss://" , async ( ) => {
184+ it . each ( [ undefined , "wss://gateway.tailnet.ts.net:18789" , "wss://old.example/rpc" ] ) (
185+ "pins a trusted discovery endpoint with previous URL %s" ,
186+ async ( previousUrl ) => {
187+ detectBinary . mockResolvedValue ( true ) ;
188+ discoverGatewayBeacons . mockResolvedValue ( [ createGatewayDiscoveryBeacon ( ) ] ) ;
189+
190+ const text : WizardPrompter [ "text" ] = vi . fn ( async ( params ) => {
191+ if ( params . message === "Gateway WebSocket URL" ) {
192+ expect ( params . initialValue ) . toBe ( "wss://gateway.tailnet.ts.net:18789" ) ;
193+ expect ( params . validate ?.( String ( params . initialValue ) ) ) . toBeUndefined ( ) ;
194+ return String ( params . initialValue ) ;
195+ }
196+ if ( params . message === "Gateway token" ) {
197+ return "token-123" ;
198+ }
199+ return "" ;
200+ } ) as WizardPrompter [ "text" ] ;
201+
202+ const { next, prompter } = await runRemotePrompt ( {
203+ cfg : {
204+ gateway : {
205+ remote : {
206+ url : previousUrl ,
207+ transport : "ssh" ,
208+ 209+ tlsFingerprint : "sha256:old-pin" ,
210+ } ,
211+ } ,
212+ } ,
213+ text,
214+ confirm : true ,
215+ selectResponses : {
216+ "Select gateway" : "0" ,
217+ "Connection method" : "direct" ,
218+ "Gateway auth" : "token" ,
219+ } ,
220+ } ) ;
221+
222+ expect ( next . gateway ?. mode ) . toBe ( "remote" ) ;
223+ expect ( next . gateway ?. remote ?. url ) . toBe ( "wss://gateway.tailnet.ts.net:18789" ) ;
224+ expect ( next . gateway ?. remote ?. transport ) . toBe ( "direct" ) ;
225+ expect ( next . gateway ?. remote ?. token ) . toBe ( "token-123" ) ;
226+ expect ( next . gateway ?. remote ?. tlsFingerprint ) . toBe ( "sha256:abc123" ) ;
227+ expect ( prompter . note ) . toHaveBeenCalledWith (
228+ [
229+ "Direct remote access defaults to TLS." ,
230+ "Using: wss://gateway.tailnet.ts.net:18789" ,
231+ "TLS pin: sha256:abc123" ,
232+ "If your gateway is loopback-only, choose SSH tunnel and keep ws://127.0.0.1:18789." ,
233+ ] . join ( "\n" ) ,
234+ "Direct remote" ,
235+ ) ;
236+ } ,
237+ ) ;
238+
239+ it ( "does not retain a saved SSH route when discovery suggests a new manual tunnel" , async ( ) => {
120240 detectBinary . mockResolvedValue ( true ) ;
121241 discoverGatewayBeacons . mockResolvedValue ( [ createGatewayDiscoveryBeacon ( ) ] ) ;
122-
123- const text : WizardPrompter [ "text" ] = vi . fn ( async ( params ) => {
124- if ( params . message === "Gateway WebSocket URL" ) {
125- expect ( params . initialValue ) . toBe ( "wss://gateway.tailnet.ts.net:18789" ) ;
126- expect ( params . validate ?.( String ( params . initialValue ) ) ) . toBeUndefined ( ) ;
127- return String ( params . initialValue ) ;
128- }
129- if ( params . message === "Gateway token" ) {
130- return "token-123" ;
131- }
132- return "" ;
133- } ) as WizardPrompter [ "text" ] ;
134-
135242 const { next, prompter } = await runRemotePrompt ( {
136- text,
243+ cfg : {
244+ gateway : {
245+ remote : {
246+ url : "ws://127.0.0.1:18789" ,
247+ transport : "ssh" ,
248+ 249+ sshIdentity : "/tmp/old-identity" ,
250+ sshHostKeyPolicy : "openssh" ,
251+ remotePort : 19443 ,
252+ } ,
253+ } ,
254+ } ,
255+ text : vi . fn ( async ( params ) => String ( params . initialValue ) ) ,
137256 confirm : true ,
138257 selectResponses : {
139258 "Select gateway" : "0" ,
140- "Connection method" : "direct " ,
141- "Gateway auth" : "token " ,
259+ "Connection method" : "ssh " ,
260+ "Gateway auth" : "off " ,
142261 } ,
143262 } ) ;
144263
145- expect ( next . gateway ?. mode ) . toBe ( "remote" ) ;
146- expect ( next . gateway ?. remote ?. url ) . toBe ( "wss://gateway.tailnet.ts.net:18789" ) ;
147- expect ( next . gateway ?. remote ?. token ) . toBe ( "token-123" ) ;
148- expect ( next . gateway ?. remote ?. tlsFingerprint ) . toBe ( "sha256:abc123" ) ;
264+ expect ( next . gateway ?. remote ) . toEqual ( { url : "ws://127.0.0.1:18789" } ) ;
149265 expect ( prompter . note ) . toHaveBeenCalledWith (
150- [
151- "Direct remote access defaults to TLS." ,
152- "Using: wss://gateway.tailnet.ts.net:18789" ,
153- "TLS pin: sha256:abc123" ,
154- "If your gateway is loopback-only, choose SSH tunnel and keep ws://127.0.0.1:18789." ,
155- ] . join ( "\n" ) ,
156- "Direct remote" ,
266+ expect . stringContaining ( "<user>@gateway.tailnet.ts.net" ) ,
267+ "SSH tunnel" ,
157268 ) ;
158269 } ) ;
159270
@@ -204,62 +315,72 @@ describe("promptRemoteGatewayConfig", () => {
204315 expect ( next . gateway ?. remote ?. tlsFingerprint ) . toBeUndefined ( ) ;
205316 } ) ;
206317
207- it ( "trusts discovery endpoint without fingerprint and omits tlsFingerprint" , async ( ) => {
208- detectBinary . mockResolvedValue ( true ) ;
209- discoverGatewayBeacons . mockResolvedValue ( [
210- {
211- instanceName : "gw" ,
212- displayName : "Gateway" ,
213- host : "gw.example" ,
214- port : 18789 ,
215- } ,
216- ] ) ;
217-
218- const text : WizardPrompter [ "text" ] = vi . fn ( async ( params ) => {
219- if ( params . message === "Gateway WebSocket URL" ) {
220- return String ( params . initialValue ) ;
221- }
222- return "" ;
223- } ) as WizardPrompter [ "text" ] ;
224-
225- const { next } = await runRemotePrompt ( {
226- text,
227- confirm : true ,
228- selectResponses : {
229- "Select gateway" : "0" ,
230- "Connection method" : "direct" ,
231- "Gateway auth" : "off" ,
232- } ,
233- } ) ;
234-
235- expect ( next . gateway ?. remote ?. url ) . toBe ( "wss://gw.example:18789" ) ;
236- expect ( next . gateway ?. remote ?. tlsFingerprint ) . toBeUndefined ( ) ;
237- } ) ;
238-
239- it ( "drops discovery tlsFingerprint when the URL is edited after trust confirmation" , async ( ) => {
240- detectBinary . mockResolvedValue ( true ) ;
241- discoverGatewayBeacons . mockResolvedValue ( [ createGatewayDiscoveryBeacon ( ) ] ) ;
318+ it . each ( [ undefined , "sha256:existing-pin" ] ) (
319+ "trusts discovery without an advertised fingerprint and retains existing pin %s" ,
320+ async ( tlsFingerprint ) => {
321+ detectBinary . mockResolvedValue ( true ) ;
322+ discoverGatewayBeacons . mockResolvedValue ( [
323+ {
324+ instanceName : "gw" ,
325+ displayName : "Gateway" ,
326+ host : "gw.example" ,
327+ port : 18789 ,
328+ } ,
329+ ] ) ;
330+
331+ const text : WizardPrompter [ "text" ] = vi . fn ( async ( params ) => {
332+ if ( params . message === "Gateway WebSocket URL" ) {
333+ return String ( params . initialValue ) ;
334+ }
335+ return "" ;
336+ } ) as WizardPrompter [ "text" ] ;
337+
338+ const { next } = await runRemotePrompt ( {
339+ cfg : { gateway : { remote : { url : "wss://gw.example:18789" , tlsFingerprint } } } ,
340+ text,
341+ confirm : true ,
342+ selectResponses : {
343+ "Select gateway" : "0" ,
344+ "Connection method" : "direct" ,
345+ "Gateway auth" : "off" ,
346+ } ,
347+ } ) ;
242348
243- const text : WizardPrompter [ "text" ] = vi . fn ( async ( params ) => {
244- if ( params . message === "Gateway WebSocket URL" ) {
245- return "wss://other.example:443" ;
246- }
247- return "" ;
248- } ) as WizardPrompter [ "text" ] ;
349+ expect ( next . gateway ?. remote ?. url ) . toBe ( "wss://gw.example:18789" ) ;
350+ expect ( next . gateway ?. remote ?. tlsFingerprint ) . toBe ( tlsFingerprint ) ;
351+ } ,
352+ ) ;
249353
250- const { next } = await runRemotePrompt ( {
251- text,
252- confirm : true ,
253- selectResponses : {
254- "Select gateway" : "0" ,
255- "Connection method" : "direct" ,
256- "Gateway auth" : "off" ,
257- } ,
258- } ) ;
354+ it . each ( [ undefined , "wss://old.example:443" , "wss://other.example:443" ] ) (
355+ "scopes discovery and saved pins after URL edits with previous URL %s" ,
356+ async ( previousUrl ) => {
357+ detectBinary . mockResolvedValue ( true ) ;
358+ discoverGatewayBeacons . mockResolvedValue ( [ createGatewayDiscoveryBeacon ( ) ] ) ;
359+
360+ const text : WizardPrompter [ "text" ] = vi . fn ( async ( params ) => {
361+ if ( params . message === "Gateway WebSocket URL" ) {
362+ return "wss://other.example:443" ;
363+ }
364+ return "" ;
365+ } ) as WizardPrompter [ "text" ] ;
366+
367+ const { next } = await runRemotePrompt ( {
368+ cfg : { gateway : { remote : { url : previousUrl , tlsFingerprint : "sha256:old-pin" } } } ,
369+ text,
370+ confirm : true ,
371+ selectResponses : {
372+ "Select gateway" : "0" ,
373+ "Connection method" : "direct" ,
374+ "Gateway auth" : "off" ,
375+ } ,
376+ } ) ;
259377
260- expect ( next . gateway ?. remote ?. url ) . toBe ( "wss://other.example:443" ) ;
261- expect ( next . gateway ?. remote ?. tlsFingerprint ) . toBeUndefined ( ) ;
262- } ) ;
378+ expect ( next . gateway ?. remote ?. url ) . toBe ( "wss://other.example:443" ) ;
379+ expect ( next . gateway ?. remote ?. tlsFingerprint ) . toBe (
380+ previousUrl === "wss://other.example:443" ? "sha256:old-pin" : undefined ,
381+ ) ;
382+ } ,
383+ ) ;
263384
264385 it ( "does not route from TXT-only discovery metadata" , async ( ) => {
265386 detectBinary . mockResolvedValue ( true ) ;
0 commit comments