Files
SupplierSystem/server/tests/orderProductCatalogService.test.ts
T
Ubuntu deb3e72750
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
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库: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 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +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']);
});
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();
});
});