import { describe, it, expect } from 'vitest';
import {
buildPickingLabelHtml,
buildPickingLabelRulerHtml,
formatLabelWeightLine,
sortItemsForLabelPrint,
} from './pickingLabelPrint';
describe('formatLabelWeightLine(与旧秤端逐字一致)', () => {
it('重量单位统一换算为「斤」', () => {
expect(formatLabelWeightLine(12, '斤')).toBe('重量:12.00 斤');
expect(formatLabelWeightLine(6, 'kg')).toBe('重量:12.00 斤');
expect(formatLabelWeightLine(6, '公斤')).toBe('重量:12.00 斤');
expect(formatLabelWeightLine(1000, '克')).toBe('重量:2.00 斤');
});
it('计数单位展示数量', () => {
expect(formatLabelWeightLine(5, '瓶')).toBe('数量:5.00 瓶');
expect(formatLabelWeightLine(3, '')).toBe('数量:3.00 斤');
});
});
describe('sortItemsForLabelPrint(批量打印排队)', () => {
// 分组值用 ASCII,避免断言依赖中文拼音排序
const items = [
{ item_name: 'C-白菜', order_code: 'O2', category_name: 'CAT-B', storage_zone: 'Z-B', sku_code: 'SKU-3', sort_order: 2 },
{ item_name: 'A-苹果', order_code: 'O1', category_name: 'CAT-A', storage_zone: 'Z-A', sku_code: 'SKU-1', sort_order: 1 },
{ item_name: 'B-菠菜', order_code: 'O1', category_name: 'CAT-A', storage_zone: 'Z-A', sku_code: 'SKU-2', sort_order: 1 },
];
it('按订单分组,组内按 sort_order 再按品名', () => {
const out = sortItemsForLabelPrint(items, 'order').map((i) => i.item_name);
expect(out).toEqual(['A-苹果', 'B-菠菜', 'C-白菜']);
});
it('按分类分组', () => {
const out = sortItemsForLabelPrint(items, 'category').map((i) => i.item_name);
expect(out).toEqual(['A-苹果', 'B-菠菜', 'C-白菜']);
});
it('按商品(SKU)分组', () => {
const out = sortItemsForLabelPrint(items, 'sku').map((i) => i.item_name);
expect(out).toEqual(['A-苹果', 'B-菠菜', 'C-白菜']);
});
it('按区域分组,且尊重分公司区域顺序', () => {
const ordered = sortItemsForLabelPrint(items, 'zone', { zoneOrder: ['Z-B', 'Z-A'] }).map((i) => i.storage_zone);
expect(ordered).toEqual(['Z-B', 'Z-A', 'Z-A']);
});
it('未配置区域顺序时按名称排', () => {
const fallback = sortItemsForLabelPrint(items, 'zone').map((i) => i.storage_zone);
expect(fallback).toEqual(['Z-A', 'Z-A', 'Z-B']);
});
it('不改动原数组', () => {
const before = items.map((i) => i.item_name);
sortItemsForLabelPrint(items, 'sku');
expect(items.map((i) => i.item_name)).toEqual(before);
});
});
describe('buildPickingLabelHtml(分拣标签 · 浏览器打印)', () => {
it('默认纸张 80×60mm(实物签尺寸;可被传入覆盖)', () => {
const html = buildPickingLabelHtml([{ itemName: '大葱', qty: 10, unit: '斤' }]);
expect(html).toContain('@page { size: 80mm 60mm; margin: 0; }');
expect(html).toContain('class="pick-label"');
expect(html).toContain('分拣标签');
});
it('纸张尺寸动态传入(不写死);非法值回落默认', () => {
const html = buildPickingLabelHtml([{ itemName: 'a', qty: 1, unit: '斤' }], {
paper: { widthMm: 80, heightMm: 60 },
});
expect(html).toContain('@page { size: 80mm 60mm; margin: 0; }');
expect(html).toMatch(/\.pick-label \{[^}]*width: 80mm/);
// 块高**只包住内容**(不是 59.5mm = 纸高):insetY 3.57 + 末行下沿 23.77 + 下边距 3.57 = 27.34
expect(html).toMatch(/\.pick-label \{[^}]*height: 27\.34mm/);
const bad = buildPickingLabelHtml([{ itemName: 'a', qty: 1, unit: '斤' }], {
paper: { widthMm: 0, heightMm: -3 },
});
expect(bad).toContain('@page { size: 80mm 60mm; margin: 0; }');
});
it('一签一页:块高只包住内容 + 强制分页(防对话框边距把块挤到下一页)', () => {
const html = buildPickingLabelHtml([{ itemName: '大葱', qty: 1, unit: '斤' }]);
// 回归①(2026-10-14 实测):块高 = 纸高 时,打印对话框的「边距」会盖掉 @page{margin:0}、
// 把可用高度压小 ⇒ 块溢出被浏览器拆到下一页 ⇒ 打两张耗三张、中间夹一张空白。
// 块高必须**小于**纸高,且默认 80×60(6 行)时为 27.34mm(远小于任何纸型的可用高)。
expect(html).not.toMatch(/\.pick-label \{[^}]*height: 59\.5mm/);
expect(html).toMatch(/\.pick-label \{[^}]*height: 27\.34mm/);
expect(html).toMatch(/\.pick-label \{[^}]*page-break-inside: avoid/);
// 一签一页改靠**强制分页**;末张复位,防尾部多出一张空白页
expect(html).toMatch(/\.pick-label \{[^}]*page-break-after: always/);
expect(html).toMatch(/\.pick-label:last-child \{[^}]*page-break-after: auto/);
// 回归②:百分比高会把每张标签撑成两页(打印宿主 @media print 把 html/body 高度置 auto)
expect(html).not.toContain('padding: 0; height: 100%');
expect(html).not.toContain('min-height: 100%');
expect(html).not.toContain('pick-page');
});
it('多张标签 = 多页(一签一页)', () => {
const html = buildPickingLabelHtml([
{ itemName: 'a', qty: 1, unit: '斤' },
{ itemName: 'b', qty: 2, unit: '斤' },
]);
expect((html.match(/class="pick-label"/g) || []).length).toBe(2);
});
it('旋转 0°(默认):不包页面框、无 transform', () => {
const html = buildPickingLabelHtml([{ itemName: 'a', qty: 1, unit: '斤' }]);
expect(html).not.toContain('class="pick-page"');
expect(html).not.toContain('rotate(');
});
it('旋转 90°/270°:页面尺寸转置 + 标签块旋转(一页一签)', () => {
const html90 = buildPickingLabelHtml([{ itemName: 'a', qty: 1, unit: '斤' }], {
paper: { widthMm: 80, heightMm: 30 },
rotation: 90,
});
// 页面转置为 30×80,标签块仍是 80×29.5 但整体转 90°(旋转模式仍用整页高,块高压缩只作用于 0°)
expect(html90).toContain('@page { size: 30mm 80mm; margin: 0; }');
expect(html90).toMatch(/\.pick-label \{[^}]*width: 80mm/);
expect(html90).toMatch(/\.pick-label \{[^}]*height: 29\.5mm/);
expect(html90).toContain('rotate(90deg)');
expect(html90).toContain('class="pick-page"');
expect(html90).toContain('break-after: page');
const html270 = buildPickingLabelHtml([{ itemName: 'a', qty: 1, unit: '斤' }], {
paper: { widthMm: 80, heightMm: 30 },
rotation: 270,
});
expect(html270).toContain('@page { size: 30mm 80mm; margin: 0; }');
expect(html270).toContain('rotate(-90deg)');
});
it('旋转 180°:页面尺寸不变、标签块翻转', () => {
const html = buildPickingLabelHtml([{ itemName: 'a', qty: 1, unit: '斤' }], {
paper: { widthMm: 80, heightMm: 30 },
rotation: 180,
});
expect(html).toContain('@page { size: 80mm 30mm; margin: 0; }');
expect(html).toContain('rotate(180deg)');
});
it('非法旋转值回落 0°', () => {
const html = buildPickingLabelHtml([{ itemName: 'a', qty: 1, unit: '斤' }], {
rotation: 45 as unknown as number,
});
expect(html).not.toContain('rotate(');
});
it('测试打印(标尺):整页刻度 + 页码数跟随 pages,纸张/旋转与标签一致', () => {
const html = buildPickingLabelRulerHtml({
paper: { widthMm: 80, heightMm: 30 },
rotation: 90,
pages: 3,
});
// 页面尺寸跟随旋转(转置)
expect(html).toContain('@page { size: 30mm 80mm; margin: 0; }');
// 每 5mm 一个刻度:横向 0..80 ⇒ 17 个;纵向 0..29.5 ⇒ 6 个;共 3 页
expect((html.match(/class="tk-v/g) || []).length).toBe(17 * 3);
expect((html.match(/class="tk-h/g) || []).length).toBe(6 * 3);
// 标注当前纸张与旋转,便于拍照回报
expect(html).toContain('80 × 30 mm');
expect(html).toContain('旋转 90');
// 页码数
expect((html.match(/class="pick-label"/g) || []).length).toBe(3);
// 旋转模式下一页一签
expect((html.match(/class="pick-page"/g) || []).length).toBe(3);
});
it('窄纸(50×30)四周留比例边距(≥1mm),且不再贴边', () => {
const html = buildPickingLabelHtml([{ itemName: 'a', qty: 1, unit: '斤', barcode: 'X' }], {
paper: { widthMm: 50, heightMm: 30 },
});
// 50 × 6% = 3mm ⇒ 二维码 right 3mm;文本块右边界 = 3 + 13.75 + 1 = 17.750mm
expect(html).toMatch(/\.t-qr \{[^}]*right: 3\.000mm/);
expect(html).toMatch(/\.pick-label > div \{[^}]*left: 3\.000mm/);
expect(html).toMatch(/\.pick-label > div \{[^}]*right: 17\.750mm/);
});
it('宽纸(80×30)受可打印宽度约束:左右各让出 10mm,二维码落在可打印区内', () => {
const html = buildPickingLabelHtml([{ itemName: 'a', qty: 1, unit: '斤', barcode: 'X' }], {
paper: { widthMm: 80, heightMm: 30 },
});
// inset = max(1, 80×6% = 4.8, 80 − 70 = 10) = 10mm
// ⇒ 内容落在 10–70mm(居中铺/左对齐铺两种映射都在可打印区内)
expect(html).toMatch(/\.t-qr \{[^}]*right: 10\.000mm/);
expect(html).toMatch(/\.pick-label > div \{[^}]*left: 10\.000mm/);
expect(html).toMatch(/\.pick-label > div \{[^}]*right: 24\.750mm/);
// @page 仍是实物签尺寸(不被内缩改写)
expect(html).toContain('@page { size: 80mm 30mm; margin: 0; }');
});
it('默认打「客户」与「区域」两行(区域紧跟客户之后)', () => {
const html = buildPickingLabelHtml(
[{ itemName: '大葱', qty: 1, unit: '斤', companyName: '甲客户', zone: 'A区' }],
{ paper: { widthMm: 50, heightMm: 30 } }
);
expect(html).toContain('客户:甲客户');
expect(html).toContain('区域:A区');
// 6 行自上而下:insetY 1.77 + n × 3.5
expect(html).toMatch(/\.t-title \{ top: 1\.770mm/);
expect(html).toMatch(/\.t-cust \{ top: 5\.270mm/);
expect(html).toMatch(/\.t-zone \{ top: 8\.770mm/);
expect(html).toMatch(/\.t-name \{ top: 12\.270mm/);
expect(html).toMatch(/\.t-weight \{ top: 15\.770mm/);
expect(html).toMatch(/\.t-date \{ top: 19\.270mm/);
});
it('可关「客户」/「区域」:关掉的行不输出,后面的行上移(不留空行)', () => {
const label = { itemName: '大葱', qty: 1, unit: '斤', companyName: '甲客户', zone: 'A区' };
const paper = { widthMm: 50, heightMm: 30 };
const noCust = buildPickingLabelHtml([label], { paper, showCustomer: false });
expect(noCust).not.toContain('客户:');
expect(noCust).toMatch(/\.t-zone \{ top: 5\.270mm/);
expect(noCust).toMatch(/\.t-name \{ top: 8\.770mm/);
const noZone = buildPickingLabelHtml([label], { paper, showZone: false });
expect(noZone).not.toContain('区域:');
expect(noZone).toMatch(/\.t-name \{ top: 8\.770mm/);
const none = buildPickingLabelHtml([label], { paper, showCustomer: false, showZone: false });
expect(none).not.toContain('客户:');
expect(none).not.toContain('区域:');
expect(none).toMatch(/\.t-title \{ top: 1\.770mm/);
expect(none).toMatch(/\.t-name \{ top: 5\.270mm/);
expect(none).toMatch(/\.t-weight \{ top: 8\.770mm/);
expect(none).toMatch(/\.t-date \{ top: 12\.270mm/);
// 行少了 ⇒ 块高跟着收缩(50×30 四行 = 20.14mm,仍由二维码列主导)
expect(none).toMatch(/\.pick-label \{[^}]*height: 20\.14mm/);
});
it('区域为空打「—」(与弹窗分组标题同口径),且仍做 HTML 转义', () => {
const empty = buildPickingLabelHtml([{ itemName: 'a', qty: 1, unit: '斤' }]);
expect(empty).toContain('区域:—');
const escaped = buildPickingLabelHtml([
{ itemName: 'a', qty: 1, unit: '斤', zone: '&区' },
]);
expect(escaped).toContain('区域:<b>&区</b>');
});
it('有 barcode 时内联二维码 SVG;无则不输出', () => {
const withQr = buildPickingLabelHtml([{ itemName: '大葱', qty: 1, unit: '斤', barcode: 'SKU-1' }]);
expect(withQr).toContain('class="t-qr"');
expect(withQr).toContain('