import { readFile } from 'node:fs/promises' import { spawn } from 'node:child_process' import { parseEnv } from 'node:util' import { resolve, dirname } from 'node:path' import { fileURLToPath, pathToFileURL } from 'node:url' import { executionOptions } from './client-workbench-test-env.mjs' import { customerProfilePath } from './client-import-template-test-env.mjs' export async function execute(args = process.argv.slice(2), execArgs = process.execArgv, env = process.env) { if (!args.length || args.length === 1 && args[0] === '--plan') return { plan: true, writes: 'Two single-row XLSX PARTIAL imports must reject foreign customer addresses; source/error files may be retained; no retry or deletion' } if (![5, 6].includes(args.length) || !args[4].startsWith('--python=')) throw Error('Explicit TEST, customer and bundled Python required') const options = executionOptions(args.slice(0, 3), execArgs, env) const credentials = parseEnv(await readFile(customerProfilePath(args[3], execArgs[0].slice(11)), 'utf8')) if (!credentials.MERCHANT_TEST_USERNAME || !credentials.MERCHANT_TEST_PASSWORD) throw Error('Missing control credentials') const python = resolve(args[4].slice(9)) if (!python.replaceAll('\\', '/').endsWith('/codex-primary-runtime/dependencies/python/python.exe')) throw Error('Use bundled Python') const root = resolve(dirname(fileURLToPath(import.meta.url)), '..') const config = { environment: 'TEST', base: options.base, runId: options.runId, source: { loginId: env.CHONGCHONG_MERCHANT_USERNAME, password: env.CHONGCHONG_MERCHANT_PASSWORD }, control: { loginId: credentials.MERCHANT_TEST_USERNAME, password: credentials.MERCHANT_TEST_PASSWORD } } if (args[5]) { const profile = databaseProfilePath(args[5], execArgs[0].slice(11)) const database = parseEnv(await readFile(profile, 'utf8')) if (database.CHONGCHONG_ENV?.toUpperCase() !== 'TEST') throw Error('TEST database required') for (const key of ['HOST', 'PORT', 'USERNAME', 'PASSWORD']) if (!database['CHONGCHONG_DB_' + key]) throw Error('Incomplete TEST database configuration') config.database = Object.fromEntries(['HOST', 'PORT', 'USERNAME', 'PASSWORD'].map(key => [key.toLowerCase(), database['CHONGCHONG_DB_' + key]])) } const child = spawn(python, ['-B', resolve(root, 'scripts/client-xlsx-address-probe.py')], { cwd: root, windowsHide: true, stdio: ['pipe', 'inherit', 'inherit'] }) child.stdin.end(JSON.stringify(config)) const code = await new Promise((accept, reject) => { child.on('error', reject); child.on('close', accept) }) return { success: code === 0, report: resolve(root, 'reports', `client-xlsx-address-${options.runId}`, 'report.json') } } export function databaseProfilePath(argument, environmentFile) { if (!argument.startsWith('--database-profile=')) throw Error('Unknown argument') const profile = resolve(argument.slice(19)) if (profile !== resolve(dirname(environmentFile), 'test-database.env')) throw Error('Use explicit test-database.env beside TEST acceptance profile') return profile } if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) execute().then(r => { console.log(JSON.stringify(r)); if (r.success === false) process.exitCode = 1 }) .catch(() => { console.error('XLSX probe did not start; check explicit TEST inputs and unused run ID.'); process.exitCode = 1 })