@@ -16,6 +16,7 @@ import * as TestConsole from "effect/testing/TestConsole";
1616import { Command } from "effect/cli" ;
1717
1818import { makeCli } from "../binCli.ts" ;
19+ import { CLOUD_CLI_DESIRED_LINK_SECRET } from "../cloud/CliState.ts" ;
1920import {
2021 makePersistedServerRuntimeState ,
2122 persistServerRuntimeState ,
@@ -59,9 +60,12 @@ it.effect("does not install the relay client when the user declines the managed
5960) ;
6061
6162it . layer ( Layer . mergeAll ( NodeServices . layer , NodeCrypto . layer , NetService . layer ) ) (
62- "t3 connect unlink" ,
63+ "t3 connect unlink and status " ,
6364 ( it ) => {
64- const runUnlink = ( recordedPid : number , options ?: { readonly portFreed ?: boolean } ) =>
65+ const runConnect = (
66+ recordedPid : number ,
67+ options ?: { readonly portFreed ?: boolean ; readonly command ?: "unlink" | "status" } ,
68+ ) =>
6569 Effect . gen ( function * ( ) {
6670 const requests : Array < string > = [ ] ;
6771 const warnings : Array < string > = [ ] ;
@@ -96,10 +100,18 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer))
96100 path : `${ baseDir } /userdata/server-runtime.json` ,
97101 state : { ...state , pid : recordedPid } ,
98102 } ) ;
103+ if ( options ?. command === "status" ) {
104+ // Status asks the server for reachability only for a managed link.
105+ yield * fs . makeDirectory ( `${ baseDir } /userdata/secrets` , { recursive : true } ) ;
106+ yield * fs . writeFileString (
107+ `${ baseDir } /userdata/secrets/${ CLOUD_CLI_DESIRED_LINK_SECRET } .bin` ,
108+ "managed" ,
109+ ) ;
110+ }
99111
100112 yield * Command . runWith ( makeCli ( { cloudEnabled : true } ) , { version : "0.0.0" } ) ( [
101113 "connect" ,
102- "unlink" ,
114+ options ?. command ?? "unlink" ,
103115 "--base-dir" ,
104116 baseDir ,
105117 ] ) . pipe (
@@ -128,7 +140,7 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer))
128140
129141 it . effect ( "does not tell the user to restart a server that is gone" , ( ) =>
130142 Effect . gen ( function * ( ) {
131- const { printed, warnings } = yield * runUnlink ( deadPid , { portFreed : true } ) ;
143+ const { printed, warnings } = yield * runConnect ( deadPid , { portFreed : true } ) ;
132144
133145 assert . include ( printed , "T3 Connect is disabled locally." ) ;
134146 assert . notInclude ( warnings . join ( "\n" ) , "Restart that server" ) ;
@@ -137,23 +149,39 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer))
137149
138150 it . effect ( "warns that a server that is gone may have left its connector running" , ( ) =>
139151 Effect . gen ( function * ( ) {
140- const { warnings } = yield * runUnlink ( deadPid , { portFreed : true } ) ;
152+ const { warnings } = yield * runConnect ( deadPid , { portFreed : true } ) ;
141153
142154 assert . include ( warnings . join ( "\n" ) , "its cloudflared process may still be running" ) ;
143155 } ) ,
144156 ) ;
145157
146158 it . effect ( "does not send a token to whatever took over a dead server's port" , ( ) =>
147159 Effect . gen ( function * ( ) {
148- const { requests } = yield * runUnlink ( deadPid ) ;
160+ const { requests } = yield * runConnect ( deadPid ) ;
161+
162+ assert . deepEqual ( requests , [ ] ) ;
163+ } ) ,
164+ ) ;
165+
166+ it . effect ( "status does not send a token to whatever took over a dead server's port" , ( ) =>
167+ Effect . gen ( function * ( ) {
168+ const { requests } = yield * runConnect ( deadPid , { command : "status" } ) ;
149169
150170 assert . deepEqual ( requests , [ ] ) ;
151171 } ) ,
152172 ) ;
153173
174+ it . effect ( "status asks a running server to check reachability" , ( ) =>
175+ Effect . gen ( function * ( ) {
176+ const { requests } = yield * runConnect ( process . pid , { command : "status" } ) ;
177+
178+ assert . deepEqual ( requests , [ "POST /api/connect/reachability" ] ) ;
179+ } ) ,
180+ ) ;
181+
154182 it . effect ( "asks a running server to stop its tunnel" , ( ) =>
155183 Effect . gen ( function * ( ) {
156- const { requests, printed, warnings } = yield * runUnlink ( process . pid ) ;
184+ const { requests, printed, warnings } = yield * runConnect ( process . pid ) ;
157185
158186 assert . deepEqual ( requests , [ "POST /api/connect/unlink" ] ) ;
159187 assert . include ( printed , "T3 Connect is disabled locally." ) ;
0 commit comments