'use strict'; const test=require('node:test');const assert=require('node:assert/strict'); const {buildTemplateValues,renderOrderTemplate}=require('../src/order-template-bridge.cjs'); test('template bridge renders category values and keeps PDF out of attachments',()=>{ const result=renderOrderTemplate({id:'tpl-1',version:3,subject:'{{产品线}} {{订单号}}',text:'{{日期}}\n{{订单列表}}',html:'
{{订单列表}}
',attachments:[]},{productLine:'海外运营',orders:[{orderNo:'000123',brief:'第一行\n第二行'},{orderNo:'000124',brief:'第二单'}],date:'2026-10-22'}); assert.equal(result.subject,'海外运营 000123、000124');assert.match(result.text,/订单号:000123 第一行 第二行/);assert.match(result.text,/订单号:000124 第二单/);assert.equal(result.attachments.length,0);assert.equal(result.templateId,'tpl-1');assert.equal(result.templateVersion,3); }); test('template bridge rejects PDF attachments and missing source values',()=>{ assert.throws(()=>renderOrderTemplate({id:'tpl',version:1,subject:'x',text:'{{订单号}}',attachments:[{filename:'x.pdf'}]},{productLine:'P',orders:[],date:'2026-10-22'}),/附件/); assert.throws(()=>buildTemplateValues({productLine:'',orders:[],date:'2026-10-22'}),/产品线/); }); test('order list keeps the template greeting but renders one order per line from its order number',()=>{ const result=renderOrderTemplate({id:'tpl-lines',version:1,subject:'海外订单 {{订单号}}',text:'大家好:\n以下为海外订单通知书,请查收!\n\n{{订单列表}}\n\nBest Regards',attachments:[]},{productLine:'海外运营',orders:[ {orderNo:'000123',brief:'多余前置说明\n订单号:000123\n机芯号:EXAMPLE-CHIP-A\n产品型号:EXAMPLE-MODEL-A'}, {orderNo:'000124',brief:'其他前置\r\n订单号:000124\r\n机芯号:EXAMPLE-CHIP-B'} ],date:'2026-10-22'}); assert.match(result.text,/^大家好:\n以下为海外订单通知书,请查收!/); assert.match(result.text,/订单号:000123 机芯号:EXAMPLE-CHIP-A 产品型号:EXAMPLE-MODEL-A/); assert.match(result.text,/订单号:000124 机芯号:EXAMPLE-CHIP-B/); assert.doesNotMatch(result.text,/多余前置说明|其他前置/); const orderLines=result.text.split('\n').filter(line=>line.startsWith('订单号')); assert.deepEqual(orderLines,[ '订单号:000123 机芯号:EXAMPLE-CHIP-A 产品型号:EXAMPLE-MODEL-A', '订单号:000124 机芯号:EXAMPLE-CHIP-B' ]); }); test('formatting cannot turn an empty brief into a sendable order line',()=>{ for(const brief of ['', ' \r\n\t',undefined])assert.throws(()=>buildTemplateValues({productLine:'P',orders:[{orderNo:'000123',brief}],date:'2026-10-23'}),/简要说明/); assert.throws(()=>buildTemplateValues({productLine:'P',orders:[{orderNo:'000123',brief:'订单号:000123'},{orderNo:'000124',brief:''}],date:'2026-10-23'}),/简要说明/); }); test('the printed order marker must match the authoritative order number and cannot be an original-order label',()=>{ const input={productLine:'P',date:'2026-10-23'}; assert.throws(()=>buildTemplateValues({...input,orders:[{orderNo:'000123',brief:'流程标题 订单号:000124 客户:测试'}]}),/订单号/); const order={orderNo:'000123',brief:'原订单号:009999\n流程标题 订单号:000123\n客户:测试'}; assert.equal(buildTemplateValues({...input,orders:[order]})['订单列表'],'订单号:000123 客户:测试'); assert.equal(order.brief,'原订单号:009999\n流程标题 订单号:000123\n客户:测试'); }); test('HTML uses a single order separator while the greeting signature and escaped values remain intact',()=>{ const result=renderOrderTemplate({id:'tpl-html',version:2,subject:'标题',text:'大家好\n{{订单列表}}\n签名',html:'大家好
签名
',attachments:[]},{productLine:'P',date:'2026-10-23',orders:[{orderNo:'000123',brief:'前缀 订单号:000123\n客户:A&B'},{orderNo:'000124',brief:'前缀 订单号:000124\n客户:<测试>'}]}); assert.equal(result.html,'大家好
签名
'); }); test('order marker compares the complete identifier while allowing an adjacent Chinese field label',()=>{ const input={productLine:'P',date:'2026-10-23'}; for(const brief of ['订单号:000123A 客户:示例','订单号:000123-2 客户:示例','订单号:000123_2 客户:示例','订单号:000123.2 客户:示例','订单号:000123/2 客户:示例'])assert.throws(()=>buildTemplateValues({...input,orders:[{orderNo:'000123',brief}]}),/订单号/); assert.equal(buildTemplateValues({...input,orders:[{orderNo:'000123',brief:'流程标题 订单号:000123机芯号:EXAMPLE'}]})['订单列表'],'订单号:000123机芯号:EXAMPLE'); for(const prefix of ['原 订单号:009999','原始订单号:009999','旧 订单号:009999'])assert.equal(buildTemplateValues({...input,orders:[{orderNo:'000123',brief:prefix+'\n订单号:000123 客户:示例'}]})['订单列表'],'订单号:000123 客户:示例'); });