import { beforeEach, describe, expect, it, vi } from 'vitest'; /** * 绑定即建客户 / 解绑即删客户(2026-10-18 用户要求) * * 用户原话:「供应商的编码被哪一个配送商分公司绑定了,那个配送商分公司应该自动出现在供应商的 * 客户列表里面,而不是等着派单过来才出现;解绑了就消失」。 */ const h = vi.hoisted(() => ({ ensureCalls: [] as unknown[], deleteCalls: [] as string[], deleteImpl: null as null | (() => Promise), updateCalls: [] as unknown[], })); vi.mock('../src/services/tenantDbContext', () => ({ runWithTenantDbContext: async (_branch: string, fn: () => unknown) => fn(), })); vi.mock('../src/services/mainSystemOrderImport', () => ({ ensureMainBranchCustomerForBinding: async (input: unknown) => { h.ensureCalls.push(input); return 'MAIN_BR002'; }, })); vi.mock('../src/services/customerDeleteService', () => ({ deleteCustomer: async (code: string) => { h.deleteCalls.push(code); if (h.deleteImpl) return h.deleteImpl(); return { customer_code: code }; }, })); vi.mock('../src/db', async (importOriginal) => { const actual = await importOriginal(); return { ...actual, pool: { query: async (text: string, params: unknown[]) => { h.updateCalls.push({ text, params }); return { rows: [], rowCount: 1 }; }, }, }; }); import { ensureCustomerForBinding, mainBranchCustomerCode, removeCustomerForBinding, } from '../src/services/mainSystemCustomerBindingService'; beforeEach(() => { h.ensureCalls = []; h.deleteCalls = []; h.deleteImpl = null; h.updateCalls = []; }); describe('绑定即建客户', () => { it('主系统分公司 BR002 ⇒ 建/取 MAIN_BR002(不再等订单)', async () => { const res = await ensureCustomerForBinding({ branchCode: 'BR001', mainBranchCode: 'br002' }); expect(res).toMatchObject({ ok: true, action: 'created_or_kept', customer_code: 'MAIN_BR002' }); expect(h.ensureCalls).toHaveLength(1); expect((h.ensureCalls[0] as any).mainBranchCode).toBe('BR002'); expect((h.ensureCalls[0] as any).branchCode).toBe('BR001'); }); it('通配绑定 * 或空 ⇒ 跳过(无法对应具体分公司)', async () => { expect(await ensureCustomerForBinding({ branchCode: 'BR001', mainBranchCode: '*' })).toMatchObject({ action: 'skipped', }); expect(await ensureCustomerForBinding({ branchCode: 'BR001', mainBranchCode: '' })).toMatchObject({ action: 'skipped', }); expect(h.ensureCalls).toHaveLength(0); }); it('客户编码口径:MAIN_<大写分公司码>', () => { expect(mainBranchCustomerCode('br002')).toBe('MAIN_BR002'); expect(mainBranchCustomerCode('*')).toBeNull(); }); }); describe('解绑即删客户', () => { it('无引用 ⇒ 硬删', async () => { const res = await removeCustomerForBinding({ branchCode: 'BR001', mainBranchCode: 'BR002' }); expect(res).toMatchObject({ ok: true, action: 'deleted', customer_code: 'MAIN_BR002' }); expect(h.deleteCalls).toEqual(['MAIN_BR002']); expect(h.updateCalls).toHaveLength(0); }); it('有历史订单(删除守卫 400)⇒ 退化为停用 status=40,不丢历史归属', async () => { h.deleteImpl = async () => { throw Object.assign(new Error('该客户存在历史订单,无法删除'), { status: 400 }); }; const res = await removeCustomerForBinding({ branchCode: 'BR001', mainBranchCode: 'BR002' }); expect(res).toMatchObject({ ok: true, action: 'disabled', customer_code: 'MAIN_BR002' }); expect(h.updateCalls).toHaveLength(1); expect(String((h.updateCalls[0] as any).text)).toContain('status = 40'); }); it('客户本来就不存在(404)⇒ 幂等成功', async () => { h.deleteImpl = async () => { throw Object.assign(new Error('客户不存在'), { status: 404 }); }; const res = await removeCustomerForBinding({ branchCode: 'BR001', mainBranchCode: 'BR002' }); expect(res).toMatchObject({ ok: true, action: 'deleted' }); }); });