来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
145 lines
5.8 KiB
TypeScript
145 lines
5.8 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
|
import { describe, expect, it } from 'vitest';
|
|
import {
|
|
backfillMissingPrepPurchaseCosts,
|
|
listMissingPrepPurchaseCostGaps,
|
|
} from '../src/services/missingPurchaseCostBackfill';
|
|
|
|
/**
|
|
* 备货完成漏写采购账本:补账与防复发契约。
|
|
* 见 docs/design/20260911-backfill-missing-prep-purchase-cost.md
|
|
*/
|
|
|
|
const CANDIDATE = {
|
|
order_item_id: '11111111-1111-1111-1111-111111111111',
|
|
order_code: 'ORD_WEB_20260909_BR002_0004',
|
|
sku_code: 'SKU86683340417',
|
|
item_name: '蛋鸡',
|
|
unit: '斤',
|
|
quantity: 47,
|
|
purchase_price: 12.5,
|
|
prepare_qty: 50,
|
|
amount: 587.5,
|
|
cycle_code: '202609',
|
|
branch_code: 'BR002',
|
|
prepare_time: '2026-09-09 20:40:50+08',
|
|
};
|
|
|
|
function fakeDb(options: { candidates: unknown[]; inserted?: number }) {
|
|
const calls: string[] = [];
|
|
return {
|
|
calls,
|
|
query: async (sql: string, params?: unknown[]) => {
|
|
calls.push(String(sql));
|
|
if (String(sql).includes('INSERT INTO cost_details')) {
|
|
const rows = (options.inserted ?? 1) > 0 ? [{ id: 'cost-1' }] : [];
|
|
return { rowCount: rows.length, rows } as never;
|
|
}
|
|
return { rowCount: options.candidates.length, rows: options.candidates } as never;
|
|
},
|
|
};
|
|
}
|
|
|
|
describe('补账:备货完成但采购账本缺行', () => {
|
|
it('候选查询排除已有采购行与已验收的行,并限定备货已完成/有价/非自采', async () => {
|
|
const db = fakeDb({ candidates: [] });
|
|
await listMissingPrepPurchaseCostGaps(db as never, {});
|
|
const sql = db.calls[0];
|
|
expect(sql).toContain("cd.cost_type IN ('purchase', 'purchase_applied')");
|
|
expect(sql).toContain('FROM inspections i');
|
|
expect(sql).toContain('COALESCE(sp.prepare_status, 10) >= 30');
|
|
expect(sql).toContain('COALESCE(sp.purchase_price, 0) > 0');
|
|
expect(sql).toContain("NOT IN ('INTERNAL', 'UNASSIGNED')");
|
|
expect(sql).toContain('COALESCE(oi.is_extra_dish, false) = false');
|
|
});
|
|
|
|
it('dry-run 只统计、不写库', async () => {
|
|
const db = fakeDb({ candidates: [CANDIDATE] });
|
|
const result = await backfillMissingPrepPurchaseCosts(db as never, { apply: false });
|
|
expect(result.candidates).toBe(1);
|
|
expect(result.inserted).toBe(0);
|
|
expect(result.amount).toBe(587.5);
|
|
expect(db.calls.some((sql) => sql.includes('INSERT INTO cost_details'))).toBe(false);
|
|
});
|
|
|
|
it('apply 按明细逐行写入 unit_cost_sync 采购行,金额=计划量×备货价', async () => {
|
|
const db = fakeDb({ candidates: [CANDIDATE] });
|
|
const result = await backfillMissingPrepPurchaseCosts(db as never, { apply: true });
|
|
expect(result.inserted).toBe(1);
|
|
const insert = db.calls.find((sql) => sql.includes('INSERT INTO cost_details')) || '';
|
|
expect(insert).toContain("'purchase', 'order_goods'");
|
|
expect(insert).toContain("'supplier'");
|
|
expect(insert).toContain("'order_item'");
|
|
expect(insert).toContain("'unit_cost_sync'");
|
|
expect(insert).toContain('ON CONFLICT DO NOTHING');
|
|
});
|
|
|
|
it('唯一索引命中(插入 0 行)时不重复计数', async () => {
|
|
const db = fakeDb({ candidates: [CANDIDATE], inserted: 0 });
|
|
const result = await backfillMissingPrepPurchaseCosts(db as never, { apply: true });
|
|
expect(result.inserted).toBe(0);
|
|
expect(result.candidates).toBe(1);
|
|
});
|
|
|
|
it('金额为 0 的候选不写库', async () => {
|
|
const db = fakeDb({ candidates: [{ ...CANDIDATE, amount: 0 }] });
|
|
const result = await backfillMissingPrepPurchaseCosts(db as never, { apply: true });
|
|
expect(result.inserted).toBe(0);
|
|
expect(db.calls.some((sql) => sql.includes('INSERT INTO cost_details'))).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('防复发:结案兜底 + failures 透出 + 静默点留痕', () => {
|
|
const fulfillment = readFileSync(
|
|
new URL('../src/services/orderWorkflowFulfillmentService.ts', import.meta.url),
|
|
'utf8'
|
|
);
|
|
|
|
it('结案置 90 前调用兜底补写,且失败不阻断', () => {
|
|
const backfillAt = fulfillment.indexOf('backfillMissingPrepPurchaseCosts(db, { orderCode, apply: true })');
|
|
const to90At = fulfillment.indexOf('UPDATE orders SET order_status = 90');
|
|
expect(backfillAt).toBeGreaterThan(-1);
|
|
expect(to90At).toBeGreaterThan(backfillAt);
|
|
expect(fulfillment).toContain('结案兜底失败(不阻断结案)');
|
|
});
|
|
|
|
it('备货状态入口把 failures 回传(管理员端与供应商端)', () => {
|
|
const admin = readFileSync(new URL('../src/routes/mobile/mobileAdminRoutes.ts', import.meta.url), 'utf8');
|
|
const supplier = readFileSync(
|
|
new URL('../src/routes/supplier/supplierBusinessRoutes.ts', import.meta.url),
|
|
'utf8'
|
|
);
|
|
const prep = readFileSync(
|
|
new URL('../src/modules/workflow/routes/preparationRoutes.ts', import.meta.url),
|
|
'utf8'
|
|
);
|
|
expect(admin).toContain('failures: data.failures');
|
|
expect(supplier).toContain('failures: data.failures');
|
|
expect(prep).toContain('data.failures');
|
|
});
|
|
|
|
it('静默漏账点写 warn 日志', () => {
|
|
const unitCost = readFileSync(
|
|
new URL('../src/services/orderItemUnitCostService.ts', import.meta.url),
|
|
'utf8'
|
|
);
|
|
const posting = readFileSync(
|
|
new URL('../src/services/orderCostPostingService.ts', import.meta.url),
|
|
'utf8'
|
|
);
|
|
expect(unitCost).toContain('setOrderItemUnitCostByOrderSku 未匹配到订单明细');
|
|
expect(posting).toContain('upsertOrderSkuPurchaseCostFromUnitCost 未匹配到明细');
|
|
});
|
|
|
|
it('补账脚本默认 dry-run,且支持 --apply 与审计', () => {
|
|
const script = readFileSync(
|
|
new URL('../scripts/backfill-missing-prep-purchase-cost.cjs', import.meta.url),
|
|
'utf8'
|
|
);
|
|
expect(script).toContain("apply: false");
|
|
expect(script).toContain('--apply');
|
|
expect(script).toContain("operation_type, target_table, target_id");
|
|
expect(script).toContain("'cost_backfill'");
|
|
});
|
|
});
|