Files
SupplierSystem/server/tests/prepBeforeInspection.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

189 lines
6.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 },
getRawPoolQuery: () => vi.fn(),
}));
vi.mock('../src/services/inspectionRejectFulfillmentService', () => ({
createSupplierInspectionIfNeeded: vi.fn(async () => false),
hasOpenPendingInspection: vi.fn(async () => false),
hasSuccessfulInboundInspection: vi.fn(async () => false),
prunePendingInspectionsWhenInboundCovered: vi.fn(async () => 0),
pruneStalePendingInspectionsAfterPass: vi.fn(async () => 0),
}));
vi.mock('../src/services/processingTaskService', () => ({
ensureProcessingTaskForInspection: vi.fn(async () => undefined),
syncProcessingTasksForOrder: vi.fn(async () => undefined),
}));
vi.mock('../src/services/orderItemFulfillmentService', () => ({
isOrderItemFulfillmentValid: vi.fn(async () => true),
listUncoveredOrderItems: vi.fn(async () => []),
}));
vi.mock('../src/services/orderInspectionStateService', () => ({
isOrderItemInboundCovered: vi.fn(async () => false),
isMergeGuestOrderItem: vi.fn(async () => false),
}));
vi.mock('../src/services/inspectionDisplayService', () => ({
lookupInspectionItemName: vi.fn(async () => '商品'),
}));
import {
countPendingSupplierPreparations,
demotePrematureInspectionStatus,
ensureInspectionsForOrder,
tryAdvanceOrderToInspection,
} from '../src/services/preparationFulfillmentService';
import { computeEffectiveOrderStatus } from '../src/services/orderStatusDisplayService';
import { createSupplierInspectionIfNeeded } from '../src/services/inspectionRejectFulfillmentService';
describe('prep before inspection gate', () => {
beforeEach(() => {
query.mockReset();
release.mockReset();
connect.mockClear();
vi.mocked(createSupplierInspectionIfNeeded).mockClear();
});
it('countPendingSupplierPreparations 以 prepare_status < 30 为准', async () => {
query.mockResolvedValueOnce({ rowCount: 1, rows: [{ cnt: 2 }] });
const cnt = await countPendingSupplierPreparations('ORD1');
expect(cnt).toBe(2);
const sql = String(query.mock.calls[0][0]);
expect(sql).toMatch(/prepare_status.*<\s*30/);
expect(sql).not.toMatch(/NOT EXISTS/);
});
it('备货未完成时 ensureInspectionsForOrder 不建待验', async () => {
const clientQuery = vi.fn(async (sql: string) => {
const s = String(sql);
if (s.includes('DELETE FROM inspections i')) {
return { rowCount: 1, rows: [{ id: 'ghost-1' }] };
}
if (s.includes('FROM supplier_preparations sp') && s.includes('prepare_qty')) {
return {
rowCount: 1,
rows: [
{
order_code: 'ORD1',
sku_code: 'SKU_DRY',
supplier_code: 'SUP1',
prepare_qty: 10,
purchase_price: 5,
item_name: '干杂',
prepare_status: 10,
},
],
};
}
if (s.includes('FROM order_items oi')) {
return {
rowCount: 1,
rows: [
{
id: 'item-1',
sku_code: 'SKU_DRY',
quantity: 10,
price: 8,
item_name: '干杂',
supplier_code: 'SUP1',
stock_cover_qty: 0,
},
],
};
}
if (s.includes('order_item_fulfillments')) {
return { rowCount: 1, rows: [{}] };
}
return { rowCount: 0, rows: [] };
});
const created = await ensureInspectionsForOrder('ORD1', 'BR001', {
client: { query: clientQuery } as any,
});
expect(created).toBe(0);
expect(createSupplierInspectionIfNeeded).not.toHaveBeenCalled();
});
it('tryAdvance:备货未完成不进 40;误进 40 可回退', async () => {
// demote path when status=40
query
.mockResolvedValueOnce({
rowCount: 1,
rows: [{ order_status: 40, branch_code: 'BR001' }],
})
// countPendingSupplierPreparations
.mockResolvedValueOnce({ rowCount: 1, rows: [{ cnt: 1 }] })
// countPendingPurchases
.mockResolvedValueOnce({ rowCount: 1, rows: [{ cnt: 0 }] })
// prune
.mockResolvedValueOnce({ rowCount: 1, rows: [{ id: 'i1' }] })
// demote update
.mockResolvedValueOnce({ rowCount: 1, rows: [{ order_code: 'ORD1' }] });
const demoted = await tryAdvanceOrderToInspection('ORD1');
expect(demoted).toMatchObject({ advanced: false, newStatus: 30 });
query.mockReset();
// status=30, pending preps > 0 → block advance
query
.mockResolvedValueOnce({
rowCount: 1,
rows: [{ order_status: 30, branch_code: 'BR001' }],
})
// listUncovered empty via mocked listUncoveredOrderItems
// pending preps
.mockResolvedValueOnce({ rowCount: 1, rows: [{ cnt: 1 }] })
.mockResolvedValueOnce({ rowCount: 1, rows: [{ cnt: 0 }] });
const blocked = await tryAdvanceOrderToInspection('ORD1');
expect(blocked.advanced).toBe(false);
expect(String(blocked.message)).toMatch(/备货未完成/);
});
it('demotePrematureInspectionStatus 在 pending 时回退 40→30', async () => {
query
.mockResolvedValueOnce({ rowCount: 1, rows: [{ cnt: 2 }] }) // pending preps
.mockResolvedValueOnce({ rowCount: 1, rows: [{ cnt: 0 }] }) // pending purchases
.mockResolvedValueOnce({ rowCount: 1, rows: [{ id: 'ghost' }] }) // prune
.mockResolvedValueOnce({ rowCount: 1, rows: [{ order_code: 'ORD1' }] }); // update
const result = await demotePrematureInspectionStatus('ORD1');
expect(result.demoted).toBe(true);
expect(result.pruned).toBe(1);
});
it('computeEffectiveOrderStatus:有未完成备货时不显示 40', () => {
const status = computeEffectiveOrderStatus(
40,
{
allocationFulfilled: true,
pendingPreps: 1,
pendingPurchases: 0,
itemCount: 3,
inspectionCount: 2,
pendingProcessing: 0,
pendingPicks: 0,
outboundPicks: 0,
deliveryStarted: false,
deliveryEnded: false,
customerSigned: false,
},
'pending',
true
);
expect(status).toBe(30);
});
});