Files
SupplierSystem/server/tests/orderProductCatalogService.test.ts
T
Ubuntu 5fa2bbfd55
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(audit-20261019): T1-T9 审计修复、存储归属与审计日志列宽
- T1 资金/库存/报价/自采 P1(BIZ-1/2/3/4/10/13/14)
- T2 主系统反向事件墓碑/改派/孤儿处理(BIZ-6/8/9)
- T3 选分公司 ticket、trust proxy、角色权限收口(SEC-2/3/4)
- T4 缺列/RLS/tenant 名单/FK/迁移执行器(DATA-1..8)
- T5 401 登出/分页/映射口径/blob/ZIP 中文名(FE-1..4/BIZ-15)
- T6 测试脚本目标守卫/config fail-fast/容器配置(OPS-2..8)
- T7 前后端测试与 lint 门禁清零
- T8 应付卡片按关联订单日(口径 A)
- T9 存储对象全链路归属校验、敏感列禁写、审计 target_id 超长修复
2026-10-19 16:16:51 +08:00

204 lines
6.5 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest';
import type { PoolClient } from 'pg';
import { ensureOrderProductsInCatalog } from '../src/services/orderProductCatalogService';
function mockClient(options?: {
catalogRows?: Array<Record<string, unknown>>;
globalByCode?: Record<string, Record<string, unknown>>;
}) {
const catalogRows = options?.catalogRows || [];
const globalByCode = options?.globalByCode || {};
const query = vi.fn(async (sql: string, params?: unknown[]) => {
const text = String(sql);
if (text.includes('pg_advisory_xact_lock')) {
return { rows: [], rowCount: 0 };
}
if (text.includes('SELECT 1 FROM product_catalog') && text.includes('AND code = $1')) {
const code = String(params?.[0] || '');
const hit = globalByCode[code];
return hit ? { rows: [hit], rowCount: 1 } : { rows: [], rowCount: 0 };
}
if (
text.includes('SELECT code, name, parent_code, branch_code') &&
text.includes('AND code = $1')
) {
const code = String(params?.[0] || '');
const hit = globalByCode[code];
return hit ? { rows: [hit], rowCount: 1 } : { rows: [], rowCount: 0 };
}
if (text.includes('FROM product_catalog') && text.includes('branch_code = $1')) {
return { rows: catalogRows, rowCount: catalogRows.length };
}
if (text.includes('INSERT INTO product_catalog')) {
return { rows: [], rowCount: 1 };
}
return { rows: [], rowCount: 1 };
});
return { query } as unknown as PoolClient & { query: typeof query };
}
describe('ensureOrderProductsInCatalog', () => {
it('locks by branch and soft name keys before strict branch lookup', async () => {
const client = mockClient({
catalogRows: [{ code: 'SKU001', name: '白菜', parent_code: 'CAT001' }],
});
const result = await ensureOrderProductsInCatalog(
client,
[{ key: 'line-1', name: ' 白菜 ', categoryCode: 'CAT999', unit: '斤', price: 3 }],
'BR002'
);
expect(result.ensured[0]).toMatchObject({
skuCode: 'SKU001',
isNewProduct: false,
});
const lockCalls = client.query.mock.calls.filter(([sql]) =>
String(sql).includes('pg_advisory_xact_lock')
);
expect(lockCalls.map(([, params]) => params?.[0]).sort()).toEqual([
'product_catalog:BR002:白菜',
'product_catalog:BR002:鲜白菜',
]);
const catalogCall = client.query.mock.calls.find(([sql]) =>
String(sql).includes('AND branch_code = $1')
);
expect(catalogCall?.[0]).toContain('AND branch_code = $1');
expect(catalogCall?.[0]).not.toContain('branch_code IS NULL');
});
it('reuses existing SKU when names differ only by punctuation or 鲜 prefix', async () => {
const client = mockClient({
catalogRows: [
{ code: 'SKU_MU', name: '木耳', parent_code: 'CAT001' },
{ code: 'SKU_YU', name: '草鱼杀好', parent_code: 'CAT001' },
],
});
const result = await ensureOrderProductsInCatalog(
client,
[
{ key: 'a', name: '鲜木耳', categoryCode: 'CAT001', unit: '斤', price: 1 },
{ key: 'b', name: '草鱼(杀好)', categoryCode: 'CAT001', unit: '斤', price: 2 },
],
'BR002'
);
expect(result.ensured[0]).toMatchObject({ skuCode: 'SKU_MU', isNewProduct: false });
expect(result.ensured[1]).toMatchObject({ skuCode: 'SKU_YU', isNewProduct: false });
expect(result.newProducts).toHaveLength(0);
});
it('writes new product without catalog prices (branch in same transaction)', async () => {
// 口径:docs/design/20260731-remove-catalog-prices.md — product_catalog 不再落 sale/cost
const client = mockClient();
const result = await ensureOrderProductsInCatalog(
client,
[
{
key: 'line-1',
name: '新商品',
categoryCode: 'CAT001',
unit: '件',
price: 8.5,
costPrice: 4.2,
},
],
'BR003'
);
expect(result.newProducts).toHaveLength(1);
const insertCall = client.query.mock.calls.find(([sql]) =>
String(sql).includes('INSERT INTO product_catalog')
);
expect(String(insertCall?.[0])).not.toMatch(/sale_price|cost_price/);
expect(insertCall?.[1]?.slice(1)).toEqual(['新商品', 'CAT001', '件', 'BR003', null]);
});
it('rejects writes without an effective branch', async () => {
const client = mockClient();
await expect(
ensureOrderProductsInCatalog(
client,
[{ key: 'line-1', name: '新商品', categoryCode: 'CAT001' }],
null
)
).rejects.toMatchObject({ code: 'operating_branch_required' });
});
it('mints a new code when input skuCode belongs to another branch', async () => {
const client = mockClient({
globalByCode: {
SKU_OTHER: {
code: 'SKU_OTHER',
name: '他司商品',
parent_code: 'CAT001',
branch_code: 'BR001',
},
},
});
const result = await ensureOrderProductsInCatalog(
client,
[
{
key: 'line-1',
name: '本司新菜',
skuCode: 'SKU_OTHER',
categoryCode: 'CAT001',
unit: '斤',
price: 2,
},
],
'BR002'
);
expect(result.ensured[0].isNewProduct).toBe(true);
expect(result.ensured[0].skuCode).not.toBe('SKU_OTHER');
const insertCall = client.query.mock.calls.find(([sql]) =>
String(sql).includes('INSERT INTO product_catalog')
);
expect(insertCall?.[1]?.[0]).not.toBe('SKU_OTHER');
// INSERT params: code, name, parent_code, unit, branch_code
expect(insertCall?.[1]?.[4]).toBe('BR002');
});
it('binds existing same-branch product found by global code lookup', async () => {
const client = mockClient({
catalogRows: [],
globalByCode: {
SKU_LOCAL: {
code: 'SKU_LOCAL',
name: '本地商品',
parent_code: 'CAT001',
branch_code: 'BR002',
},
},
});
const result = await ensureOrderProductsInCatalog(
client,
[
{
key: 'line-1',
name: '别名',
skuCode: 'SKU_LOCAL',
categoryCode: 'CAT001',
},
],
'BR002'
);
expect(result.ensured[0]).toMatchObject({
skuCode: 'SKU_LOCAL',
isNewProduct: false,
});
expect(result.newProducts).toHaveLength(0);
const insertCall = client.query.mock.calls.find(([sql]) =>
String(sql).includes('INSERT INTO product_catalog')
);
expect(insertCall).toBeUndefined();
});
});