来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
291 lines
17 KiB
TypeScript
291 lines
17 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||
|
||
const llmMocks = vi.hoisted(() => ({ callWithImage: vi.fn(), callWithText: vi.fn(), hasRuntimeConfig: vi.fn() }));
|
||
vi.mock('../src/services/llmService', () => llmMocks);
|
||
|
||
import { parseDeliveryTemplateImage } from '../src/services/deliveryTemplateOcrService';
|
||
|
||
describe('parseDeliveryTemplateImage', () => {
|
||
beforeEach(() => {
|
||
llmMocks.callWithImage.mockReset();
|
||
llmMocks.callWithText.mockReset();
|
||
llmMocks.hasRuntimeConfig.mockReset();
|
||
llmMocks.hasRuntimeConfig.mockResolvedValue(true);
|
||
});
|
||
|
||
it('returns an independent HTML/CSS layout and validates bindings', async () => {
|
||
llmMocks.callWithImage.mockResolvedValue(JSON.stringify({
|
||
name: '天府物业配送单',
|
||
paper: { widthMm: 210, heightMm: 297, marginMm: 8 },
|
||
html: '<h1>{{order.branch_name}}配送单</h1><table><tbody>{{#items}}<tr><td>{{item.seq_no}}</td><td>{{item.sku_name}}</td><td>{{item.quantity}}</td></tr>{{/items}}</tbody></table>',
|
||
css: 'h1{text-align:center;font-size:20px}table{border-collapse:collapse;width:100%}',
|
||
}));
|
||
const result = await parseDeliveryTemplateImage('data:image/jpeg;base64,AAAA', 'BR002');
|
||
expect(result.name).toBe('天府物业配送单');
|
||
expect(result.html).toContain('{{#items}}');
|
||
expect(result.bindings).toContain('item.quantity');
|
||
expect(result.paper.widthMm).toBe(210);
|
||
});
|
||
|
||
it('rejects unsafe output and missing item loop', async () => {
|
||
llmMocks.callWithImage.mockResolvedValue(JSON.stringify({
|
||
name: 'bad', html: '<script>alert(1)</script>', css: 'body{}',
|
||
}));
|
||
await expect(parseDeliveryTemplateImage('data:image/jpeg;base64,AAAA')).rejects.toMatchObject({ code: 'unsafe_ai_result' });
|
||
llmMocks.callWithImage.mockResolvedValue(JSON.stringify({ name: 'bad', html: '<div>{{order.order_code}}</div>', css: 'body{}' }));
|
||
await expect(parseDeliveryTemplateImage('data:image/jpeg;base64,AAAA')).rejects.toMatchObject({ code: 'missing_items_loop' });
|
||
});
|
||
|
||
it('rejects missing images and missing vision configuration', async () => {
|
||
await expect(parseDeliveryTemplateImage('')).rejects.toMatchObject({ code: 'missing_image' });
|
||
llmMocks.hasRuntimeConfig.mockResolvedValue(false);
|
||
await expect(parseDeliveryTemplateImage('data:image/jpeg;base64,AAAA', 'BR002')).rejects.toMatchObject({ code: 'ARK_NOT_CONFIGURED' });
|
||
expect(llmMocks.callWithImage).not.toHaveBeenCalled();
|
||
});
|
||
/**
|
||
* 提示词必须明确「对齐照图片、不擅自设置、拿不准居中」(2026-10-06 用户要求)。
|
||
* 起因:AI 生成模板时自行给文字设了偏左,与参考模板不一致。
|
||
*/
|
||
it('提示词要求对齐照图片效果、不得擅自设置、拿不准居中', async () => {
|
||
llmMocks.callWithImage.mockResolvedValue(JSON.stringify({
|
||
name: 't', paper: { widthMm: 210, heightMm: 297, marginMm: 8 },
|
||
html: '<table><tbody>{{#items}}<tr><td>{{item.seq_no}}</td></tr>{{/items}}</tbody></table>',
|
||
css: 'td{text-align:center}',
|
||
}));
|
||
await parseDeliveryTemplateImage('data:image/jpeg;base64,AAAA', 'BR002');
|
||
const prompt = String(llmMocks.callWithImage.mock.calls[0]?.[0] || '');
|
||
expect(prompt).toContain('对齐一律照图片效果,不要擅自设置');
|
||
expect(prompt).toContain('拿不准的一律居中');
|
||
expect(prompt).toContain('text-align:left');
|
||
expect(prompt).toContain('text-align:center');
|
||
expect(prompt).toContain('版面几何对齐必须由图片判断');
|
||
expect(prompt).toContain('上下排列的标签和值,应保持图片里的起始位置');
|
||
expect(prompt).toContain('不能因某个值较长而挤偏相邻字段');
|
||
expect(prompt).toContain('抬头区域要做坐标复刻而不是语义重排');
|
||
expect(prompt).toContain('逐个估计图片中每个标签、字段值和页码相对于纸张的归一化 x/y 位置');
|
||
expect(prompt).toContain('不能用前一个字段的内容长度决定下一个字段的位置');
|
||
// 标题要能被压缩:不许 nowrap 锁死、要给可覆盖类名与 max-width
|
||
expect(prompt).toContain('white-space:nowrap');
|
||
expect(prompt).toContain('max-width:100%');
|
||
// 「自动缩小字号」2026-10-07 删除:代码里没有按宽度回缩字号的实现(只有 overflow-wrap 换行),
|
||
// 留着就是让模型照着一个不存在的机制设计版式。这里反向钉住,别再被写回来。
|
||
expect(prompt).not.toContain('自动缩小字号');
|
||
expect(prompt).toContain('让过长的标题**自动换行**');
|
||
});
|
||
|
||
/**
|
||
* 提示词加固(2026-10-07):按两份线上真实模板的实测缺陷逐条补条款。
|
||
*
|
||
* 起因与证据(渲染后统计):
|
||
* ① 绿洲「订单备注 / 收货日期」等页眉标签是 `<span><span class="label">…</span>…</span>`
|
||
* 行内嵌套 ⇒ transform 不生效、手动编辑里拖不动;
|
||
* ② 绿洲 tfoot `colspan="8"` + 1 格 = 9 列,表头只有 8 列 ⇒ 打印时合计行撑出错位;
|
||
* ③ 邮政「出库量」列绑了 `item.actual_amount` ⇒ 金额被印进数量列;
|
||
* ④ 邮政把参考图上的「页码: 1/2」抄成固定文字 ⇒ 每页都印 1/2。
|
||
*/
|
||
it('提示词要求文字项可块级化、逐行列数相等、列名与字段语义一致、禁止写死页码与越界 CSS 选择器', async () => {
|
||
llmMocks.callWithImage.mockResolvedValue(JSON.stringify({
|
||
name: 't', paper: { widthMm: 210, heightMm: 297, marginMm: 8 },
|
||
html: '<table><tbody>{{#items}}<tr><td>{{item.seq_no}}</td></tr>{{/items}}</tbody></table>',
|
||
css: 'td{text-align:center}',
|
||
}));
|
||
await parseDeliveryTemplateImage('data:image/jpeg;base64,AAAA', 'BR002');
|
||
const prompt = String(llmMocks.callWithImage.mock.calls[0]?.[0] || '');
|
||
// ① 文字项必须可块级化(否则行内元素既不能位移也不能调字号)
|
||
expect(prompt).toContain('每一段会被单独调整的文字');
|
||
expect(prompt).toContain('display:inline-block');
|
||
expect(prompt).toContain('严禁“行内里再嵌行内”');
|
||
expect(prompt).toContain('无法被单独移动位置、也无法被单独调字号');
|
||
// ② 逐行折算列数必须相等
|
||
expect(prompt).toContain('每一行的折算列数必须相等');
|
||
expect(prompt).toContain('colspan="表头列数 − 数值格数"');
|
||
expect(prompt).toContain('绝不能写成 colspan="8" + 1 格凑成 9 列');
|
||
// ③ 列名与字段语义一致,不许拿别语义字段顶替
|
||
expect(prompt).toContain('列标题必须和绑定字段的语义一致');
|
||
expect(prompt).toContain('严禁拿语义不同的字段顶替');
|
||
expect(prompt).toContain('把 *_amount 金额填进数量列属于严重错误');
|
||
// ④ 无页码字段 ⇒ 禁止写死页码
|
||
expect(prompt).toContain('没有页码、页数');
|
||
expect(prompt).toContain('严禁输出任何页码文字');
|
||
// ⑤ CSS 作用域:html/body/:root 会被丢弃、@page 与 paper 字段冲突
|
||
expect(prompt).toContain('不要写 html、body、:root 选择器');
|
||
expect(prompt).toContain('也不要写 @page');
|
||
});
|
||
|
||
/**
|
||
* 模板语法必须限定在渲染器支持的四种植式里(2026-10-06 事故)。
|
||
*
|
||
* 起因:模型想表达「规格有值才输出」,写成 mustache 条件段
|
||
* <td>{{item.item_name}}{{#item.spec}} {{item.spec}}{{/item.spec}}</td>
|
||
* 当时渲染器只认 {{#items}} / {{#categories}},于是这两个标记被原样印到单据上:
|
||
* 「五花肉{{#item.spec}}」。渲染器现已支持可选块并兜底抹掉未知标记,
|
||
* 但提示词仍须把可用的四种植式写死,别让模型再发明写法。
|
||
*/
|
||
it('提示词把可用语法限定为四种,并禁止其它 mustache 写法', async () => {
|
||
llmMocks.callWithImage.mockResolvedValue(JSON.stringify({
|
||
name: 't', paper: { widthMm: 210, heightMm: 297, marginMm: 8 },
|
||
html: '<table><tbody>{{#items}}<tr><td>{{item.seq_no}}</td></tr>{{/items}}</tbody></table>',
|
||
css: 'table{width:100%}',
|
||
}));
|
||
await parseDeliveryTemplateImage('data:image/jpeg;base64,AAAA', 'BR002');
|
||
const prompt = String(llmMocks.callWithImage.mock.calls[0]?.[0] || '');
|
||
expect(prompt).toContain('模板语法只允许下面四种');
|
||
expect(prompt).toContain('{{#字段路径}}…{{/字段路径}} 可选块');
|
||
expect(prompt).toContain('{{item.item_name}}{{#item.spec}} {{item.spec}}{{/item.spec}}');
|
||
expect(prompt).toContain('{{^字段}}');
|
||
expect(prompt).toContain('{{!注释}}');
|
||
// 标题与单号不得互相压盖(起因:单号绝对定位压到居中标题上)
|
||
expect(prompt).toContain('同一水平行里的元素不得互相压盖');
|
||
expect(prompt).toContain('不要用 position:absolute 把单号或页码叠到标题上');
|
||
});
|
||
|
||
/**
|
||
* 分类不能被抄成固定标题(2026-10-06 用户反馈)。
|
||
*
|
||
* 起因:上传的配送单里「蔬菜类」「肉禽水产」是**按分类分组**产生的小节标题,
|
||
* 模型把它当固定标题抄进模板 → 打印时那一段里其实是冻货/水产/水果混排,
|
||
* 小节标题写着「蔬菜类」,分类对不上。
|
||
* 本模板的明细是逐行渲染、不支持按分类分组,故必须把分类落到明细的「品类」列。
|
||
*/
|
||
it('提示词要求分类动态(摆进品类列绑定 item.category_name),不得写成固定标题', async () => {
|
||
llmMocks.callWithImage.mockResolvedValue(JSON.stringify({
|
||
name: 't', paper: { widthMm: 210, heightMm: 297, marginMm: 8 },
|
||
html: '<table><tbody>{{#items}}<tr><td>{{item.category_name}}</td></tr>{{/items}}</tbody></table>',
|
||
css: 'td{text-align:left}',
|
||
}));
|
||
await parseDeliveryTemplateImage('data:image/jpeg;base64,AAAA', 'BR002');
|
||
const prompt = String(llmMocks.callWithImage.mock.calls[0]?.[0] || '');
|
||
expect(prompt).toContain('分类必须动态,不能写成固定标题');
|
||
expect(prompt).toContain('{{item.category_name}}');
|
||
// 活数据 vs 版式文字的总原则
|
||
expect(prompt).toContain('固定文字只允许是版式文字');
|
||
expect(prompt).toContain('只是样例');
|
||
// 现在支持「按分类分组 + 组小计」,提示词里要写明语法与可用字段
|
||
expect(prompt).toContain('{{#categories}}');
|
||
expect(prompt).toContain('{{category.name}}');
|
||
expect(prompt).toContain('{{category.total_amount}}');
|
||
expect(prompt).toContain('分类分组字段');
|
||
// 分类小计不能漏(2026-10-06 追加):图上有就必须还原,且金额必须绑字段
|
||
expect(prompt).toContain('分类小计必须跟分类一起还原');
|
||
expect(prompt).toContain('金额小计:1841.27元');
|
||
expect(prompt).toContain('绝不能写死成参考图里的那个金额');
|
||
expect(prompt).toContain('一层都不能少'.replace('一层','一组'));
|
||
});
|
||
|
||
/**
|
||
* 分组模板必须能通过字段白名单校验(2026-10-06)。
|
||
* 参考图按分类分组时,模型会输出 {{#categories}} + {{category.*}};
|
||
* 只要 FIELD_PATHS 少一个,生成就会被判 invalid_bindings 直接失败。
|
||
*/
|
||
it('接受按分类分组的模板(category.* 字段在白名单内)', async () => {
|
||
llmMocks.callWithImage.mockResolvedValue(JSON.stringify({
|
||
name: '分组配送单',
|
||
paper: { widthMm: 210, heightMm: 297, marginMm: 8 },
|
||
html:
|
||
'<table><tbody>{{#categories}}' +
|
||
'<tr><td colspan="3">{{category.name}}</td></tr>' +
|
||
'{{#items}}<tr><td>{{item.seq_no}}</td><td>{{item.sku_name}}</td><td>{{item.amount}}</td></tr>{{/items}}' +
|
||
'<tr><td colspan="2">小计</td><td>{{category.total_amount}}</td></tr>' +
|
||
'{{/categories}}</tbody></table>' +
|
||
'<div>整单合计:{{group.total_amount}}</div>',
|
||
css: 'td{text-align:center}',
|
||
}));
|
||
const result = await parseDeliveryTemplateImage('data:image/jpeg;base64,AAAA', 'BR002');
|
||
expect(result.bindings).toContain('category.name');
|
||
expect(result.bindings).toContain('category.total_amount');
|
||
// 循环标记不算字段
|
||
expect(result.bindings).not.toContain('items');
|
||
expect(result.bindings).not.toContain('categories');
|
||
});
|
||
|
||
/**
|
||
* 日期拆分字段必须在白名单里,用法也要写进提示词(2026-10-07 用户反馈)。
|
||
*
|
||
* 起因:老式「送(销)货单」的日期是印成一格一格的 —— `__年__月__日`,数字写在 年/月/日 前面。
|
||
* AI 手上只有 `order.delivery_date` 一整个字符串(后端给的是 `YYYY-MM-DD`),模板语法又只允许四种、
|
||
* 没有过滤器,于是它只能把整串塞在中间,实测印成「年 2026-10-07 月日」。
|
||
* ⚠️ FIELD_PATHS 少一个,AI 生成的模板就会被 invalid_bindings 直接判失败。
|
||
*/
|
||
it('接受使用日期拆分字段的模板,并把「年月日要拆开」写进提示词', async () => {
|
||
llmMocks.callWithImage.mockResolvedValue(JSON.stringify({
|
||
name: '手写销货单',
|
||
paper: { widthMm: 210, heightMm: 297, marginMm: 8 },
|
||
html:
|
||
'<div>{{order.delivery_year}} 年 {{order.delivery_month}} 月 {{order.delivery_day}} 日</div>' +
|
||
'<div>{{order.delivery_year}}年{{order.delivery_month_pad}}月{{order.delivery_day_pad}}日</div>' +
|
||
'<div>{{order.delivery_date_cn}}</div>' +
|
||
'<table><tbody>{{#items}}<tr><td>{{item.seq_no}}</td></tr>{{/items}}</tbody></table>',
|
||
css: 'div{text-align:center}',
|
||
}));
|
||
const result = await parseDeliveryTemplateImage('data:image/jpeg;base64,AAAA', 'BR002');
|
||
for (const path of [
|
||
'order.delivery_year', 'order.delivery_month', 'order.delivery_day',
|
||
'order.delivery_month_pad', 'order.delivery_day_pad', 'order.delivery_date_cn',
|
||
]) {
|
||
expect(result.bindings).toContain(path);
|
||
}
|
||
const prompt = String(llmMocks.callWithImage.mock.calls[0]?.[0] || '');
|
||
expect(prompt).toContain('日期拆分字段');
|
||
expect(prompt).toContain('order.delivery_year(2026)');
|
||
expect(prompt).toContain('{{order.delivery_year}} 年 {{order.delivery_month}} 月 {{order.delivery_day}} 日');
|
||
expect(prompt).toContain('严禁把 order.delivery_date 这一个整串夹在“年”和“月日”之间');
|
||
});
|
||
|
||
it('提示词要求按图片还原签收等手写区域,不擅自新增线条', async () => {
|
||
llmMocks.callWithImage.mockResolvedValue(JSON.stringify({
|
||
name: 't', paper: { widthMm: 210, heightMm: 297, marginMm: 8 },
|
||
html: '<table><tbody>{{#items}}<tr><td>{{item.seq_no}}</td></tr>{{/items}}</tbody></table>',
|
||
css: 'td{text-align:center}',
|
||
}));
|
||
await parseDeliveryTemplateImage('data:image/jpeg;base64,AAAA', 'BR002');
|
||
const prompt = String(llmMocks.callWithImage.mock.calls[0]?.[0] || '');
|
||
expect(prompt).toContain('签字/手写区域必须按图片还原');
|
||
expect(prompt).toContain('签收人、收货人、客户签字');
|
||
expect(prompt).toContain('图片中有横线、边框或明显手写空白时才还原');
|
||
expect(prompt).toContain('严禁擅自新增下划线、边框、签字行');
|
||
expect(prompt).toContain('不要把签字位置绑定到配送员等普通业务字段');
|
||
});
|
||
|
||
it('微调时把当前模板和用户说明交给视觉模型', async () => {
|
||
llmMocks.callWithImage.mockResolvedValue(JSON.stringify({
|
||
name: '调整后模板',
|
||
paper: { widthMm: 210, heightMm: 297, marginMm: 8 },
|
||
html: '<div>{{order.order_code}}</div><table><tbody>{{#items}}<tr><td>{{item.seq_no}}</td></tr>{{/items}}</tbody></table>',
|
||
css: 'div{text-align:left}',
|
||
}));
|
||
await parseDeliveryTemplateImage('data:image/jpeg;base64,AAAA', 'BR002', {
|
||
current: {
|
||
name: '当前模板', html: '<div>{{order.order_code}}</div>', css: 'div{}',
|
||
paper: { widthMm: 210, heightMm: 297, marginMm: 8 }, bindings: ['order.order_code'],
|
||
},
|
||
instruction: '送货人和仓号左边对齐',
|
||
});
|
||
const prompt = String(llmMocks.callWithImage.mock.calls[0]?.[0] || '');
|
||
expect(prompt).toContain('当前模板');
|
||
expect(prompt).toContain('送货人和仓号左边对齐');
|
||
expect(prompt).toContain('已有模板的局部微调');
|
||
expect(prompt).toContain('用户本次明确提出的调整要求优先于通用提示词');
|
||
expect(prompt).toContain('这指元素位置对齐,不是只给各自容器设置 text-align:left');
|
||
expect(prompt).toContain('目标标签的可见文字起点实际重合');
|
||
});
|
||
|
||
it('没有参考图时使用文字模型按指令调整当前模板', async () => {
|
||
llmMocks.callWithText.mockResolvedValue(JSON.stringify({
|
||
name: '文字调整模板',
|
||
paper: { widthMm: 210, heightMm: 297, marginMm: 8 },
|
||
html: '<div>{{order.order_code}}</div><table><tbody>{{#items}}<tr><td>{{item.seq_no}}</td></tr>{{/items}}</tbody></table>',
|
||
css: 'div{text-align:left}',
|
||
}));
|
||
await parseDeliveryTemplateImage('', 'BR002', {
|
||
current: {
|
||
name: '当前模板', html: '<div>{{order.order_code}}</div>', css: 'div{}',
|
||
paper: { widthMm: 210, heightMm: 297, marginMm: 8 }, bindings: ['order.order_code'],
|
||
},
|
||
instruction: '签收人和配送时间左边缘对齐',
|
||
});
|
||
expect(llmMocks.callWithText).toHaveBeenCalledOnce();
|
||
expect(llmMocks.callWithImage).not.toHaveBeenCalled();
|
||
expect(String(llmMocks.callWithText.mock.calls[0]?.[1] || '')).toContain('签收人和配送时间左边缘对齐');
|
||
});
|
||
});
|