来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
167 lines
5.6 KiB
TypeScript
167 lines
5.6 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest';
|
|
import {
|
|
assertOrderItemsFulfilled,
|
|
dropStalePendingPreparationsForSku,
|
|
isOrderItemFulfillmentValid,
|
|
linkOrderItemToPreparation,
|
|
linkOrderItemToPurchase,
|
|
shouldDeletePendingPrepOnReconcile,
|
|
shouldDropStalePrepOnSupplierSwitch,
|
|
} from '../src/services/orderItemFulfillmentService';
|
|
|
|
describe('orderItemFulfillmentService', () => {
|
|
it('links an order item to one aggregated supplier preparation', async () => {
|
|
const query = vi.fn().mockResolvedValue({ rowCount: 1, rows: [] });
|
|
await linkOrderItemToPreparation(
|
|
{ query } as any,
|
|
{
|
|
orderItemId: '11111111-1111-1111-1111-111111111111',
|
|
orderCode: 'ORD1',
|
|
preparationId: '22222222-2222-2222-2222-222222222222',
|
|
}
|
|
);
|
|
expect(query).toHaveBeenCalledOnce();
|
|
expect(query.mock.calls[0][1]).toEqual([
|
|
'11111111-1111-1111-1111-111111111111',
|
|
'ORD1',
|
|
'22222222-2222-2222-2222-222222222222',
|
|
0,
|
|
]);
|
|
expect(query.mock.calls[0][0]).toContain('purchase_code = NULL');
|
|
});
|
|
|
|
it('switches an order item from preparation to purchase', async () => {
|
|
const query = vi.fn().mockResolvedValue({ rowCount: 1, rows: [] });
|
|
await linkOrderItemToPurchase(
|
|
{ query } as any,
|
|
{
|
|
orderItemId: '11111111-1111-1111-1111-111111111111',
|
|
orderCode: 'ORD1',
|
|
purchaseCode: 'PUR1',
|
|
}
|
|
);
|
|
expect(query.mock.calls[0][0]).toContain('supplier_preparation_id = NULL');
|
|
expect(query.mock.calls[0][1]).toEqual([
|
|
'11111111-1111-1111-1111-111111111111',
|
|
'ORD1',
|
|
'PUR1',
|
|
0,
|
|
]);
|
|
});
|
|
|
|
it('only accepts a live and matching source', async () => {
|
|
const query = vi.fn().mockResolvedValue({ rowCount: 1, rows: [{ ok: 1 }] });
|
|
await expect(
|
|
isOrderItemFulfillmentValid(
|
|
{ query } as any,
|
|
'11111111-1111-1111-1111-111111111111'
|
|
)
|
|
).resolves.toBe(true);
|
|
const sql = query.mock.calls[0][0] as string;
|
|
expect(sql).toContain('sp.sku_code IS NOT DISTINCT FROM oi.sku_code');
|
|
expect(sql).toContain('po.purchase_status <> 40');
|
|
// 同日合单允许采购单挂在主单,不再要求 po.order_code = oi.order_code
|
|
expect(sql).not.toMatch(/po\.order_code\s*=\s*oi\.order_code/);
|
|
});
|
|
|
|
it('drops pending prep for old supplier after switch, keeps sibling and inbound completed', async () => {
|
|
expect(
|
|
shouldDeletePendingPrepOnReconcile({ prepareStatus: 10, hasMatchingActiveOrderItem: false })
|
|
).toBe(true);
|
|
expect(
|
|
shouldDeletePendingPrepOnReconcile({ prepareStatus: 10, hasMatchingActiveOrderItem: true })
|
|
).toBe(false);
|
|
expect(
|
|
shouldDeletePendingPrepOnReconcile({ prepareStatus: 30, hasMatchingActiveOrderItem: false })
|
|
).toBe(false);
|
|
|
|
expect(
|
|
shouldDropStalePrepOnSupplierSwitch({
|
|
prepareStatus: 30,
|
|
hasMatchingActiveOrderItem: false,
|
|
inboundForThisSupplier: false,
|
|
})
|
|
).toBe(true);
|
|
expect(
|
|
shouldDropStalePrepOnSupplierSwitch({
|
|
prepareStatus: 30,
|
|
hasMatchingActiveOrderItem: false,
|
|
inboundForThisSupplier: true,
|
|
})
|
|
).toBe(false);
|
|
|
|
const query = vi.fn(async (sql: string) => {
|
|
const key = String(sql).replace(/\s+/g, ' ');
|
|
if (key.includes('AND sp.supplier_code IS DISTINCT FROM $3')) {
|
|
return {
|
|
rows: [
|
|
{
|
|
id: 'aaaaaaaa-1111-1111-1111-111111111111',
|
|
sku_code: 'SKU_GARLIC',
|
|
supplier_code: 'OLD_SUP',
|
|
prepare_status: 30,
|
|
inbound_for_supplier: false,
|
|
},
|
|
],
|
|
};
|
|
}
|
|
return { rows: [], rowCount: 1 };
|
|
});
|
|
const deleted = await dropStalePendingPreparationsForSku(
|
|
{ query } as any,
|
|
{ orderCode: 'ORD1', skuCode: 'SKU_GARLIC', keepSupplierCode: 'NEW_SUP' }
|
|
);
|
|
expect(deleted).toBe(1);
|
|
const sqls = query.mock.calls.map((c) => String(c[0]));
|
|
expect(sqls.some((s) => s.includes('DELETE FROM supplier_preparations'))).toBe(true);
|
|
expect(sqls.some((s) => s.includes('inbound_for_supplier') || s.includes('inbound_batch_no'))).toBe(true);
|
|
expect(
|
|
sqls.some(
|
|
(s) =>
|
|
s.includes('DELETE FROM inspections i') &&
|
|
s.includes('order_items oi') &&
|
|
s.includes('inbound_batch_no')
|
|
)
|
|
).toBe(true);
|
|
|
|
const inboundKeepQuery = vi.fn(async (sql: string) => {
|
|
const key = String(sql).replace(/\s+/g, ' ');
|
|
if (key.includes('AND sp.supplier_code IS DISTINCT FROM $3')) {
|
|
return {
|
|
rows: [
|
|
{
|
|
id: 'bbbbbbbb-1111-1111-1111-111111111111',
|
|
sku_code: 'SKU_GARLIC',
|
|
supplier_code: 'OLD_SUP',
|
|
prepare_status: 30,
|
|
inbound_for_supplier: true,
|
|
},
|
|
],
|
|
};
|
|
}
|
|
return { rows: [], rowCount: 1 };
|
|
});
|
|
const inboundKept = await dropStalePendingPreparationsForSku(
|
|
{ query: inboundKeepQuery } as any,
|
|
{ orderCode: 'ORD1', skuCode: 'SKU_GARLIC', keepSupplierCode: 'NEW_SUP' }
|
|
);
|
|
expect(inboundKept).toBe(0);
|
|
expect(
|
|
inboundKeepQuery.mock.calls.some((c) => String(c[0]).includes('DELETE FROM supplier_preparations'))
|
|
).toBe(false);
|
|
});
|
|
|
|
it('rejects transaction commit when any item is uncovered', async () => {
|
|
const query = vi.fn().mockResolvedValue({
|
|
rowCount: 1,
|
|
rows: [{ id: 'item-1', item_name: '土豆', sku_code: 'SKU1' }],
|
|
});
|
|
await expect(
|
|
assertOrderItemsFulfilled({ query } as any, 'ORD1')
|
|
).rejects.toMatchObject({
|
|
code: 'order_item_fulfillment_incomplete',
|
|
status: 409,
|
|
});
|
|
});
|
|
});
|