Files
baiyue-firstleg/scripts/client-xlsx-address-test-env.mjs
T

31 lines
2.4 KiB
JavaScript

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 (args.length !== 5 || !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 } }
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') }
}
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 })