fix(fleet): reuse reviewed handover vehicle and check formal responses
This commit is contained in:
@@ -14,13 +14,23 @@ function fixture(fault = '') {
|
||||
const dispatchDetails = ['A', 'B'].map(customer => ({ dispatchNo: `PC-${customer}`, customerId: customer, overseasWarehouseId: '92',
|
||||
status: 'READY_TO_DEPART', packageCount: 10, pickedPackageCount: 10, shippedPackageCount: 0, ...originalTransport,
|
||||
items: [{ id: `ITEM-${customer}`, warehousePalletId: 'PALLET-INTERNAL', inboundCode: `IN-${customer}`, packageCount: 10 }] }))
|
||||
const liveDispatches = dispatchDetails.map(({ customerId, ...row }) => ({ ...row, status: 'DEPARTED', shippedPackageCount: 10 }))
|
||||
const cargo = ['A', 'B'].map(customer => ({ customerId: customer, dispatchNo: `PC-${customer}`, dispatchItemId: `ITEM-${customer}`, warehousePalletId: 'PALLET-INTERNAL', palletNo: 'PAL-REAL', packages: 10, destinationKey: 'FBA:ONE' }))
|
||||
if (fault === 'r5-layout') {
|
||||
Object.assign(dispatchDetails[0], { packageCount: 5, pickedPackageCount: 5, items: [
|
||||
{ id: 'ITEM-A1', warehousePalletId: 'PALLET-1', packageCount: 1 }, { id: 'ITEM-A2', warehousePalletId: 'PALLET-2', packageCount: 4 }] })
|
||||
Object.assign(dispatchDetails[1], { packageCount: 1, pickedPackageCount: 1, items: [{ id: 'ITEM-B1', warehousePalletId: 'PALLET-3', packageCount: 1 }] })
|
||||
}
|
||||
const liveDispatches = dispatchDetails.map(({ customerId, ...row }) => ({ ...row, status: 'DEPARTED', shippedPackageCount: row.packageCount }))
|
||||
const cargo = dispatchDetails.flatMap(row => row.items.map((item, index) => ({ customerId: row.customerId, dispatchNo: row.dispatchNo,
|
||||
dispatchItemId: item.id, warehousePalletId: item.warehousePalletId, palletNo: 'PAL-REAL', packages: item.packageCount,
|
||||
destinationKey: fault === 'r5-layout' && index === 1 ? 'FBA:TWO' : 'FBA:ONE' })))
|
||||
const pallets = [...new Set(cargo.map(row => row.warehousePalletId))].map(warehousePalletId => ({ warehousePalletId, palletSpaces: 1, weightKg: 100, volumeCbm: 1 }))
|
||||
const originalHistory = { action: 'DEPART', recordVersion: '7', snapshot: { trip: copy(trip), confirmedLoad: { cargo,
|
||||
pallets: [{ warehousePalletId: 'PALLET-INTERNAL', palletSpaces: 1, weightKg: 100, volumeCbm: 1 }] } } }
|
||||
pallets } } }
|
||||
const resources = new Map([['VEHICLE:11', 'TR-ONE'], ['DRIVER:21', 'TR-ONE']])
|
||||
const vehicles = new Map(), drivers = new Map(), probes = new Map(), calls = []
|
||||
let createdVehicles = 0, handover = null, preview = null
|
||||
let createdVehicles = fault === 'reviewed-vehicle' ? 1 : 0, handover = null, preview = null
|
||||
if (fault === 'reviewed-vehicle') vehicles.set('12', { id: '12', plateNumber: 'HO-24255909-3BC', recordVersion: '0',
|
||||
status: 'IN_SERVICE', region: 'OVERSEAS', ownership: 'OWNED', maxPalletSpaces: 30, maxLoadKg: 18000, maxVolumeCbm: 90 })
|
||||
const ok = data => ({ http: 200, body: { code: '200', data: copy(data) }, requestId: 'request', traceId: 'trace' })
|
||||
const conflict = message => ({ http: 200, body: { code: 'A0409', message }, requestId: 'request', traceId: 'trace' })
|
||||
const bump = () => { trip.recordVersion = String(BigInt(trip.recordVersion) + 1n) }
|
||||
@@ -30,11 +40,12 @@ function fixture(fault = '') {
|
||||
const context = {
|
||||
context: { async checkpointFleetHandover() {} },
|
||||
marker, createdResources: { fleetTrips: [proof], fleetAcceptance: { marker, tripNo: proof.tripNo,
|
||||
departure: { tripNo: proof.tripNo, status: 'IN_TRANSIT', departedAt: now, shippedPackages: 20, dispatches: dispatchDetails.map(row => ({ dispatchNo: row.dispatchNo, shippedPackages: 10 })) } } },
|
||||
departure: { tripNo: proof.tripNo, status: 'IN_TRANSIT', departedAt: now, shippedPackages: dispatchDetails.reduce((n,row)=>n+row.packageCount,0), dispatches: dispatchDetails.map(row => ({ dispatchNo: row.dispatchNo, shippedPackages: row.packageCount })) } } },
|
||||
assert(_module, label, _response, passed) { calls.push({ assertion: label, passed }) },
|
||||
async requestMultipart(method, path, file) {
|
||||
calls.push({ method, path, certificate: file.name }); assert.ok(file.name.includes('TEST-NOT-REAL')); assert.ok(file.content.byteLength > 0)
|
||||
drivers.get('22').recordVersion = '1'; return ok({ id: '22', recordVersion: '1', licenseFileId: 'TEST-LICENSE-FILE' })
|
||||
Object.assign(drivers.get('22'), { recordVersion: '1', licenseFileId: 'TEST-LICENSE-FILE' });
|
||||
return ok(fault === 'upload-identity' ? { ...drivers.get('22'), id: 'OTHER' } : drivers.get('22'))
|
||||
},
|
||||
async request(method, path, body, token) {
|
||||
calls.push({ method, path, body: copy(body), token })
|
||||
@@ -47,12 +58,13 @@ function fixture(fault = '') {
|
||||
const dispatchNo = path.split('/').at(-2), customer = dispatchNo.slice(-1)
|
||||
const current = handover && fault !== 'pda-keeps-original' ? handover.snapshot.replacement : originalTransport
|
||||
return ok({ dispatchNo, tripNo: trip.tripNo, originalDeparture: fault === 'rewritten-pda-departure' && handover ? current : originalTransport,
|
||||
deliveries: [{ deliveryNo: `DL-${customer}`, status: 'WAIT_DELIVERY', packages: 10, terminalAt: null,
|
||||
transport: { vehiclePlate: current.vehiclePlate, driverName: current.driverName, driverPhone: current.driverPhone } }],
|
||||
deliveries: cargo.filter(row=>row.dispatchNo===dispatchNo).map((row,index)=>({ deliveryNo: `DL-${customer}-${index}`, status: 'WAIT_DELIVERY', packages: row.packages, terminalAt: null,
|
||||
transport: { vehiclePlate: current.vehiclePlate, driverName: current.driverName, driverPhone: current.driverPhone } })),
|
||||
latestHandover: handover ? { handoverNo: handover.handoverNo } : null })
|
||||
}
|
||||
if (path.startsWith('/api/platform/v1/last-leg/dispatches/')) return ok(liveDispatches.find(row => row.dispatchNo === path.split('/').at(-1)))
|
||||
if (path.endsWith('/master-data/vehicles') && method === 'POST') {
|
||||
assert.equal(body.plateNumber, body.plateNumber.toUpperCase(), 'request must use the formal uppercase vehicle plate')
|
||||
const vehicle = { ...body, id: String(12 + createdVehicles++), recordVersion: '0' }; vehicles.set(vehicle.id, vehicle); return ok(vehicle)
|
||||
}
|
||||
if (path.includes('/master-data/vehicles/')) {
|
||||
@@ -61,13 +73,18 @@ function fixture(fault = '') {
|
||||
return ok(vehicle)
|
||||
}
|
||||
if (path.endsWith('/fleet/drivers') && method === 'POST') {
|
||||
const driver = { id: '22', recordVersion: '0', details: body, active: true }; drivers.set(driver.id, driver); return ok(driver)
|
||||
const profile = fault === 'driver-default-null' ? { warehouseId: null, vehicleModels: null, languagesAndArea: null,
|
||||
medicalExpiresOn: null, trainingExpiresOn: null, insuranceExpiresOn: null } : {}
|
||||
const driver = { id: '22', driverNo: 'DR-TEST', recordVersion: '0', details: { ...body, profile,
|
||||
...(fault === 'driver-default-null' ? { licenseIssuedOn: null } : {}) }, active: true, reviewStatus: 'PENDING' }; drivers.set(driver.id, driver);
|
||||
if (fault === 'driver-default-nonempty') driver.details.profile.warehouseId = 'OTHER'
|
||||
return ok(fault === 'driver-identity' ? { ...driver, recordVersion: '3' } : driver)
|
||||
}
|
||||
if (path.includes('/fleet/drivers/')) {
|
||||
const driver = drivers.get('22')
|
||||
if (path.endsWith('/review')) { driver.reviewStatus = 'APPROVED'; driver.recordVersion = '2' }
|
||||
if (path.endsWith('/status')) { driver.active = body.active; driver.recordVersion = '3' }
|
||||
return ok(driver)
|
||||
return ok(fault === 'review-version' && path.endsWith('/review') ? { ...driver, recordVersion: '1' } : driver)
|
||||
}
|
||||
if (path.endsWith('/fleet/trips') && method === 'POST') {
|
||||
const next = { ...body, tripNo: `PROBE-${probes.size + 1}`, status: 'PLANNED', recordVersion: '0' }; probes.set(next.tripNo, next); return ok({ trip: next, dispatches: [] })
|
||||
@@ -93,9 +110,13 @@ function fixture(fault = '') {
|
||||
return ok({ records, total: records.length })
|
||||
}
|
||||
if (path.endsWith('/handovers/preview')) {
|
||||
preview = { cargoHash: 'a'.repeat(64), driverQualification: { canDispatch: true }, cargo: { remainingCargo: cargo.map(row => ({ customerId: row.customerId, dispatchNo: row.dispatchNo, itemId: row.dispatchItemId,
|
||||
palletId: row.warehousePalletId, destinationKey: row.destinationKey, packages: row.packages })), completedDeliveries: [], remainingPalletIds: ['PALLET-INTERNAL'], occupied: { palletSpaces: 1, weightKg: 100, volumeCbm: 1 } } }
|
||||
preview = { tripNo: trip.tripNo, recordVersion: trip.recordVersion,
|
||||
previous: { vehicleId: '11', driverId: '21', ...originalTransport }, replacement: { vehicleId: '12', driverId: '22' },
|
||||
capacity: { palletSpaces: 30, weightKg: 18000, volumeCbm: 90 }, cargoHash: 'a'.repeat(64), driverQualification: { canDispatch: true }, cargo: { remainingCargo: cargo.map(row => ({ customerId: row.customerId, dispatchNo: row.dispatchNo, itemId: row.dispatchItemId,
|
||||
palletId: row.warehousePalletId, destinationKey: row.destinationKey, packages: row.packages })), completedDeliveries: [], remainingPalletIds: pallets.map(row=>row.warehousePalletId), occupied: { palletSpaces: pallets.length, weightKg: pallets.length*100, volumeCbm: pallets.length } } }
|
||||
if (fault === 'wrong-customer') preview.cargo.remainingCargo[0].customerId = 'OTHER'
|
||||
if (fault === 'preview-identity') preview.replacement.driverId = 'OTHER'
|
||||
if (fault === 'preview-capacity') preview.capacity.palletSpaces = 29
|
||||
if (fault === 'double-count-pallet') preview.cargo.occupied.palletSpaces = 2
|
||||
return ok(preview)
|
||||
}
|
||||
@@ -120,8 +141,10 @@ function fixture(fault = '') {
|
||||
return ok(handover)
|
||||
}
|
||||
assert.equal(String(body.recordVersion), handover.recordVersion)
|
||||
resources.delete(resource); handover[field] = { actualEndAt: body.actualEndAt, location: body.location }
|
||||
handover.recordVersion = String(BigInt(handover.recordVersion) + 1n); bump(); return ok(handover)
|
||||
resources.delete(resource); handover[field] = { actualEndAt: body.actualEndAt, location: body.location,
|
||||
idempotencyKey: body.idempotencyKey, vehicleStatus: body.vehicleStatus }
|
||||
handover.recordVersion = String(BigInt(handover.recordVersion) + 1n); bump();
|
||||
return ok(fault === 'release-version' && vehicle ? { ...handover, recordVersion: '99' } : handover)
|
||||
}
|
||||
if (path.endsWith('/trips/TR-ONE') && method === 'GET') return ok({ trip, dispatches: bindings })
|
||||
throw new Error(`Unexpected test route ${method} ${path}`)
|
||||
@@ -210,7 +233,9 @@ test('every HTTP call observes a completed pre-send checkpoint; post-response di
|
||||
assert.equal(saved.at(-1).requests.at(-1).status, 'SENDING')
|
||||
})
|
||||
|
||||
for (const [fault, message] of Object.entries({ 'binding-customer': '每张排车已发车', 'dispatch-cargo': '每张排车已发车', 'wrong-customer': '预览精确包含', 'double-count-pallet': '共享实托只计一次', 'stale-accepted': '陈旧版本交接必须拒绝', 'unrelated-conflict': '陈旧版本交接必须拒绝',
|
||||
for (const [fault, message] of Object.entries({ 'driver-default-nonempty': '接替司机返回身份和版本', 'driver-identity': '接替司机返回身份和版本', 'upload-identity': '测试证照有正式文件归属',
|
||||
'review-version': '接替司机已正式审核', 'preview-identity': '交接预览绑定原车次', 'preview-capacity': '交接预览容量', 'release-version': '原车辆已结束',
|
||||
'binding-customer': '每张排车已发车', 'dispatch-cargo': '每张排车已发车', 'wrong-customer': '预览精确包含', 'double-count-pallet': '共享实托只计一次', 'stale-accepted': '陈旧版本交接必须拒绝', 'unrelated-conflict': '陈旧版本交接必须拒绝',
|
||||
'pda-keeps-original': 'PDA保持原发车资源', 'rewritten-pda-departure': 'PDA保持原发车资源', 'fee-changed': '交接不新增或改写派送费用',
|
||||
'rewritten-departure': '只新增一次交接且原DEPART内容保持不变', 'replay-releases-next': '旧请求重放不能释放下一计划车辆' })) {
|
||||
test(`business counterexample ${fault} cannot be reported as successful handover`, async () => {
|
||||
@@ -247,8 +272,35 @@ test('explicit closure cleanup cancels only test probes and disables newly creat
|
||||
assert.ok(!f.calls.some(row => row.method === 'DELETE'))
|
||||
})
|
||||
|
||||
test('formal driver response accepts empty profile with omitted or NULL optional fields', async () => {
|
||||
const f = fixture('driver-default-null')
|
||||
assert.equal((await verifyFleetHandover(f.context, 'test', f.proof, 'picker-secret', f.dispatchDetails)).status, 'VERIFIED')
|
||||
})
|
||||
|
||||
test('r5 two dispatches, three pallets and three deliveries preserve multi-destination cargo through handover and cleanup', async () => {
|
||||
const f = fixture('r5-layout')
|
||||
const result = await verifyFleetHandover(f.context, 'test', f.proof, 'picker-secret', f.dispatchDetails)
|
||||
assert.equal(result.status, 'VERIFIED'); assert.equal(result.pdaAfter.flatMap(row=>row.deliveries).length, 3)
|
||||
assert.equal(result.preview.cargo.remainingPalletIds.length, 3)
|
||||
f.proof.closure = { status: 'CARGO_COMPLETED_AND_RESOURCES_RELEASED' }
|
||||
f.trip.status = 'COMPLETED'; f.trip.vehicleRelease = { actualEndAt: new Date().toISOString() }; f.trip.driverRelease = { actualEndAt: new Date().toISOString() }
|
||||
f.resources.delete('VEHICLE:12'); f.resources.delete('DRIVER:22')
|
||||
assert.equal((await cleanupFleetHandoverResources(f.context, 'test', f.proof)).status, 'COMPLETED')
|
||||
})
|
||||
|
||||
test('explicit reviewed vehicle reuse skips the original POST and proceeds from driver creation', async () => {
|
||||
const f = fixture('reviewed-vehicle'), snapshot = copy(f.vehicles.get('12'))
|
||||
const reviewedVehicle = { snapshot, occupation: { vehicleId: '12', assignmentCount: 0 },
|
||||
creationRequest: { method: 'POST', status: 'RESPONDED', http: 200, code: '200', requestId: 'r', traceId: 't',
|
||||
body: { plateNumber: 'HO-24255909-3bc' }, result: snapshot } }
|
||||
const result = await verifyFleetHandover(f.context, 'test', f.proof, 'picker-secret', f.dispatchDetails, { reviewedVehicle })
|
||||
assert.equal(result.status, 'VERIFIED'); assert.equal(result.replacement.vehicleId, '12')
|
||||
const created = f.calls.filter(row=>row.method==='POST' && row.path?.endsWith('/master-data/vehicles'))
|
||||
assert.equal(created.length, 1); assert.match(created[0].body.plateNumber, /^HO-PROBE-/)
|
||||
})
|
||||
|
||||
test('internal helper has no environment loader, SQL or standalone execution path', async () => {
|
||||
const source = await readFile(new URL('./fleet-handover-acceptance.mjs', import.meta.url), 'utf8')
|
||||
assert.doesNotMatch(source, /process\.env|process\.argv|fetch\(|execFile|INSERT INTO|UPDATE ll_|DELETE FROM/)
|
||||
assert.match(source, /export async function verifyFleetHandover\(context, module, tripProof, pickerToken, dispatchDetails\)/)
|
||||
assert.match(source, /export async function verifyFleetHandover\(context, module, tripProof, pickerToken, dispatchDetails, \{ reviewedVehicle \} = \{\}\)/)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user