manicanldes-backend/scripts/test-native.mjs

84 lines
2.5 KiB
JavaScript

import { spawnSync } from 'node:child_process';
import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
export function assertNativeReport(report) {
const nativeTests = (report.testResults ?? []).flatMap((suite) =>
(suite.assertionResults ?? []).filter((test) =>
test.fullName.includes('native PostgreSQL'),
),
);
if (
report.success !== true ||
nativeTests.length < 6 ||
!(report.numTotalTests > 0) ||
report.numPassedTests !== report.numTotalTests ||
report.numPendingTests !== 0 ||
report.numTodoTests !== 0 ||
report.numRuntimeErrorTestSuites !== 0
) {
throw new Error(
'Native PostgreSQL gate requires all six concurrency checks and no skipped tests',
);
}
}
function run() {
const value = process.env.TEST_DATABASE_URL;
if (
!value ||
!URL.canParse(value) ||
!['postgres:', 'postgresql:'].includes(new URL(value).protocol)
) {
throw new Error(
'TEST_DATABASE_URL must identify a disposable native PostgreSQL database',
);
}
const directory = mkdtempSync(join(tmpdir(), 'mani-native-'));
try {
const reportPath = join(directory, 'report.json');
const child = spawnSync(
process.execPath,
[
'--experimental-vm-modules',
'node_modules/jest/bin/jest.js',
'--runInBand',
'--runTestsByPath',
'test/checkout-concurrency.spec.ts',
'test/events-concurrency.spec.ts',
'test/inventory.spec.ts',
'test/migration-integrity.spec.ts',
'--json',
'--outputFile',
reportPath,
],
{ stdio: 'inherit', env: process.env },
);
if (child.error) throw new Error('Could not start native PostgreSQL tests');
if (child.status !== 0) throw new Error('Native PostgreSQL tests failed');
assertNativeReport(JSON.parse(readFileSync(reportPath, 'utf8')));
console.log('Native PostgreSQL gate passed: no selected tests skipped');
} finally {
rmSync(directory, { recursive: true, force: true });
}
}
if (
process.argv[1] &&
resolve(process.argv[1]) === fileURLToPath(import.meta.url)
) {
try {
run();
} catch (error) {
// Database credentials and provider errors must not appear in gate diagnostics.
console.error(
error instanceof Error && error.message.startsWith('Native PostgreSQL')
? error.message
: 'Native gate failed; check TEST_DATABASE_URL and test output',
);
process.exitCode = 1;
}
}