import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import test from 'node:test' import { CONTAINER_CONVERT_ENDPOINT, CONTAINER_CONVERT_EXPECTED_ORDERS, distinctPhysicalPalletCount, groupOrdersByCustomer, stableReplay, validContainerBatchResult, validOrderFact, validPdaCandidate, zeroDrift, } from './container-inbound-plan-batch-contract.mjs' const read = (relativeUrl) => readFileSync(new URL(relativeUrl, import.meta.url), 'utf8') const scenario = read('phase4-container-inbound-plan-batch.mjs') const controller = read('../../lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/controller/InboundPlanController.java') const service = read('../../lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/service/InboundPlanService.java') const openapi = read('../../lastleg-service/src/main/resources/openapi/overseas-business-v1.yaml') const pda = read('../../lastleg-service/src/main/java/com/baiyue/lastleg/inboundplan/pda/service/ReceivingContainerService.java') const adminApi = read('../../../apps/admin-web/src/modules/overseas-warehouse/api/lastMileOrders.ts') const adminView = read('../../../apps/admin-web/src/modules/overseas-warehouse/pages/LastMileOrdersView.vue') function orders() { return Array.from({ length: 15 }, (_, index) => ({ orderId: String(9_000 + index), customerId: index < 10 ? '101' : '202', packageCount: 2, volumeCbm: '1.2500', productQuantity: 20, palletCount: 1, volumeWeightKg: '18.750', estimatedArrivalAt: '2026-09-01T08:30:00', actualArrivalAt: null, estimatedWarehouseAt: null, })) } test('semantic contract creates one independent plan per order even for the same customer', () => { const source = orders() assert.equal(CONTAINER_CONVERT_EXPECTED_ORDERS, 15) assert.equal(groupOrdersByCustomer(source).size, 2) const result = { plans: source.map((order, index) => ({ customerId: order.customerId, planNo: `LIP-${index + 1}`, status: 'PENDING_ASSIGNMENT', lines: [{ orderId: order.orderId }], pallets: [{}], allocations: [{}], })) } assert.equal(validContainerBatchResult(result, source), true) }) test('facts preserve source values and physical pallets are not counted once per SKU', () => { const expected = orders()[0] assert.equal(validOrderFact({ ...expected }, expected), true) assert.equal(validOrderFact({ ...expected, actualArrivalAt: '2026-09-02T08:00:00' }, expected), false) assert.equal(distinctPhysicalPalletCount([ { physicalPalletId: 'P1', sku: 'A' }, { physicalPalletId: 'P1', sku: 'B' }, { physicalPalletId: 'P2', sku: 'A' }, ]), 2) }) test('atomic failure, replay and PDA count helpers reject drift', () => { const snapshot = { orders: [{ id: '1', status: 'PENDING_ASSIGNMENT' }], plans: [] } assert.equal(zeroDrift(snapshot, structuredClone(snapshot)), true) assert.equal(stableReplay({ plans: [{ planNo: 'LIP-1' }] }, { plans: [{ planNo: 'LIP-1' }] }), true) assert.equal(validPdaCandidate({ containerNo: 'C1', planCount: '2', inboundCodeCount: '15' }, { containerNo: 'C1', planCount: 2, inboundCodeCount: 15 }), true) }) test('HTTP scenario freezes normal, invalid, replay, concurrency, partial and PDA evidence', () => { assert.equal(CONTAINER_CONVERT_ENDPOINT, '/api/platform/v1/last-leg/inbound-plans/convert') for (const marker of ['P0整柜一次转换并逐订单生成独立计划', 'P1任一非法票整柜转换拒绝', 'P1同键同请求重放', 'P1并发双击A', 'P1部分已转换整柜冲突', 'P0同柜PDA候选计划数与入仓码数一致', 'P3箱码服务端命中当前柜唯一入仓码']) { assert.match(scenario, new RegExp(marker)) } assert.match(scenario, /Promise\.all/) assert.match(scenario, /Idempotency-Key/) assert.match(scenario, /requestId/) assert.match(scenario, /traceId/) }) test('platform API keeps two long string identifiers and Admin uses the selected row customer', () => { const endpoint = openapi.slice(openapi.indexOf(` ${CONTAINER_CONVERT_ENDPOINT}:`), openapi.indexOf(' /api/platform/v1/last-leg/dispatches:', openapi.indexOf(` ${CONTAINER_CONVERT_ENDPOINT}:`))) assert.match(endpoint, /required: \[customerId, orderId\]/) assert.match(endpoint, /customerId:\s*\{ type: string/) assert.match(endpoint, /orderId:\s*\{ type: string/) assert.match(controller, /convertScope\(user, request\.customerId\(\)\)/) assert.match(adminApi, /orderId: string[\s\S]{0,80}customerId: string/) assert.match(adminView, /selectedRows\.value\.length !== 1/) assert.match(adminView, /customerId = typeof selected\.customerId === 'string' \? selected\.customerId\.trim\(\)/) assert.match(adminView, /orderId: String\(selected\.id\), customerId/) }) test('backend batch conversion is one transaction and never merges same-customer orders', () => { assert.match(controller, /ApiResponse<[^>]*(?:Batch|List|Container)[^>]*>/) assert.match(service, /@Transactional[\s\S]{0,120}(?:convertContainer|convertBatch|convert)/) assert.match(service, /(?:List|Container.*Response|Batch.*Response)/) assert.match(service, /(?:for \([^)]*(?:order|source)|\.map\([^)]*(?:order|source))/) assert.doesNotMatch(service, /groupingBy\([^)]*(?:customerId|getCustomerId)/) assert.match(service, /(?:partial|部分已转换|already belongs)[\s\S]{0,180}(?:CONFLICT|HttpStatus\.CONFLICT)/i) }) test('PDA candidate and resolve contracts remain server-owned', () => { assert.match(pda, /grouped\.computeIfAbsent\(plan\.getContainerNo\(\)/) assert.match(pda, /entry\.getValue\(\)\.size\(\)/) assert.match(pda, /totals\.inboundCount\(\)/) assert.match(pda, /requireReceivingAvailable\(customerId, containerNo\)/) assert.match(pda, /requireCartonRef|requireUnmatchedFbaCartonPrefix/) })