|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { spawnSync } from "node:child_process"; |
| 3 | +import test from "node:test"; |
| 4 | + |
| 5 | +const CONFIG = new URL("../src/config.mjs", import.meta.url); |
| 6 | + |
| 7 | +function loadConfig(env) { |
| 8 | + return spawnSync(process.execPath, [ |
| 9 | + "--input-type=module", |
| 10 | + "-e", |
| 11 | + `import(${JSON.stringify(CONFIG.href)}) |
| 12 | + .then(({ config }) => { |
| 13 | + console.log(JSON.stringify({ port: config.port, origin: config.origin, rpID: config.rpID })); |
| 14 | + }) |
| 15 | + .catch((err) => { |
| 16 | + console.error(err.message); |
| 17 | + process.exit(1); |
| 18 | + });`, |
| 19 | + ], { |
| 20 | + env: { |
| 21 | + ...process.env, |
| 22 | + PUBLIC_ORIGIN: "", |
| 23 | + PORT: "", |
| 24 | + ...env, |
| 25 | + }, |
| 26 | + encoding: "utf8", |
| 27 | + }); |
| 28 | +} |
| 29 | + |
| 30 | +test("config trims PORT before building the fallback origin", () => { |
| 31 | + const res = loadConfig({ PORT: "3000 " }); |
| 32 | + assert.equal(res.status, 0, res.stderr); |
| 33 | + assert.deepEqual(JSON.parse(res.stdout), { |
| 34 | + port: 3000, |
| 35 | + origin: "http://localhost:3000", |
| 36 | + rpID: "localhost", |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +test("config rejects a non-integer PORT before building the fallback origin", () => { |
| 41 | + const res = loadConfig({ PORT: "abc" }); |
| 42 | + assert.equal(res.status, 1); |
| 43 | + assert.match(res.stderr, /PORT must be a decimal integer/); |
| 44 | +}); |
| 45 | + |
| 46 | +test("config rejects a PORT outside the TCP range", () => { |
| 47 | + const res = loadConfig({ PORT: "65536" }); |
| 48 | + assert.equal(res.status, 1); |
| 49 | + assert.match(res.stderr, /PORT must be between 0 and 65535/); |
| 50 | +}); |
0 commit comments