/** * 小程序「仓库管理」面板契约(2026-10-23 开发者): * · 与其它模块一致:作为**首页一级 Tab 内嵌面板**,不是另开二级列表页; * · 列表字段对齐 Web 库存管理:商品名 / 批次 / 可用数量 / 成本价 / 报告状态; * · 支持**批量上传报告**、行内改成本价 / 可用数量、按批次查看与删除报告; * · 默认仓管可见,其他角色由 `warehouse_manage` 授权(注册表 grantable)。 */ const fs = require('fs'); const path = require('path'); const assert = require('assert'); const read = (rel) => fs.readFileSync(path.join(__dirname, rel), 'utf8'); const panelJs = read('index.js'); const panelWxml = read('index.wxml'); const registry = read('../../modules/registry.js'); const homeJs = read('../../pages/home/home.js'); const homeWxml = read('../../pages/home/home.wxml'); const homeJson = read('../../pages/home/home.json'); const guard = read('../../utils/roleGuard.js'); const api = read('../../utils/request.js'); const pageJs = read('../../pages/warehouse/warehouse.js'); const wxss = read('index.wxss'); let failed = 0; function it(name, fn) { try { fn(); console.log(' ✓', name); } catch (err) { failed += 1; console.log(' ✗', name, '→', err.message); } } it('是一级界面:首页内嵌面板(与加工/库存入库同规矩),页面只是薄壳', () => { assert.ok(registry.includes("homeEmbed: 'warehouse-panel'"), '注册表必须声明 homeEmbed'); assert.ok(homeJson.includes('"warehouse-panel": "/components/warehouse-panel/index"')); assert.ok(homeWxml.includes("activeHomeModuleId === 'warehouse'")); assert.ok(homeWxml.includes(' { assert.ok(registry.includes("permissionKey: 'warehouse_manage'")); assert.ok(registry.includes('grantable: true')); // 可见性:角色命中 或 权限命中 assert.ok(registry.includes('if (!roleHit && !permissionHit) return false;')); // 首页内嵌判定也支持权限兜底 assert.ok(registry.includes('function isHomeWorkspaceModule(mod, availableRoles, fallbackRole, permissions)')); assert.ok(registry.includes('return perms.includes(mod.permissionKey);')); // 激活/跳转:被授权但角色不匹配时不切角色 assert.ok(homeJs.includes('const grantedOnly =')); assert.ok(homeJs.includes('const ok = grantedOnly ? true : await ensureMobileRole(roles, ensureOpts);')); // 回弹回归(2026-10-25 录单员反馈):重算一级 Tab 时必须带权限,否则切过去又被退回默认模块 assert.ok( homeJs.includes( 'isHomeWorkspaceModule(mod, roleValues, this.data.roleType, this.data.permissions)' ), 'refreshMenuModules 必须把 permissions 传给 isHomeWorkspaceModule' ); assert.ok(homeJs.includes("warehouse: '#warehouseHomePanel'"), '切回同一 Tab 时能刷新面板'); assert.ok(homeJs.includes('wx.navigateTo({ url });')); // 页面守卫 assert.ok(guard.includes('function hasWarehouseModuleAccess(session)')); assert.ok(guard.includes("perms.includes('warehouse_manage')")); assert.ok(guard.includes('function requireWarehouseModuleRole()')); assert.ok(panelJs.includes('requireWarehouseModuleRole()')); }); it('列表是表格布局:表头 + 列对齐(不是卡片堆叠)', () => { assert.ok(panelWxml.includes('class="table"'), '必须用表格容器'); assert.ok(panelWxml.includes('tr thead'), '必须有表头行'); assert.ok(panelWxml.includes('td-product')); assert.ok(panelWxml.includes('td-qty')); assert.ok(panelWxml.includes('td-cost')); assert.ok(panelWxml.includes('td-report')); // 行点击 → **直接弹修改弹窗**(不再先出选择菜单) assert.ok(panelJs.includes('onRowTap(e)')); assert.ok(panelJs.includes('this.openEditSheetById(')); assert.ok(!panelJs.includes('报告(查看/删除)'), '行点击不得再弹 actionSheet'); assert.ok(panelWxml.includes('bindtap="onRowTap"')); // 弹窗一次给出可用数量 + 成本价 + 报告入口 assert.ok(panelWxml.includes('onEditAvailableInput')); assert.ok(panelWxml.includes('onEditCostInput')); assert.ok(panelWxml.includes('catchtap="openReportsFromEdit"')); assert.ok(panelJs.includes('async submitEditSheet()')); // 从安全区底部升起(home 指示条留白) assert.ok(wxss.includes('env(safe-area-inset-bottom)')); assert.ok(wxss.includes('constant(safe-area-inset-bottom)')); // 表格样式:flex 行 + 固定列宽,保证上下对齐 assert.ok(wxss.includes('.tr {')); assert.ok(wxss.includes('.td-qty {')); assert.ok(wxss.includes('.td-cost {')); assert.ok(wxss.includes('.td-report {')); }); it('列表字段对齐 Web 库存管理(商品名/批次/可用/成本/报告状态)', () => { assert.ok(api.includes('function fetchInventoryBatches(')); assert.ok(api.includes('/mobile/inventory/batches')); assert.ok(panelJs.includes('available_text')); assert.ok(panelJs.includes('cost_missing')); assert.ok(panelJs.includes('report_missing')); assert.ok(panelWxml.includes('{{item.sku_name}}')); assert.ok(panelWxml.includes('{{item.batch_no}}')); assert.ok(panelWxml.includes('可用数量')); assert.ok(panelWxml.includes('成本价')); assert.ok(panelWxml.includes('缺少')); // 表格列窄,缺报告在报告列显示「缺少」 // 入库时间(2026-10-25 开发者:列表缺少入库时间)——口径同 Web:批次最早一条入库流水 assert.ok(panelJs.includes('inbound_at: row.inbound_at'), '列表行必须带 inbound_at'); assert.ok(panelWxml.includes('入库 {{item.inbound_at'), '商品列必须展示入库时间'); // 一进面板默认只看可用(2026-10-25 开发者:「仅有可用这个是默认选择的 一进去」) assert.ok(panelJs.includes('onlyAvailable: true'), '默认必须勾选「仅有可用」'); assert.ok(panelWxml.includes("chip {{onlyAvailable ? 'is-on' : ''}}"), '筛选项选中态绑定不变'); }); it('顶部统计卡:库存批次 / 可用总量 / 可用货值(与列表同筛选口径)', () => { // 数据来自服务端同一 where 的 stats,不在小程序侧另算 assert.ok(panelJs.includes("this.triggerEvent('statschange'")); assert.ok(panelJs.includes('stats.total_available_qty')); assert.ok(panelJs.includes('stats.total_value')); // 首页:面板上报 → 顶部绿卡(无数据时归零) assert.ok(homeWxml.includes('bind:statschange="onWarehouseStatsChange"')); assert.ok(homeJs.includes('onWarehouseStatsChange(e)')); assert.ok(homeWxml.includes('wx:if="{{showWarehouseStats}}"')); assert.ok(homeWxml.includes('库存批次')); assert.ok(homeWxml.includes('可用总量')); assert.ok(homeWxml.includes('可用货值')); // 布局:文字在上、数字在下(2026-10-25 开发者) const labelIdx = homeWxml.indexOf('>库存批次<'); const numIdx = homeWxml.indexOf('{{warehouseBatchCount}}'); assert.ok(labelIdx > -1 && numIdx > labelIdx, '统计卡必须文字在上、数字在下'); // 有统计卡时不再另外渲染单纯的模块标题卡 assert.ok(homeJs.includes('showWarehouseStats;'), '必须计入 hasDedicatedProgress'); }); it('列表分页:每页 20,首页触底转发给内嵌面板', () => { assert.ok(panelJs.includes('const PAGE_SIZE = 20;'), '每页固定 20 条'); assert.ok(panelJs.includes('this.loadPage(this.data.page + 1, true)'), '触底加载下一页(追加)'); assert.ok(panelJs.includes('if (!this.data.hasMore || this.data.loading || this.data.loadingMore) return;')); // 首页内嵌面板必须收到页面触底事件,否则只能看到第 1 页 assert.ok(homeJs.includes('onReachBottom()'), '首页必须有 onReachBottom'); assert.ok( homeJs.includes('HOME_PANEL_SELECTORS[this.data.activeHomeModuleId]'), '触底必须转给当前模块面板' ); assert.ok(homeJs.includes("warehouse: '#warehouseHomePanel'")); }); it('批量上传:上传 → 识别 → 匹配候选 → 人工确认(与 Web 同流程)', () => { // 入口:不是先勾选行,而是打开上传弹层 assert.ok(panelWxml.includes('bindtap="openUploadSheet"')); assert.ok(!panelJs.includes('toggleSelectAll'), '不应再有「勾选商品再上传」'); assert.ok(!panelJs.includes('selectedIds'), '不应再有行勾选状态'); // 上传文件 + 服务端解析(返回候选批次) assert.ok(panelJs.includes('uploadQualityReportStorageFile(')); assert.ok(panelJs.includes("await parseInventoryBatchReports({ files: paths, report_type: 'batch' })")); assert.ok(api.includes('function parseInventoryBatchReports(')); assert.ok(api.includes('/mobile/inventory/batch-reports/parse')); // 唯一命中自动勾选,其余人工确认 assert.ok(panelJs.includes("const selected = match.status === 'unique';")); assert.ok(panelWxml.includes('catchtap="toggleUploadCandidate"')); // 确认匹配(逐个报告提交) assert.ok(panelJs.includes('await confirmInventoryBatchReportMatches(payloads[i].reportId, payloads[i].matches);')); assert.ok(api.includes('function confirmInventoryBatchReportMatches(')); assert.ok(panelWxml.includes('确认匹配(')); // 工具栏:筛选 + 总数 + 批量上传都在同一行(方形选择框,不用椭圆) const filterStart = panelWxml.indexOf('class="filter-row"'); const uploadIdx = panelWxml.indexOf('class="upload-btn'); assert.ok(filterStart > -1, '必须有筛选行'); assert.ok(uploadIdx > filterStart, '批量上传按钮必须在筛选行内(同一行)'); assert.ok(!panelWxml.includes('class="action-row"'), '不得再有单独的操作行'); assert.ok(wxss.includes('.filter-row {')); assert.ok(wxss.includes('.upload-btn::after')); // 筛选项 / 上传按钮都用矩形圆角(10rpx),不得回到 999rpx 糖丸 const chipBlock = wxss.slice(wxss.indexOf('.chip {'), wxss.indexOf('.chip.is-on')); assert.ok(chipBlock.includes('border-radius: 10rpx'), '筛选项必须是方形圆角'); assert.ok(!chipBlock.includes('999rpx'), '筛选项不得用椭圆'); const btnBlock = wxss.slice(wxss.indexOf('.upload-btn {'), wxss.indexOf('.upload-btn::after')); assert.ok(btnBlock.includes('border-radius: 10rpx'), '上传按钮必须是矩形圆角'); assert.ok(!btnBlock.includes('999rpx'), '上传按钮不得用椭圆'); }); it('行内改成本价 / 可用数量(按本批次 inventoryId)', () => { assert.ok(api.includes('function updateInventoryBatchCostPrice(')); assert.ok(api.includes('function updateInventoryBatchAvailableQty(')); assert.ok(api.includes('/cost-price')); assert.ok(api.includes('/available-qty')); assert.ok(panelJs.includes('await updateInventoryBatchCostPrice(item.id, nextCost)')); assert.ok(panelJs.includes('await updateInventoryBatchAvailableQty(item.id, nextQty)')); // 只提交真正改过的字段(两条写路径各带自己的审计/流水) assert.ok(panelJs.includes('if (qtyChanged) await updateInventoryBatchAvailableQty')); assert.ok(panelJs.includes('if (costChanged) await updateInventoryBatchCostPrice')); assert.ok(!panelJs.includes('sku_code: '), '改数量必须按批次 id,不得按 SKU'); }); it('弹层走 root-portal:固定视口、不跟页面滚、盖住底部 tab', () => { // 每个弹层单独一个 root-portal:一个 portal 塞多个弹层会让 portal 不生效 // (弹层会跟着页面滚动、且被底部 tab 栏盖住)——与加工/分拣面板同款做法。 const portalCount = panelWxml.split('')); assert.ok(panelWxml.includes('')); const portalStart = panelWxml.indexOf(' portalStart, '弹层必须在 portal 内'); assert.ok(panelWxml.indexOf('catchtouchmove="noop"', portalStart) > portalStart, '遮罩要吃掉滚动'); assert.ok(wxss.includes('z-index: 10000'), '必须压过底部 tab(z-index 100)'); assert.ok(wxss.includes('max-height: 82vh'), '弹层超高时可内部滚动'); // 固定视口四边显式写全(不依赖 inset 简写) assert.ok(wxss.includes('top: 0;') && wxss.includes('bottom: 0;'), '遮罩必须铺满视口'); // 头/脚固定,只有 body 滚动(长列表不把按钮推出屏幕) assert.ok(panelWxml.includes(' { // 编辑弹窗的「批次报告」行有「上传」 assert.ok(panelWxml.includes('catchtap="onUploadFromEdit"')); assert.ok(panelWxml.includes('upload-chip')); // 报告弹窗顶部也有「上传报告」 assert.ok(panelWxml.includes('catchtap="onUploadFromReports"')); // 单批次上传同样走「文件上传一次 → 绑定该批次」的既有通道 assert.ok(panelJs.includes('async uploadReportsForBatch(inventoryId)')); assert.ok(panelJs.includes('await uploadInventoryBatchReports(id, { files: paths, report_type: \'batch\' });')); // 上传后刷新:报告列表 + 编辑弹窗状态 + 表格报告列 assert.ok(panelJs.includes('async refreshBatchReports(inventoryId)')); assert.ok(panelJs.includes("patch['editItem.report_count'] = list.length")); assert.ok(panelJs.includes('await this.loadPage(1, false);')); }); it('报告:按批次查看 + 删除确认', () => { assert.ok(api.includes('function fetchInventoryBatchReports(')); assert.ok(api.includes('function deleteInventoryBatchReport(')); assert.ok(panelJs.includes('await deleteInventoryBatchReport(reportId)')); assert.ok(panelJs.includes("title: '删除报告'")); assert.ok(panelWxml.includes('批次报告')); }); if (failed) { console.error(`\n ${failed} 条失败`); process.exit(1); } console.log('\n 全部通过');