import test from 'node:test' import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import { resolve } from 'node:path' import { AcceptanceRun, configureReadOnlyEvidence, unloadRecoveryWasVerified, parseArguments, validateExecutionOptions } from './admin-test-env-acceptance.mjs' const source = readFileSync(resolve('scripts/admin-test-env-acceptance.mjs'), 'utf8') test('full CLI evidence preflight attaches readers and fails before business writes when unavailable', async () => { const run = { options: { modules: ['manual-state-matrix'] }, context: {} } const readReceiving = () => {}, readCompletion = () => {} let checked = false await configureReadOnlyEvidence(run, {}, () => ({ readReceiving, readCompletion, preflight: async () => { checked = true } })) assert.equal(checked, true) assert.equal(run.context.phase4ReceivingEvidenceReader, readReceiving) assert.equal(run.context.f40ReadOnlyOutboxEvidence, readCompletion) const unavailable = { options: run.options, context: {} } await assert.rejects(configureReadOnlyEvidence(unavailable, {}, () => ({ preflight: async () => { throw Error('evidence unavailable') } })), /unavailable/) assert.deepEqual(unavailable.context, {}) }) test('explicit unloading fault uses a separate first-container hook and restores both directions on failure', async () => { const calls = [] const run = { marker: 'TEST-UNLOAD', options: { modules: ['phase4-overseas-pda', 'phase4-cross-region-events'], verifyUnloadEventRecovery: true }, context: {} } const fault = name => ({ prepare: async scope => calls.push([name, 'prepare', scope]), readProof: async scope => ({ name, scope, restored: true }), cleanup: async () => { calls.push([name, 'cleanup']); if (name === 'reverse') throw Error('recovery failed') } }) const cleanup = await configureReadOnlyEvidence(run, {}, () => ({ readReceiving() {}, readCompletion() {}, preflight: async () => {} }), { createTestFaultExecutor: () => fault('forward'), createTestUnloadFaultExecutor: () => fault('reverse') }) assert.equal(calls.length, 0) await run.context.beforeReceivingComplete({ customerId: '7', bookingId: '66' }) await run.context.beforeReceivingComplete({ customerId: '8', bookingId: '77' }) assert.deepEqual(calls, [['reverse', 'prepare', { marker: 'TEST-UNLOAD', customerId: '7', containerBookingId: '66' }]]) assert.equal(await run.context.afterReceivingComplete({ customerId: '8', bookingId: '77' }), null) const proof = await run.context.afterReceivingComplete({ customerId: '7', bookingId: '66' }) assert.equal(proof.name, 'reverse') await assert.rejects(cleanup.cleanup(), /恢复失败/) assert.deepEqual(calls.slice(-2), [['reverse', 'cleanup'], ['forward', 'cleanup']]) }) test('unloading recovery is opt-in and cannot run against unrelated modules', () => { const args = ['--modules', 'phase4-overseas-return-followups', '--gateway', 'http://localhost:9000', '--username', 'contract', '--password', 'contract'] assert.equal(parseArguments(args).verifyUnloadEventRecovery, false) assert.throws(() => validateExecutionOptions(parseArguments([...args, '--verify-unload-event-recovery', 'true'])), /收货链/) }) test('requested unloading fault cannot pass without exact current-run recovery and inventory proof', () => { const run = { marker: 'CURRENT', createdResources: {} } assert.equal(unloadRecoveryWasVerified(run), false) const row = { status: 'VERIFIED', eventRecoveryInventoryUnchanged: true, eventRecovery: { restored: true, marker: 'CURRENT', containerBookingId: '66' } } run.createdResources.phase4OverseasPda = { receivingCompletionEvidence: { '66': row } } assert.equal(unloadRecoveryWasVerified(run), true) for (const change of [r => r.eventRecoveryInventoryUnchanged = false, r => r.status = 'UNPROVEN', r => r.eventRecovery.restored = false, r => r.eventRecovery.marker = 'OLD', r => r.eventRecovery.containerBookingId = '77']) { const changed = structuredClone(run) change(changed.createdResources.phase4OverseasPda.receivingCompletionEvidence['66']) assert.equal(unloadRecoveryWasVerified(changed), false) } run.createdResources.phase4OverseasPda.receivingCompletionEvidence['77'] = structuredClone(row) assert.equal(unloadRecoveryWasVerified(run), false) }) test('F40 optional chain always creates its customer and rejects absent domestic PDA credentials', () => { const options = parseArguments(['--modules', 'phase5-root-fulfillment', '--gateway', 'http://localhost:9000', '--username', 'contract-user', '--password', 'contract-password', '--merchant-username', 'prior-client', '--merchant-password', 'prior-password']) assert.deepEqual(options.modules, ['merchant-qualification-fixture', 'phase5-root-fulfillment']) options.pdaUsername = ''; options.pdaPassword = '' assert.throws(() => validateExecutionOptions(options), /PDA/) }) test('phase-four HTTP acceptance modules are registered in the Admin acceptance entry', () => { for (const module of [ 'phase4-overseas-warehouse', 'phase4-overseas-pda', 'phase4-overseas-pda-operations', 'phase4-overseas-inbound-operations', 'phase4-overseas-client-services', 'phase4-overseas-dispatch', 'phase4-cross-region-events', 'phase4-overseas-business', 'phase4-overseas-master-data', 'phase4-carrier-nodes', 'phase4-overseas-wallet-and-pallet-cost', ]) { assert.match(source, new RegExp(`CONFIGURED_MODULES\\s*=\\s*\\[[^\\]]*'${module}'`, 's')) } }) test('wallet and destuff cost closure expands the fresh merchant and completed overseas PDA chain', () => { const modules = parseArguments(['--modules', 'phase4-overseas-wallet-and-pallet-cost']).modules for (const prerequisite of ['merchant-qualification-fixture', 'phase3-loading-finalize', 'phase4-overseas-pda', 'phase4-overseas-pda-operations', 'phase4-overseas-business']) { assert.ok(modules.indexOf(prerequisite) >= 0 && modules.indexOf(prerequisite) < modules.indexOf('phase4-overseas-wallet-and-pallet-cost')) } }) test('overseas inbound commands, client services and dispatch follow completed PDA receiving', () => { for (const consumer of ['phase4-overseas-inbound-operations', 'phase4-overseas-client-services', 'phase4-overseas-dispatch']) { const modules = parseArguments(['--modules', consumer]).modules for (const prerequisite of ['merchant-qualification-fixture', 'phase3-loading', 'phase3-loading-finalize', 'phase4-overseas-pda', 'phase4-overseas-pda-operations']) { assert.ok(modules.indexOf(prerequisite) >= 0 && modules.indexOf(prerequisite) < modules.indexOf(consumer)) } } }) test('entry uses one TEST gateway and rejects retired regional options', () => { const options = parseArguments(['--gateway', 'http://localhost:9000/', '--modules', 'phase4-overseas-warehouse']) assert.equal(options.gateway, 'http://localhost:9000') assert.equal(options.environment, 'test') assert.throws(() => parseArguments(['--overseas-gateway', 'http://localhost:9300']), /one TEST gateway/) assert.throws(() => parseArguments(['--environment', 'cn']), /TEST|test/) assert.doesNotMatch(source, /async requestOverseas|this\.overseasToken|overseasAccessToken/) assert.match(source, /this\.refreshToken = login\.refreshToken/) assert.match(source, /system后新admin统一Token只读探针可用/) }) test('overseas business keeps its marker-scoped customer fixture with reusable credentials configured', () => { const previousUsername = process.env.MERCHANT_TEST_USERNAME const previousPassword = process.env.MERCHANT_TEST_PASSWORD process.env.MERCHANT_TEST_USERNAME = 'reusable-client' process.env.MERCHANT_TEST_PASSWORD = 'reusable-password' try { const modules = parseArguments(['--modules', 'phase4-overseas-business']).modules assert.ok(modules.includes('merchant-qualification-fixture')) assert.ok(modules.indexOf('merchant-qualification-fixture') < modules.indexOf('phase4-overseas-business')) } finally { if (previousUsername === undefined) delete process.env.MERCHANT_TEST_USERNAME else process.env.MERCHANT_TEST_USERNAME = previousUsername if (previousPassword === undefined) delete process.env.MERCHANT_TEST_PASSWORD else process.env.MERCHANT_TEST_PASSWORD = previousPassword } }) test('a fixed origin route does not require randomized order fixtures', () => { const options = parseArguments(['--origin-warehouse-name', '东莞仓', '--modules', 'phase4-overseas-pda']) assert.equal(options.originWarehouseName, '东莞仓') assert.equal(options.routeMode, 'fixed') assert.equal(options.orderProfileMode, 'fixed') assert.match(source, /else if \(this\.options\.originWarehouseName \|\| this\.options\.channelName \|\| this\.options\.channelCode\)/) assert.match(source, /this\.context\.waitSplitRoute = \{[\s\S]*randomize: false/) assert.match(source, /this\.context\.allowAnyExistingPallet = true/) }) test('unified-session refresh replaces the whole session only after a complete response', async () => { const run = new AcceptanceRun({ gateway: 'http://localhost:9000', username: 'contract-user', password: 'contract-password', modules: [], output: '' }) Object.assign(run, { token: 'old-access', refreshToken: 'old-refresh', expiresAt: 1 }) run.request = async (method, path, body, token) => { assert.equal(method, 'POST'); assert.equal(path, '/api/auth/refresh') assert.deepEqual(body, { refreshToken: 'old-refresh' }); assert.equal(token, '') return { method, path, http: 200, body: { code: '200', data: { accessToken: 'new-access', refreshToken: 'new-refresh', expiresIn: 900, } }, requestId: 'refresh-request', traceId: 'refresh-trace' } } assert.equal(await run.refreshSession('contract'), true) assert.equal(run.token, 'new-access') assert.equal(run.refreshToken, 'new-refresh') assert.ok(run.expiresAt > Date.now()) const frozen = { token: run.token, refreshToken: run.refreshToken, expiresAt: run.expiresAt } run.request = async (method, path) => ({ method, path, http: 200, body: { code: '200', data: { accessToken: 'partial-only' } }, requestId: 'partial-request', traceId: 'partial-trace' }) assert.equal(await run.refreshSession('incomplete-contract'), false) assert.deepEqual({ token: run.token, refreshToken: run.refreshToken, expiresAt: run.expiresAt }, frozen) }) test('entry has no built-in environment address or password and requires explicit execution options', () => { assert.doesNotMatch(source, /8\.163\.|ChangeMe123|DEFAULT_PASSWORD|DEFAULT_GATEWAY/) assert.throws(() => validateExecutionOptions(parseArguments(['--modules', 'phase4-overseas-pda'])), /缺少必填执行参数/) const options = parseArguments([ '--gateway', 'http://localhost:9000', '--username', 'contract-user', '--password', 'contract-password', '--modules', 'phase4-overseas-pda', '--pda-username', 'contract-picker', '--pda-password', 'contract-picker-password', ]) assert.equal(validateExecutionOptions(options), options) }) test('loading dependency chains require PDA credentials before making any HTTP requests', () => { for (const module of ['phase3-loading', 'phase3-container-loading', 'phase5-overseas-fulfillment']) { const options = parseArguments(['--gateway', 'http://localhost:9000', '--username', 'admin', '--password', 'fixture', '--modules', module]) options.pdaUsername = '' options.pdaPassword = '' assert.throws(() => validateExecutionOptions(options), /PDA 作业员凭据/) options.pdaUsername = 'picker' assert.throws(() => validateExecutionOptions(options), /PDA 作业员凭据/) options.pdaPassword = 'fixture' assert.equal(validateExecutionOptions(options), options) } }) test('core coverage gaps fail closed while device and external-provider gaps remain report-only', () => { const run = new AcceptanceRun({ gateway: 'http://localhost:9000', username: 'contract-user', password: 'contract-password', modules: [], output: '' }) run.addCoverageGap('phase4', 'core', 'fixture', 'missing fixture', 'fixture') assert.equal(run.steps.at(-1)?.passed, false) assert.equal(run.steps.at(-1)?.deploymentBlocker, true) const stepCount = run.steps.length run.addCoverageGap('phase3', 'device', 'Android', 'manual device validation', 'device') run.addCoverageGap('phase3', 'provider', 'captcha', 'external captcha', 'external-third-party') run.addCoverageGap('phase3', 'device', 'Android', 'manual device validation', 'device') run.addCoverageGap('phase3', 'provider', 'captcha', 'external captcha', 'external-third-party') assert.equal(run.steps.length, stepCount) assert.equal(run.coverageGaps.length, 3) }) test('PDA entry expands the real phase-three producer chain and builds an FBA/OVERSEAS/second-container matrix', () => { const modules = parseArguments(['--modules', 'phase4-overseas-pda']).modules for (const prerequisite of ['merchant-qualification-fixture', 'phase3-loading', 'phase3-pda-convergence', 'phase3-evidence-gate', 'phase3-loading-finalize']) { assert.ok(modules.indexOf(prerequisite) >= 0 && modules.indexOf(prerequisite) < modules.indexOf('phase4-overseas-pda')) } assert.match(source, /async ensurePhase4PdaProducerMatrix\(\)/) assert.match(source, /runPhase4ProducerVariant\('DROPSHIP-PRIMARY', 'FBA', 'ONE_CLICK_DROPSHIP', true\)/) assert.match(source, /runPhase4ProducerVariant\('OVERSEAS-PRIMARY', 'OVERSEAS', 'ONE_CLICK_DROPSHIP', true\)/) assert.match(source, /runPhase4ProducerVariant\('FBA-STANDARD', 'FBA', 'STANDARD', true\)/) assert.match(source, /runPhase4ProducerVariant\('FBA-OPERATIONS', 'FBA', 'STANDARD', true\)/) assert.match(source, /-P4-FBA-STANDARD\(\?:-R\\d\+\)\?:/) assert.match(source, /runPhase4ProducerVariant\('SECONDARY', 'FBA', 'STANDARD', false\)/) assert.match(source, /runUntilPickupReady\(this\)/) const variant = source.slice(source.indexOf('async runPhase4ProducerVariant'), source.indexOf('async ensurePhase4PdaProducerMatrix')) assert.match(variant, /this\.phase4ProducerAttempts\.get\(label\)/) assert.match(variant, /attempt === 1 \? '' : `-R\$\{attempt\}`/) assert.ok(variant.indexOf('ensureInitialCalculated') < variant.indexOf('phase3-loading-phase4-')) assert.match(variant, /fulfillmentType === 'ONE_CLICK_DROPSHIP'/) assert.doesNotMatch(variant, /inboundFees\.run\(this\)/) assert.ok(variant.indexOf('phase3-outbound-dispatch-phase4-') < variant.indexOf('phase3-container-documents-phase4-')) assert.ok(variant.indexOf('phase3-container-documents-phase4-') < variant.indexOf('phase3-carrier-schedule-queries-phase4-')) assert.match(source, /\['phase3-container-documents', \['phase3-outbound-dispatch'\]\]/) assert.doesNotMatch(source, /\['phase3-outbound-dispatch', \['phase3-container-documents'/) assert.match(source, /carrierScheduleQueryId/) assert.match(source, /readyForPhase4Pda === true/) assert.equal(modules.indexOf('phase4-overseas-pda-operations'), modules.indexOf('phase4-overseas-pda') + 1) })