← back to Stayclaim
scripts/tests/hcm-live-preflight.cjs
31 lines
// Read-only operational proof: execute the actual importer against fresh public
// metadata. PG is replaced with a constructor that throws if reached.
const assert = require('node:assert/strict');
const { readFileSync } = require('node:fs');
const { stripTypeScriptTypes } = require('node:module');
const vm = require('node:vm');
const path = require('node:path');
const source = readFileSync(path.join(__dirname, '../ingest-la-historic-monuments.ts'), 'utf8');
assert.equal((source.match(/import \{ Pool \} from 'pg';/g) || []).length, 1);
const executable = stripTypeScriptTypes(source.replace("import { Pool } from 'pg';", 'const Pool = globalThis.TestPool;'));
const evidence = { timestamp: new Date().toISOString(), ticket: 'TK-11379', mode: 'actual importer preflight / real public GET / forbidden PG', calls: [], logs: [], errors: [], exit_code: 0 };
const context = {
TestPool: class { constructor() { evidence.calls.push({ boundary: 'pg-create' }); throw new Error('PG forbidden in preflight'); } },
URL, Date,
process: { env: {}, argv: ['node', 'importer', '--preflight-only'], exit(code) { evidence.exit_code = code; } },
console: { log(...args) { evidence.logs.push(args.map(String).join(' ')); }, error(...args) { evidence.errors.push(args.map(String).join(' ')); } },
async fetch(url) {
assert.equal(String(url), 'https://maps.lacity.org/arcgis/rest/services/Mapping/NavigateLA/MapServer/75?f=json');
const response = await fetch(url, { cache: 'no-store', signal: AbortSignal.timeout(30000) });
const metadata = await response.json();
evidence.calls.push({ boundary: 'public-metadata-get', url: String(url), status: response.status, name: metadata?.name, type: metadata?.type, fields: metadata?.fields?.map(f => f.name) });
return { ok: response.ok, status: response.status, async json() { return metadata; } };
},
};
(async () => {
await vm.runInNewContext(executable, context);
evidence.verdict = evidence.exit_code === 0 && evidence.calls.length === 1 && evidence.calls[0].boundary === 'public-metadata-get' ? 'PASS' : 'FAIL';
console.log(JSON.stringify(evidence, null, 2));
process.exitCode = evidence.verdict === 'PASS' ? 0 : 1;
})().catch(error => { console.error(error); process.exitCode = 1; });