okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
64b48c0d0
main
1import test from 'node:test' ; 2import assert from 'node:assert/strict' ; 3import net from 'node:net' ; 4import { spawn } from 'node:child_process' ; 5import { mkdtempSync , realpathSync , rmSync } from 'node:fs' ; 6import { tmpdir } from 'node:os' ; 7import { join } from 'node:path' ; 8import { fileURLToPath } from 'node:url' ; 9import { findFreePort , listenProblem } from '../server/ports.mjs' ; 10 11const cwd = fileURLToPath ( new URL ( '..' , import . meta. url )); 12const hold = port => new Promise ( resolve => { const s = net . createServer (); s . listen ( port , '127.0.0.1' , () => resolve ( s )); }); 13 14test ( 'a taken preferred port gives the next free one, never the taken one' , async () => { 15const first = await hold ( 0 ); const taken = first . address (). port ; 16try { 17const got = await findFreePort ( taken , '127.0.0.1' ); 18assert . notEqual ( got , taken ); 19assert . ok ( got > 0 ); 20assert . notEqual ( await findFreePort ( got , '127.0.0.1' , { skip :[ got ] }), got ); 21} finally { first . close (); } 22}); 23 24test ( 'the plain sentences name the cause and the one next step' , () => { 25const use = listenProblem ({ code :'EADDRINUSE' }, 8791 ); 26assert . match ( use , / Port 8791 is already in use / ); assert . match ( use , / STRATA_PORT / ); 27const denied = listenProblem ({ code :'EACCES' }, 8792 , '127.0.0.1' , 'win32' ); 28assert . match ( denied , / Hyper-V, WSL and Docker / ); assert . match ( denied , / excludedportrange / ); assert . match ( denied , / STRATA_PORT / ); 29assert . doesNotMatch ( denied , / EACCES | permission denied / i ); 30}); 31 32test ( 'F12: the permission-denied sentence follows the platform, Windows keeps its own' , () => { 33const win = listenProblem ({ code :'EACCES' }, 8792 , '127.0.0.1' , 'win32' ); 34assert . match ( win , / ^Windows or a firewall does not let Strata use port 8792\. Hyper-V, WSL and Docker / ); 35for ( const platform of [ 'linux' , 'darwin' ]) { 36const other = listenProblem ({ code :'EACCES' }, 80 , '127.0.0.1' , platform ); 37assert . equal ( other , 'This system does not let Strata use port 80. On Linux and macOS, ports below 1024 need administrator rights. Start Strata with another port (STRATA_PORT), for example 8801.' ); 38assert . doesNotMatch ( other , / Windows | Hyper-V | netsh / ); 39} 40assert . equal ( listenProblem ({ code :'EACCES' }, 80 ), listenProblem ({ code :'EACCES' }, 80 , '127.0.0.1' , process . platform )); 41}); 42 43test ( 'the host on a taken port prints the plain sentence and exits, with no stack trace' , async t => { 44const home = realpathSync ( mkdtempSync ( join ( tmpdir (), 'strata-port-' ))); t . after (() => rmSync ( home , { recursive :true , force :true })); 45const blocker = await hold ( 0 ); t . after (() => blocker . close ()); 46const port = blocker . address (). port ; 47const child = spawn ( process . execPath , [ 'server/index.mjs' ], { cwd, env :{ ...process . env , STRATA_HOME :home , STRATA_PORT :String ( port ), STRATA_MODELS_DIR :join ( home , 'm' ) } }); 48let err = '' ; child . stderr . on ( 'data' , d => { err += d ; }); 49const code = await new Promise ( resolve => { const timer = setTimeout (() => { child . kill (); resolve ( 'timeout' ); }, 30000 ); child . once ( 'exit' , c => { clearTimeout ( timer ); resolve ( c ); }); }); 50assert . equal ( code , 1 , err ); 51assert . match ( err , new RegExp ( `Port ${ port } is already in use` )); 52assert . doesNotMatch ( err , / at Server\.setupListenHandle / ); 53});