Files
lyun 54813accaa 优化:整理指标目标并支持中文字段公式编辑
黑片和亮片按结果展示完整公式,在同一窗口维护条件与未匹配取值。支持中文字段选择、运算符和求和,并保留原计算语义及特殊策略。

移除重复账号入口与配置说明,统一紧凑表格。保留仓库既有权限判断,不包含此前未同步的角色迁移、预览快照或运行数据。

验证:前端构建及68项相关检查通过。
2026-11-03 16:31:23 +08:00

64 lines
6.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import test from 'node:test';
import assert from 'node:assert/strict';
import { chineseFormula, compileChineseFormula, formulaFieldReference, insertFormulaPart, rowFormulaFields, metricFormulaFields } from '../src/formulaLanguage.ts';
const schema={sourceFields:{black:{platingWeight:'送电镀重量',shipmentWeight:'黑片出货重量',inspectionNote:'检验备注',issueLocation:'发料地点'},bright:{scrapWeight:'检包入废品库重量',applicability:'是否电镀来料/二等品库来料'}},rowTargets:{black:['black_good_weight','black_scrap_weight','black_total_weight'],bright:['bright_scrap_weight']},componentLabels:{black_good_weight:'黑片合格重量',black_scrap_weight:'黑片报废重量',black_total_weight:'黑片总重量',bright_scrap_weight:'亮片报废重量'},metricCodes:['blackRate','brightRate','compositeRate'],metricLabels:{blackRate:'黑片合格率',brightRate:'亮片合格率'}};
const fields=rowFormulaFields(schema,'black');
test('formula catalogs are stage-specific, exclude text, and forbid future metric references',()=>{
assert.ok(fields.some(f=>f.key==='platingWeight'));assert.ok(!fields.some(f=>['inspectionNote','issueLocation','scrapWeight'].includes(f.key)));
assert.ok(rowFormulaFields(schema,'bright').some(f=>f.key==='scrapWeight'));
assert.deepEqual(metricFormulaFields(schema,'brightRate').filter(f=>f.group==='已计算指标').map(f=>f.key),['blackRate']);
assert.ok(!metricFormulaFields(schema,'blackRate').some(f=>f.key==='compositeRate'));
});
test('existing formulas display Chinese references, including SUM and arithmetic',()=>{
assert.equal(chineseFormula('platingWeight + shipmentWeight',fields),'[送电镀重量] + [黑片出货重量]');
assert.equal(chineseFormula('SUM(black_good_weight, black_scrap_weight) / black_total_weight',fields),'求和([黑片合格重量], [黑片报废重量]) ÷ [黑片总重量]');
});
test('raw formulas round-trip exactly, including whitespace, numbers and nesting',()=>{
for(const source of ['platingWeight + shipmentWeight','SUM(black_good_weight, black_scrap_weight) / black_total_weight',' ( -platingWeight + .50 ) * 2.00\n','0']){
assert.deepEqual(compileChineseFormula(chineseFormula(source,fields),fields),{formula:source});
assert.deepEqual(compileChineseFormula(source,fields),{formula:source});
}
});
test('users can type Chinese names with or without field brackets',()=>{
assert.equal(compileChineseFormula('送电镀重量 + 黑片出货重量',fields).formula,'platingWeight + shipmentWeight');
assert.equal(compileChineseFormula('求和([黑片合格重量],[黑片报废重量]) ÷ [黑片总重量]',fields).formula,'SUM(black_good_weight,black_scrap_weight) / black_total_weight');
assert.equal(compileChineseFormula('−[送电镀重量] × 2 + 1',fields).formula,'-platingWeight * 2 + 1');
});
test('longest field name wins without rewriting raw alias prefixes',()=>{
const catalog=[{key:'a',label:'重量',group:'来源字段'},{key:'ab',label:'重量合计',group:'来源字段'},{key:'goodWeight',label:'合格',group:'来源字段'},{key:'g',label:'good',group:'计算结果'}];
assert.equal(compileChineseFormula('重量合计 - 重量',catalog).formula,'ab - a');
assert.equal(compileChineseFormula('goodWeight + g',catalog).formula,'goodWeight + g');
});
test('duplicate display names require a qualified Chinese selection',()=>{
const catalog=[{key:'source',label:'合格重量',group:'来源字段'},{key:'result',label:'合格重量',group:'计算结果'}];
assert.match(compileChineseFormula('[合格重量]',catalog).error,/重名/);
assert.equal(chineseFormula('source + result',catalog),'[来源字段·合格重量] + [计算结果·合格重量]');
assert.equal(compileChineseFormula('[来源字段·合格重量] + [计算结果·合格重量]',catalog).formula,'source + result');
});
test('field references escape closing brackets and never translate identifiers inside a label',()=>{
const catalog=[{key:'a',label:'字段]a',group:'来源字段'}];
const reference=formulaFieldReference(catalog[0],catalog);assert.equal(reference,'[字段]]a]');
assert.equal(chineseFormula(reference,catalog),reference);assert.equal(compileChineseFormula(reference,catalog).formula,'a');
});
test('unrecognized and text fields cannot be silently dropped',()=>{
for(const text of ['[不存在] + 1','[检验备注]','missingWeight','[送电镀重量'])assert.ok(compileChineseFormula(text,fields).error,text);
assert.match(compileChineseFormula('compositeRate',metricFormulaFields(schema,'blackRate')).error,/找不到字段/);
});
test('syntax validation catches incomplete arithmetic and unsupported functions without evaluating',()=>{
for(const text of ['','[送电镀重量] +','([送电镀重量]','[送电镀重量][黑片出货重量]','求和()','求和(1,)','1,2','1.2.3','2[送电镀重量]','IF(1,2,3)','1e3'])assert.ok(compileChineseFormula(text,fields).error,text);
for(const text of ['1 / 0','[送电镀重量] - -2','SUM(1,2,(3+4))'])assert.equal(compileChineseFormula(text,fields).error,undefined,text);
});
test('editor validation mirrors engine bounds on length, nesting, function arguments and numbers',()=>{
assert.match(compileChineseFormula('('.repeat(33)+'1'+')'.repeat(33),fields).error,/32/);
assert.match(compileChineseFormula('求和('+Array(21).fill('1').join(',')+')',fields).error,/20/);
assert.match(compileChineseFormula('1'.repeat(51),fields).error,/50/);
assert.match(compileChineseFormula('[送电镀重量] + 1',fields,10).error,/最多10/);
assert.equal(compileChineseFormula('求和('+Array(20).fill('1').join(',')+')',fields).error,undefined);
});
test('inserting fields replaces only the selected range and returns the new caret',()=>{
const text='[送电镀重量] + [黑片出货重量]',start=text.indexOf('[黑片出货重量]');
assert.deepEqual(insertFormulaPart(text,start,text.length,'[黑片报废重量]'),{text:'[送电镀重量] + [黑片报废重量]',caret:start+'[黑片报废重量]'.length});
assert.deepEqual(insertFormulaPart('1 + 2',4,5,'',true),{text:'1 + 求和(2)',caret:8});
assert.deepEqual(insertFormulaPart('',0,0,'',true),{text:'求和()',caret:3});
});