import { describe, expect, it, vi, beforeEach } from 'vitest'; /** * 拉取容错(2026-10-22):一个供应商的 token/接口失败,不能把整个分公司的导入拖死。 * * 真机事故:分公司有一条历史残留绑定(token 403),`pullPreparationsForBranch` 直接整轮 * `unavailable` ⇒ 同一分公司其它正常供应商的单也进不来。现在改为"失败跳过并回报, * 其余照常";全部失败才整分公司不可用。 */ const mocks = vi.hoisted(() => ({ listAutoPullable: vi.fn(), listActive: vi.fn(), fetchPreps: vi.fn(), })); vi.mock('../src/services/branchAccessCode', () => ({ listAutoPullableBindingsForBranch: (...args: unknown[]) => mocks.listAutoPullable(...args), listActiveBindingsForBranch: (...args: unknown[]) => mocks.listActive(...args), })); vi.mock('../src/services/mainSystemPreparations', () => ({ fetchMainSystemPreparations: (...args: unknown[]) => mocks.fetchPreps(...args), })); const { pullPreparationsForBranch } = await import('../src/services/mainSystemBranchPull'); const binding = (supplierCode: string) => ({ branchCode: 'BR002', accessCode: 'SCQEM2X7B6P5', mainBranchCode: 'BR002', mainSupplierCode: supplierCode, }); const okOutcome = (supplierCode: string) => ({ kind: 'ok' as const, supplierCode, branchCode: 'BR002', orders: [], rowCount: 0, }); beforeEach(() => { vi.clearAllMocks(); mocks.listActive.mockResolvedValue([]); }); describe('按绑定拉取:单个供应商失败不拖垮整分公司', () => { it('一个失败 ⇒ 跳过该供应商,返回 ok + failures,其余照常拉', async () => { mocks.listAutoPullable.mockResolvedValue([binding('SUP_A'), binding('SUP_B')]); mocks.fetchPreps.mockImplementation(async (supplierCode: string) => supplierCode === 'SUP_A' ? okOutcome(supplierCode) : { kind: 'unavailable', reason: 'forbidden: 未授权' } ); const r = await pullPreparationsForBranch('BR002', 'active'); expect(r.kind).toBe('ok'); if (r.kind === 'ok') { expect(r.pulls.map((p) => p.supplierCode)).toEqual(['SUP_A']); expect(r.failures).toHaveLength(1); expect(r.failures[0]).toMatchObject({ supplierCode: 'SUP_B' }); expect(r.failures[0].reason).toContain('未授权'); } }); it('全部失败 ⇒ 仍返回 unavailable(整分公司本轮不可用,调度计 failed)', async () => { mocks.listAutoPullable.mockResolvedValue([binding('SUP_A'), binding('SUP_B')]); mocks.fetchPreps.mockResolvedValue({ kind: 'unavailable', reason: 'forbidden' }); const r = await pullPreparationsForBranch('BR002', 'active'); expect(r.kind).toBe('unavailable'); if (r.kind === 'unavailable') expect(r.reason).toContain('forbidden'); }); it('没有可自动拉取的绑定 ⇒ not_bound(正常状态,不是失败)', async () => { mocks.listAutoPullable.mockResolvedValue([]); mocks.listActive.mockResolvedValue([]); const r = await pullPreparationsForBranch('BR002', 'active'); expect(r.kind).toBe('not_bound'); }); });