@@ -36,6 +36,7 @@ import {
3636 type RelayLinkProofRequest ,
3737} from "@t3tools/contracts/relay" ;
3838import {
39+ CLOUD_ENDPOINT_CONFIRMED_ORIGIN ,
3940 CLOUD_ENDPOINT_RUNTIME_CONFIG ,
4041 CLOUD_LINKED_USER_ID ,
4142 decodeRuntimeConfig ,
@@ -48,9 +49,11 @@ import {
4849 linkProofScopes ,
4950 pendingServiceUpdateExists ,
5051 reconcileDesiredCloudLink ,
52+ reconcileDesiredCloudLinkIfStillDesired ,
5153 recoverManagedCloudTunnel ,
5254 registerManagedCloudTunnelRecovery ,
5355 releaseManagedTunnelOnShutdown ,
56+ startManagedCloudTunnelIfOriginConfirmed ,
5457} from "./http.ts" ;
5558import * as ManagedEndpointRuntime from "./ManagedEndpointRuntime.ts" ;
5659import { traceAuthenticatedRelayRequest , traceRelayRequest } from "./traceRelayRequest.ts" ;
@@ -379,10 +382,27 @@ describe("releaseManagedTunnelOnShutdown", () => {
379382 // The persisted state of a CLI-managed link whose tunnel is releasable.
380383 const managedLinkSecrets = [
381384 [ CLOUD_ENDPOINT_RUNTIME_CONFIG , "runtime-config" ] ,
385+ [ CLOUD_ENDPOINT_CONFIRMED_ORIGIN , "confirmed-origin" ] ,
382386 [ RELAY_URL_SECRET , "https://relay.example.test" ] ,
383387 [ CLOUD_CLI_DESIRED_LINK_SECRET , "managed" ] ,
384388 ] as const ;
385389
390+ it . effect ( "does not recreate a link that was unlinked while startup registration retried" , ( ) => {
391+ const { store, values } = makeMemorySecretStore ( managedLinkSecrets ) ;
392+ const applyConfigCalls : Array < unknown > = [ ] ;
393+ const requests : Array < HttpClientRequest . HttpClientRequest > = [ ] ;
394+
395+ return Effect . gen ( function * ( ) {
396+ // Registration started while this marker existed. Unlink removes it
397+ // before startup receives the relay's final not_linked response.
398+ values . delete ( CLOUD_CLI_DESIRED_LINK_SECRET ) ;
399+
400+ expect ( yield * reconcileDesiredCloudLinkIfStillDesired ( "http://127.0.0.1:3773" ) ) . toBeNull ( ) ;
401+ expect ( requests ) . toEqual ( [ ] ) ;
402+ expect ( applyConfigCalls ) . toEqual ( [ ] ) ;
403+ } ) . pipe ( provideReleaseHarness ( { store, applyConfigCalls, requests } ) ) ;
404+ } ) ;
405+
386406 it . effect ( "stops the connector, releases the relay tunnel, and drops the dead token" , ( ) => {
387407 const { store, values } = makeMemorySecretStore ( managedLinkSecrets ) ;
388408 const applyConfigCalls : Array < unknown > = [ ] ;
@@ -401,6 +421,7 @@ describe("releaseManagedTunnelOnShutdown", () => {
401421 ) ;
402422 expect ( request . headers . authorization ) . toBe ( "Bearer cli-access-token" ) ;
403423 expect ( values . has ( CLOUD_ENDPOINT_RUNTIME_CONFIG ) ) . toBe ( false ) ;
424+ expect ( values . has ( CLOUD_ENDPOINT_CONFIRMED_ORIGIN ) ) . toBe ( false ) ;
404425 } ) . pipe ( provideReleaseHarness ( { store, applyConfigCalls, requests } ) ) ;
405426 } ) ;
406427
@@ -611,7 +632,7 @@ describe("releaseManagedTunnelOnShutdown", () => {
611632 ) ;
612633 } ) ;
613634
614- it . effect ( "registers an existing tunnel without provisioning or restarting it " , ( ) => {
635+ it . effect ( "registers an existing tunnel and starts the confirmed connector " , ( ) => {
615636 const { store } = makeMemorySecretStore ( [
616637 [
617638 CLOUD_ENDPOINT_RUNTIME_CONFIG ,
@@ -625,7 +646,9 @@ describe("releaseManagedTunnelOnShutdown", () => {
625646 const requests : Array < HttpClientRequest . HttpClientRequest > = [ ] ;
626647
627648 return Effect . gen ( function * ( ) {
628- expect ( yield * registerManagedCloudTunnelRecovery ( ) ) . toBe ( true ) ;
649+ expect ( yield * registerManagedCloudTunnelRecovery ( "http://127.0.0.1:3773" ) ) . toMatchObject ( {
650+ status : "ready" ,
651+ } ) ;
629652 expect ( requests ) . toHaveLength ( 1 ) ;
630653 expect ( requests [ 0 ] ?. method ) . toBe ( "POST" ) ;
631654 expect ( requests [ 0 ] ?. url ) . toBe (
@@ -640,12 +663,118 @@ describe("releaseManagedTunnelOnShutdown", () => {
640663 ) . toMatchObject ( {
641664 cloudUserId : "user-123" ,
642665 tunnelId : "existing-tunnel" ,
666+ origin : { localHttpHost : "127.0.0.1" , localHttpPort : 3773 } ,
643667 } ) ;
644668 }
669+ expect ( applyConfigCalls ) . toHaveLength ( 1 ) ;
670+ } ) . pipe (
671+ provideReleaseHarness ( {
672+ store,
673+ applyConfigCalls,
674+ requests,
675+ respond : ( ) => Response . json ( { status : "ready" } ) ,
676+ } ) ,
677+ ) ;
678+ } ) ;
679+
680+ it . effect (
681+ "starts a connector with a marker for the current origin without contacting relay" ,
682+ ( ) => {
683+ const configJson =
684+ '{"providerKind":"cloudflare_tunnel","connectorToken":"existing-token","tunnelId":"existing-tunnel"}' ;
685+ const config = {
686+ providerKind : "cloudflare_tunnel" as const ,
687+ connectorToken : "existing-token" ,
688+ tunnelId : "existing-tunnel" ,
689+ } ;
690+ const { store } = makeMemorySecretStore ( [
691+ [ CLOUD_ENDPOINT_RUNTIME_CONFIG , configJson ] ,
692+ [
693+ CLOUD_ENDPOINT_CONFIRMED_ORIGIN ,
694+ `{"config":${ configJson } ,"origin":{"localHttpHost":"127.0.0.1","localHttpPort":3773}}` ,
695+ ] ,
696+ ] ) ;
697+ const applyConfigCalls : Array < unknown > = [ ] ;
698+ const requests : Array < HttpClientRequest . HttpClientRequest > = [ ] ;
699+
700+ return Effect . gen ( function * ( ) {
701+ expect ( yield * startManagedCloudTunnelIfOriginConfirmed ( "http://127.0.0.1:3773" ) ) . toBe ( true ) ;
702+ expect ( applyConfigCalls ) . toEqual ( [ config ] ) ;
703+ expect ( requests ) . toEqual ( [ ] ) ;
704+ } ) . pipe ( provideReleaseHarness ( { store, applyConfigCalls, requests } ) ) ;
705+ } ,
706+ ) ;
707+
708+ it . effect . each ( [
709+ { name : "missing" , marker : undefined , origin : "http://127.0.0.1:3773" } ,
710+ {
711+ name : "stale" ,
712+ marker :
713+ '{"config":{"providerKind":"cloudflare_tunnel","connectorToken":"existing-token","tunnelId":"existing-tunnel"},"origin":{"localHttpHost":"127.0.0.1","localHttpPort":3773}}' ,
714+ origin : "http://127.0.0.1:4884" ,
715+ } ,
716+ ] ) ( "does not start a connector with a $name origin marker" , ( { marker, origin } ) => {
717+ const entries : Array < readonly [ string , string ] > = [
718+ [
719+ CLOUD_ENDPOINT_RUNTIME_CONFIG ,
720+ '{"providerKind":"cloudflare_tunnel","connectorToken":"existing-token","tunnelId":"existing-tunnel"}' ,
721+ ] ,
722+ ] ;
723+ if ( marker !== undefined ) entries . push ( [ CLOUD_ENDPOINT_CONFIRMED_ORIGIN , marker ] ) ;
724+ const { store } = makeMemorySecretStore ( entries ) ;
725+ const applyConfigCalls : Array < unknown > = [ ] ;
726+ const requests : Array < HttpClientRequest . HttpClientRequest > = [ ] ;
727+
728+ return Effect . gen ( function * ( ) {
729+ expect ( yield * startManagedCloudTunnelIfOriginConfirmed ( origin ) ) . toBe ( false ) ;
645730 expect ( applyConfigCalls ) . toEqual ( [ ] ) ;
731+ expect ( requests ) . toEqual ( [ ] ) ;
646732 } ) . pipe ( provideReleaseHarness ( { store, applyConfigCalls, requests } ) ) ;
647733 } ) ;
648734
735+ it . effect . each ( [ "replaced" , "removed" ] as const ) (
736+ "does not activate a tunnel when its runtime config is %s during registration" ,
737+ ( mutation ) => {
738+ const originalConfig =
739+ '{"providerKind":"cloudflare_tunnel","connectorToken":"existing-token","tunnelId":"existing-tunnel"}' ;
740+ const { store, values } = makeMemorySecretStore ( [
741+ [ CLOUD_ENDPOINT_RUNTIME_CONFIG , originalConfig ] ,
742+ [ RELAY_URL_SECRET , "https://relay.example.test" ] ,
743+ [ CLOUD_LINKED_USER_ID , "user-123" ] ,
744+ [ RELAY_ENVIRONMENT_CREDENTIAL_SECRET , "environment-credential" ] ,
745+ ] ) ;
746+ const applyConfigCalls : Array < unknown > = [ ] ;
747+ const requests : Array < HttpClientRequest . HttpClientRequest > = [ ] ;
748+
749+ return Effect . gen ( function * ( ) {
750+ expect ( yield * registerManagedCloudTunnelRecovery ( "http://127.0.0.1:3773" ) ) . toEqual ( {
751+ status : "superseded" ,
752+ } ) ;
753+ expect ( applyConfigCalls ) . toEqual ( [ ] ) ;
754+ expect ( values . has ( CLOUD_ENDPOINT_CONFIRMED_ORIGIN ) ) . toBe ( false ) ;
755+ } ) . pipe (
756+ provideReleaseHarness ( {
757+ store,
758+ applyConfigCalls,
759+ requests,
760+ respond : ( ) => {
761+ if ( mutation === "replaced" ) {
762+ values . set (
763+ CLOUD_ENDPOINT_RUNTIME_CONFIG ,
764+ new TextEncoder ( ) . encode (
765+ '{"providerKind":"cloudflare_tunnel","connectorToken":"fresh-token","tunnelId":"fresh-tunnel"}' ,
766+ ) ,
767+ ) ;
768+ } else {
769+ values . delete ( CLOUD_ENDPOINT_RUNTIME_CONFIG ) ;
770+ }
771+ return Response . json ( { status : "ready" } ) ;
772+ } ,
773+ } ) ,
774+ ) ;
775+ } ,
776+ ) ;
777+
649778 it . effect ( "does not register recovery without a recorded tunnel ID" , ( ) => {
650779 const { store } = makeMemorySecretStore ( [
651780 [
@@ -660,7 +789,9 @@ describe("releaseManagedTunnelOnShutdown", () => {
660789 const requests : Array < HttpClientRequest . HttpClientRequest > = [ ] ;
661790
662791 return Effect . gen ( function * ( ) {
663- expect ( yield * registerManagedCloudTunnelRecovery ( ) ) . toBe ( false ) ;
792+ expect ( yield * registerManagedCloudTunnelRecovery ( "http://127.0.0.1:3773" ) ) . toEqual ( {
793+ status : "not_linked" ,
794+ } ) ;
664795 expect ( requests ) . toEqual ( [ ] ) ;
665796 expect ( applyConfigCalls ) . toEqual ( [ ] ) ;
666797 } ) . pipe ( provideReleaseHarness ( { store, applyConfigCalls, requests } ) ) ;
0 commit comments