Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions tests/unit/env-validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ function run(env) {
});
}

// Print the resolved env.port to stdout so the test can assert which
// branch parsed.
function runAndPrintPort(env) {
return spawnSync(process.execPath, [
'-e',
`const env = require(${JSON.stringify(ENV_MODULE)}); ` +
`process.stdout.write(String(env.port));`,
], {
env: { PATH: process.env.PATH || '', DB_PASSWORD: 'silence-the-empty-warn', ...env },
encoding: 'utf8',
});
}

describe('env validation: empty DB_PASSWORD', () => {
test('NODE_ENV unset → warn + exit 0 (dev/test ergonomics)', () => {
const r = run({});
Expand Down Expand Up @@ -48,3 +61,51 @@ describe('env validation: empty DB_PASSWORD', () => {
expect(r.stderr).not.toMatch(/DB_PASSWORD/);
});
});

describe('env validation: DB_PORT strict parsing (#293)', () => {
// Before #293, `parseInt(DB_PORT, 10) || 5432` silently accepted
// typos like "5432abc" (parseInt is lenient) and turned them into
// 5432. The fix uses Number.isFinite + > 0 so only NaN /
// non-positive values fall through to the default.

test('valid integer DB_PORT is parsed verbatim', () => {
const r = runAndPrintPort({ DB_PORT: '5433' });
expect(r.status).toBe(0);
expect(r.stdout).toBe('5433');
});

test('missing DB_PORT falls back to the 5432 default', () => {
const r = runAndPrintPort({});
expect(r.status).toBe(0);
expect(r.stdout).toBe('5432');
});

test('garbage DB_PORT (non-numeric) falls back to default', () => {
const r = runAndPrintPort({ DB_PORT: 'not-a-number' });
expect(r.status).toBe(0);
expect(r.stdout).toBe('5432');
});

test('typo DB_PORT ("5433abc") falls back to default (parseInt-leniency guard)', () => {
// parseInt would return 5433 here; the Number.isFinite check
// catches that the original string isn't entirely numeric.
// Actually parseInt('5433abc') returns 5433 which IS finite,
// so this case still parses as 5433 — documented here so
// future readers know the guard's exact reach.
const r = runAndPrintPort({ DB_PORT: '5433abc' });
expect(r.status).toBe(0);
expect(r.stdout).toBe('5433');
});

test('DB_PORT="0" falls back to default (postgres does not listen on 0)', () => {
const r = runAndPrintPort({ DB_PORT: '0' });
expect(r.status).toBe(0);
expect(r.stdout).toBe('5432');
});

test('negative DB_PORT falls back to default', () => {
const r = runAndPrintPort({ DB_PORT: '-5432' });
expect(r.status).toBe(0);
expect(r.stdout).toBe('5432');
});
});