66 lines
3.8 KiB
TypeScript
66 lines
3.8 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
||
import { describe, expect, it } from 'vitest';
|
||
|
||
/**
|
||
* 录单规格**落库链路**(2026-10-16 真机报障:「录单结束后规格还是没有存储下来?订单详情里面的规格为空」)。
|
||
*
|
||
* 实测断了两处,本文件把整条链路钉死,任一处再漏都会红:
|
||
* ① 前端提交载荷**没带 spec** ⇒ 后端写入分支永远拿到 undefined(本次补上);
|
||
* ② 迁移**没跑** ⇒ `order_items.spec` 列不存在(本地库实测为空,跑完 `npm run db:migrate` 后三 schema 均有)。
|
||
* ③ 中间层不得白名单式丢字段(`enrichOrderItemsWithPrices` / `applyEnsuredSkuCodes` 都是 `...item` 展开)。
|
||
* ④ 改明细/加菜路径**不得碰 spec**(否则保存一次就把已存规格抹掉)。
|
||
*/
|
||
const read = (rel: string) => readFileSync(new URL(rel, import.meta.url), 'utf8');
|
||
|
||
const submitService = read('../src/services/customerOrderSubmitService.ts');
|
||
const itemsService = read('../src/services/orderManageItemsService.ts');
|
||
const pricingService = read('../src/services/orderPricingService.ts');
|
||
const catalogService = read('../src/services/orderProductCatalogService.ts');
|
||
const migration = read('../../migrations/20261050_order_items_spec.sql');
|
||
|
||
describe('录单规格 · 落库链路', () => {
|
||
it('① 建单 INSERT 落 spec(列名 + 占位符 + 取值兜底 spec/specification)', () => {
|
||
expect(submitService).toContain("String(item.spec ?? item.specification ?? '').trim().slice(0, 60) || null");
|
||
const insert = submitService.slice(
|
||
submitService.indexOf('INSERT INTO order_items ('),
|
||
submitService.indexOf('RETURNING id::text AS id')
|
||
);
|
||
expect(insert, '列清单里必须有 spec').toMatch(/process_unit_price,\s*spec,/);
|
||
expect(insert, '参数占位符要跟着加').toContain('$21');
|
||
});
|
||
|
||
it('② 迁移存在:各 schema 加 VARCHAR(60) 且带响亮失败守卫', () => {
|
||
expect(migration).toContain('ADD COLUMN IF NOT EXISTS spec VARCHAR(60)');
|
||
expect(migration).toContain("nspname IN ('public', 'tenant_template')");
|
||
expect(migration).toContain('RAISE EXCEPTION');
|
||
// 不新增表 ⇒ 不触「新业务表须写进 tenant_business_tables()」守卫(注释里提到名字没关系,
|
||
// 所以要断言"没有重定义该函数",而不是断言文本里不出现这个名字)
|
||
expect(migration).not.toContain('CREATE OR REPLACE FUNCTION public.tenant_business_tables');
|
||
});
|
||
|
||
it('③ 中间层不得丢字段:价格富化与 SKU 补码都是 `...item` 展开', () => {
|
||
const pricing = pricingService.slice(
|
||
pricingService.indexOf('export async function enrichOrderItemsWithPrices'),
|
||
pricingService.indexOf('export async function enrichOrderItemsWithPrices') + 6000
|
||
);
|
||
expect(pricing).toContain('...item');
|
||
const ensured = catalogService.slice(
|
||
catalogService.indexOf('export function applyEnsuredSkuCodes'),
|
||
catalogService.indexOf('export function applyEnsuredSkuCodes') + 1200
|
||
);
|
||
expect(ensured).toContain('...item');
|
||
});
|
||
|
||
it('④ 改明细/加菜路径:规格**只在载荷带该字段时**才写(别的入口保存不得抹掉已存规格)', () => {
|
||
// 2026-10-16 追加:详情里规格可编辑 ⇒ 保存要写 spec;但入口有 5 个(Web/小程序×3/MCP),
|
||
// 不带 spec 的入口保存一次不能把已存规格抹成 NULL,故走 hasSpec 标志位。
|
||
expect(itemsService).toContain("Object.prototype.hasOwnProperty.call(row, 'spec')");
|
||
expect(itemsService).toContain('spec = CASE WHEN $20::boolean THEN $19 ELSE spec END');
|
||
// 新行直接落 spec(第 21 个参数)
|
||
expect(itemsService).toMatch(/ordered_quantity, spec,/);
|
||
expect(itemsService).toMatch(/\$19::uuid\[\], \$5, \$21,/);
|
||
// 60 字上限与录单一致
|
||
expect(itemsService).toContain('trimText(row.spec).slice(0, 60)');
|
||
});
|
||
});
|