157 lines
13 KiB
JavaScript
157 lines
13 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
import test from 'node:test'
|
|
import { fileURLToPath } from 'node:url'
|
|
|
|
import { run } from './phase4-overseas-warehouse.mjs'
|
|
|
|
const here = path.dirname(fileURLToPath(import.meta.url))
|
|
const source = fs.readFileSync(path.join(here, 'phase4-overseas-warehouse.mjs'), 'utf8')
|
|
|
|
function response(data, code = '200') { return { http: 200, body: { code, message: code === '200' ? 'success' : 'conflict', data }, requestId: 'request-test', traceId: 'trace-test' } }
|
|
function voidResponse() { return { http: 200, body: { code: '200', message: 'success' }, requestId: 'request-test', traceId: 'trace-test' } }
|
|
function page(records = []) { return { records, page: 1, size: 20, total: records.length, pages: records.length ? 1 : 0 } }
|
|
function contextFixture({ tenants = [{ id: '10001', status: 'ACTIVE' }], warehouseRecords = [{ id: '20001', status: 'ACTIVE', warehouseType: 'OVERSEAS', warehouseCode: 'LAX', warehouseName: '洛杉矶仓' }] } = {}) {
|
|
const calls = []; const steps = []; const gaps = []; let createdPallet = null; let createdLocation = null
|
|
const context = {
|
|
context: {}, createdResources: {}, marker: 'ACC-PHASE4-TEST', calls, steps, gaps,
|
|
async request(method, requestPath, body) {
|
|
calls.push({ channel: 'domestic', method, path: requestPath, body })
|
|
if (requestPath.startsWith('/api/platform/v1/iam/tenants?')) return response(page(tenants))
|
|
if (method === 'GET' && requestPath.startsWith('/api/platform/v1/warehouse-admin?')) return response(page(warehouseRecords))
|
|
if (method === 'GET' && requestPath.includes('/warehouse-admin/20001?')) return response({ warehouse: { id: '20001', customerId: '0', warehouseType: 'OVERSEAS', status: 'ACTIVE' } })
|
|
if (method === 'POST' && requestPath === '/api/platform/v1/warehouse-admin') return response('20001')
|
|
if (method === 'PATCH' && requestPath.includes('/warehouse-admin/20001/status?')) return response(null)
|
|
throw new Error(`Unexpected domestic path: ${requestPath}`)
|
|
},
|
|
async requestOverseas(method, requestPath, body) {
|
|
calls.push({ channel: 'overseas', method, path: requestPath, body })
|
|
if (method === 'POST' && requestPath.endsWith('/warehouse-admin/20001/locations')) { createdLocation = { id: '72002', customerId: '0', warehouseId: '20001', ...body }; return response('72002') }
|
|
if (method === 'PUT' && requestPath.endsWith('/warehouse-admin/20001/locations/72002')) { createdLocation = { ...createdLocation, ...body }; return voidResponse() }
|
|
if (method === 'DELETE' && requestPath.endsWith('/warehouse-admin/20001/locations/72002')) { createdLocation = null; return voidResponse() }
|
|
if (method === 'POST' && requestPath.endsWith('/warehouse-admin/20001/pallets')) { createdPallet = { id: '71001', customerId: '0', warehouseId: '20001', locationId: '72001', version: 0, ...body }; return response('71001') }
|
|
if (method === 'PUT' && requestPath.endsWith('/warehouse-admin/20001/pallets/71001')) {
|
|
if (Number(body.version) !== Number(createdPallet.version)) return response(null, 'A0409')
|
|
createdPallet = { ...createdPallet, ...body, version: createdPallet.version + 1 }; return voidResponse()
|
|
}
|
|
if (requestPath.startsWith('/api/platform/v1/last-leg/orders?')) return response(page([{ id: '30001', orderNo: 'LL-1', currentStatus: 'PENDING_ASSIGNMENT' }]))
|
|
if (requestPath.includes('/last-leg/orders/30001?')) return response({ order: { id: '30001' }, cargo: [], statusHistory: [] })
|
|
if (requestPath.startsWith('/api/platform/v1/last-leg/relabel-applications?')) return response(page([{ id: '40001', relabelNo: 'RL-1', status: 'PENDING_PROCESSING' }]))
|
|
if (requestPath.includes('/relabel-applications/RL-1?')) return response({ application: { relabelNo: 'RL-1' }, items: [], history: [] })
|
|
if (requestPath.startsWith('/api/platform/v1/last-leg/inbound-plans?')) return response(page([{ lineId: '50002', planId: '50001', orderId: '30001', planNo: 'IP-1', customerId: '10001', status: 'PENDING_ASSIGNMENT' }]))
|
|
if (requestPath.includes('/inbound-plans/IP-1?')) return response({ planNo: 'IP-1', sourceOrderId: '30001', lines: [], pallets: [], allocations: [] })
|
|
if (requestPath.includes('/inventory/summary?')) return response({ inboundOrderCount: 1, palletCount: 1, totalPackages: 10, availablePackages: 10, holdPackages: 0, inTransitPackages: 0, receivingPackages: 0, procurementPackages: 0 })
|
|
if (requestPath.includes('/inventory/balances?')) return response(page([{ id: '60001', customerId: '10001', skuCode: 'SKU-1', totalPackages: 10, availablePackages: 10, updatedAt: '2026-08-21T10:00:00' }]))
|
|
if (requestPath.includes('/inventory/balances/60001/positions?')) return response([{ id: '61001', balanceId: '60001', totalPackages: 10 }])
|
|
if (requestPath.includes('/inventory/ledger?')) return response(page([{ id: '62001', customerId: '10001', businessType: 'STOCK_IN', beforePackages: 0, deltaPackages: 10, afterPackages: 10, occurredAt: '2026-08-21T10:00:00' }]))
|
|
if (requestPath.endsWith('/warehouse-admin/20001/locations?page=1&size=20')) return response(page([{ id: '72001', customerId: '0', warehouseId: '20001', locationCode: 'A-01', locationType: 'STORAGE', maxLoadKg: 1000, status: 'ACTIVE' }]))
|
|
if (requestPath.endsWith('/warehouse-admin/20001/locations/72001')) return response({ id: '72001', customerId: '0', warehouseId: '20001', locationCode: 'A-01', status: 'ACTIVE' })
|
|
if (requestPath.endsWith('/warehouse-admin/20001/locations/72002')) return createdLocation == null
|
|
? response(undefined, 'A0404') : response(createdLocation)
|
|
if (requestPath.endsWith('/warehouse-admin/20001/pallets?page=1&size=20')) return response(page([{ id: '70001', customerId: '0', warehouseId: '20001', palletCode: 'PLT-1', status: 'IDLE' }]))
|
|
if (requestPath.endsWith('/warehouse-admin/20001/pallets/70001')) return response({ id: '70001', customerId: '0', warehouseId: '20001' })
|
|
if (requestPath.endsWith('/warehouse-admin/20001/pallets/71001')) return response(createdPallet)
|
|
throw new Error(`Unexpected path: ${requestPath}`)
|
|
},
|
|
async requestOverseasBinary(method, requestPath) { calls.push({ channel: 'overseas-binary', method, path: requestPath }); return { http: 200, bytes: Buffer.from('id,status\n'), contentType: 'text/csv', requestId: 'request-binary', traceId: 'trace-binary' } },
|
|
apiSuccess(_module, _name, result) { return result?.http === 200 && String(result?.body?.code) === '200' ? result.body.data : null },
|
|
assert(module, name, result, passed, evidence, options) { steps.push({ module, name, result, passed: Boolean(passed), evidence, options }) },
|
|
addStep(module, name, result, passed, evidence, options) { steps.push({ module, name, result, passed: Boolean(passed), evidence, options }) },
|
|
addCoverageGap(...args) { gaps.push(args) },
|
|
}
|
|
return context
|
|
}
|
|
|
|
test('模块只依赖注入请求,不读取环境或自行联网', () => {
|
|
assert.match(source, /export async function run\(context\)/)
|
|
assert.match(source, /context\.requestOverseas\('GET'/)
|
|
assert.match(source, /context\.requestOverseasBinary\('GET'/)
|
|
assert.doesNotMatch(source, /https?:\/\//)
|
|
assert.doesNotMatch(source, /process\.env|fetch\(|requestBinary/)
|
|
})
|
|
|
|
test('既有仓库场景只写本轮库位与托盘生命周期', () => {
|
|
const context = contextFixture()
|
|
return run(context).then(() => {
|
|
assert.ok(context.calls.length >= 12)
|
|
assert.ok(context.calls.every((call) => ['GET', 'POST', 'PUT', 'DELETE'].includes(call.method)))
|
|
assert.equal(context.calls.filter((call) => call.channel === 'domestic').length, 3)
|
|
assert.ok(context.calls.some((call) => call.channel === 'domestic' && call.path.startsWith('/api/platform/v1/warehouse-admin?')))
|
|
assert.ok(context.calls.some((call) => call.channel === 'domestic' && call.path.includes('/warehouse-admin/20001?')))
|
|
assert.equal(context.calls.some((call) => call.channel === 'overseas' && call.path === '/api/platform/v1/warehouse-admin'), false)
|
|
assert.ok(context.calls.filter((call) => call.channel === 'overseas').every((call) => call.path.startsWith('/api/platform/v1/')))
|
|
const writes = context.calls.filter((call) => call.method !== 'GET')
|
|
assert.ok(writes.some((call) => call.method === 'DELETE' && call.path.includes('/locations/72002')))
|
|
assert.ok(writes.some((call) => call.path.includes('/warehouse-admin/20001/pallets')))
|
|
assert.equal(writes.some((call) => call.path.includes('/relabel-applications')), false)
|
|
assert.ok(writes.every((call) => call.path.includes('/warehouse-admin/20001/')))
|
|
for (const name of ['编辑本轮公共库位', '删除本轮公共库位', '报废本轮公共托盘']) {
|
|
const step = context.steps.find((item) => item.name === name)
|
|
assert.ok(step?.passed, name)
|
|
assert.equal(Object.hasOwn(step.result?.body || {}, 'data'), false, name)
|
|
}
|
|
const deletedReadback = context.steps.find((step) => step.name === '本轮公共库位已删除且回查不存在')
|
|
assert.equal(deletedReadback?.result?.body?.code, 'A0404')
|
|
assert.ok(deletedReadback?.passed)
|
|
assert.ok(context.steps.every((step) => step.passed), context.steps.filter((step) => !step.passed).map((step) => `${step.name}: ${step.evidence}`).join('\n'))
|
|
})
|
|
})
|
|
|
|
test('覆盖冻结的海外只读列表与有记录才查详情', async () => {
|
|
const context = contextFixture()
|
|
await run(context)
|
|
for (const pathToken of ['/last-leg/orders?', '/relabel-applications?', '/inbound-plans?', '/inventory/summary?', '/inventory/balances?', '/inventory/ledger?', '/locations?', '/pallets?']) {
|
|
assert.ok(context.calls.some((call) => call.channel === 'overseas' && call.path.includes(pathToken)), pathToken)
|
|
}
|
|
assert.ok(context.calls.some((call) => call.channel === 'domestic' && call.path.includes('/warehouse-admin?')))
|
|
assert.ok(context.calls.some((call) => call.channel === 'domestic' && call.path.includes('/warehouse-admin/20001?')))
|
|
for (const detailToken of ['/orders/30001?', '/relabel-applications/RL-1?', '/inbound-plans/IP-1?', '/balances/60001/positions?', '/pallets/70001']) {
|
|
assert.ok(context.calls.some((call) => call.path.includes(detailToken)), detailToken)
|
|
}
|
|
for (const call of context.calls.filter((item) => item.path.includes('/inventory/summary?') || item.path.includes('/inventory/balances/export?') || item.path.includes('/inventory/ledger/export?'))) {
|
|
const params = new URL(`http://acceptance${call.path}`).searchParams
|
|
assert.equal(params.has('page'), false, call.path)
|
|
assert.equal(params.has('size'), false, call.path)
|
|
}
|
|
assert.deepEqual(context.context.phase4OverseasWarehouse, { customerId: '10001', warehouseId: '20001', palletId: '71001' })
|
|
assert.deepEqual(context.createdResources.phase4OverseasWarehouse, {
|
|
marker: 'ACC-PHASE4-TEST', customerId: '10001', warehouseId: '20001', palletId: '71001', terminalStatus: 'SCRAPPED',
|
|
locationId: '72002', locationTerminalStatus: 'DELETED',
|
|
})
|
|
})
|
|
|
|
test('缺少客户时明确失败,缺少仓库时通过正式接口创建并停用fixture', async () => {
|
|
const noCustomer = contextFixture({ tenants: [] })
|
|
await run(noCustomer)
|
|
assert.equal(noCustomer.gaps.length, 0)
|
|
assert.ok(noCustomer.steps.some((step) => step.name === '海外仓验收必须取得ACTIVE客户' && !step.passed))
|
|
assert.equal(noCustomer.calls.filter((call) => call.channel === 'overseas').length, 0)
|
|
|
|
const noWarehouse = contextFixture({ warehouseRecords: [] })
|
|
await run(noWarehouse)
|
|
assert.equal(noWarehouse.gaps.some((gap) => gap[2] === '公共托盘真实仓库前置'), false)
|
|
assert.ok(noWarehouse.calls.some((call) => call.method === 'POST' && call.path === '/api/platform/v1/warehouse-admin'))
|
|
assert.ok(noWarehouse.calls.some((call) => call.method === 'PATCH' && call.path.includes('/warehouse-admin/20001/status?')))
|
|
assert.ok(noWarehouse.calls.some((call) => call.path.includes('/pallets')))
|
|
})
|
|
|
|
test('合同覆盖库存CSV并明确排除暂缓、离仓、PDA与内部路径', () => {
|
|
assert.match(source, /inventory\/balances\/export/)
|
|
assert.match(source, /inventory\/ledger\/export/)
|
|
assert.match(source, /inventory\/summary[^\n]+\{ customerId \}/)
|
|
assert.match(source, /inventory\/balances\/export[^\n]+\{ customerId \}/)
|
|
assert.match(source, /inventory\/ledger\/export[^\n]+\{ customerId \}/)
|
|
for (const forbidden of ['/cartons', '/products', '/outbound', '/dispatch', '/delivery', '/pda', '/internal']) assert.equal(source.includes(forbidden), false, forbidden)
|
|
assert.match(source, /requestId=/)
|
|
assert.match(source, /traceId=/)
|
|
for (const field of ['lineId', 'planId', 'orderId']) {
|
|
assert.match(source, new RegExp(`longId\\(plan\\.${field}\\)`))
|
|
}
|
|
assert.match(source, /id\(pallet\.customerId\) === '0'/)
|
|
assert.doesNotMatch(source, /创建本轮换标单|verifyRelabelLifecycle/)
|
|
assert.match(source, /创建本轮公共库位/)
|
|
assert.match(source, /公共托盘旧版本编辑返回冲突/)
|
|
assert.doesNotMatch(source, /deploymentBlocker|addCoverageGap|coverageGap/)
|
|
})
|