52 lines
3.7 KiB
JavaScript
52 lines
3.7 KiB
JavaScript
// Permanent runner candidate: fresh loopback PostgreSQL, no app config/.env.
|
|
import EmbeddedPostgres from 'embedded-postgres'
|
|
import { mkdtemp, rm, readFile, writeFile, mkdir } from 'node:fs/promises'
|
|
import { tmpdir } from 'node:os'
|
|
import { join, resolve, relative, isAbsolute } from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
import { randomBytes, createHash } from 'node:crypto'
|
|
import { createServer } from 'node:net'
|
|
import { spawn } from 'node:child_process'
|
|
const args = process.argv.slice(2)
|
|
if (args.length > 1 || args[0] && !args[0].startsWith('--production-root=')) throw Error('Only an explicit source-root override is supported for TMP review')
|
|
const sourceRoot = args[0] ? resolve(args[0].slice('--production-root='.length)) : fileURLToPath(new URL('../../', import.meta.url))
|
|
const sources = ['backend/src/services/partition-write-transaction.ts', 'backend/src/db/sqlite-import-control-writes.ts', 'backend/src/db/sqlite-import.ts', 'backend/src/db/schema.ts']
|
|
async function pins() {
|
|
const result = []
|
|
for (const path of sources) result.push({ path, sha256: createHash('sha256').update(await readFile(join(sourceRoot, path))).digest('hex') })
|
|
return result
|
|
}
|
|
const before = await pins(), directory = await mkdtemp(join(tmpdir(), 'mm-import-control-'))
|
|
const listener = createServer(); await new Promise(resolve => listener.listen(0, '127.0.0.1', resolve))
|
|
const port = listener.address().port; await new Promise(resolve => listener.close(resolve))
|
|
const password = randomBytes(24).toString('hex')
|
|
const postgres = new EmbeddedPostgres({ databaseDir: join(directory, 'pg'), port, user: 'import_operator', password,
|
|
persistent: true, createPostgresUser: false, initdbFlags: ['--encoding=UTF8'], postgresFlags: ['-h', '127.0.0.1'], onLog: () => {}, onError: () => {} })
|
|
try {
|
|
await postgres.initialise(); await postgres.start()
|
|
process.exitCode = await new Promise((done, reject) => {
|
|
const child = spawn(process.execPath, ['--import', import.meta.resolve('tsx'), fileURLToPath(new URL('../src/db/sqlite-import-control-writes.sql.test.ts', import.meta.url))], {
|
|
cwd: directory, windowsHide: true, stdio: 'inherit', env: {
|
|
...Object.fromEntries(['SystemRoot', 'WINDIR', 'TEMP', 'TMP', 'PATH', 'Path'].filter(k => process.env[k]).map(k => [k, process.env[k]])),
|
|
MMAPP_OWN_IMPORT_CONTROL: 'true', MMAPP_OWN_IMPORT_DIRECTORY: directory, MMAPP_OWN_IMPORT_SOURCE_ROOT: sourceRoot,
|
|
MMAPP_OWN_IMPORT_ADMIN_URL: `postgresql://import_operator:${password}@127.0.0.1:${port}/postgres` },
|
|
})
|
|
child.once('error', reject); child.once('exit', code => done(code ?? 1))
|
|
})
|
|
const after = await pins(), unchanged = JSON.stringify(before) === JSON.stringify(after)
|
|
const report = { format: 'sqlite-import-control-owned-pins-v1', date: new Date().toISOString(), before, after, unchanged, childExitCode: process.exitCode }
|
|
// Keep generated evidence out of source directories after integration.
|
|
// This is a local test artifact, never a production authority record.
|
|
await mkdir(join(sourceRoot, 'tmp'), { recursive: true })
|
|
await writeFile(join(sourceRoot, 'tmp', 'sqlite-import-control-source-pins.json'), JSON.stringify(report, null, 2))
|
|
console.log('Current production source pins unchanged:', unchanged)
|
|
if (!unchanged) process.exitCode = 1
|
|
} finally {
|
|
await postgres.stop()
|
|
const root = resolve(tmpdir()), target = resolve(directory), rel = relative(root, target)
|
|
if (!rel || rel.startsWith('..') || isAbsolute(rel) || !target.includes('mm-import-control-')) throw Error('Unsafe owned import cleanup path')
|
|
await rm(target, { recursive: true, force: true })
|
|
console.log('Owned importer PostgreSQL stopped and exact temp directory removed')
|
|
}
|
|
process.exit(process.exitCode ?? 0)
|