11import * as NodeServices from "@effect/platform-node/NodeServices" ;
22import { describe , expect , it } from "@effect/vitest" ;
3+ import * as Deferred from "effect/Deferred" ;
34import * as Effect from "effect/Effect" ;
5+ import * as Fiber from "effect/Fiber" ;
46import * as FileSystem from "effect/FileSystem" ;
57import * as Layer from "effect/Layer" ;
68import * as Option from "effect/Option" ;
@@ -9,6 +11,7 @@ import * as PlatformError from "effect/PlatformError";
911import * as Schema from "effect/Schema" ;
1012import * as Tracer from "effect/Tracer" ;
1113import * as Stream from "effect/Stream" ;
14+ import * as TestClock from "effect/testing/TestClock" ;
1215import {
1316 HttpClient ,
1417 HttpClientResponse ,
@@ -48,13 +51,15 @@ import {
4851 isSupportedLinkProviderKind ,
4952 linkProofScopes ,
5053 pendingServiceUpdateExists ,
54+ parseManagedEndpointLocalOrigin ,
5155 reconcileDesiredCloudLink ,
5256 reconcileDesiredCloudLinkIfStillDesired ,
5357 recoverManagedCloudTunnel ,
5458 registerManagedCloudTunnelRecovery ,
5559 releaseManagedTunnelOnShutdown ,
5660 startManagedCloudTunnelIfOriginConfirmed ,
5761} from "./http.ts" ;
62+ import { managedTunnelStartupAction } from "./managedTunnelStartup.ts" ;
5863import * as ManagedEndpointRuntime from "./ManagedEndpointRuntime.ts" ;
5964import { traceAuthenticatedRelayRequest , traceRelayRequest } from "./traceRelayRequest.ts" ;
6065
@@ -256,6 +261,39 @@ describe("reconcileDesiredCloudLink", () => {
256261 ) ;
257262} ) ;
258263
264+ describe ( "parseManagedEndpointLocalOrigin" , ( ) => {
265+ it . each ( [
266+ {
267+ input : "http://127.0.0.1:80" ,
268+ httpBaseUrl : "http://127.0.0.1" ,
269+ wsBaseUrl : "ws://127.0.0.1" ,
270+ port : 80 ,
271+ } ,
272+ {
273+ input : "https://127.0.0.1:443" ,
274+ httpBaseUrl : "https://127.0.0.1" ,
275+ wsBaseUrl : "wss://127.0.0.1" ,
276+ port : 443 ,
277+ } ,
278+ ] ) ( "accepts an explicit default port in $input" , ( { input, httpBaseUrl, wsBaseUrl, port } ) => {
279+ expect ( parseManagedEndpointLocalOrigin ( input ) ) . toEqual ( {
280+ httpBaseUrl,
281+ wsBaseUrl,
282+ origin : { localHttpHost : "127.0.0.1" , localHttpPort : port } ,
283+ } ) ;
284+ } ) ;
285+
286+ it . each ( [
287+ "ftp://127.0.0.1:3773" ,
288+ "http://user:[email protected] :3773" , 289+ "http://127.0.0.1:3773/api" ,
290+ "http://127.0.0.1:3773?mode=test" ,
291+ "http://127.0.0.1:3773#fragment" ,
292+ ] ) ( "rejects non-origin URL %s" , ( input ) => {
293+ expect ( ( ) => parseManagedEndpointLocalOrigin ( input ) ) . toThrow ( "Invalid local origin" ) ;
294+ } ) ;
295+ } ) ;
296+
259297describe ( "releaseManagedTunnelOnShutdown" , ( ) => {
260298 const cliToken : CliTokenManager . PersistedToken = {
261299 accessToken : "cli-access-token" ,
@@ -290,7 +328,9 @@ describe("releaseManagedTunnelOnShutdown", () => {
290328 readonly store : ServerSecretStore . ServerSecretStore [ "Service" ] ;
291329 readonly applyConfigCalls : Array < unknown > ;
292330 readonly requests : Array < HttpClientRequest . HttpClientRequest > ;
331+ readonly onRequest ?: ( request : HttpClientRequest . HttpClientRequest ) => Effect . Effect < void > ;
293332 readonly respond ?: ( ) => Response ;
333+ readonly respondEffect ?: Effect . Effect < Response > ;
294334 }
295335
296336 // Writes the launcher's durable state file into this test's baseDir with
@@ -362,11 +402,14 @@ describe("releaseManagedTunnelOnShutdown", () => {
362402 HttpClient . make ( ( request ) =>
363403 Effect . sync ( ( ) => {
364404 harness . requests . push ( request ) ;
365- return HttpClientResponse . fromWeb (
366- request ,
367- ( harness . respond ?? ( ( ) => Response . json ( { ok : true } ) ) ) ( ) ,
368- ) ;
369- } ) ,
405+ } ) . pipe (
406+ Effect . andThen ( harness . onRequest ?.( request ) ?? Effect . void ) ,
407+ Effect . andThen (
408+ harness . respondEffect ??
409+ Effect . sync ( ( ) => ( harness . respond ?? ( ( ) => Response . json ( { ok : true } ) ) ) ( ) ) ,
410+ ) ,
411+ Effect . map ( ( response ) => HttpClientResponse . fromWeb ( request , response ) ) ,
412+ ) ,
370413 ) ,
371414 ) ,
372415 // The release consults the launcher state file under the configured
@@ -775,7 +818,7 @@ describe("releaseManagedTunnelOnShutdown", () => {
775818 } ,
776819 ) ;
777820
778- it . effect ( "does not register recovery without a recorded tunnel ID" , ( ) => {
821+ it . effect ( "requests startup recovery for a legacy config without a recorded tunnel ID" , ( ) => {
779822 const { store } = makeMemorySecretStore ( [
780823 [
781824 CLOUD_ENDPOINT_RUNTIME_CONFIG ,
@@ -789,8 +832,19 @@ describe("releaseManagedTunnelOnShutdown", () => {
789832 const requests : Array < HttpClientRequest . HttpClientRequest > = [ ] ;
790833
791834 return Effect . gen ( function * ( ) {
792- expect ( yield * registerManagedCloudTunnelRecovery ( "http://127.0.0.1:3773" ) ) . toEqual ( {
793- status : "not_linked" ,
835+ const registration = yield * registerManagedCloudTunnelRecovery ( "http://127.0.0.1:3773" ) ;
836+ expect ( registration ) . toEqual ( {
837+ status : "recovery_required" ,
838+ config : { providerKind : "cloudflare_tunnel" , connectorToken : "token" } ,
839+ } ) ;
840+ expect (
841+ managedTunnelStartupAction ( {
842+ wantsCliLink : false ,
843+ registration,
844+ } ) ,
845+ ) . toEqual ( {
846+ action : "request_recovery" ,
847+ config : { providerKind : "cloudflare_tunnel" , connectorToken : "token" } ,
794848 } ) ;
795849 expect ( requests ) . toEqual ( [ ] ) ;
796850 expect ( applyConfigCalls ) . toEqual ( [ ] ) ;
@@ -844,6 +898,58 @@ describe("releaseManagedTunnelOnShutdown", () => {
844898 ) ;
845899 } ) ;
846900
901+ it . effect ( "allows managed tunnel provisioning to take longer than ten seconds" , ( ) =>
902+ Effect . gen ( function * ( ) {
903+ const oldConfig =
904+ '{"providerKind":"cloudflare_tunnel","connectorToken":"old-token","tunnelId":"old-tunnel"}' ;
905+ const nextConfig = {
906+ providerKind : "cloudflare_tunnel" as const ,
907+ connectorToken : "new-token" ,
908+ tunnelId : "new-tunnel" ,
909+ } ;
910+ const { store } = makeMemorySecretStore ( [
911+ [ CLOUD_ENDPOINT_RUNTIME_CONFIG , oldConfig ] ,
912+ [ RELAY_URL_SECRET , "https://relay.example.test" ] ,
913+ [ CLOUD_LINKED_USER_ID , "user-123" ] ,
914+ [ RELAY_ENVIRONMENT_CREDENTIAL_SECRET , "environment-credential" ] ,
915+ ] ) ;
916+ const applyConfigCalls : Array < unknown > = [ ] ;
917+ const requests : Array < HttpClientRequest . HttpClientRequest > = [ ] ;
918+ const requestStarted = yield * Deferred . make < void > ( ) ;
919+ const response = yield * Deferred . make < Response > ( ) ;
920+ const recovery = yield * recoverManagedCloudTunnel ( "http://127.0.0.1:3773" ) . pipe (
921+ provideReleaseHarness ( {
922+ store,
923+ applyConfigCalls,
924+ requests,
925+ onRequest : ( ) => Deferred . succeed ( requestStarted , undefined ) ,
926+ respondEffect : Deferred . await ( response ) ,
927+ } ) ,
928+ Effect . forkChild ( { startImmediately : true } ) ,
929+ ) ;
930+
931+ yield * Deferred . await ( requestStarted ) ;
932+ expect ( requests ) . toHaveLength ( 1 ) ;
933+ yield * TestClock . adjust ( "11 seconds" ) ;
934+ yield * Effect . yieldNow ;
935+ yield * Deferred . succeed (
936+ response ,
937+ Response . json ( {
938+ endpoint : {
939+ httpBaseUrl : "https://environment.example.test/" ,
940+ wsBaseUrl : "wss://environment.example.test/ws" ,
941+ providerKind : "cloudflare_tunnel" ,
942+ } ,
943+ endpointRuntime : nextConfig ,
944+ } ) ,
945+ ) ;
946+
947+ expect ( yield * Fiber . join ( recovery ) ) . toBe ( true ) ;
948+ expect ( requests ) . toHaveLength ( 1 ) ;
949+ expect ( applyConfigCalls ) . toEqual ( [ nextConfig ] ) ;
950+ } ) ,
951+ ) ;
952+
847953 it . effect ( "does not recover an environment without a managed tunnel credential" , ( ) => {
848954 const { store } = makeMemorySecretStore ( [
849955 [ CLOUD_ENDPOINT_RUNTIME_CONFIG , "old-config" ] ,
0 commit comments