Files
weiguang-jewelry-ledger/新账系统/test/print-layout.test.js
summer a506fb1228 完善订单审批、每日对账与信用卡手续费
加入助理订单变更复核、收款人汇总、每日报销和应转金额;优化库存筛选、订单明细与连续浏览。开单身份必填,信用卡手续费按实收4‰自动计算并默认由麦麦承担,打印优惠显示在人民币大写旁。

验证:484项测试通过;独立HTTP与浏览器覆盖普通订单、锁价、修改审批、断线恢复和打印隔离。旧报表兼容,本机升级原29表295行数据保持一致。
2026-10-23 09:49:36 +08:00

86 lines
6.8 KiB
JavaScript

'use strict';
const {test}=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),path=require('node:path'),vm=require('node:vm');
const {receipt,settings,defaults}=require('../printing');
const script=fs.readFileSync(path.join(__dirname,'../public/print.js'),'utf8');
const mm=value=>Number.parseFloat(value)*96/25.4;
function classList(initial=[]){const values=new Set(initial);return {add:value=>values.add(value),remove:value=>values.delete(value),contains:value=>values.has(value)};}
// 模拟 beforeprint 的两种浏览器时序:进入打印媒体之前,或屏幕 HTML 已被打印 CSS 隐藏之后。
function page({long=false,fontsReady=true}={}){
const events=new Map(),mediaEvents=new Map(),clicks=new Map(),media={matches:false,addEventListener:(name,fn)=>mediaEvents.set(name,fn)};
const body={classList:classList(),children:[],appendChild(node){this.children.push(node);node.parentNode=this;}};
const frame={dataset:{layoutWidthMm:'217',layoutHeightMm:'120'},get clientWidth(){return media.matches?640:Math.floor(mm('217'));},get clientHeight(){return media.matches?980:Math.floor(mm('120'));}};
const makeContent=original=>({
style:{},dataset:{},classList:classList(),textContent:'首行商品 尾行商品 注意事项 珠宝首饰保养须知 备注 经办人:麦麦',
get offsetWidth(){return Number.parseFloat(this.style.width?.endsWith('px')?this.style.width:this.parentNode.clientWidth);},
get scrollWidth(){return this.offsetWidth;},
get scrollHeight(){if(original&&media.matches)return 0;return Math.ceil((long?2800000:490000)/this.offsetWidth*(this.classList.contains('compact')?.8:1));},
get offsetHeight(){return this.scrollHeight;},getBoundingClientRect(){return {height:this.scrollHeight};},
cloneNode(){const copy=makeContent(false);copy.style={...this.style};copy.dataset={...this.dataset};return copy;},
removeAttribute(name){if(name==='data-scale')delete this.dataset.scale;}
});
const content=makeContent(true);content.parentNode=frame;
const foreignObject={attributes:{},children:[],setAttribute(name,value){this.attributes[name]=value;},replaceChildren(child){this.children=[child];child.parentNode=this;}};
const canvas={attributes:{},setAttribute(name,value){this.attributes[name]=value;},querySelector:()=>foreignObject};
const button={disabled:true,addEventListener:(name,fn)=>clicks.set(name,fn)},status={textContent:''};
const window={matchMedia:()=>media,addEventListener:(name,fn)=>events.set(name,fn),printCalls:0,print(){this.printCalls++;}};
const document={
body,fonts:{ready:fontsReady?Promise.resolve():new Promise(()=>{})},getElementById:id=>id==='print'?button:status,
querySelector:selector=>({'.print-frame':frame,'.content':content,'.print-canvas':canvas})[selector],
createElement:()=>({style:{},get clientWidth(){return Math.floor(mm(this.style.width));},get clientHeight(){return Math.floor(mm(this.style.height));},appendChild(child){this.child=child;child.parentNode=this;},remove(){body.children.splice(body.children.indexOf(this),1);}})
};
vm.runInNewContext(script,{window,document});
return {body,content,foreignObject,canvas,button,status,window,media,events,mediaEvents,click:()=>clicks.get('click')()};
}
test('默认、旧设置和客户端传入宽高均固定为241×140mm横版,不能放大到A4页面',()=>{
const sample={lines:[],total:0,paper_number:'20261022001',status:'delivered'};
assert.equal(defaults.width,241);assert.equal(defaults.height,140);
for(const raw of [{},{width:241,height:140},{width:241,height:279},{width:140,height:241},{width:300,height:400},{width:'无效旧宽度',height:-1},{width:null,height:''}]){
const before=JSON.stringify(raw);
const saved=settings(raw),html=receipt(sample,saved),print=html.slice(html.indexOf('@page'));
assert.equal(saved.width,241);assert.equal(saved.height,140);assert.equal(JSON.stringify(raw),before);
assert.match(print,/@page\{size:241mm 140mm;margin:0\}/);
assert.match(print,/\.sheet\{width:241mm;height:calc\(140mm - 1px\);max-width:241mm;max-height:140mm/);
assert.doesNotMatch(print,/size:auto|100vh|100vw|rotate\(/);
assert.match(html,/\.sheet\{width:241mm;height:140mm/);
assert.match(html,/\.print-frame\{position:absolute;max-width:192mm/);
assert.match(html,/\.totals-summary\{[^}]*min-width:0[^}]*flex-wrap:wrap/);
assert.match(html,/\.totals-summary \.uppercase\{min-width:0\}/);
assert.match(html,/\.totals-summary \.discount\{white-space:nowrap\}/);
assert.match(html,/data-layout-width-mm="217" data-layout-height-mm="120"/);
assert.match(html,/preserveAspectRatio="xMinYMin meet"/);
}
});
test('打印在媒体切换前后准备都不沿用屏幕缩放,且保留尾部所有文字',async()=>{
for(const mediaFirst of [false,true]){
const p=page({long:true});await Promise.resolve();
const screenScale=Number(p.content.dataset.scale),viewBox=p.canvas.attributes.viewBox;
assert.ok(screenScale>0&&screenScale<1);
if(mediaFirst)p.media.matches=true;
p.events.get('beforeprint')();p.media.matches=true;
assert.equal(p.canvas.attributes.viewBox,viewBox);
const copy=p.foreignObject.children[0];
assert.equal(copy.style.transform,'none');assert.equal(copy.dataset.scale,undefined);
assert.equal(copy.textContent,p.content.textContent);assert.ok(Number(p.foreignObject.attributes.height)>0);
assert.equal(p.body.classList.contains('fit-error'),false);
assert.equal(p.body.children.length,0,'测量节点在打印前清理,不产生额外打印页');
p.events.get('resize')();assert.equal(p.body.classList.contains('fit-error'),false);
assert.equal(Number(p.content.dataset.scale),screenScale);
p.media.matches=false;p.events.get('afterprint')();
assert.equal(Number(p.content.dataset.scale),screenScale);assert.equal(p.foreignObject.children.length,1);
}
});
test('快捷键在字体等待期间进入打印也能同步生成完整客户副本',()=>{
const p=page({fontsReady:false});p.media.matches=true;p.events.get('beforeprint')();
assert.ok(p.canvas.attributes.viewBox);assert.equal(p.foreignObject.children.length,1);
assert.match(p.foreignObject.children[0].textContent,/尾行商品.*注意事项.*经办人:麦麦/);
assert.equal(p.body.classList.contains('fit-error'),false);
});
test('打印按钮等待排版,关闭打印后恢复预览且再次打印不累积副本',async()=>{
const p=page();await Promise.resolve();assert.equal(p.button.disabled,false);
await p.click();assert.equal(p.window.printCalls,1);
p.events.get('beforeprint')();p.media.matches=true;p.events.get('resize')();
p.media.matches=false;p.mediaEvents.get('change')({matches:false});p.events.get('afterprint')();
await p.click();assert.equal(p.window.printCalls,2);assert.equal(p.foreignObject.children.length,1);
assert.equal(p.button.disabled,false);assert.equal(p.body.children.length,0);
});