import { beforeEach, describe, expect, it, vi } from 'vitest'; import { readFileSync } from 'fs'; import path from 'path'; const { query } = vi.hoisted(() => ({ query: vi.fn(), })); vi.mock('../src/db', () => ({ pool: { query, connect: vi.fn() }, getRawPoolQuery: () => vi.fn(), })); const getBidSettings = vi.hoisted(() => vi.fn()); vi.mock('../src/services/bidConfigService', () => ({ getBidSettings, assertBidEnabled: vi.fn(), })); vi.mock('../src/services/inventoryStockCoverService', () => ({ applyStockCoverForOrder: vi.fn(), })); vi.mock('../src/services/bidAwardService', () => ({ notifySuppliersBidOpened: vi.fn(), })); import { listOrderSkusNeedingBidProcure } from '../src/services/bidRoundService'; describe('指定供应商直供接线', () => { const bidSrc = readFileSync(path.join(__dirname, '../src/services/bidRoundService.ts'), 'utf8'); const allocSrc = readFileSync( path.join(__dirname, '../src/services/orderAllocationService.ts'), 'utf8' ); const mgmtSrc = readFileSync( path.join(__dirname, '../../src/pages/orders/BidManagement.tsx'), 'utf8' ); const histSrc = readFileSync(path.join(__dirname, '../../src/pages/orders/BidHistory.tsx'), 'utf8'); const zhBids = readFileSync(path.join(__dirname, '../../src/i18n/locales/zh-CN/bids.ts'), 'utf8'); beforeEach(() => { query.mockReset(); getBidSettings.mockReset(); }); it('闸门:商品行指定的 SKU 不进 needingBid', async () => { getBidSettings.mockResolvedValue({ enabled: true, sku_codes: ['MUSHROOM', 'TOFU'], category_codes: [], sku_items: [ { sku_code: 'MUSHROOM', supplier_code: 'SUP_A' }, { sku_code: 'TOFU', supplier_code: '' }, ], category_items: [], }); query.mockResolvedValueOnce({ rowCount: 2, rows: [ { sku_code: 'MUSHROOM', category_code: 'VEG', suppliers: null, procure_qty: 2 }, { sku_code: 'TOFU', category_code: 'VEG', suppliers: null, procure_qty: 1 }, ], }); const skus = await listOrderSkusNeedingBidProcure({ orderCode: 'ORD_MIX', branchCode: 'BR001' }); expect(skus).toEqual(['TOFU']); expect(skus).not.toContain('MUSHROOM'); }); it('闸门:订单行真实供应商的 SKU 不进 needingBid', async () => { getBidSettings.mockResolvedValue({ enabled: true, sku_codes: ['MUSHROOM'], category_codes: [], sku_items: [{ sku_code: 'MUSHROOM', supplier_code: '' }], category_items: [], }); query.mockResolvedValueOnce({ rowCount: 1, rows: [ { sku_code: 'MUSHROOM', category_code: 'VEG', suppliers: ['SUP_ORDER'], procure_qty: 3, }, ], }); const skus = await listOrderSkusNeedingBidProcure({ orderCode: 'ORD_LINE' }); expect(skus).toEqual([]); }); it('闸门:INTERNAL 不算指定,仍进竞价(无设置指定时)', async () => { getBidSettings.mockResolvedValue({ enabled: true, sku_codes: ['MUSHROOM'], category_codes: [], sku_items: [{ sku_code: 'MUSHROOM', supplier_code: '' }], category_items: [], }); query.mockResolvedValueOnce({ rowCount: 1, rows: [ { sku_code: 'MUSHROOM', category_code: 'VEG', suppliers: ['INTERNAL'], procure_qty: 1 }, ], }); const skus = await listOrderSkusNeedingBidProcure({ orderCode: 'ORD_SELF' }); expect(skus).toEqual(['MUSHROOM']); }); it('openBidRound 全指定不写假中标、取消轮次并直接分配;混开删除指定明细', () => { expect(bidSrc).toMatch(/allDirect/); expect(bidSrc).toMatch(/全部指定直供,跳过请报价通知/); expect(bidSrc).toMatch(/DELETE FROM bid_round_items WHERE round_id = \$1 AND sku_code = ANY/); expect(bidSrc).not.toMatch(/allCategoriesDesignated/); expect(bidSrc).not.toMatch(/writeDirectAwards/); expect(bidSrc).not.toMatch(/SELECT cost_price FROM purchase_costs/); const start = bidSrc.indexOf('export async function openBidRound'); const next = bidSrc.indexOf('export async function submitBidQuotes'); const openFn = bidSrc.slice(start, next > start ? next : undefined); expect(openFn).toMatch(/status = 'cancelled'/); expect(openFn).toMatch(/reprocessOrderAllocation/); expect(openFn).not.toMatch(/INSERT INTO bid_awards/); }); it('分配两条路径盖章且禁止智能分配改派指定 SKU', () => { expect(allocSrc).toMatch(/stampDesignatedSuppliersOnOrderItems/); expect(allocSrc).toMatch(/const designatedSupplier = designatedBySku\.get/); expect(allocSrc).toMatch(/supplierCode: designatedSupplier/); }); it('竞价设置与历史页文案', () => { // 文案已迁 i18n:页面用 key,中文文案在 zh-CN/bids.ts(语义不变) expect(mgmtSrc).toContain('bids.scope.designateSupplier'); expect(mgmtSrc).toContain('bids.scope.noDesignate'); expect(zhBids).toContain('指定供应商(跳过报价,直接供货)'); expect(zhBids).toContain('不指定(邀同品类全部报价)'); expect(zhBids).not.toContain('首选供应商(非独家)'); expect(histSrc).toMatch(/detail\.round\.status !== 'awarded'/); }); it('加载范围不把最近中标回填到已保存的空供应商', () => { expect(mgmtSrc).toMatch(/已保存供应商(含空=不指定)原样展示/); expect(mgmtSrc).not.toMatch(/award && bound\.has\(award\)/); expect(mgmtSrc).not.toMatch(/i\.supplier_code \|\| awardsSku/); }); });