import test from 'node:test' import assert from 'node:assert/strict' import { attachLooseboardToTrip, verifyLooseboardDelivered } from './looseboard-fulfillment.mjs' function fixture(fault = '') { const demand = { marker: 'fixture', orderNo: 'LB-1', customerId: '101', finalStatus: 'PENDING_ACCEPTANCE' } const tripProof = { marker: 'FT-independent', tripNo: 'TR-1', dispatchNo: 'DP-1', warehouseId: '42', vehicleId: '11', driverId: '12', stops: [{}, { destinationKey: 'FBA:TEST' }] } const calls = [], stages = []; let reviewed = false, complete = false const context = { marker: 'fixture', createdResources: { fleetTrips: [tripProof], phase4OverseasBusiness: { looseboard: demand } }, assert(_m, label, _r, ok) { assert.ok(ok, label) }, async checkpoint() {}, async request(method, path, body) { calls.push({ method, path, body }) let data if (path.includes('/local-looseboard-orders/')) data = { summary: { orderNo: 'LB-1', customerId: '101', status: complete ? 'COMPLETED' : 'PENDING_ACCEPTANCE', estimatedPalletCount: 2, packageCount: 3, fleetArrangement: complete ? { fulfillmentStage: 'COMPLETED', bookingNo: 'BK-1', tripNo: 'TR-1', dispatchNo: 'DP-1', incomingNo: 'IN-1', deliveryNo: 'DE-1', signedPackages: fault === 'signed quantity' ? 2 : 3 } : null } } else if (path.includes('/inventory/ledger?')) data = { total: 1, records: [{ id: '91', businessOrderNo: 'IN-1', businessType: 'STOCK_IN', customerId: '101', overseasWarehouseId: '42', deltaPackages: fault === 'ledger' ? 4 : 3 }] } else if (method === 'POST') { stages.push(path.split('/').at(-1)) reviewed = stages.length === 3 data = { recordVersion: stages.length + 1 } } else data = { dispatchNo: 'DP-1', stage: reviewed ? 'LOADING' : 'BEFORE_LOADING', recordVersion: 1, counts: { pickedPackageCount: 0 }, reviewState: 'APPROVED', reviews: { dispatchReview: { status: 'APPROVED' }, loadingReview: { status: 'APPROVED' } }, vehicleInfo: { vehicle: { vehicleId: fault === 'vehicle' ? '99' : '11' }, driver: { driverId: '12' } } } return { http: 200, body: { code: '200', data }, requestId: 'req', traceId: 'trace' } } } const helpers = { async prepare(ctx, args) { assert.equal(ctx, context); assert.equal(args.demand, demand); assert.equal(args.tripProof, tripProof) demand.poolBinding = { bookingNo: 'BK-1', createRequest: { cargo: { packageCount: 3 } } } }, async receive(ctx, args) { assert.equal(args.pdaToken, 'pda'); assert.deepEqual(args.measurements.map(m => m.cartonCount), [2, 1]) demand.receipt = { incomingNo: 'IN-1' } }, async arrange() { return { status: 'BOUND_AWAITING_REVIEW', dispatchNo: 'DP-1', addedItems: [{ sourceKey: 'NEW-1' }, { sourceKey: 'NEW-2' }] } }, } return { context, tripProof, demand, helpers, calls, stages, markCompleted: () => { complete = true } } } test('default orchestration uses original demand and existing trip, then verifies original signed order', async () => { const f = fixture() const result = await attachLooseboardToTrip(f.context, f.tripProof, 'pda', f.helpers) assert.equal(result.proof.status, 'READY_FOR_SHARED_LOADING') assert.deepEqual(f.stages, ['submit-review', 'dispatch-review', 'loading-review']) assert.deepEqual(result.proof.sourceKeys, ['NEW-1', 'NEW-2']) f.markCompleted() await verifyLooseboardDelivered(f.context, f.tripProof, [{ deliveryNo: 'DE-1', customerId: '101' }]) assert.equal(f.demand.finalStatus, 'COMPLETED') assert.equal(f.demand.fulfillment.status, 'COMPLETED_HTTP_VERIFIED') }) for (const fault of ['ledger', 'vehicle']) test(`orchestration stops on ${fault}`, async () => { const f = fixture(fault) await assert.rejects(attachLooseboardToTrip(f.context, f.tripProof, 'pda', f.helpers)) assert.equal(f.demand.fulfillment.status, 'FAILED') if (fault === 'ledger') assert.equal(f.stages.length, 0) }) test('whole vehicle signed does not substitute for original looseboard signed quantity', async () => { const f = fixture('signed quantity') await attachLooseboardToTrip(f.context, f.tripProof, 'pda', f.helpers) f.markCompleted() await assert.rejects(verifyLooseboardDelivered(f.context, f.tripProof, [{ deliveryNo: 'DE-1', customerId: '101' }])) assert.notEqual(f.demand.fulfillment.status, 'COMPLETED_HTTP_VERIFIED') })