Files
baiyue-firstleg/scripts/admin-test-env-modules/phase3-producer-chain-contract.test.mjs
T
2026-08-12 10:59:39 +08:00

202 lines
9.0 KiB
JavaScript

import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import test from 'node:test'
import { selectCustomsFlow } from './phase3-customs.mjs'
import { buildFinalizeResource } from './phase3-loading-finalize.mjs'
import { AcceptanceRun } from '../admin-test-env-acceptance.mjs'
function source(name) {
return readFileSync(new URL(name, import.meta.url), 'utf8')
}
function assertOrder(value, snippets) {
let cursor = -1
for (const snippet of snippets) {
const next = value.indexOf(snippet)
assert.ok(next > cursor, `${snippet} must appear after the previous production step`)
cursor = next
}
}
test('pairs customs source fixture with the matching loading container', () => {
const resources = {
orders: { tenantId: 10001, orderId: 101, inboundId: 201 },
orchestrationFixtures: {
phase3Container: { tenantId: 10001, orderId: 102, inboundId: 202 },
},
phase3Loading: { tenantId: 10001, inboundId: 201, containerBookingId: 301 },
phase3ContainerLoading: { tenantId: 10001, inboundId: 202, containerBookingId: 302 },
}
const selected = selectCustomsFlow(resources)
assert.equal(selected.fixture.orderId, 101)
assert.equal(selected.container.containerBookingId, 301)
})
test('falls back to the matching container fixture without crossing source identities', () => {
const resources = {
orchestrationFixtures: { phase3Container: { tenantId: 10001, orderId: 102, inboundId: 202 } },
phase3Loading: { tenantId: 10001, inboundId: 201, containerBookingId: 301 },
phase3ContainerLoading: { tenantId: 10001, inboundId: 202, containerBookingId: 302 },
}
const selected = selectCustomsFlow(resources)
assert.equal(selected.fixture.orderId, 102)
assert.equal(selected.container.containerBookingId, 302)
})
test('records LOADING and a cleanup reason when finalize returns S0500', () => {
const result = buildFinalizeResource(
{ inboundStatus: 'LOADING', inboundVersion: 5, finalStatus: 'COMPLETED' },
{ summary: { processStatus: 'LOADING', version: 5 } },
{ http: 200, body: { code: 'S0500' } },
)
assert.equal(result.finalStatus, 'LOADING')
assert.equal(result.finalizeSucceeded, false)
assert.match(result.notCleanedReason, /S0500/)
})
test('records LOADED and clears the reason only after a real LOADED reread', () => {
const result = buildFinalizeResource(
{ inboundStatus: 'LOADING', inboundVersion: 5, finalStatus: 'COMPLETED' },
{ summary: { processStatus: 'LOADED', version: 6 } },
{ http: 200, body: { code: '200' } },
)
assert.equal(result.finalStatus, 'LOADED')
assert.equal(result.finalizeSucceeded, true)
assert.equal(result.notCleanedReason, null)
})
test('container loading follows push, claim, real pick, completed sequence', () => {
const value = source('./phase3-container-loading.mjs')
assert.match(value, /containerDetail\?\.planStatus === 'WAIT_LOADING'/)
assertOrder(value, [
"pushed?.status === 'EDITING'",
"claimedPlan?.status === 'LOADING'",
"const pickResponse = await request",
"finalPlan?.status === 'COMPLETED'",
])
})
test('bridge loading starts through a real PDA batch before preload confirmation', () => {
const value = source('./phase3-loading.mjs')
assertOrder(value, [
"pushedLoadingPlan?.status === 'EDITING'",
"claimedPlan?.status === 'LOADING'",
"const canonicalPick = await request",
"pickedPlan?.status === 'COMPLETED'",
"const confirming = await request",
])
assert.match(value, /confirmed\?\.mainStatus === 'LOADING' && confirmed\?\.preloadPlan\?\.status === 'CONFIRMING'/)
assert.match(value, /confirmed\?\.preloadPlan\?\.status === 'CONFIRMED'/)
assert.doesNotMatch(value, /关联柜次计划进入 LOADING/)
assert.match(value, /confirmed\?\.mainStatus === 'LOADING'/)
assert.match(value, /confirmed\?\.preloadPlan\?\.status === 'CONFIRMING'/)
assert.match(value, /Number\(confirmed\?\.preloadPlan\?\.version\) === 2/)
})
test('PDA inventory snapshot gates batch and records the only safe mutation gap', () => {
const loading = source('./phase3-loading.mjs')
assertOrder(loading, [
'读取 PDA 提交前实时库存快照',
'未发生库存变化时重复读取实时库存快照',
'客户身份无权读取平台 PDA 实时库存快照',
'const overflowResource =',
'通过公开 A0 换绑接口制造真实库存变化',
'真实库存变化后旧 snapshot token 提交返回冲突',
'旧 token 冲突对任务、明细、已拣数量和操作历史零写入',
'库存变化后刷新 PDA 实时库存快照',
'const canonicalPick = await request',
'const canonicalBatchReplay = await request',
])
assert.match(loading, /inventorySnapshotToken: refreshedInventory\.snapshotToken/)
assert.match(loading, /inventorySnapshotVerified: inventorySnapshotVerified && directPdaCompletion && atomicBatchReplayVerified/)
assert.match(loading, /inventoryMutationVerified, inventoryMutationCleanup/)
assert.match(loading, /当前唯一平台代理库存变更入口只允许 A0 临时占用换绑/)
const convergence = source('./phase3-pda-convergence.mjs')
assert.doesNotMatch(convergence, /未公开实时库存查询或库存版本快照端点/)
assert.match(convergence, /fixture\?\.inventorySnapshotVerified !== true/)
assert.match(convergence, /fixture\?\.inventoryMutationVerified !== true/)
})
test('PDA completion acceptance verifies immutable BATCH audit with operator metadata', () => {
const container = source('./phase3-container-loading.mjs')
const convergence = source('./phase3-pda-convergence.mjs')
assert.match(container, /回查完成后的 PDA 操作审计/)
assert.match(container, /item\.actionType === 'BATCH'/)
assert.match(container, /Boolean\(item\.operatorId\) && Boolean\(item\.createdAt\)/)
assert.match(convergence, /PDA 原子批次完成审计包含 BATCH 操作/)
assert.match(convergence, /item\.actionType === 'BATCH'/)
})
test('customs provisions released booking and completed truck for the selected loading container', () => {
const value = source('./phase3-customs.mjs')
assertOrder(value, [
'selectCustomsFlow(context.createdResources)',
'if (!await ensureServiceCenterFlow(context, container)) return',
'const slicesPath =',
])
assert.match(value, /oceanBookings\.run\(context\)/)
assert.match(value, /serviceCenter\.run\(context\)/)
})
test('downstream producers prefer the phase3Loading container fixture', () => {
const ocean = source('./ocean-bookings.mjs')
const customs = source('./phase3-customs.mjs')
const serviceCenter = source('./service-center-flow.mjs')
assert.match(ocean, /context\.createdResources\?\.phase3Loading\s*\|\|\s*context\.createdResources\?\.phase3ContainerLoading/)
assert.match(customs, /createdResources\?\.phase3Loading,\s*\n\s*createdResources\?\.phase3ContainerLoading/)
assert.match(serviceCenter, /context\.createdResources\?\.phase3Loading\s*\|\|\s*context\.createdResources\?\.phase3ContainerLoading/)
assert.match(ocean, /await action\('放舱', 'release'/)
})
test('default acceptance order verifies loading evidence before finalizing LOADED', () => {
const entry = source('../admin-test-env-acceptance.mjs')
assertOrder(entry, [
"'phase3-loading'",
"'phase3-loading-model-bridge'",
"'phase3-pda-convergence'",
"'phase3-evidence-gate'",
"'phase3-customs'",
"'phase3-loading-finalize'",
])
})
test('phase3 loading provisions its own marker-isolated WAIT_SPLIT fixture', async () => {
const run = new AcceptanceRun({ gateway: 'http://127.0.0.1', modules: [], username: '', password: '', output: '' })
const entry = source('../admin-test-env-acceptance.mjs')
assert.match(entry, /module === 'phase3-loading'/)
assert.match(entry, /preparePhase3LoadingFixture\(\)/)
assert.match(entry, /orders-phase3-loading-fixture/)
assert.match(entry, /inbound-measurement-phase3-loading-fixture/)
assert.match(entry, /marker: originalMarker/)
assert.match(entry, /sourceMarker: fixtureMarker/)
assert.match(entry, /phase3Loading: loadingFixture,\s*\n\s*inboundMeasurement: loadingFixture/)
assert.equal(run.marker.includes('-LOAD'), false)
})
test('customs and finalize consume the same loading inbound identity', () => {
const loadingFixture = { marker: 'RUN-1', tenantId: 10001, orderId: 101, inboundId: 201 }
const loading = {
marker: 'RUN-1', tenantId: 10001, inboundId: 201,
containerBookingId: 301, loadingPlanId: 401, finalStatus: 'COMPLETED',
}
const selected = selectCustomsFlow({
orchestrationFixtures: {
phase3Loading: loadingFixture,
inboundMeasurement: loadingFixture,
},
phase3Loading: loading,
})
assert.equal(selected.fixture.marker, loading.marker)
assert.equal(selected.fixture.inboundId, loading.inboundId)
assert.equal(selected.container.inboundId, loading.inboundId)
const finalized = buildFinalizeResource(loading, {
summary: { processStatus: 'LOADED', version: 6 },
}, { http: 200, body: { code: '200' } })
assert.equal(finalized.marker, loading.marker)
assert.equal(finalized.inboundId, loading.inboundId)
assert.equal(finalized.finalStatus, 'LOADED')
})