来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
96 lines
4.2 KiB
TypeScript
96 lines
4.2 KiB
TypeScript
// @vitest-environment jsdom
|
||
import { describe, expect, it } from 'vitest';
|
||
import { readFileSync } from 'node:fs';
|
||
import path from 'node:path';
|
||
import { fitOverflowingPrintHeadings } from './deliveryNotePrint';
|
||
|
||
/**
|
||
* 标题自动缩放(2026-10-06 用户要求):
|
||
* AI 照参考图生成的标题常是「一行且不换行」,真实标题(公司名/客户名)更长时就顶出纸张。
|
||
* 打印前按可用宽度把溢出的标题字号逐级缩小,缩到下限仍放不下才改为换行。
|
||
*
|
||
* jsdom 没有布局引擎,这里用可控的假布局:scrollWidth = 字符数 × 字号 × 0.5,
|
||
* 于是缩小字号必然让 scrollWidth 变小,可以真实地驱动循环。
|
||
*/
|
||
function buildHeading(options: { base: number; available: number; units: number }) {
|
||
const { base, available, units } = options;
|
||
const host = document.createElement('div');
|
||
const root = document.createElement('div');
|
||
root.className = 'delivery-html-template-root';
|
||
const h = document.createElement('h1');
|
||
h.textContent = '配'.repeat(units);
|
||
h.style.fontSize = `${base}px`;
|
||
Object.defineProperty(h, 'clientWidth', { get: () => available, configurable: true });
|
||
Object.defineProperty(h, 'scrollWidth', {
|
||
get(this: HTMLElement) {
|
||
const size = Number.parseFloat(this.style.fontSize) || base;
|
||
return Math.ceil(units * size * 0.5);
|
||
},
|
||
configurable: true,
|
||
});
|
||
root.appendChild(h);
|
||
host.appendChild(root);
|
||
return { host, h };
|
||
}
|
||
|
||
describe('打印前标题自动缩放', () => {
|
||
it('没有横向溢出时保持模板原样(不动字号、不动换行)', () => {
|
||
// 40 字 × 20px × 0.5 = 400;可用 400 → 刚好不溢出
|
||
const { host, h } = buildHeading({ base: 20, available: 400, units: 40 });
|
||
fitOverflowingPrintHeadings(host);
|
||
expect(h.style.fontSize).toBe('20px');
|
||
expect(h.style.whiteSpace).toBe('');
|
||
expect(h.style.wordBreak).toBe('');
|
||
});
|
||
|
||
it('溢出时逐级缩小到刚好放下,不触发换行', () => {
|
||
// 40 字:400px 起溢出,可用 300 → 需要 ≤15px(15×20=300)
|
||
const { host, h } = buildHeading({ base: 20, available: 300, units: 40 });
|
||
fitOverflowingPrintHeadings(host);
|
||
expect(h.style.fontSize).toBe('15px');
|
||
expect(h.style.getPropertyPriority('font-size')).toBe('important');
|
||
expect(h.style.whiteSpace).toBe('');
|
||
});
|
||
|
||
it('缩到下限仍放不下时改为允许换行(不溢出纸张)', () => {
|
||
// 200 字:即便缩到下限 10px 也有 1000px,远大于可用 300 → 必须换行兜底
|
||
const { host, h } = buildHeading({ base: 20, available: 300, units: 200 });
|
||
fitOverflowingPrintHeadings(host);
|
||
// 下限 = max(9, base*0.5) = 10
|
||
expect(h.style.fontSize).toBe('10px');
|
||
expect(h.style.whiteSpace).toBe('normal');
|
||
expect(h.style.wordBreak).toBe('break-word');
|
||
});
|
||
|
||
it('非 AI 模板(没有 .delivery-html-template-root)完全不动', () => {
|
||
const host = document.createElement('div');
|
||
const h = document.createElement('h1');
|
||
h.textContent = '很长很长的标题'.repeat(20);
|
||
host.appendChild(h);
|
||
fitOverflowingPrintHeadings(host);
|
||
expect(h.style.fontSize).toBe('');
|
||
expect(h.style.whiteSpace).toBe('');
|
||
});
|
||
|
||
it('打印路径里确实在 window.print() 之前调用了缩放', () => {
|
||
const src = readFileSync(path.join(__dirname, 'deliveryNotePrint.ts'), 'utf8');
|
||
const at = src.indexOf('fitOverflowingPrintHeadings(host);');
|
||
expect(at).toBeGreaterThan(-1);
|
||
expect(src.slice(at, at + 200)).toContain('window.print();');
|
||
});
|
||
|
||
/**
|
||
* 顺序不能反:缩字号会改变元素高度,所以必须**先**缩字号、**再**把目标坐标换算成位移,
|
||
* 否则位移是按旧高度算的,打印出来会偏。
|
||
*/
|
||
it('先缩字号、再套目标位移,最后才 window.print()', () => {
|
||
const src = readFileSync(path.join(__dirname, 'deliveryNotePrint.ts'), 'utf8');
|
||
const fitAt = src.indexOf('fitOverflowingPrintHeadings(host);');
|
||
const targetAt = src.indexOf('applyLayoutTargets(host);');
|
||
const printAt = src.indexOf('window.print();', fitAt);
|
||
expect(fitAt).toBeGreaterThan(-1);
|
||
expect(targetAt).toBeGreaterThan(fitAt);
|
||
expect(printAt).toBeGreaterThan(targetAt);
|
||
});
|
||
});
|