来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
126 lines
4.4 KiB
TypeScript
126 lines
4.4 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
const { resolveInboundCostUnitPrice, applyBidCostAfterInbound } = vi.hoisted(() => ({
|
|
resolveInboundCostUnitPrice: vi.fn(),
|
|
applyBidCostAfterInbound: vi.fn(),
|
|
}));
|
|
|
|
vi.mock('../src/db', () => ({
|
|
pool: { connect: vi.fn(), query: vi.fn() },
|
|
getRawPoolQuery: () => vi.fn(),
|
|
}));
|
|
|
|
vi.mock('../src/services/bidAwardService', () => ({
|
|
resolveInboundCostUnitPrice,
|
|
applyBidCostAfterInbound,
|
|
}));
|
|
|
|
import { writeInspectionInboundCostWithBidSafe } from '../src/services/inspectionComplianceService';
|
|
|
|
describe('writeInspectionInboundCostWithBidSafe', () => {
|
|
beforeEach(() => {
|
|
resolveInboundCostUnitPrice.mockReset();
|
|
applyBidCostAfterInbound.mockReset();
|
|
});
|
|
|
|
it('竞价步骤失败时 ROLLBACK SAVEPOINT 后降级写成本,外层事务可继续', async () => {
|
|
const released: string[] = [];
|
|
const rolledBack: string[] = [];
|
|
const sqlLog: string[] = [];
|
|
|
|
const client = {
|
|
query: vi.fn(async (sql: string) => {
|
|
const s = String(sql);
|
|
sqlLog.push(s);
|
|
if (s.startsWith('SAVEPOINT ')) return { rowCount: 0, rows: [] };
|
|
if (s.startsWith('RELEASE SAVEPOINT ')) {
|
|
released.push(s.replace('RELEASE SAVEPOINT ', '').trim());
|
|
return { rowCount: 0, rows: [] };
|
|
}
|
|
if (s.startsWith('ROLLBACK TO SAVEPOINT ')) {
|
|
rolledBack.push(s.replace('ROLLBACK TO SAVEPOINT ', '').trim());
|
|
return { rowCount: 0, rows: [] };
|
|
}
|
|
// writeInspectionCost:幂等检查
|
|
if (s.includes('reference_type = \'inspection\'') || s.includes("reference_type = 'inspection'")) {
|
|
return { rowCount: 0, rows: [] };
|
|
}
|
|
// reconcile 依赖的查询:返回空即可让路径走完或早退
|
|
if (s.includes('FROM orders') || s.includes('FROM inspection') || s.includes('cost_details')) {
|
|
return { rowCount: 0, rows: [] };
|
|
}
|
|
return { rowCount: 0, rows: [] };
|
|
}),
|
|
};
|
|
|
|
resolveInboundCostUnitPrice.mockResolvedValue(8.5);
|
|
applyBidCostAfterInbound.mockRejectedValue(
|
|
Object.assign(new Error('bid sync failed'), { code: '23505', status: 409 })
|
|
);
|
|
|
|
await writeInspectionInboundCostWithBidSafe(client as any, {
|
|
orderCode: 'ORD-BID-1',
|
|
skuCode: 'SKU1',
|
|
supplierCode: 'SUP1',
|
|
actualQty: 10,
|
|
purchasePrice: 7,
|
|
branchCode: 'BR001',
|
|
referenceId: 'insp-1',
|
|
operatorId: null,
|
|
});
|
|
|
|
expect(rolledBack).toContain('insp_bid_cost');
|
|
expect(released).toContain('insp_bid_cost_fallback');
|
|
expect(sqlLog.some((s) => s.includes('25P02'))).toBe(false);
|
|
// 降级后不应再抛,且曾进入竞价路径
|
|
expect(resolveInboundCostUnitPrice).toHaveBeenCalled();
|
|
expect(applyBidCostAfterInbound).toHaveBeenCalled();
|
|
});
|
|
|
|
it('降级仍失败时抛出原始竞价错误而非 25P02', async () => {
|
|
const primary = Object.assign(new Error('unique conflict on bid cost'), {
|
|
code: '23505',
|
|
status: 409,
|
|
});
|
|
|
|
let bidPathDone = false;
|
|
const client = {
|
|
query: vi.fn(async (sql: string) => {
|
|
const s = String(sql);
|
|
if (s.startsWith('SAVEPOINT ') || s.startsWith('RELEASE ') || s.startsWith('ROLLBACK ')) {
|
|
return { rowCount: 0, rows: [] };
|
|
}
|
|
if (s.includes("reference_type = 'inspection'") || s.includes('reference_type = \'inspection\'')) {
|
|
if (bidPathDone) {
|
|
throw Object.assign(new Error('current transaction is aborted'), { code: '25P02' });
|
|
}
|
|
return { rowCount: 0, rows: [] };
|
|
}
|
|
if (s.includes('FROM orders') || s.includes('cost_details') || s.includes('FROM inspection')) {
|
|
return { rowCount: 0, rows: [] };
|
|
}
|
|
return { rowCount: 0, rows: [] };
|
|
}),
|
|
};
|
|
|
|
resolveInboundCostUnitPrice.mockResolvedValue(9);
|
|
applyBidCostAfterInbound.mockImplementation(async () => {
|
|
bidPathDone = true;
|
|
throw primary;
|
|
});
|
|
|
|
// 强制降级路径的 writeInspectionCost 也失败:在 fallback 里对 reference 检查抛错
|
|
// 上面已在 bidPathDone 后抛 25P02;此处验证抛出的是 primary
|
|
await expect(
|
|
writeInspectionInboundCostWithBidSafe(client as any, {
|
|
orderCode: 'ORD-BID-2',
|
|
skuCode: 'SKU2',
|
|
supplierCode: 'SUP2',
|
|
actualQty: 2,
|
|
purchasePrice: 5,
|
|
referenceId: 'insp-2',
|
|
})
|
|
).rejects.toMatchObject({ code: '23505', message: 'unique conflict on bid cost' });
|
|
});
|
|
});
|