'use strict'; const test=require('node:test'); const assert=require('node:assert/strict'); const fs=require('node:fs');const os=require('node:os');const path=require('node:path');const crypto=require('node:crypto'); const {transition}=require('../../src/modules/order-rules/state.cjs'); const {buildBatch}=require('../../src/modules/order-rules/batch.cjs'); const cutoff='2026-10-19T01:00:00.000Z'; function rule(patch={}){return {id:'r1',version:1,categoryId:'notice',name:'订单通知',subjectPrefix:'订单通知',intro:'请处理以下订单。',signature:'业务部',to:['USER@example.com','user@example.com'],cc:['copy@example.com','USER@example.com'],attachPdf:false,archiveDirectory:'\\\\nas\\orders',...patch};} function order(id='1',patch={}){return {id,event:{orderNo:`SO-${id}`,brief:'型号 ',receivedAt:'2026-10-19T00:00:00.000Z'},ruleSnapshot:rule(),archive:{archivedAt:'2026-10-19T00:30:00.000Z',files:[{name:`SO-${id}.pdf`,path:`\\\\nas\\orders\\SO-${id}.pdf`,sha256:'a'.repeat(64),size:100,kind:'pdf'}]},...patch};} test('state actions enforce archive, pending and batch progression without skipping evidence stages',()=>{ assert.equal(transition('order','identified','prepare'),'preparing'); assert.equal(transition('order','preparing','archive'),'archived'); assert.equal(transition('order','archived','queue'),'pending'); assert.equal(transition('order','pending','batch'),'batched'); assert.equal(transition('order','batched','sent'),'sent'); assert.throws(()=>transition('order','identified','sent'),{status:409}); assert.throws(()=>transition('order','blocked','sent'),{status:409}); assert.throws(()=>transition('batch','failed','send'),{status:409}); assert.throws(()=>transition('batch','unknown','send'),{status:409}); assert.equal(transition('batch','prepared','send'),'sending'); assert.equal(transition('batch','sending','unknown'),'unknown'); assert.equal(transition('batch','unknown','resolve_sent'),'sent'); assert.equal(transition('batch','unknown','retry'),'cancelled'); }); test('batch includes every order above 200 with subject numbers and normalized independent recipients',()=>{ const orders=Array.from({length:205},(_,i)=>order(String(i+1))); const batch=buildBatch(rule(),orders,cutoff); assert.equal(batch.orderIds.length,205); assert.deepEqual(batch.attachments,[]); assert.match(batch.subject,/SO-1/);assert.match(batch.subject,/SO-205/); assert.deepEqual(batch.to,['user@example.com']);assert.deepEqual(batch.cc,['copy@example.com']); assert.equal(batch.cutoff,cutoff); orders[0].archive.files[0].name='mutated';orders[0].event.orderNo='mutated'; assert.equal(batch.orderNos[0],'SO-1');assert.match(batch.text,/订单号:SO-1/); }); test('mail keeps intro signature and escaped summaries with blue clickable UNC evidence',()=>{ const batch=buildBatch(rule({intro:'<团队>',signature:'研发 & 业务'}),[order()],cutoff); assert.match(batch.text,/<团队>/);assert.match(batch.text,/研发 & 业务/); assert.match(batch.html,/<script>/);assert.doesNotMatch(batch.html,/