Files
baiyue-firstleg/scripts/admin-test-env-modules/master-data.mjs
T

195 lines
16 KiB
JavaScript
Raw Normal View History

2026-08-08 10:11:04 +08:00
function id(value) { return value == null ? '' : String(value) }
function pageOk(value) { return Array.isArray(value?.records) && Number.isFinite(Number(value?.total)) }
function xlsxOk(response) {
return response.http === 200 && response.bytes?.length > 4
&& response.bytes[0] === 0x50 && response.bytes[1] === 0x4b
}
2026-08-08 15:25:46 +08:00
async function uploadTemplate(context, path, bytes, fileName) {
const form = new FormData()
form.append('file', new Blob([bytes], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }), fileName)
try {
const response = await fetch(`${context.options.gateway}${path}`, {
method: 'POST', headers: { Accept: 'application/json', Authorization: `Bearer ${context.token}` },
body: form, signal: AbortSignal.timeout(60_000),
})
const raw = await response.text(); let body = null
try { body = raw ? JSON.parse(raw) : null } catch { /* bounded evidence only */ }
return { method: 'POST', path, http: response.status, body, raw: body === null ? raw.slice(0, 500) : null,
requestId: body?.requestId || response.headers.get('x-request-id'), traceId: body?.traceId || response.headers.get('x-trace-id') }
} catch (error) { return { method: 'POST', path, http: 0, body: null, raw: String(error), requestId: null, traceId: null } }
}
2026-08-08 10:11:04 +08:00
export async function run(context) {
const module = 'master-data'
const suffix = context.marker.replace(/\D/g, '').slice(-12)
const warehouseListResponse = await context.request('GET', '/api/platform/v1/warehouse-admin?page=1&size=20')
const warehousePage = context.apiSuccess(module, '查询平台仓库列表', warehouseListResponse)
context.assert(module, '仓库列表分页结构正确', warehouseListResponse, pageOk(warehousePage), `total=${warehousePage?.total ?? 'n/a'}`)
const warehousePayload = {
tenantId: 0, warehouseCode: `AW${suffix}`, warehouseName: `Admin验收仓-${suffix}`,
warehouseType: 'DOMESTIC', countryCode: 'CN', timezone: 'Asia/Shanghai',
contactName: '仓库验收员', contactPhone: '13800000001', contactEmail: `warehouse-${suffix}@example.com`,
status: 'ACTIVE', remark: context.marker,
address: {
addressType: 'WAREHOUSE', countryCode: 'CN', stateProvince: '广东省', city: '深圳市',
district: '南山区', postalCode: '518000', addressLine1: 'Admin API 验收仓地址',
addressLine2: null, latitude: null, longitude: null,
},
capabilities: [{ capabilityCode: 'RECEIVING', capabilityName: '收货', enabled: true, cutoffTime: '16:00:00', description: context.marker }],
}
const createWarehouseResponse = await context.request('POST', '/api/platform/v1/warehouse-admin', warehousePayload)
const warehouseId = id(context.apiSuccess(module, '创建平台公共仓库', createWarehouseResponse))
context.assert(module, '仓库创建返回字符串兼容 ID', createWarehouseResponse, Boolean(warehouseId), `warehouseId=${warehouseId || 'missing'}`)
if (!warehouseId) throw new Error('平台仓库创建失败,库位和托盘场景无法继续')
context.context.warehouseId = warehouseId
context.createdResources.warehouse = { id: warehouseId, code: warehousePayload.warehouseCode, finalStatus: 'INACTIVE' }
const warehousePath = `/api/platform/v1/warehouse-admin/${warehouseId}?tenantId=0`
const warehouseDetailResponse = await context.request('GET', warehousePath)
const warehouseDetail = context.apiSuccess(module, '查询平台仓库详情', warehouseDetailResponse)
context.assert(module, '仓库详情、地址和能力正确', warehouseDetailResponse,
id(warehouseDetail?.warehouse?.id) === warehouseId && warehouseDetail?.addresses?.length === 1 && warehouseDetail?.capabilities?.length === 1,
`addresses=${warehouseDetail?.addresses?.length ?? 'n/a'}, capabilities=${warehouseDetail?.capabilities?.length ?? 'n/a'}`)
const editedWarehouse = { ...warehousePayload, warehouseName: `${warehousePayload.warehouseName}-已编辑`, remark: `${context.marker}-EDITED` }
const updateWarehouseResponse = await context.request('PUT', warehousePath, editedWarehouse)
context.apiSuccess(module, '编辑平台仓库', updateWarehouseResponse)
const warehouseAfterEditResponse = await context.request('GET', warehousePath)
const warehouseAfterEdit = context.apiSuccess(module, '回查编辑后的平台仓库', warehouseAfterEditResponse)
context.assert(module, '仓库编辑已持久化', warehouseAfterEditResponse,
warehouseAfterEdit?.warehouse?.warehouseName === editedWarehouse.warehouseName && warehouseAfterEdit?.warehouse?.remark === editedWarehouse.remark,
`warehouseId=${warehouseId}`)
const locationPayload = {
warehouseId, zoneCode: `Z${suffix.slice(-5)}`, zoneName: 'Admin验收区',
locationCode: `L${suffix}`, locationName: 'Admin验收库位', locationType: 'SHELF',
maxWeightKg: 1000, status: 'AVAILABLE', remark: context.marker,
}
const createLocationResponse = await context.request('POST', '/api/platform/v1/locations?tenantId=0', locationPayload)
const locationId = id(context.apiSuccess(module, '创建仓库库位', createLocationResponse))
if (!locationId) throw new Error('库位创建失败')
context.createdResources.location = { id: locationId, warehouseId, deleted: false }
const locationPath = `/api/platform/v1/locations/${locationId}?tenantId=0&warehouseId=${warehouseId}`
const locationDetailResponse = await context.request('GET', locationPath)
const locationDetail = context.apiSuccess(module, '查询库位详情', locationDetailResponse)
context.assert(module, '库位详情关联正确仓库', locationDetailResponse,
id(locationDetail?.location?.id) === locationId && id(locationDetail?.location?.warehouseId) === warehouseId,
`locationId=${locationId}`)
const locationVersion = Number(locationDetail?.location?.version)
const updateLocationResponse = await context.request('PUT', locationPath, { ...locationPayload, locationName: 'Admin验收库位-已编辑', maxWeightKg: 1200, version: locationVersion })
context.apiSuccess(module, '编辑库位', updateLocationResponse)
const locationAfterEditResponse = await context.request('GET', locationPath)
const locationAfterEdit = context.apiSuccess(module, '回查编辑后的库位', locationAfterEditResponse)
context.assert(module, '库位编辑与版本递增正确', locationAfterEditResponse,
locationAfterEdit?.location?.locationName === 'Admin验收库位-已编辑' && Number(locationAfterEdit?.location?.version) === locationVersion + 1,
`version=${locationAfterEdit?.location?.version ?? 'missing'}`)
const disabledLocationVersion = Number(locationAfterEdit?.location?.version)
const disableLocationResponse = await context.request('PATCH', `/api/platform/v1/locations/${locationId}/status?tenantId=0&warehouseId=${warehouseId}`, { status: 'DISABLED', version: disabledLocationVersion })
context.apiSuccess(module, '停用库位', disableLocationResponse)
const deleteLocationResponse = await context.request('DELETE', `/api/platform/v1/locations/${locationId}?tenantId=0&warehouseId=${warehouseId}&version=${disabledLocationVersion + 1}`)
const deletedLocation = context.apiSuccess(module, '删除未占用测试库位', deleteLocationResponse)
if (deletedLocation === null && String(deleteLocationResponse.body?.code) === '200') context.createdResources.location.deleted = true
const palletPayload = { warehouseId, palletType: 'STANDARD', capacityCbm: 1.5, weightKg: 25, color: 'BLUE', remark: context.marker }
const createPalletResponse = await context.request('POST', '/api/platform/v1/pallets?tenantId=0', palletPayload)
const palletId = id(context.apiSuccess(module, '创建仓库托盘', createPalletResponse))
if (!palletId) throw new Error('托盘创建失败')
context.createdResources.pallet = { id: palletId, warehouseId, deleted: false }
const palletPath = `/api/platform/v1/pallets/${palletId}?tenantId=0&warehouseId=${warehouseId}`
const palletDetailResponse = await context.request('GET', palletPath)
const pallet = context.apiSuccess(module, '查询托盘详情', palletDetailResponse)
context.assert(module, '托盘详情关联正确仓库', palletDetailResponse,
id(pallet?.id) === palletId && id(pallet?.warehouseId) === warehouseId, `palletId=${palletId}`)
const updatePalletResponse = await context.request('PUT', palletPath, { ...palletPayload, capacityCbm: 1.75, weightKg: 26, remark: `${context.marker}-EDITED` })
context.apiSuccess(module, '编辑托盘', updatePalletResponse)
const palletAfterEditResponse = await context.request('GET', palletPath)
const palletAfterEdit = context.apiSuccess(module, '回查编辑后的托盘', palletAfterEditResponse)
context.assert(module, '托盘编辑已持久化', palletAfterEditResponse,
Number(palletAfterEdit?.capacityCbm) === 1.75 && palletAfterEdit?.remark === `${context.marker}-EDITED`, `palletId=${palletId}`)
const deletePalletResponse = await context.request('DELETE', palletPath)
context.apiSuccess(module, '删除未使用测试托盘', deletePalletResponse)
if (String(deletePalletResponse.body?.code) === '200') context.createdResources.pallet.deleted = true
const expressPayload = {
companyCode: `EX${suffix}`, companyName: `Admin验收快递-${suffix}`, servicePhone: '4000000000',
website: 'https://example.com', trackingUrlTemplate: 'https://example.com/{trackingNo}', status: 'ACTIVE', remark: context.marker,
}
const createExpressResponse = await context.request('POST', '/api/platform/v1/express-companies?tenantId=0', expressPayload)
const expressId = id(context.apiSuccess(module, '创建快递公司', createExpressResponse))
if (!expressId) throw new Error('快递公司创建失败')
context.createdResources.expressCompany = { id: expressId, code: expressPayload.companyCode, finalStatus: 'INACTIVE' }
const expressPath = `/api/platform/v1/express-companies/${expressId}?tenantId=0`
const expressDetailResponse = await context.request('GET', expressPath)
const express = context.apiSuccess(module, '查询快递公司详情', expressDetailResponse)
context.assert(module, '快递公司详情正确', expressDetailResponse, id(express?.id) === expressId && express?.companyCode === expressPayload.companyCode, `expressId=${expressId}`)
const updateExpressResponse = await context.request('PUT', expressPath, { ...expressPayload, companyName: `${expressPayload.companyName}-已编辑`, remark: `${context.marker}-EDITED` })
context.apiSuccess(module, '编辑快递公司', updateExpressResponse)
const disableExpressResponse = await context.request('PATCH', `/api/platform/v1/express-companies/${expressId}/status?tenantId=0`, { status: 'INACTIVE' })
context.apiSuccess(module, '停用快递公司', disableExpressResponse)
const expressListResponse = await context.request('GET', `/api/platform/v1/express-companies?page=1&size=20&keyword=${expressPayload.companyCode}`)
const expressPage = context.apiSuccess(module, '筛选快递公司列表', expressListResponse)
context.assert(module, '快递公司列表回显停用状态', expressListResponse,
expressPage?.records?.some((item) => id(item.id) === expressId && item.status === 'INACTIVE'), `records=${expressPage?.records?.length ?? 'n/a'}`)
const partnerPayload = {
partnerCode: `PT${suffix}`, partnerName: `Admin验收往来单位-${suffix}`, partnerType: 'SUPPLIER', types: ['SUPPLIER'],
contactName: '往来单位验收员', contactPhone: '13800000002', contactEmail: `partner-${suffix}@example.com`,
countryCode: 'CN', province: '广东省', city: '深圳市', district: '南山区', addressLine: 'Admin API 往来单位地址',
postalCode: '518000', status: 'ACTIVE', remark: context.marker,
}
const createPartnerResponse = await context.request('POST', '/api/platform/v1/partners?tenantId=0', partnerPayload)
const partnerId = id(context.apiSuccess(module, '创建往来单位', createPartnerResponse))
if (!partnerId) throw new Error('往来单位创建失败')
context.createdResources.partner = { id: partnerId, code: partnerPayload.partnerCode, finalStatus: 'INACTIVE' }
const partnerPath = `/api/platform/v1/partners/${partnerId}?tenantId=0`
const partnerDetailResponse = await context.request('GET', partnerPath)
const partner = context.apiSuccess(module, '查询往来单位详情', partnerDetailResponse)
context.assert(module, '往来单位详情正确', partnerDetailResponse,
id(partner?.id) === partnerId && partner?.types?.includes('SUPPLIER'), `partnerId=${partnerId}`)
const updatePartnerResponse = await context.request('PUT', partnerPath, { ...partnerPayload, partnerName: `${partnerPayload.partnerName}-已编辑`, types: ['SUPPLIER', 'CARRIER'], remark: `${context.marker}-EDITED` })
context.apiSuccess(module, '编辑往来单位', updatePartnerResponse)
const disablePartnerResponse = await context.request('PATCH', `/api/platform/v1/partners/${partnerId}/status?tenantId=0`, { status: 'INACTIVE' })
context.apiSuccess(module, '停用往来单位', disablePartnerResponse)
const partnerListResponse = await context.request('GET', `/api/platform/v1/partners?page=1&size=20&keyword=${partnerPayload.partnerCode}`)
const partnerPage = context.apiSuccess(module, '筛选往来单位列表', partnerListResponse)
context.assert(module, '往来单位列表回显编辑与停用状态', partnerListResponse,
partnerPage?.records?.some((item) => id(item.id) === partnerId && item.status === 'INACTIVE' && item.types?.includes('CARRIER')),
`records=${partnerPage?.records?.length ?? 'n/a'}`)
const templates = [
['仓库', '/api/platform/v1/warehouse-admin/import-template'],
['快递公司', '/api/platform/v1/express-companies/import-template'],
['往来单位', '/api/platform/v1/partners/import-template'],
]
for (const [name, path] of templates) {
const response = await context.requestBinary('GET', path)
context.addStep(module, `下载${name}导入模板`, response, xlsxOk(response),
`bytes=${response.bytes?.length ?? 0}, contentType=${response.contentType || 'missing'}`)
2026-08-08 15:25:46 +08:00
if (xlsxOk(response)) {
const importPath = `${path.replace('/import-template', '/import')}?tenantId=0&dryRun=true`
const first = await uploadTemplate(context, importPath, response.bytes, `${context.marker}-${name}.xlsx`)
const firstData = first.body?.data
context.addStep(module, `${name}合法模板 dryRun 导入契约`, first,
first.http === 200 && String(first.body?.code) === '200',
`HTTP=${first.http}, code=${first.body?.code ?? 'missing'}, rows=${firstData?.totalRows ?? 'n/a'}, parserSuccess=${firstData?.success ?? 'n/a'}`,
{ scenario: 'valid-import', deploymentBlocker: first.http === 404 || first.http === 405 || first.http === 503 })
const second = await uploadTemplate(context, importPath, response.bytes, `${context.marker}-${name}-replay.xlsx`)
context.addStep(module, `${name}合法模板重复 dryRun 幂等结果稳定`, second,
second.http === 200 && String(second.body?.code) === '200'
&& second.body?.data?.totalRows === firstData?.totalRows && second.body?.data?.importedRows === firstData?.importedRows,
`firstRows=${firstData?.totalRows ?? 'n/a'}, replayRows=${second.body?.data?.totalRows ?? 'n/a'}`,
{ scenario: 'valid-import', deploymentBlocker: second.http === 404 || second.http === 405 || second.http === 503 })
context.addCoverageGap(module, 'valid-import', `${name}带数据行的合法 XLSX 写入`,
'当前验收运行时无 XLSX 写入依赖;仅上传真实服务模板做解析/重复结果稳定性验证,未伪造数据行或写入主数据', 'runtime-dependency')
} else {
context.addCoverageGap(module, 'valid-import', `${name}合法 XLSX 导入`, '模板下载契约不可用,无法安全取得真实字段模板;未构造伪 XLSX', 'deployment')
}
2026-08-08 10:11:04 +08:00
}
2026-08-08 15:25:46 +08:00
context.addCoverageGap(module, 'optimistic-lock', '仓库/托盘/快递/往来单位旧 version 冲突',
'真实 DTO 未声明 version/expectedVersion 字段,不能伪造并发冲突断言;库位版本已由 supplemental-boundaries 覆盖', 'contract-gap')
2026-08-08 10:11:04 +08:00
const disableWarehouseResponse = await context.request('PATCH', `/api/platform/v1/warehouse-admin/${warehouseId}/status?tenantId=0&status=INACTIVE`)
context.apiSuccess(module, '停用测试仓库', disableWarehouseResponse)
}