@@ -245,6 +245,9 @@ describe("ssh tunnel scripts", () => {
245245 '[ -n "$REMOTE_PID" ] && [ -n "$REMOTE_PORT" ] && kill -0 "$REMOTE_PID" 2>/dev/null' ,
246246 ) ;
247247 assert . notInclude ( launch , "RUNNER_CHANGED" ) ;
248+ assert . include ( launch , 'STARTED_RUNNER_FILE="$STATE_DIR/run-t3-started.sh"' ) ;
249+ assert . include ( launch , 'if [ -f "$RUNNER_FILE" ] && [ ! -f "$STARTED_RUNNER_FILE" ]; then' ) ;
250+ assert . include ( launch , 'cp "$RUNNER_FILE" "$STARTED_RUNNER_FILE"' ) ;
248251 assert . include ( launch , "ensure_remote_node_path()" ) ;
249252 assert . include ( launch , "if ! ensure_remote_node_path; then" ) ;
250253 assert . include ( devLaunch , `T3_NODE_ENGINE_RANGE='${ TEST_NODE_ENGINE_RANGE } '` ) ;
@@ -263,6 +266,14 @@ describe("ssh tunnel scripts", () => {
263266 SshTunnel . buildRemotePairingScript ( stateKey , ARCHIVE ) ,
264267 '"$RUNNER_FILE" auth pairing create --base-dir "$PAIRING_BASE_DIR" --json' ,
265268 ) ;
269+ assert . include (
270+ SshTunnel . buildRemotePairingScript ( stateKey , ARCHIVE ) ,
271+ '"$STARTED_RUNNER_FILE" auth pairing create --base-dir "$PAIRING_BASE_DIR" --json' ,
272+ ) ;
273+ assert . include (
274+ SshTunnel . buildRemotePairingScript ( stateKey , ARCHIVE ) ,
275+ 'kill -0 "$REMOTE_PID" 2>/dev/null; then' ,
276+ ) ;
266277 assert . include (
267278 SshTunnel . buildRemotePairingScript ( stateKey , ARCHIVE ) ,
268279 'PAIRING_BASE_DIR="$DEFAULT_SERVER_HOME"' ,
@@ -929,6 +940,82 @@ exec "$REAL_NODE" "$@"
929940 if ( ready ) assert . deepEqual ( JSON . parse ( stdout ) , { remotePort : 3773 , serverKind : "managed" } ) ;
930941 else assert . include ( stderr , "server was left running" ) ;
931942 assert . equal ( yield * fs . readFileString ( `${ state } /pid` ) , String ( server . pid ) ) ;
943+ // The pairing handshake needs the runtime that owns the live server.
944+ assert . equal ( yield * fs . readFileString ( `${ state } /run-t3-started.sh` ) , "old runner" ) ;
932945 } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
933946 ) ;
934947} ) ;
948+
949+ describe . skipIf ( HostProcess . Platform . defaultValue ( ) === "win32" ) ( "remote pairing script" , ( ) => {
950+ it . effect . each ( [ { live : true } , { live : false } ] as const ) (
951+ "uses the started runner while a managed server is live: %j" ,
952+ ( { live } ) =>
953+ Effect . gen ( function * ( ) {
954+ const fs = yield * FileSystem . FileSystem ;
955+ const spawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
956+ const root = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-ssh-pairing-" } ) ;
957+ const state = `${ root } /.t3/ssh-launch/test` ;
958+ yield * fs . makeDirectory ( state , { recursive : true } ) ;
959+ const startedRunner = `${ state } /run-t3-started.sh` ;
960+ yield * fs . writeFileString (
961+ startedRunner ,
962+ `#!/bin/sh
963+ printf '{"credential":"started"}\\n'
964+ ` ,
965+ ) ;
966+ yield * fs . chmod ( startedRunner , 0o700 ) ;
967+ const nodeScript = `${ root } /runner.mjs` ;
968+ yield * fs . writeFileString (
969+ nodeScript ,
970+ `console.log(JSON.stringify({ credential: "new" }));\n` ,
971+ ) ;
972+ if ( live ) {
973+ const server = yield * spawner . spawn ( ChildProcess . make ( "sleep" , [ "120" ] ) ) ;
974+ yield * fs . writeFileString ( `${ state } /pid` , String ( server . pid ) ) ;
975+ yield * fs . writeFileString ( `${ state } /managed` , "managed" ) ;
976+ }
977+ const script = `${ root } /pair.sh` ;
978+ yield * fs . writeFileString (
979+ script ,
980+ SshTunnel . buildRemotePairingScript ( "test" , {
981+ nodeScriptPath : nodeScript ,
982+ nodeEngineRange : ">=20" ,
983+ } ) ,
984+ ) ;
985+ const child = yield * spawner . spawn (
986+ ChildProcess . make ( "sh" , [ script ] , {
987+ env : {
988+ HOME : root ,
989+ PATH : `${ process . execPath . slice ( 0 , process . execPath . lastIndexOf ( "/" ) ) } :${ process . env . PATH ?? "" } ` ,
990+ } ,
991+ extendEnv : false ,
992+ } ) ,
993+ ) ;
994+ const [ stdout , stderr , code ] = yield * Effect . all (
995+ [
996+ child . stdout . pipe (
997+ Stream . decodeText ( ) ,
998+ Stream . runFold (
999+ ( ) => "" ,
1000+ ( a , b ) => a + b ,
1001+ ) ,
1002+ ) ,
1003+ child . stderr . pipe (
1004+ Stream . decodeText ( ) ,
1005+ Stream . runFold (
1006+ ( ) => "" ,
1007+ ( a , b ) => a + b ,
1008+ ) ,
1009+ ) ,
1010+ child . exitCode ,
1011+ ] ,
1012+ { concurrency : "unbounded" } ,
1013+ ) ;
1014+ assert . equal ( Number ( code ) , 0 , stderr ) ;
1015+ // A live managed server must never run the newer runner: its
1016+ // migrations would run underneath the older running server.
1017+ assert . equal ( stdout . trim ( ) , live ? '{"credential":"started"}' : '{"credential":"new"}' ) ;
1018+ assert . equal ( yield * fs . exists ( `${ state } /run-t3.sh` ) , ! live ) ;
1019+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
1020+ ) ;
1021+ } ) ;
0 commit comments