okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-0213-qafix into f-0213-int64b48c0d0

main
3.1 KiB53 linesraw
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 () => {
15  const first = await hold(0); const taken = first.address().port;
16  try {
17    const got = await findFreePort(taken, '127.0.0.1');
18    assert.notEqual(got, taken);
19    assert.ok(got > 0);
20    assert.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', () => {
25  const use = listenProblem({ code: 'EADDRINUSE' }, 8791);
26  assert.match(use, /Port 8791 is already in use/); assert.match(use, /STRATA_PORT/);
27  const denied = listenProblem({ code: 'EACCES' }, 8792, '127.0.0.1', 'win32');
28  assert.match(denied, /Hyper-V, WSL and Docker/); assert.match(denied, /excludedportrange/); assert.match(denied, /STRATA_PORT/);
29  assert.doesNotMatch(denied, /EACCES|permission denied/i);
30});
31
32test('F12: the permission-denied sentence follows the platform, Windows keeps its own', () => {
33  const win = listenProblem({ code: 'EACCES' }, 8792, '127.0.0.1', 'win32');
34  assert.match(win, /^Windows or a firewall does not let Strata use port 8792\. Hyper-V, WSL and Docker/);
35  for (const platform of ['linux', 'darwin']) {
36    const other = listenProblem({ code: 'EACCES' }, 80, '127.0.0.1', platform);
37    assert.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.');
38    assert.doesNotMatch(other, /Windows|Hyper-V|netsh/);
39  }
40  assert.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 => {
44  const home = realpathSync(mkdtempSync(join(tmpdir(), 'strata-port-'))); t.after(() => rmSync(home, { recursive: true, force: true }));
45  const blocker = await hold(0); t.after(() => blocker.close());
46  const port = blocker.address().port;
47  const child = spawn(process.execPath, ['server/index.mjs'], { cwd, env: { ...process.env, STRATA_HOME: home, STRATA_PORT: String(port), STRATA_MODELS_DIR: join(home, 'm') } });
48  let err = ''; child.stderr.on('data', d => { err += d; });
49  const code = await new Promise(resolve => { const timer = setTimeout(() => { child.kill(); resolve('timeout'); }, 30000); child.once('exit', c => { clearTimeout(timer); resolve(c); }); });
50  assert.equal(code, 1, err);
51  assert.match(err, new RegExp(`Port ${port} is already in use`));
52  assert.doesNotMatch(err, /at Server\.setupListenHandle/);
53});