const MODULE = 'phase3-container-overview' const OPTIONS = { scenario: 'phase3-container-overview-read-only', deploymentBlocker: true } const CONTAINERS = '/api/platform/v1/containers' function id(value) { return value == null ? '' : String(value) } export function controlledPhotoDownload(downloadPath, response) { const controlledPath = typeof downloadPath === 'string' && /^\/api\/platform\/v1\/containers\/[1-9]\d*\/pda\/photos\/[1-9]\d*\/download$/.test(downloadPath) const contentType = String(response?.contentType || '').toLowerCase() return controlledPath && response?.http === 200 && response?.bytes?.length > 0 && Boolean(contentType) && !contentType.includes('json') && !contentType.includes('text/html') } export async function run(context) { const loadingRequested = context.options?.modules?.includes('phase3-loading') === true const fixture = loadingRequested ? context.createdResources?.phase3Loading : context.createdResources?.phase3Loading || context.createdResources?.phase3ContainerLoading const containerId = id(fixture?.containerBookingId) const planId = id(fixture?.loadingPlanId) const warehouseId = id(fixture?.warehouseId) const markerMatches = fixture?.marker === context.marker || fixture?.marker === `${context.marker}-CAB` || fixture?.sourceMarker === `${context.marker}-CAB` if (!containerId || !planId || !warehouseId || !markerMatches) { context.addStep(MODULE, '复用智能装柜产生的货柜 overview fixture', null, false, `source=${loadingRequested ? 'phase3Loading-required' : 'available-container-fixture'}, containerBookingId=${containerId || 'missing'}, loadingPlanId=${planId || 'missing'}, warehouseId=${warehouseId || 'missing'}`, OPTIONS) return } const overviewResponse = await context.request('GET', `${CONTAINERS}/${encodeURIComponent(containerId)}/overview`) const overview = context.apiSuccess(MODULE, '查询货柜 overview 聚合详情', overviewResponse, `containerBookingId=${containerId}`, OPTIONS) context.assert(MODULE, 'overview 返回货柜身份、版本和时间线', overviewResponse, id(overview?.container?.id) === containerId && Number.isFinite(Number(overview?.container?.version)) && Array.isArray(overview?.documents) && Array.isArray(overview?.exceptionHistory) && Array.isArray(overview?.timeline), `documents=${overview?.documents?.length ?? 'missing'}, timeline=${overview?.timeline?.length ?? 'missing'}`, OPTIONS) const timeline = Array.isArray(overview?.timeline) ? overview.timeline : [] context.assert(MODULE, 'overview timeline 按 occurredAt 升序返回', overviewResponse, timeline.every((item, index) => index === 0 || new Date(timeline[index - 1]?.occurredAt) <= new Date(item?.occurredAt)), `events=${timeline.length}`, OPTIONS) if (overview?.pda) { const expectsPhotos = fixture === context.createdResources?.phase3Loading && Array.isArray(fixture?.evidenceTypes) && fixture.evidenceTypes.length > 0 const requiredFields = ['taskId', 'taskStatus', 'workflowStatus', 'taskVersion', 'itemCount', 'completedItemCount', 'unresolvedItemCount'] const missingFields = requiredFields.filter((field) => overview.pda[field] === undefined) context.assert(MODULE, 'overview PDA 摘要字段完整', overviewResponse, missingFields.length === 0 && Array.isArray(overview.pda.photoCounts) && Array.isArray(overview.pda.photos) && Array.isArray(overview.pda.exceptionReasons) && (!expectsPhotos || overview.pda.photos.length > 0), `taskId=${id(overview.pda.taskId)}, photos=${overview.pda.photos?.length ?? 'not-array'}, missingFields=${missingFields.join('|') || 'none'}, photoCountsArray=${Array.isArray(overview.pda.photoCounts)}, photosArray=${Array.isArray(overview.pda.photos)}, exceptionReasonsArray=${Array.isArray(overview.pda.exceptionReasons)}`, OPTIONS) const progressIsComplete = Number(overview.pda.itemCount) > 0 && Number(overview.pda.completedItemCount) === Number(overview.pda.itemCount) && Number(overview.pda.unresolvedItemCount) === 0 context.assert(MODULE, 'overview PDA 100%进度区分作业中与待复核', overviewResponse, !progressIsComplete || overview.pda.taskStatus !== 'LOADING' || overview.pda.workflowStatus === 'AWAITING_REVIEW', `taskStatus=${overview.pda.taskStatus || 'missing'}, workflowStatus=${overview.pda.workflowStatus || 'missing'}, completed=${overview.pda.completedItemCount ?? 'missing'}/${overview.pda.itemCount ?? 'missing'}`, OPTIONS) for (const [index, photo] of overview.pda.photos.entries()) { context.assert(MODULE, `overview PDA 照片摘要 ${index + 1} 字段完整`, overviewResponse, ['evidenceId', 'fileId', 'status'].every((field) => photo[field] !== undefined), `fileId=${id(photo.fileId)}`, OPTIONS) const downloadPath = String(photo.downloadPath || '') if (photo.status === 'VOID') { context.assert(MODULE, `overview PDA 作废照片 ${index + 1} 无下载入口`, overviewResponse, !downloadPath, `fileId=${id(photo.fileId)}, status=${photo.status}, downloadPath=${downloadPath || 'none'}`, OPTIONS) continue } const controlledPath = downloadPath.startsWith('/api/') && !downloadPath.startsWith('//') && !downloadPath.includes('://') if (photo.status !== 'ACTIVE' || !controlledPath) { context.assert(MODULE, `overview PDA 照片 ${index + 1} 下载路径受控`, overviewResponse, false, `fileId=${id(photo.fileId)}, status=${photo.status || 'missing'}, downloadPath=${downloadPath || 'missing'}`, OPTIONS) continue } const download = await context.requestBinary('GET', downloadPath) context.assert(MODULE, `overview PDA 照片 ${index + 1} 可鉴权下载`, download, controlledPhotoDownload(downloadPath, download), `fileId=${id(photo.fileId)}, bytes=${download?.bytes?.length ?? 0}, contentType=${download?.contentType || 'missing'}`, OPTIONS) } } for (const documentType of ['DELIVERY_ORDER']) { const response = await context.request('GET', `${CONTAINERS}/${encodeURIComponent(containerId)}/documents/${documentType}`) context.assert(MODULE, `国内未出仓时 ${documentType} 不生成国外工作台数据`, response, response.http === 200 && String(response?.body?.code) === 'A0404' && String(response?.body?.message || '').includes('国外工作台货柜不存在'), `HTTP=${response.http}, code=${response?.body?.code ?? 'missing'}, documentType=${documentType}`, OPTIONS) } const clearance = await context.request('GET', `${CONTAINERS}/${encodeURIComponent(containerId)}/clearance`) context.assert(MODULE, '国内未出仓时目的港清关不生成国外工作台数据', clearance, clearance.http === 200 && String(clearance?.body?.code) === 'A0404' && clearance?.body?.message === '货柜尚未离仓或实际开船,不能维护目的港清关资料', `HTTP=${clearance.http}, code=${clearance?.body?.code ?? 'missing'}, containerBookingId=${containerId}`, OPTIONS) const customs = await context.request('GET', `/api/platform/v1/first-leg/loading-lists/${encodeURIComponent(planId)}/customs?warehouseId=${encodeURIComponent(warehouseId)}`) if (fixture?.finalStatus === 'LOADED') { context.apiSuccess(MODULE, '查询出口报关资料', customs, `loadingPlanId=${planId}, warehouseId=${warehouseId}`, OPTIONS) } else { context.assert(MODULE, '运行中计划尚未生成完成装柜清单,出口报关资料拒绝读取', customs, customs.http === 200 && String(customs.body?.code) === 'A0404', `loadingPlanId=${planId}, status=${fixture?.finalStatus || 'missing'}`, OPTIONS) } context.addStep(MODULE, '异常标记/取消、单证上传和完成锁定保持写入阻断', null, true, '默认全量脚本只读;写场景需显式独立授权和一次性测试柜次', OPTIONS) }