// Tests for scripts/check-ports.sh: a real listener on a random port is a // conflict, a free port passes, a port held by the same compose project // passes, and compose files are parsed without a Docker daemon. const { test, before, after } = require('node:test') const assert = require('node:assert/strict') const net = require('node:net') const fs = require('node:fs') const os = require('node:os') const path = require('node:path') const { spawn } = require('node:child_process') const SCRIPT = path.join(__dirname, 'check-ports.sh') let server let busyPort let freePort async function randomPort() { const s = net.createServer() await new Promise((resolve) => s.listen(0, '127.0.0.1', resolve)) const { port } = s.address() await new Promise((resolve) => s.close(resolve)) return port } before(async () => { server = net.createServer() await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)) busyPort = server.address().port freePort = await randomPort() }) after(() => server.close()) function run(args, env = {}) { return new Promise((resolve) => { // No Docker in the test: the publisher lookup is stubbed per test. const child = spawn('bash', [SCRIPT, ...args], { env: { ...process.env, CHECK_PORTS_DOCKER_PS: 'true', ...env }, }) let out = '' child.stdout.on('data', (d) => (out += d)) child.stderr.on('data', (d) => (out += d)) child.on('close', (code) => resolve({ code, out })) }) } test('passes for a free port', async () => { const { code, out } = await run([String(freePort)]) assert.equal(code, 0, out) assert.match(out, new RegExp(`${freePort}: szabad`)) }) test('fails fast for a port held by another process', async () => { const { code, out } = await run([String(busyPort)]) assert.equal(code, 1, out) assert.match(out, new RegExp(`${busyPort}: foglalt`)) assert.match(out, /portregiszterbe/) }) test('accepts a port held by the same compose project', async () => { const stub = `printf 'mozdit-app-staging\\twebsitedev\\n' #` const { code, out } = await run(['--project', 'websitedev', String(busyPort)], { CHECK_PORTS_DOCKER_PS: stub }) assert.equal(code, 0, out) assert.match(out, /websitedev projekt használja/) }) test('rejects a port held by a container of another project', async () => { const stub = `printf 'peakxi-dev-postgres-1\\tpeakxi-dev\\n' #` const { code, out } = await run(['--project', 'websitedev', String(busyPort)], { CHECK_PORTS_DOCKER_PS: stub }) assert.equal(code, 1, out) assert.match(out, /peakxi-dev-postgres-1/) }) test('reads the published ports of a compose file without Docker', async () => { const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'check-ports-')) const file = path.join(dir, 'docker-compose.yml') fs.writeFileSync( file, [ 'services:', ' app:', ' ports:', ` - "127.0.0.1:${freePort}:3000" # comment`, ' db:', ' ports:', ` - "\${DB_PORT:-${busyPort}}:5432"`, ' environment:', ' - POSTGRES_DB=mozdit_web', ].join('\n'), ) // PATH without docker forces the plain parser. const { code, out } = await run(['--compose', file], { PATH: '/usr/bin:/bin:/usr/sbin:/sbin' }) assert.equal(code, 1, out) assert.match(out, new RegExp(`${freePort}: szabad`)) assert.match(out, new RegExp(`${busyPort}: foglalt`)) assert.doesNotMatch(out, /POSTGRES/) }) test('rejects a port held by another service of the same project (compose mode)', async () => { const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'check-ports-')) const file = path.join(dir, 'docker-compose.dev.yml') fs.writeFileSync(file, ['services:', ' cms:', ' ports:', ` - "127.0.0.1:${busyPort}:8081"`].join('\n')) const stub = `printf 'mozdit-app-staging\\twebsitedev\\tapp\\n' #` const { code, out } = await run(['--project', 'websitedev', '--compose', file], { PATH: '/usr/bin:/bin:/usr/sbin:/sbin', CHECK_PORTS_DOCKER_PS: stub, }) assert.equal(code, 1, out) assert.match(out, /mozdit-app-staging/) }) test('refuses to run without ports', async () => { const { code } = await run([]) assert.equal(code, 2) })