Files
SupplierSystem/miniprogram/components/processing-panel/standalone.contract.test.js
T
Ubuntu 841ac4e0ca
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
fix(小程序独立加工): 成品搜索框改回受控输入(value 绑定 + data 声明 + 事件兜底)——非受控+epoch 重建导致真机输入始终为空
2026-10-23 17:18:22 +08:00

137 lines
7.1 KiB
JavaScript

/**
* 小程序「独立加工 ≠ 正常录单加工」契约(2026-10-23 开发者:记住两者是两回事)。
*
* 只读校验:独立加工(source_type='standalone')在小程序里
* ① 创建 = 多行原料(materials)+ 幂等请求号;
* ② 完工 = 申报成品(成品多行 / 成本自动均分 / 损耗自动差额 / 两个继承开关);
* ③ 订单加工与需求单的「毛重/净重 + 加工单价」流程**保持原样**。
*/
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const js = fs.readFileSync(path.join(__dirname, 'index.js'), 'utf8');
const wxml = fs.readFileSync(path.join(__dirname, 'index.wxml'), 'utf8');
const wxss = fs.readFileSync(path.join(__dirname, 'index.wxss'), 'utf8');
const api = fs.readFileSync(path.join(__dirname, '../../utils/request.js'), 'utf8');
let failed = 0;
function it(name, fn) {
try {
fn();
console.log(' ✓', name);
} catch (err) {
failed += 1;
console.log(' ✗', name, '→', err.message);
}
}
it('创建:独立加工走多行原料 + client_request_no', () => {
assert.ok(js.includes('materials: rows.map((row) => ({ sku_code: row.sku_code, qty: Number(row.qty) }))'));
assert.ok(js.includes('client_request_no:'));
assert.ok(wxml.includes('原料明细(可多行)'));
assert.ok(wxml.includes('onCreateMaterialQty'));
assert.ok(wxml.includes('removeCreateMaterial'));
});
it('完工:独立加工走成品申报(不再要求毛重/净重)', () => {
assert.ok(js.includes("String(row.source_type || '') === 'standalone'"));
assert.ok(js.includes('this.openOutputForm(row);'));
assert.ok(js.includes('await completeStandaloneProcessingTask(outputTaskId'));
assert.ok(wxml.includes('申报加工成品'));
assert.ok(wxml.includes('确认完工并回仓'));
});
it('成品:成本默认均分 + 损耗自动差额 + 两个继承开关', () => {
assert.ok(js.includes('this.data.outputMaterialCost / totalQty'));
assert.ok(js.includes('outputAutoLoss'));
assert.ok(wxml.includes('inherit_batch_report'));
assert.ok(wxml.includes('inherit_inspection_report'));
});
/**
* 2026-10-23 开发者:「这两个按钮是默认勾选上的,选中状态显眼点,绿色的勾选箭头」。
* 三个新建成品的入口(原料带出 / 单据头回退 / 搜索添加)都默认勾选,
* 选中态用绿色实心块 + 白色 ✓(不再用 ☑/☐ 字符 —— 小程序里跟正文同色,看不出是否勾选)。
*/
it('两个继承开关默认勾选,且选中态是绿色勾', () => {
assert.ok((js.match(/inherit_batch_report: true/g) || []).length >= 3, '三处新建成品行都要默认勾选');
assert.ok((js.match(/inherit_inspection_report: true/g) || []).length >= 3);
assert.ok(!js.includes('inherit_batch_report: false'));
assert.ok(!js.includes('inherit_inspection_report: false'));
// 模板:类名带 is-on 控制选中态,勾号只在选中时渲染
assert.ok(wxml.includes("inherit-toggle {{item.inherit_batch_report ? 'is-on' : ''}}"));
assert.ok(wxml.includes("item.inherit_batch_report ? '✓'"));
assert.ok(!wxml.includes("'☑'"));
// 样式:选中态绿色实心 + 白勾 + 标签变绿加粗
assert.ok(wxss.includes('.inherit-toggle.is-on .inherit-check'));
assert.ok(wxss.includes('background: #10b981'));
assert.ok(wxss.includes('.inherit-toggle.is-on .inherit-label'));
});
/**
* 2026-10-23 开发者报障:「成品明细」搜索框显示字面量 undefined、搜索商品没反应。
* 根因:WXML 绑定的 `outputSkuSearch` / `outputSkuOptions` 没在 `data` 里声明,
* setData 写不进数据层 ⇒ input 把 undefined 渲染成字面量,每次重渲染又把输入值刷回 undefined。
*/
it('成品搜索:input 绑定的键必须在 data 中声明', () => {
const dataStart = js.indexOf(' data: {');
const dataEnd = js.indexOf('\n observers:');
assert.ok(dataStart > 0 && dataEnd > dataStart, ' 解析不到 data 块');
const dataBlock = js.slice(dataStart, dataEnd);
assert.ok(/\boutputSkuSearch:\s*''/.test(dataBlock), 'data 缺少 outputSkuSearch');
assert.ok(/\boutputSkuOptions:\s*\[\]/.test(dataBlock), 'data 缺少 outputSkuOptions');
assert.ok(wxml.includes('bindinput="searchOutputSkus"'));
});
/**
* 2026-10-23 真机两轮复现:
* ① 先出现字面量 `undefined`(受控但数据层被写入 undefined,又被 bindinput 当关键字提交);
* ② 改成"非受控 + epoch 重建"后,真机表现为**输入什么都被清空**。
* 现行修法:**受控输入 + data 声明 + 处理器 `|| ''` 兜底**(不再重建输入框)。
*/
it('搜索框必须**受控**(绑定 value),且处理器对空事件兜底', () => {
// 2026-10-23 复查:改成"非受控 + epoch 重建"后真机表现为"输入什么都被清空" ⇒ 必须受控
assert.ok(/value="\{\{outputSkuSearch\}\}"/.test(wxml), '搜索框必须绑定 value(否则真机上输入会被丢弃)');
assert.ok(!js.includes('outputSearchEpochList'), '不应再用 epoch 重建输入框');
// 只认「搜索处理器内部」的兜底(整个文件里别处也有同样写法,全局 includes 会误判)
const searchFn = js.slice(js.indexOf('async searchOutputSkus(e)'), js.indexOf('addOutputSku(e)'));
assert.ok(searchFn.includes("String((e && e.detail && e.detail.value) || '')"), '搜索处理器必须对 undefined 事件兜底');
const dataBlock = js.slice(js.indexOf(' data: {'), js.indexOf('\n observers:'));
assert.ok(/outputSkuSearch:\s*''/.test(dataBlock), 'data 必须声明 outputSkuSearch');
});
it('成品搜索与 Web 同交互:打开即载入候选 + 本地即时过滤 + 本地没有再回服务端', () => {
const dataStart = js.indexOf(' data: {');
const dataBlock = js.slice(dataStart, js.indexOf('\n observers:'));
assert.ok(/\ballOutputSkus:\s*\[\]/.test(dataBlock), 'data 缺少 allOutputSkus');
// 打开弹窗就载入(不必先打字)
assert.ok(js.includes('this.loadOutputSkuOptions();'));
assert.ok(js.includes('const rows = res && Array.isArray(res.data) ? res.data : [];'));
// 输入时本地过滤
assert.ok(js.includes('const local = all.filter('));
assert.ok(js.includes('.includes(keyword)'));
// 本地没命中才请求服务端,且丢弃过期响应
assert.ok(js.includes('const seq = (this._outputSkuSearchSeq || 0) + 1;'));
assert.ok(js.includes('if (seq !== this._outputSkuSearchSeq) return;'));
});
it('订单/需求单加工:毛重净重流程保持原样(不许被改)', () => {
assert.ok(js.includes('await completeProcessingWork({'));
assert.ok(js.includes('gross_weight: grossWeight,'));
assert.ok(js.includes('net_weight: netWeight,'));
assert.ok(wxml.includes('加工损耗(毛重 − 净重)'));
});
it('接口:新完工/行查询走移动端路由(与 Web 同一服务)', () => {
assert.ok(api.includes("'/mobile/processing/standalone/'") || api.includes('/mobile/processing/standalone/${encodeURIComponent(taskId)}/complete'));
assert.ok(api.includes('/complete`'));
assert.ok(api.includes('/lines`'));
});
if (failed) {
console.error(`\n ${failed} 条失败`);
process.exit(1);
}
console.log('\n 全部通过');