import test from 'node:test' import assert from 'node:assert/strict' import { readCurrentBoxedInventory } from './current-boxed-inventory.mjs' function fixture(fault = '') { const calls = [] const context = { marker: 'RUN', createdResources: { phase4OverseasPda: { marker: fault === 'old-run' ? 'OLD' : 'RUN', finalStatus: 'COMPLETED', overseasWarehouseId: 'WH', generatedPalletsByInbound: { IN: [{}] }, } }, apiSuccess(_m, _n, response) { return response.body.data }, async request(method, path, body, token) { calls.push({ method, path }); assert.equal(method, 'GET'); assert.equal(token, 'merchant') let data if (path.includes('/cargo-discards/')) data = [{ inboundCode: 'IN', orderId: '100', overseasWarehouseId: 'WH', cartons: [{ cartonId: '200', cartonMark: 'BOX', palletNo: 'REAL' }] }] else if (path.includes('/inventory-source-cartons?')) { data = [{ cartonMark: 'BOX', items: [{ skuCode: 'SKU', quantity: fault === 'unknown-quantity' ? null : '10' }] }] if (fault === 'duplicate-carton') data.push(data[0]) } else data = [{ sourceType: 'INBOUND', inboundCode: fault === 'foreign-inbound' ? 'OTHER' : 'IN', overseasWarehouseId: fault === 'foreign-warehouse' ? 'OTHER' : 'WH', inventoryBalanceId: '10', inboundOrderId: '100', skuCode: 'SKU', positions: [{ positionId: '20', palletNo: fault === 'wrong-pallet' ? 'UNBOUND' : 'REAL', availablePackages: '2' }] }] return { http: 200, body: { code: '200', data } } } } return { context, calls } } test('boxed source joins only current inbound, authoritative carton SKU quantities and real physical pallet', async () => { const { context, calls } = fixture() const [source] = await readCurrentBoxedInventory(context, 'merchant', 'WH', 'test') assert.equal(source.cartons[0].cartonMark, 'BOX') assert.equal(source.cartons[0].positionId, '20') assert.equal(source.cartons[0].items[0].quantity, '10') assert.ok(calls.every(call => !call.path.includes('OTHER'))) }) for (const fault of ['unknown-quantity', 'wrong-pallet', 'foreign-inbound', 'foreign-warehouse']) test(`boxed source rejects unsupported or foreign facts: ${fault}`, async () => { assert.deepEqual(await readCurrentBoxedInventory(fixture(fault).context, 'merchant', 'WH', 'test'), []) }) test('old marker blocks before HTTP; duplicate authoritative carton marks stop instead of choosing first', async () => { const old = fixture('old-run') await assert.rejects(readCurrentBoxedInventory(old.context, 'merchant', 'WH', 'test'), /本轮/) assert.equal(old.calls.length, 0) await assert.rejects(readCurrentBoxedInventory(fixture('duplicate-carton').context, 'merchant', 'WH', 'test'), /重复/) })