import test from 'node:test' import assert from 'node:assert/strict' import { findPackingOrders, getReturnCartonLabel, mergeReturnCartons, normalizePackingPreview } from '../src/api/return-packing.js' import { session } from '../src/stores/session.js' test('packing preview keeps customer scope internal and exposes stable order references', () => { const value = normalizePackingPreview({ customerId: '100', orders: [{ returnNo: 'RT-1', customerId: '100', customerReferenceNo: 'REF-1', status: 'COMPLETED' }], skus: [{ skuCode: 'SKU-1', productNameZh: '商品', availableQuantity: 8 }] }) assert.equal(value.customerKey, '100') assert.equal(value.orders[0].returnNo, 'RT-1') assert.equal('customerId' in value.orders[0], false) assert.equal(value.skus[0].availableQuantity, 8) }) test('legacy single-customer preview is normalized to all customer orders', () => { const value = normalizePackingPreview({ customerId: '100', orders: [{ returnNo: 'RT-1' }, { returnNo: 'RT-2' }], skus: [{ skuCode: 'SKU-1', availableQuantity: 8 }] }) assert.deepEqual(value.customers[0].orders.map((item) => item.returnNo), ['RT-1', 'RT-2']) }) test('return packing API uses exact PDA inventory routes without submitting customer or warehouse scope', async () => { const calls = [] session.apiBase = '/api'; session.token = 'packing-token' globalThis.uni = { request(options) { calls.push({ method: options.method, url: options.url, data: options.data, authorization: options.header.Authorization }); let data = {}; if (options.url.includes('/packing/orders')) data = { customerId: '100', orders: [], skus: [] }; else if (options.url.endsWith('/cartons/merge')) data = { cartonMark: 'RC-1', skuCode: 'SKU-1', totalQuantity: 2, label: { cartonMark: 'RC-1', items: [] } }; else data = { cartonMark: 'RC/1', totalQuantity: 2, items: [] }; options.success({ statusCode: 200, data: { code: '200', data }, header: {} }) } } await findPackingOrders('RT/1') const request = { returnNos: ['RT/1'], items: [{ skuCode: 'SKU-1', quantity: 2 }], length: 10, width: 20, height: 30, weight: 4, idempotencyKey: 'merge-key' } await mergeReturnCartons(request) await getReturnCartonLabel('RC/1') assert.deepEqual(calls.map((item) => item.url), [ '/api/pda/v1/last-leg/return-inventory/packing/orders?returnNo=RT%2F1', '/api/pda/v1/last-leg/return-inventory/cartons/merge', '/api/pda/v1/last-leg/return-inventory/cartons/RC%2F1/label' ]) assert.deepEqual(calls[1].data, request) assert.ok(calls.every((item) => item.authorization === 'Bearer packing-token')) assert.ok(calls.every((item) => !Object.hasOwn(item.data || {}, 'customerId') && !Object.hasOwn(item.data || {}, 'warehouseId'))) }) test('return packing lookup treats not found as an empty preview', async () => { session.apiBase = '/api'; session.token = 'packing-token' globalThis.uni = { request(options) { options.success({ statusCode: 404, data: { code: 'A0404', message: '没有匹配的可合箱库存' }, header: {} }) } } assert.deepEqual(await findPackingOrders('MISSING-RETURN'), { customerKey: '', orders: [], skus: [] }) }) test('return packing lookup still rejects real service errors', async () => { session.apiBase = '/api'; session.token = 'packing-token' globalThis.uni = { request(options) { options.success({ statusCode: 500, data: { code: 'B0500', message: '服务暂不可用' }, header: {} }) } } await assert.rejects(() => findPackingOrders('RT-1'), (error) => error.code === 'B0500') })