Files
SupplierSystem/server/tests/extraDishCostSource.test.ts
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

212 lines
7.3 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
const { query, release, connect } = vi.hoisted(() => {
const query = vi.fn();
const release = vi.fn();
const connect = vi.fn(async () => ({ query, release }));
return { query, release, connect };
});
vi.mock('../src/db', () => ({
pool: { connect, query },
}));
import {
EXTRA_DISH_COST_SOURCE,
reconcileExtraDishPurchaseCosts,
upsertOrderSkuPurchaseCostFromUnitCost,
} from '../src/services/orderCostPostingService';
describe('extra dish cost → 自采侧', () => {
beforeEach(() => {
query.mockReset();
release.mockReset();
connect.mockClear();
});
it('unit_cost_sync 汇总数量排除加菜行', async () => {
const client = {
query: vi
.fn()
// order meta
.mockResolvedValueOnce({
rowCount: 1,
rows: [{ cycle_code: '202608', branch_code: 'BR001' }],
})
// qty(应带 is_extra_dish = false 条件)
.mockResolvedValueOnce({ rowCount: 1, rows: [{ qty: 0 }] })
// existingAny
.mockResolvedValueOnce({ rowCount: 0, rows: [] })
// purchase_orders lookup
.mockResolvedValueOnce({ rowCount: 0, rows: [] })
// SAVEPOINT / INSERT path
.mockResolvedValue({ rowCount: 0, rows: [] }),
};
await upsertOrderSkuPurchaseCostFromUnitCost(client as any, 'ORD1', 'SKU1', 10);
const qtySql = String(client.query.mock.calls[1][0] || '');
expect(qtySql).toMatch(/is_extra_dish/);
expect(qtySql).toMatch(/false/);
});
it('按明细入账时 reference_id 与 order_item_id 分别转型,避免 $7 类型冲突', async () => {
const client = {
query: vi
.fn()
.mockResolvedValueOnce({
rowCount: 1,
rows: [{ cycle_code: '202608', branch_code: 'BR001' }],
})
.mockResolvedValueOnce({
rowCount: 1,
rows: [{ quantity: 2, is_extra_dish: false, is_returned: false, supplier_code: 'INTERNAL' }],
})
.mockResolvedValue({ rowCount: 0, rows: [] }),
};
await upsertOrderSkuPurchaseCostFromUnitCost(client as any, 'ORD1', 'SKU1', 10, {
orderItemId: '11111111-1111-1111-1111-111111111111',
});
const insertSql = String(
client.query.mock.calls.find(([sql]) => String(sql).includes('INSERT INTO cost_details'))?.[0] || ''
);
expect(insertSql).toMatch(/\$7::text/);
expect(insertSql).toMatch(/\$7::uuid/);
});
it('reconcile 写入 extra_dish 并清理误记的 unit_cost_sync 供应行', async () => {
const client = {
query: vi.fn(async (sql: string, params?: unknown[]) => {
const s = String(sql);
if (s.includes('FROM orders WHERE order_code')) {
return {
rowCount: 1,
rows: [{ cycle_code: '202608', branch_code: 'BR001', order_status: 90 }],
};
}
if (s.includes('FROM customer_quote_sheet')) {
return { rowCount: 0, rows: [] };
}
if (s.includes('FROM order_items oi') && s.includes('is_extra_dish')) {
return {
rowCount: 1,
rows: [
{
id: 'item-1',
sku_code: 'SKU_EXTRA',
quantity: 2,
purchase_code: null,
supplier_preparation_id: null,
},
],
};
}
if (s.includes('SELECT id FROM cost_details') && s.includes('reference_id')) {
return { rowCount: 0, rows: [] };
}
if (s.includes('SAVEPOINT')) return { rowCount: 0, rows: [] };
if (s.includes('RELEASE SAVEPOINT')) return { rowCount: 0, rows: [] };
if (s.startsWith('INSERT INTO cost_details')) {
expect(params?.[7] ?? params?.[params.length - 2]).toBeDefined();
// cost_source 参数应为 extra_dish
expect(params).toContain(EXTRA_DISH_COST_SOURCE);
return { rowCount: 1, rows: [] };
}
if (s.includes('DELETE FROM cost_details') && s.includes('cost_source = $2')) {
return { rowCount: 0, rows: [] };
}
if (s.includes('DELETE FROM cost_details cd') && s.includes('unit_cost_sync')) {
return { rowCount: 1, rows: [] };
}
return { rowCount: 0, rows: [] };
}),
};
const result = await reconcileExtraDishPurchaseCosts(client as any, 'ORD1', {
costBySku: { SKU_EXTRA: 5 },
});
expect(result.posted).toBe(1);
expect(result.amount).toBe(10);
const sqls = client.query.mock.calls.map((c) => String(c[0]));
expect(sqls.some((s) => s.includes('unit_cost_sync'))).toBe(true);
expect(sqls.some((s) => s.includes("cost_source = 'supplier'"))).toBe(true);
});
it('配送中(60)可写入 extra_dish;待配送(50)/待验收(40)不入账', async () => {
const client40 = {
query: vi.fn(async (sql: string) => {
if (String(sql).includes('FROM orders WHERE order_code')) {
return { rowCount: 1, rows: [{ cycle_code: '202608', branch_code: 'BR001', order_status: 40 }] };
}
return { rowCount: 0, rows: [] };
}),
};
const skipped40 = await reconcileExtraDishPurchaseCosts(client40 as any, 'ORD1', {
costBySku: { SKU_EXTRA: 5 },
});
expect(skipped40.posted).toBe(0);
expect(client40.query.mock.calls.length).toBe(1);
const client50 = {
query: vi.fn(async (sql: string) => {
if (String(sql).includes('FROM orders WHERE order_code')) {
return { rowCount: 1, rows: [{ cycle_code: '202608', branch_code: 'BR001', order_status: 50 }] };
}
return { rowCount: 0, rows: [] };
}),
};
const skipped50 = await reconcileExtraDishPurchaseCosts(client50 as any, 'ORD1', {
costBySku: { SKU_EXTRA: 5 },
});
expect(skipped50.posted).toBe(0);
expect(client50.query.mock.calls.length).toBe(1);
const client60 = {
query: vi.fn(async (sql: string, params?: unknown[]) => {
const s = String(sql);
if (s.includes('FROM orders WHERE order_code')) {
return {
rowCount: 1,
rows: [{ cycle_code: '202608', branch_code: 'BR001', order_status: 60 }],
};
}
if (s.includes('FROM customer_quote_sheet')) return { rowCount: 0, rows: [] };
if (s.includes('FROM order_items oi') && s.includes('is_extra_dish')) {
return {
rowCount: 1,
rows: [
{
id: 'item-1',
sku_code: 'SKU_EXTRA',
quantity: 2,
purchase_code: null,
supplier_preparation_id: null,
},
],
};
}
if (s.includes('SELECT id FROM cost_details') && s.includes('reference_id')) {
return { rowCount: 0, rows: [] };
}
if (s.includes('SAVEPOINT') || s.includes('RELEASE SAVEPOINT')) {
return { rowCount: 0, rows: [] };
}
if (s.startsWith('INSERT INTO cost_details')) {
expect(params).toContain(EXTRA_DISH_COST_SOURCE);
return { rowCount: 1, rows: [] };
}
return { rowCount: 0, rows: [] };
}),
};
const posted = await reconcileExtraDishPurchaseCosts(client60 as any, 'ORD1', {
costBySku: { SKU_EXTRA: 5 },
});
expect(posted.posted).toBe(1);
expect(posted.amount).toBe(10);
});
});