import { dirname, resolve } from 'node:path' import { fileURLToPath, pathToFileURL } from 'node:url' import { runPoolAcceptance, poolPlan } from './fleet-capacity-pool-test-env.mjs' import { runFleetPoolGrayCrossCustomer } from './fleet-cross-customer-test-env.mjs' const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..') export function parseGrayPoolArguments(args) { const config = { execute: false, environment: 'GRAY', runId: 'plan', warehouseId: '', destinationWarehouseId: '' } const seen = new Set() for (let index = 0; index < args.length; index++) { const name = args[index] if (['--execute', '--plan'].includes(name)) { if (seen.has('action')) throw Error('Duplicate execution mode') seen.add('action'); config.execute = name === '--execute'; continue } const field = { '--environment': 'environment', '--run-id': 'runId', '--warehouse-id': 'warehouseId', '--destination-warehouse-id': 'destinationWarehouseId' }[name] if (!field || seen.has(name) || !args[index + 1] || args[index + 1].startsWith('--')) throw Error('Unsupported or duplicate gray pool argument') seen.add(name); config[field] = args[++index] } if (config.environment !== 'GRAY' || !/^[A-Za-z0-9][A-Za-z0-9_-]{0,47}$/.test(config.runId) || [config.warehouseId, config.destinationWarehouseId].some(value => value && !/^[1-9][0-9]{0,19}$/.test(value)) || config.execute && (!seen.has('--environment') || config.runId === 'plan' || !config.warehouseId || !config.destinationWarehouseId)) throw Error('Explicit GRAY, fresh run-id and warehouse/destination IDs required') return config } export async function runGrayPoolAcceptance(config, { env = process.env, outputRoot = ROOT, execute = runPoolAcceptance } = {}) { if (config.environment !== 'GRAY') throw Error('Dedicated GRAY pool configuration required') if (!config.execute) return { ...poolPlan(config), scope: 'FLEET_CAPACITY_POOL_GRAY', fullAcceptance: false, evidence: 'createGrayEvidenceReaders: registered production SSH target, immutable container/profile/database identity checks; no TEST agent', excluded: ['old FBA order/container/customs flow', 'generic Admin module runner', 'fault injection', 'deployment', 'business SQL writes'] } if (!['prod', 'gray'].includes(env.CHONGCHONG_ENV)) throw Error('Dedicated GRAY pool requires actual gray or prod runtime profile; TEST is refused') return execute(config, { env, outputRoot, runner: runFleetPoolGrayCrossCustomer }) } if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) { try { const config = parseGrayPoolArguments(process.argv.slice(2)), result = await runGrayPoolAcceptance(config) console.log(JSON.stringify(config.execute ? { success: result.success, environment: 'GRAY', runtimeProfile: result.runtimeProfile, error: result.error, summary: result.summary, fullAcceptance: false, output: poolPlan(config).output, covered: result.createdResources?.fleetCapacityPool?.covered, remaining: result.remainingSeparateAcceptance } : result, null, 2)) if (config.execute && !result.success) process.exitCode = 1 } catch (error) { console.error(error.message); process.exitCode = 1 } }