@@ -469,34 +469,35 @@ it.effect(
469469 ) . pipe ( Effect . provide ( NodeHttpServer . layerTest ) ) ,
470470) ;
471471
472- /** A's `tailscale`, whose `status --json` lists `peerIps` as another machine on its tailnet. */
473- const layerTailscalePeers = ( peerIps : ReadonlyArray < string > ) =>
472+ /** `tailscale status --json` with `peerIps` as another machine on its tailnet. */
473+ const tailscaleStatus = ( backendState : string , peerIps : ReadonlyArray < string > ) =>
474+ JSON . stringify ( {
475+ BackendState : backendState ,
476+ Self : { DNSName : "laptop.tail1234.ts.net." , TailscaleIPs : [ "100.101.0.1" ] } ,
477+ Peer : { "nodekey:box" : { DNSName : "box.tail1234.ts.net." , TailscaleIPs : peerIps } } ,
478+ } ) ;
479+
480+ /** A's `tailscale`, whose `status --json` prints what `status` holds at each run. */
481+ const layerTailscale = ( status : Ref . Ref < string > ) =>
474482 Layer . succeed (
475483 ChildProcessSpawner . ChildProcessSpawner ,
476484 ChildProcessSpawner . make ( ( ) =>
477- Effect . succeed (
478- ChildProcessSpawner . makeHandle ( {
479- pid : ChildProcessSpawner . ProcessId ( 1 ) ,
480- exitCode : Effect . succeed ( ChildProcessSpawner . ExitCode ( 0 ) ) ,
481- isRunning : Effect . succeed ( false ) ,
482- kill : ( ) => Effect . void ,
483- unref : Effect . succeed ( Effect . void ) ,
484- stdin : Sink . drain ,
485- stdout : Stream . make (
486- new TextEncoder ( ) . encode (
487- JSON . stringify ( {
488- Self : { DNSName : "laptop.tail1234.ts.net." , TailscaleIPs : [ "100.101.0.1" ] } ,
489- Peer : {
490- "nodekey:box" : { DNSName : "box.tail1234.ts.net." , TailscaleIPs : peerIps } ,
491- } ,
492- } ) ,
493- ) ,
494- ) ,
495- stderr : Stream . empty ,
496- all : Stream . empty ,
497- getInputFd : ( ) => Sink . drain ,
498- getOutputFd : ( ) => Stream . empty ,
499- } ) ,
485+ Ref . get ( status ) . pipe (
486+ Effect . map ( ( json ) =>
487+ ChildProcessSpawner . makeHandle ( {
488+ pid : ChildProcessSpawner . ProcessId ( 1 ) ,
489+ exitCode : Effect . succeed ( ChildProcessSpawner . ExitCode ( 0 ) ) ,
490+ isRunning : Effect . succeed ( false ) ,
491+ kill : ( ) => Effect . void ,
492+ unref : Effect . succeed ( Effect . void ) ,
493+ stdin : Sink . drain ,
494+ stdout : Stream . make ( new TextEncoder ( ) . encode ( json ) ) ,
495+ stderr : Stream . empty ,
496+ all : Stream . empty ,
497+ getInputFd : ( ) => Sink . drain ,
498+ getOutputFd : ( ) => Stream . empty ,
499+ } ) ,
500+ ) ,
500501 ) ,
501502 ) ,
502503 ) ;
@@ -520,15 +521,27 @@ it.effect(
520521 Effect . gen ( function * ( ) {
521522 const b = yield * serveB ( box ) ;
522523 const port = new URL ( b . url ) . port ;
524+ const peerIps = [ "100.100.1.2" , "fd7a:115c:a1e0::1" ] ;
525+ const status = yield * Ref . make ( tailscaleStatus ( "Stopped" , peerIps ) ) ;
523526 const a = yield * makeA (
524527 laptop ,
525528 undefined ,
526- Layer . mergeAll (
527- layerTailscalePeers ( [ "100.100.1.2" , "fd7a:115c:a1e0::1" ] ) ,
528- layerRouteHostToLoopback ( "100.100.1.2" ) ,
529- ) ,
529+ Layer . mergeAll ( layerTailscale ( status ) , layerRouteHostToLoopback ( "100.100.1.2" ) ) ,
530530 ) ;
531531
532+ // A stopped Tailscale still lists its peers, but nothing would encrypt
533+ // the traffic to them.
534+ const stopped = yield * a . links
535+ . link ( {
536+ url : `http://100.100.1.2:${ port } ` ,
537+ pairingCode : ( yield * b . auth . issuePairingCredential ( ) ) . credential ,
538+ access : "auto" ,
539+ } )
540+ . pipe ( Effect . flip ) ;
541+ expect ( stopped . message ) . toContain ( "plain http" ) ;
542+ expect ( yield * linkedSessions ( b ) ) . toEqual ( [ ] ) ;
543+ yield * Ref . set ( status , tailscaleStatus ( "Running" , peerIps ) ) ;
544+
532545 // WARP and carrier-grade NAT share the range; an address Tailscale does
533546 // not know would carry the code and token in clear.
534547 const unknown = yield * a . links
@@ -552,6 +565,16 @@ it.effect(
552565 urls : [ `http://100.100.1.2:${ port } ` ] ,
553566 } ) ;
554567 expect ( ( yield * linkedSessions ( b ) ) . length ) . toBe ( 1 ) ;
568+ yield * probe ( a ) ;
569+ expect ( ( yield * Ref . get ( b . bearers ) ) . length ) . toBeGreaterThan ( 0 ) ;
570+
571+ // Its token stays here once Tailscale stops, as it would have at link time.
572+ yield * Ref . set ( status , tailscaleStatus ( "NeedsLogin" , peerIps ) ) ;
573+ yield * Ref . set ( b . bearers , [ ] ) ;
574+ const refused = yield * a . links . resolve ( box . environmentId ) . pipe ( Effect . flip ) ;
575+ expect ( refused . message ) . toContain ( "Box did not answer" ) ;
576+ yield * probe ( a ) . pipe ( Effect . flip ) ;
577+ expect ( yield * Ref . get ( b . bearers ) ) . toEqual ( [ ] ) ;
555578 } ) ,
556579 ) . pipe ( Effect . provide ( NodeHttpServer . layerTest ) ) ,
557580) ;
0 commit comments