/** * features/chat/api/transfer.api.spec.ts * 用途:锁定在途仓自动调拨 JSON POST 请求头契约——必须携带 Content-Type: application/json, * 否则后端 Flask request.get_json 解析不到 body,导致默认 source_codes=[] 返回 400。 * 依赖:Vitest、vi.mock(@/shared/api/client)、transfer.api、getAuthHeaders。 * 逻辑:POST(create/execute/cancel/retry/manual-close)断言 headers 含 application/json, * 且 body 序列化正确;GET/blob 端点不强制 Content-Type。 */ import { beforeEach, describe, expect, it, vi } from 'vitest'; const requestJson = vi.hoisted(() => vi.fn()); const requestBlob = vi.hoisted(() => vi.fn()); vi.mock('@/shared/api/client', () => ({ BusinessError: class BusinessError extends Error {}, requestJson, requestBlob, })); vi.mock('@/shared/utils/auth', () => ({ getAuthHeaders: () => ({ Authorization: 'Bearer TEST-TOKEN' }), })); import { cancelTransferRun, createTransferPrecheck, executeTransferPrecheck, getTransferBacklogSummary, getTransferPrecheck, getTransferRun, listTransferSources, manualCloseTransferItem, retryTransferItem, } from './transfer.api'; describe('transfer.api JSON request contract', () => { beforeEach(() => { requestJson.mockReset(); requestBlob.mockReset(); }); it('创建预检 POST 带 Content-Type: application/json 与序列化 source_codes', () => { createTransferPrecheck({ source_codes: ['DBS-AWSBI'], business_date: '2026-08-18' }); const [url, init] = requestJson.mock.calls[0] as [string, RequestInit]; expect(url).toBe('/api/in-transit-transfer/prechecks'); expect(init.method).toBe('POST'); expect((init.headers as Record)['Content-Type']).toBe('application/json'); expect(init.body).toBe(JSON.stringify({ source_codes: ['DBS-AWSBI'], business_date: '2026-08-18' })); }); it('直接生成调拨单 POST 带 application/json', () => { executeTransferPrecheck('TPC-1', ['INV-001']); const [, init] = requestJson.mock.calls[0] as [string, RequestInit]; expect(init.method).toBe('POST'); expect((init.headers as Record)['Content-Type']).toBe('application/json'); expect(init.body).toBe(JSON.stringify({ invoice_nos: ['INV-001'] })); }); it('重试/关闭/取消 POST 带 application/json', () => { retryTransferItem(7); manualCloseTransferItem(7, '线下已处理'); cancelTransferRun('RUN-1', '紧急'); for (const [, init] of requestJson.mock.calls as [string, RequestInit][]) { expect(init.method).toBe('POST'); expect((init.headers as Record)['Content-Type']).toBe('application/json'); } }); it('GET 端点只需鉴权头,不需要强制 Content-Type', () => { getTransferPrecheck('TPC-1'); getTransferRun('RUN-1'); getTransferBacklogSummary(); listTransferSources(); for (const [, init] of requestJson.mock.calls as [string, RequestInit][]) { expect(init.headers).toBeDefined(); } }); });