Files
order-notification-automation/tests/order-rules/excel-import-contract.test.cjs
T

51 lines
5.1 KiB
JavaScript

'use strict';
const test=require('node:test'),assert=require('node:assert/strict');
const {seedRules}=require('../../src/modules/order-rules/catalog.cjs');
const {previewImport,commitImport}=require('../../src/modules/order-rules/import.cjs');
function fixture(){
const records=new Map(seedRules().map(draft=>['rules:'+draft.id,{id:draft.id,draft,published:null}]));
const store={all:kind=>[...records].filter(([key])=>key.startsWith(kind+':')).map(([,value])=>structuredClone(value)),get:(kind,id)=>structuredClone(records.get(kind+':'+id)),put:(kind,value)=>records.set(kind+':'+value.id,structuredClone(value)),adapters:()=>[],now:()=>new Date('2026-10-23T02:00:00Z').toISOString(),audit(){},transaction(work){const before=structuredClone(records);try{return work();}catch(error){records.clear();for(const [key,value]of before)records.set(key,value);throw error;}}};
return store;
}
function sourceRows(rule,body='BPM摘要信息'){
return {rows:[['序号','服务器存档地址','订单评审流程','邮件命名','邮件正文','电子流状态','收件人'],[1,'测试归档',rule.processNames.join('/'),rule.subjectPrefix,body,rule.nodeNames.join('/'),'recipient@example.test']]};
}
test('Excel scope import uses the same Unicode normalization as the authoritative source without adding node aliases',()=>{
const store=fixture(),old=store.get('rules','processing-notice').draft,input=sourceRows(old);
input.rows[1][5]='订单专员(激活)';
const plan=previewImport(store,input,'admin');assert.equal(plan.canCommit,true);
commitImport(store,{id:plan.id},'admin');
assert.deepEqual(store.get('rules',old.id).draft.scopes,[{processName:'加工订单评审',nodeName:'订单专员(激活)'}]);
const overseas=store.get('rules','overseas-notice').draft,p=previewImport(store,sourceRows(overseas),'admin');commitImport(store,{id:p.id},'admin');
assert.deepEqual(store.get('rules',overseas.id).draft.nodeNames,['订单专员(订单通知书)']);
});
test('legacy Excel attachment descriptions and explicit yes cannot enable PDF email attachments',()=>{
for(const explicit of [false,true]){
const store=fixture(),old=store.get('rules','bs-change').draft,input=sourceRows(old,'附件:订单通知书PDF');
if(explicit){input.rows[0].push('附带PDF');input.rows[1].push('是');}
const before=store.all('rules'),plan=previewImport(store,input,'admin');assert.equal(plan.canCommit,true);assert.deepEqual(store.all('rules'),before);
assert(plan.rows[0].warnings.some(message=>/PDF.*仅归档|不附.*PDF/.test(message)));
commitImport(store,{id:plan.id},'admin');assert.equal(store.get('rules',old.id).draft.attachPdf,false);
}
});
test('import previews expose actual scopes and distribution values so administrators can verify before writing',()=>{
const store=fixture(),old=store.get('rules','bs-change').draft,plan=previewImport(store,sourceRows(old),'admin');
assert.deepEqual(plan.rows[0].preview.scopes,[{processName:old.processNames[0],nodeName:old.nodeNames[0]}]);
assert.deepEqual(plan.rows[0].preview.to,['recipient@example.test']);assert.equal(plan.rows[0].preview.archiveDirectory,'测试归档');assert.equal(plan.rows[0].preview.attachPdf,false);
assert.equal(plan.rows[0].candidate,undefined);assert.equal(store.get('rules',old.id).draft.version,old.version);
});
test('exported rules can round trip all fifteen categories and keep published and restricted snapshots intact',async()=>{
const store=fixture(),XLSX=require('xlsx'),{parse}=require('../../web/modules/order-rules/import-parser.js');
for(const record of store.all('rules')){record.draft.to=['recipient@example.test'];record.draft.scopes=record.draft.processNames.flatMap(processName=>record.draft.nodeNames.map(nodeName=>({processName,nodeName:nodeName.normalize('NFKC')})));record.draft.nodeNames=[...new Set(record.draft.scopes.map(row=>row.nodeName))];record.published={...structuredClone(record.draft),status:'enabled'};store.put('rules',record);}
store.put('restricted-test',{id:'main',value:{id:'fixed-run',rules:store.all('rules').map(r=>r.published)}});
const before=store.all('rules'),run=store.get('restricted-test','main'),bytes=await require('../../src/modules/order-rules/import-template.cjs').template(store),plan=previewImport(store,parse(XLSX,bytes),'admin');
assert.equal(plan.canCommit,true);assert.equal(plan.rows.length,15);commitImport(store,{id:plan.id},'admin');
for(const record of before){const saved=store.get('rules',record.id);assert.deepEqual(saved.draft.scopes,record.draft.scopes);assert.deepEqual(saved.published,record.published);assert.equal(saved.draft.attachPdf,false);}
assert.deepEqual(store.get('restricted-test','main'),run);
});
test('rule exports keep readable column widths, a frozen header, filter and bold labels',async()=>{
const bytes=await require('../../src/modules/order-rules/import-template.cjs').template(fixture()),zip=await require('jszip').loadAsync(bytes);
const sheet=await zip.file('xl/worksheets/sheet1.xml').async('string'),styles=await zip.file('xl/styles.xml').async('string');
assert.match(sheet,/<pane[^>]*ySplit="1"[^>]*state="frozen"/);assert.match(sheet,/<autoFilter ref="A1:K1"/);assert.match(sheet,/<col[^>]*width="60"/);assert.match(styles,/<b\s*\/>/);
});