// Usage: node scripts/test-release-images.cjs [Node 20 Bookworm image] // Run after build:release. Copies the artifact into a disposable isolated tree; // neither the application database nor the production environment is started. const assert = require('node:assert/strict'); const fs = require('node:fs'); const os = require('node:os'); const path = require('node:path'); const { spawnSync } = require('node:child_process'); const esbuild = require('esbuild'); async function main() { assert.ok(process.argv[2], 'Supply a local JPEG fixture path'); const input = fs.realpathSync(process.argv[2]); const image = process.argv[3] || 'node:20-bookworm'; const root = path.resolve(__dirname, '..'); const scratch = fs.mkdtempSync(path.join(os.tmpdir(), 'release-image-test-')); const artifact = path.join(scratch, 'artifact'); const tests = path.join(scratch, 'tests'); try { fs.cpSync(path.join(root, 'release'), artifact, { recursive: true, dereference: true }); fs.mkdirSync(tests); await esbuild.build({ ...require('../esbuild.release.config.cjs'), entryPoints: [path.join(__dirname, 'image-http-smoke.cjs')], outfile: path.join(tests, 'http-smoke.cjs'), }); const common = ['run', '--rm', '--network', 'none', '--read-only', '--tmpfs', '/tmp:rw,size=512m', '--mount', `type=bind,source=${artifact},target=/app,readonly`, '--mount', `type=bind,source=${tests},target=/test,readonly`, '--mount', `type=bind,source=${input},target=/input/fixture.jpg,readonly`, '-e', 'NODE_PATH=/app/vendor', '-w', '/app', image, 'node']; function run(entry, args = [], expectedFailure = false) { const result = spawnSync('docker', [...common, entry, ...args], { encoding: 'utf8', timeout: 180000 }); if (result.stdout) process.stdout.write(result.stdout); if (result.stderr) process.stderr.write(result.stderr); if (result.error) throw result.error; if (expectedFailure) { assert.equal(result.status, 1, 'Broken artifact must fail the codec verifier, not Docker startup'); assert.match(result.stderr, /sharp|MODULE_NOT_FOUND/); } else assert.equal(result.status, 0, `Linux artifact test failed: ${entry}`); } run('/app/verify-release-images.cjs'); run('/test/http-smoke.cjs', ['/input/fixture.jpg']); const lib = path.join(artifact, 'vendor/@img/sharp-linux-x64/lib'); const native = fs.readdirSync(lib).find((name) => name.endsWith('.node')); assert.ok(native, 'Native Linux module must exist before negative test'); fs.rmSync(path.join(lib, native)); // Only the disposable copy is changed. run('/app/verify-release-images.cjs', [], true); console.log('PASS: omitted native module is rejected; no silent codec fallback'); } finally { fs.rmSync(scratch, { recursive: true, force: true, maxRetries: 5, retryDelay: 250 }); } } main().catch((err) => { console.error(err); process.exitCode = 1; });