Files
SupplierSystem/server/tests/mpDeliveryNoteImage.test.ts
T
雨天 2613b8f800
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
fix(delivery-note): 生产时间列移到金额之后
列序改为 编号/产品/单位/总数量/单价/金额/生产时间/备注;列宽合计仍 920。
2026-10-20 10:07:53 +08:00

229 lines
10 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 小程序「送货单**图片**」(canvas 出图)契约 + 金额大写**两侧对拍**。
*
* 开发者 2026-10-15 需求:分享出去的是**一张图片**(参照「秒账」的分享卡片),
* 与免登录 H5 送货单同字段:编号、产品、单位、总数量、「单价=销售价」、金额、备注,加合计与金额大写,
* + 订货电话/地址 + 制单人/收货人(签字);**不打印成本价**、**商品名取下单原文**、
* **不打印收款记录**(2026-10-15 裁决)。
*
* 设计:docs/design/20261015-小程序送货单图片(canvas出图分享).md
* 入口已于 2026-10-15 二次修改:**订单列表卡片**(不再是详情页)——开发者:「按钮别放到详情里面,
* 放到订单列表的外面就行」。
*/
import { createRequire } from 'module';
import { readFileSync } from 'fs';
import { describe, expect, it } from 'vitest';
import { amountInWords as serverAmountInWords } from '../src/utils/amountInWords';
const requireCjs = createRequire(import.meta.url);
const miniWords = requireCjs('../../miniprogram/utils/amountInWords.js') as {
amountInWords: (v: unknown) => string;
};
const utilSrc = readFileSync(
new URL('../../miniprogram/utils/deliveryNoteImage.js', import.meta.url),
'utf8'
);
const pageJs = readFileSync(
new URL('../../miniprogram/components/order-manage-panel/index.js', import.meta.url),
'utf8'
);
const pageWxml = readFileSync(
new URL('../../miniprogram/components/order-manage-panel/index.wxml', import.meta.url),
'utf8'
);
const pageWxss = readFileSync(
new URL('../../miniprogram/components/order-manage-panel/index.wxss', import.meta.url),
'utf8'
);
const detailJs = readFileSync(
new URL('../../miniprogram/pages/order-manage-detail/order-manage-detail.js', import.meta.url),
'utf8'
);
const detailWxml = readFileSync(
new URL('../../miniprogram/pages/order-manage-detail/order-manage-detail.wxml', import.meta.url),
'utf8'
);
const mobileRoutes = readFileSync(
new URL('../src/routes/mobile/mobileAdminRoutes.ts', import.meta.url),
'utf8'
);
const outboundBatchService = readFileSync(
new URL('../src/services/orderOutboundBatchService.ts', import.meta.url),
'utf8'
);
describe('金额大写:小程序副本与服务端逐字一致', () => {
it('同一批边界值两侧输出完全相同(改动任一侧都会被拦下)', () => {
const cases = [
0, 0.05, 0.5, 8, 12.3, 100, 948, 948.05, 948.5, 1000, 1001, 10002, 100000,
100000001, 1234567.89, 99999999.99, 'abc', Number.NaN, -8,
];
for (const value of cases) {
expect(miniWords.amountInWords(value), `值 ${String(value)}`).toBe(
serverAmountInWords(value)
);
}
});
it('与送货单截图同款:948 ⇒ 玖佰肆拾捌元整', () => {
expect(miniWords.amountInWords(948)).toBe('玖佰肆拾捌元整');
});
});
describe('出图版式(与 H5 送货单同字段)', () => {
it('画的是送货单:标题/单号/销售日期/客户 + 八列表头 + 合计', () => {
expect(utilSrc).toContain("ctx.fillText('送货单'");
expect(utilSrc).toContain("'NO.' + String(note.order_code");
expect(utilSrc).toContain("'销售日期:'");
expect(utilSrc).toContain("'客户:'");
for (const label of ['编号', '产品', '单位', '生产时间', '总数量', '单价', '金额', '备注', '合计']) {
expect(utilSrc).toContain(`'${label}'`);
}
});
it('金额大写 + 电话/地址/制单人/收货人签字都在,订单号与日期取自真实数据', () => {
expect(utilSrc).toContain('amountInWords(totalAmount)');
expect(utilSrc).toContain("'订货电话:'");
expect(utilSrc).toContain("'订货地址:'");
expect(utilSrc).toContain("'制单人:'");
expect(utilSrc).toContain("'收货人(签字):'");
});
it('绝不打印成本价(单价是销售价)', () => {
expect(utilSrc).not.toContain('cost_price');
expect(utilSrc).not.toContain('unit_cost');
});
it('不打印收款记录(2026-10-15 裁决:库里没有按单收款,不编数字)', () => {
expect(utilSrc).not.toContain('已收款');
expect(utilSrc).not.toContain('未收款');
});
it('图片高度随行数增长(长单不会被截断)', () => {
const util = requireCjs('../../miniprogram/utils/deliveryNoteImage.js') as {
deliveryNoteHeight: (n: number) => number;
};
expect(util.deliveryNoteHeight(3)).toBeLessThan(util.deliveryNoteHeight(12));
});
it('生产时间列:8 列且列宽合计 920(W - 2*MARGIN,不会溢出)', () => {
const matched = utilSrc.match(/const widths = \[([^\]]+)\]/);
expect(matched).toBeTruthy();
const widths = String(matched![1])
.split(',')
.map((value) => Number(value.trim()));
expect(widths).toHaveLength(8);
expect(widths.reduce((sum, value) => sum + value, 0)).toBe(920);
// 列序钉死:生产时间在「金额」之后、「备注」之前(2026-10-20 开发者指定)
expect(utilSrc).toContain(
"drawRow(['编号', '产品', '单位', '总数量', '单价', '金额', '生产时间', '备注']"
);
// 右对齐列跟着列序走:总数量/单价/金额 = 下标 3/4/5
expect(utilSrc).toContain('const rightAligned = i === 3 || i === 4 || i === 5;');
});
it('多批次把行撑高:deliveryNoteHeight 按每行日期数增长(单批次不改变旧行高)', () => {
const util = requireCjs('../../miniprogram/utils/deliveryNoteImage.js') as {
deliveryNoteHeight: (value: number | Array<Record<string, unknown>>) => number;
};
const one = [{ item_name: 'A', production_dates: ['2026-10-01'] }];
const three = [
{ item_name: 'A', production_dates: ['2026-10-01', '2026-10-02', '2026-10-03'] },
];
expect(util.deliveryNoteHeight(three)).toBeGreaterThan(util.deliveryNoteHeight(one));
// 单批次 ⇒ 仍是默认行高;空单与 1 行同高(不因新列变矮/变高)
expect(util.deliveryNoteHeight(one)).toBe(util.deliveryNoteHeight(1));
expect(util.deliveryNoteHeight([])).toBe(util.deliveryNoteHeight(1));
});
it('建单据:批次生产日期透传为 production_dates(未出库为空数组,已退货行不打印)', () => {
const util = requireCjs('../../miniprogram/utils/deliveryNoteImage.js') as {
buildDeliveryNotePayload: (
order: Record<string, unknown>,
items: Array<Record<string, unknown>>
) => { items: Array<{ production_dates: string[] }> };
};
const note = util.buildDeliveryNotePayload({ order_code: 'X' }, [
{ item_name: 'A', batch_production_dates: ['2026-10-01', '2026-10-02'] },
{ item_name: 'B' },
{ item_name: 'C', batch_production_dates: ['2026-10-03'], is_returned: true },
]);
expect(note.items.map((item) => item.production_dates)).toEqual([
['2026-10-01', '2026-10-02'],
[],
]);
});
});
describe('订单列表卡片接线(2026-10-15 二次修改:入口从详情页移到列表)', () => {
it('列表卡片挂了隐藏画布并调用出图工具', () => {
expect(pageWxml).toContain('type="2d" id="deliveryNoteCanvas"');
expect(pageWxss).toContain('.print-canvas');
expect(pageJs).toContain("require('../../utils/deliveryNoteImage')");
expect(pageJs).toContain("exportDeliveryNoteImage(");
expect(pageJs).toContain("'#deliveryNoteCanvas'");
expect(pageJs).toContain('buildDeliveryNotePayload(order, items)');
});
it('按钮在列表卡片上(catchtap 不冒泡到开详情);独立进货单不给(不是给客户的货)', () => {
expect(pageWxml).toContain('送货单图片');
expect(pageWxml).toContain('catchtap="onDeliveryNoteImage"');
expect(pageWxml).toMatch(/wx:if="\{\{item\.customer_code !== '__INDEPENDENT_INBOUND__'\}\}"/);
});
it('与配送/推进按钮**同一行**(放在左组 .actions-main 里,不新开一行)', () => {
expect(pageWxml).toContain('<view class="actions-main">');
expect(pageWxml).not.toContain('actions--note');
expect(pageWxss).toContain('.actions-main {');
});
it('详情页不再有送货单图片入口(按钮/画布/处理函数都清掉)', () => {
expect(detailWxml).not.toContain('送货单图片');
expect(detailWxml).not.toContain('deliveryNoteCanvas');
expect(detailJs).not.toContain('onDeliveryNoteImage');
expect(detailJs).not.toContain('buildDeliveryNotePayload');
});
it('出图后给:预览 / 保存到相册 / 分享给好友(不支持图片分享菜单时降级提示)', () => {
expect(pageJs).toContain("itemList: ['预览', '保存到相册', '分享给好友']");
expect(pageJs).toContain('wx.saveImageToPhotosAlbum(');
expect(pageJs).toContain('wx.previewImage(');
expect(pageJs).toContain("typeof wx.showShareImageMenu === 'function'");
expect(pageJs).toContain('长按图片可保存或转发');
// 无相册权限要能引导去设置,而不是静默失败
expect(pageJs).toContain('wx.openSetting()');
});
it('数据口径:下单原文 + 散客单显示「散客录单」 + 已退货行不打印', () => {
expect(utilSrc).toContain("item_name: item.item_name || item.sku_name || ''");
expect(utilSrc).toContain("? '散客录单'");
expect(utilSrc).toContain('.filter((item) => !item.is_returned)');
});
});
describe('后端补的两个字段', () => {
it('移动端订单详情返回 contact_phone 与 created_by_name(只加字段)', () => {
expect(mobileRoutes).toContain("NULLIF(TRIM(c.contact_phone), '') AS contact_phone");
expect(mobileRoutes).toContain('AS created_by_name');
expect(mobileRoutes).toContain('LEFT JOIN public.profiles p ON p.id = o.created_by');
});
it('送货单「生产时间」取自本单实际用掉的批次(出库流水 ⋈ 批次生产日期)', () => {
// 事实源必须是出库流水(未出库就没有流水 ⇒ 不猜批次)
expect(outboundBatchService).toContain(
'JOIN inventory inv ON inv.batch_no = il.batch_no AND inv.sku_code = il.sku_code'
);
expect(outboundBatchService).toContain(
"il.operation_type IN ('picking_out', 'delivery_loading_out', 'retail_out')"
);
expect(outboundBatchService).toContain('AND inv.production_date IS NOT NULL');
expect(outboundBatchService).toContain('ORDER BY il.sku_code, inv.production_date ASC');
// 只加字段:每个明细行带 batch_production_dates(去重升序的数组)
expect(mobileRoutes).toContain('listOrderBatchProductionDates(orderCode)');
expect(mobileRoutes).toContain(
"batch_production_dates: productionDatesBySku.get(String(row.sku_code || '')) || [],"
);
});
});