import { describe, it, expect } from 'vitest'; import { buildPickingLabelHtml, 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('50×30mm 纸宽,一签一分区', () => { const html = buildPickingLabelHtml([{ itemName: '大葱', qty: 10, unit: '斤' }]); expect(html).toContain('@page { size: 50.000mm 30.000mm; margin: 0; }'); expect(html).toContain('class="pick-label"'); expect(html).toContain('分拣标签'); }); it('标签自然平铺:不强制分页、不设百分比高(防每页留一张空白标签)', () => { const html = buildPickingLabelHtml([{ itemName: '大葱', qty: 1, unit: '斤' }]); expect(html).toMatch(/\.pick-label \{[^}]*page-break-inside: avoid/); // 回归①:强制分页在「浏览器页高=标签高整数倍」时,每页剩下一整张空白标签(打 N 张耗 2N 张) expect(html).not.toContain('page-break-after: always'); // 回归②:百分比高会把每张标签撑成两页 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('有 barcode 时内联二维码 SVG;无则不输出', () => { const withQr = buildPickingLabelHtml([{ itemName: '大葱', qty: 1, unit: '斤', barcode: 'SKU-1' }]); expect(withQr).toContain('class="t-qr"'); expect(withQr).toContain(' { const html = buildPickingLabelHtml([ { itemName: '大葱', qty: 1, unit: '斤', companyName: '', barcode: 'a&b' }, ]); expect(html).toContain('客户:客户'); expect(html).toContain('品名:<b>大葱</b>'); expect(html).toContain('a&b'); expect(html).not.toContain('订单号'); }); });