@@ -68,13 +68,14 @@ function assertCarriesNoSecret(error: object, secret: string): void {
6868const tailscaleStatusJson = `{"Self":{"DNSName":"desktop.tail.ts.net.","TailscaleIPs":["100.100.100.100","fd7a:115c:a1e0::1","192.168.1.20"]}}` ;
6969const tailscaleStatusWithSingleIpJson = `{"Self":{"DNSName":"desktop.tail.ts.net.","TailscaleIPs":["100.90.1.2"]}}` ;
7070const emptyServeStatusJson = `{"TCP":{},"Web":{}}` ;
71+ const serveConfig = ( servePort : number , proxy : string ) => ( {
72+ TCP : { [ servePort ] : { HTTPS : true } } ,
73+ Web : {
74+ [ `desktop.tail.ts.net:${ String ( servePort ) } ` ] : { Handlers : { "/" : { Proxy : proxy } } } ,
75+ } ,
76+ } ) ;
7177const serveStatusJson = ( servePort : number , proxy : string ) =>
72- JSON . stringify ( {
73- TCP : { [ servePort ] : { HTTPS : true } } ,
74- Web : {
75- [ `desktop.tail.ts.net:${ String ( servePort ) } ` ] : { Handlers : { "/" : { Proxy : proxy } } } ,
76- } ,
77- } ) ;
78+ JSON . stringify ( serveConfig ( servePort , proxy ) ) ;
7879
7980function mockHandle ( result : { stdout ?: string ; stderr ?: string ; code ?: number } ) {
8081 return ChildProcessSpawner . makeHandle ( {
@@ -385,6 +386,112 @@ describe("tailscale", () => {
385386 } ) ;
386387 } ) ;
387388
389+ it . effect . each ( [
390+ {
391+ name : "foreground-only" ,
392+ background : { } ,
393+ foreground : serveConfig ( 8443 , "http://127.0.0.1:13773" ) ,
394+ } ,
395+ {
396+ name : "foreground overriding an exact background" ,
397+ background : serveConfig ( 8443 , "http://127.0.0.1:13773" ) ,
398+ foreground : serveConfig ( 8443 , "http://127.0.0.1:13773" ) ,
399+ } ,
400+ {
401+ name : "foreground TCP-only" ,
402+ background : { } ,
403+ foreground : { TCP : { 8443 : { TCPForward : "127.0.0.1:39831" } } } ,
404+ } ,
405+ {
406+ name : "foreground web-only" ,
407+ background : { } ,
408+ foreground : { Web : serveConfig ( 8443 , "http://127.0.0.1:13773" ) . Web } ,
409+ } ,
410+ {
411+ name : "foreground Funnel-only" ,
412+ background : { } ,
413+ foreground : { AllowFunnel : { "desktop.tail.ts.net:8443" : true } } ,
414+ } ,
415+ ] ) ( "refuses to reuse, replace, or disable a $name handler" , ( { background, foreground } ) => {
416+ const commands : ReadonlyArray < string > [ ] = [ ] ;
417+ const layer = mockSpawnerLayer ( ( _command , args ) => {
418+ commands . push ( args ) ;
419+ return {
420+ stdout : JSON . stringify ( {
421+ ...background ,
422+ Foreground : { "session-id" : foreground } ,
423+ } ) ,
424+ } ;
425+ } ) ;
426+
427+ return Effect . gen ( function * ( ) {
428+ const ensureError = yield * ensureTailscaleServe ( {
429+ localPort : 13773 ,
430+ servePort : 8443 ,
431+ replaceVerifiedHandler : true ,
432+ } ) . pipe ( Effect . flip , Effect . provide ( layer ) ) ;
433+ assert . instanceOf ( ensureError , TailscaleServePortOccupiedError ) ;
434+
435+ const disableError = yield * disableTailscaleServe ( {
436+ localPort : 13773 ,
437+ servePort : 8443 ,
438+ } ) . pipe ( Effect . flip , Effect . provide ( layer ) ) ;
439+ assert . instanceOf ( disableError , TailscaleServePortOccupiedError ) ;
440+ assert . deepEqual ( commands , [
441+ [ "serve" , "status" , "--json" ] ,
442+ [ "serve" , "status" , "--json" ] ,
443+ ] ) ;
444+ } ) ;
445+ } ) ;
446+
447+ it . effect ( "configures an unused port while another port has a foreground handler" , ( ) => {
448+ const commands : ReadonlyArray < string > [ ] = [ ] ;
449+ const layer = mockSpawnerLayer ( ( _command , args ) => {
450+ commands . push ( args ) ;
451+ return {
452+ stdout : JSON . stringify ( {
453+ Foreground : { "session-id" : serveConfig ( 443 , "http://127.0.0.1:39831" ) } ,
454+ } ) ,
455+ } ;
456+ } ) ;
457+
458+ return Effect . gen ( function * ( ) {
459+ yield * ensureTailscaleServe ( { localPort : 13773 , servePort : 8443 } ) . pipe (
460+ Effect . provide ( layer ) ,
461+ ) ;
462+ assert . deepEqual ( commands , [
463+ [ "serve" , "status" , "--json" ] ,
464+ [ "serve" , "--bg" , "--https=8443" , "http://127.0.0.1:13773" ] ,
465+ ] ) ;
466+ } ) ;
467+ } ) ;
468+
469+ it . effect (
470+ "disables an exact background handler while another port has a foreground handler" ,
471+ ( ) => {
472+ const commands : ReadonlyArray < string > [ ] = [ ] ;
473+ const layer = mockSpawnerLayer ( ( _command , args ) => {
474+ commands . push ( args ) ;
475+ return {
476+ stdout : JSON . stringify ( {
477+ ...serveConfig ( 8443 , "http://127.0.0.1:13773" ) ,
478+ Foreground : { "session-id" : serveConfig ( 443 , "http://127.0.0.1:39831" ) } ,
479+ } ) ,
480+ } ;
481+ } ) ;
482+
483+ return Effect . gen ( function * ( ) {
484+ yield * disableTailscaleServe ( { localPort : 13773 , servePort : 8443 } ) . pipe (
485+ Effect . provide ( layer ) ,
486+ ) ;
487+ assert . deepEqual ( commands , [
488+ [ "serve" , "status" , "--json" ] ,
489+ [ "serve" , "--https=8443" , "off" ] ,
490+ ] ) ;
491+ } ) ;
492+ } ,
493+ ) ;
494+
388495 it . effect ( "refuses to replace a configured port even when its backend differs" , ( ) => {
389496 const layer = mockSpawnerLayer ( ( ) => ( {
390497 stdout : serveStatusJson ( 8443 , "http://127.0.0.1:39831" ) ,
0 commit comments