Files
SupplierSystem/server/tests/orderItemsSpecPersistence.test.ts
T
Ubuntu 2d9fbf4a9f
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
feat(订单详情规格): 规格可编辑 + 切换商品时跟随新 SKU(保存只在载荷带 spec 时写)
开发者三点意见的前两点:「我切换商品时会不会切换规格 订单详情 当前切换时好像规格不会动」
「这边 规格是可以在订单详情这块修改的」。

① 详情「规格」列由只读改为**可编辑**(可编辑行给输入框,60 字上限,写回
   `onUpdateItem(id,{spec})`;只读行仍只展示)。列数不变(同一格换控件)⇒ colCount 与合计行
   colSpan 无需再动。
② 切换商品时**规格跟随新 SKU**(`applyEditItemSku`):`spec: skuSpec || item.spec || null`
   —— 新 SKU **有**规格才覆盖,主数据没维护(空)则**保留原值**:否则录单从客户单据读到的规格
   会被"切换一次就切没了",而当时没有可依据的新值(不造)。
③ 服务端 `orderManageItemsService` 保存规格,但**只在载荷真的带该字段时**写:
   `hasSpec = hasOwnProperty(row,'spec')`;UPDATE 走 `spec = CASE WHEN $20::boolean THEN $19 ELSE spec END`;
   新行 INSERT 落 `$21`。原因:改明细有 5 个入口(Web / 小程序×3 / MCP),别的入口不带 spec,
   无条件写会把已存规格抹成 NULL。`OrderManageItemInput` 补 `spec`。

证据:前端 OrderParsedItemRow.spec.contract.test.ts **8 通过**(新增 ⑧:输入框/上限/写回/只读展示 +
切换跟随的 `skuSpec || item.spec || null`);详情既有 orderDetailSpecColumn.contract.test.ts 6 通过;
后端 orderItemsSpecPersistence 4 通过(④ 改为钉"只在带字段时写")+ orderParseSpec 7 +
orderSpecDisambiguation 6 共 17 通过;`tsc` 前端 exit 0(后端仅剩既有 defaults.ts variablesSchema 缺失)。
设计文档加 §9/§10(v3)。

**未做(需单独立项)**:新增商品弹窗加「规格」字段 —— 它要写商品主数据(`product_catalog.spec` 与
`skus.spec` 两处,否则"建了就丢"),属商品主数据冻结面,先确认写哪张表再动。
2026-10-16 17:03:36 +08:00

66 lines
3.8 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.
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\s*\)/);
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\s*\)/);
expect(itemsService).toContain('$19::uuid[], $5, $21)');
// 60 字上限与录单一致
expect(itemsService).toContain('trimText(row.spec).slice(0, 60)');
});
});