@@ -2,7 +2,12 @@ import test from "node:test";
22import assert from "node:assert/strict" ;
33import { spawn } from "node:child_process" ;
44import net from "node:net" ;
5- import { generateKeyPairSync , createHash } from "node:crypto" ;
5+ import { generateKeyPairSync } from "node:crypto" ;
6+ import { dirname , join } from "node:path" ;
7+ import { fileURLToPath } from "node:url" ;
8+
9+ const __dirname = dirname ( fileURLToPath ( import . meta. url ) ) ;
10+ const SERVER_ENTRY = join ( __dirname , ".." , ".." , "server.mjs" ) ;
611
712function freePort ( ) {
813 return new Promise ( ( resolve , reject ) => {
@@ -24,27 +29,50 @@ function makeKeys() {
2429 return {
2530 privatePemB64,
2631 publicRaw32B64 : raw32 . toString ( "base64" ) ,
27- kid : createHash ( "sha256" ) . update ( raw32 ) . digest ( "base64url" ) . slice ( 0 , 16 ) ,
2832 } ;
2933}
3034
3135async function startServer ( extraEnv ) {
3236 const port = await freePort ( ) ;
33- const proc = spawn ( process . execPath , [ "server.mjs" ] , {
34- cwd : process . cwd ( ) ,
37+ const proc = spawn ( process . execPath , [ SERVER_ENTRY ] , {
3538 env : { ...process . env , HOST : "127.0.0.1" , PORT : String ( port ) , ...extraEnv } ,
3639 stdio : [ "ignore" , "pipe" , "pipe" ] ,
3740 } ) ;
41+ let stdout = "" ;
42+ let stderr = "" ;
43+ proc . stdout . on ( "data" , ( chunk ) => {
44+ stdout += chunk . toString ( "utf8" ) ;
45+ } ) ;
46+ proc . stderr . on ( "data" , ( chunk ) => {
47+ stderr += chunk . toString ( "utf8" ) ;
48+ } ) ;
3849
3950 const base = `http://127.0.0.1:${ port } ` ;
4051 for ( let i = 0 ; i < 80 ; i ++ ) {
52+ if ( proc . exitCode !== null ) {
53+ throw new Error (
54+ [
55+ `server exited before boot (exitCode=${ proc . exitCode } )` ,
56+ `entry=${ SERVER_ENTRY } ` ,
57+ `stdout:\n${ stdout || "<empty>" } ` ,
58+ `stderr:\n${ stderr || "<empty>" } ` ,
59+ ] . join ( "\n" )
60+ ) ;
61+ }
4162 try {
4263 const r = await fetch ( `${ base } /health` ) ;
4364 if ( r . ok ) return { proc, base } ;
4465 } catch { }
4566 await new Promise ( ( r ) => setTimeout ( r , 100 ) ) ;
4667 }
47- throw new Error ( "server did not boot" ) ;
68+ throw new Error (
69+ [
70+ `server did not boot after retries` ,
71+ `entry=${ SERVER_ENTRY } ` ,
72+ `stdout:\n${ stdout || "<empty>" } ` ,
73+ `stderr:\n${ stderr || "<empty>" } ` ,
74+ ] . join ( "\n" )
75+ ) ;
4876}
4977
5078async function stop ( proc ) {
0 commit comments