import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import ts from 'typescript'; import React from 'react'; import { renderToStaticMarkup } from 'react-dom/server'; import { isInternalDataField, readableMetricFormula, readableRuleDescription } from '../src/publicDataDisplay.ts'; const source = fs.readFileSync(new URL('../src/BusinessValue.tsx', import.meta.url), 'utf8').replace(/^import .*\n/gm, '').replace(/export /g, ''); const code = ts.transpileModule(source, {compilerOptions:{jsx:ts.JsxEmit.React,target:ts.ScriptTarget.ES2022}}).outputText; const {BusinessValue,currentMetricFormula} = new Function('React','isInternalDataField','readableMetricFormula',code+';return {BusinessValue,currentMetricFormula};')(React,isInternalDataField,readableMetricFormula); test('current formula and nondefault policies take priority over reference text',()=>{ const row=Object.freeze({formula:'REFERENCE',formulaStatus:'CURRENT',currentFormula:'blackGood / (blackTotal + 7);运算空值=ZERO;零分母=ZERO;空结果=NULL'}); assert.equal(currentMetricFormula(row),'黑片合格重量 ÷ (黑片总重量 + 7);运算中遇到空值:按0处理;分母为0:取0;计算结果为空:保留空值'); }); test('missing current formula never silently uses a reference formula',()=>{ for(const row of [{formula:'REFERENCE'}, {formula:'REFERENCE',formulaStatus:'CURRENT',currentFormula:''}]) { assert.match(currentMetricFormula(row),/暂不可用/); assert.doesNotMatch(currentMetricFormula(row),/REFERENCE/); } assert.equal(currentMetricFormula({formulaStatus:'NO_CURRENT_DATA',formula:'REFERENCE'}),'暂无已生效数据'); }); test('Chinese rule details hide metadata and translate status and policies',()=>{ const value=Object.freeze({schemaVersion:'private',metricRules:{blackRate:{formula:'blackGood / blackTotal',zeroDenominatorPolicy:'ONE'}},enabled:true}); const html=renderToStaticMarkup(React.createElement(BusinessValue,{value})); assert.match(html,/黑片合格率/);assert.match(html,/返回1/);assert.match(html,/已启用/);assert.doesNotMatch(html,/private|schemaVersion|zeroDenominatorPolicy|true/); }); test('original source strings are preserved, including slashes and formulas as data',()=>{ const html=renderToStaticMarkup(React.createElement(BusinessValue,{value:{'产品':'A/B','备注':'blackGood / blackTotal'}})); assert.match(html,/A\/B/);assert.match(html,/blackGood \/ blackTotal/); }); test('seeded technical descriptions are displayed as business wording while custom descriptions remain intact',()=>{ assert.equal(readableRuleDescription('保留原 PQ / PP 计算口径,后续可直接维护字段、逐行公式和指标公式。'),'可维护来源字段、逐行计算和指标公式。'); assert.equal(readableRuleDescription('保留原模型本部报废调整版本,不改变默认正式亮片率。'),'将本部报废重量计入亮片合格重量,总重量不变。'); assert.equal(readableRuleDescription('自定义业务说明'),'自定义业务说明'); }); test('real source mapping states are translated without changing source codes',()=>{ const html=renderToStaticMarkup(React.createElement(BusinessValue,{value:{projectGroupStatus:'mapped_null',categoryMappingStatus:'mapped',sourcePdtCode:'P008'}})); assert.match(html,/已匹配(归属为空)/); assert.match(html,/已匹配/);assert.match(html,/P008/);assert.doesNotMatch(html,/mapped_null/); });