Files
syops/frontend/src/features/chat/api/transfer.api.spec.ts
T
hugo 6b53b563e7 feat(in-transit-transfer): 实现在途仓自动调拨Agent一期完整功能
包含后端服务层、任务处理器与消费Worker、前端聊天组件、数据库模型与迁移脚本、测试用例,同时新增外部系统连接器框架基础能力并更新相关文档。
2026-08-28 17:16:06 +08:00

79 lines
3.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 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<string, string>)['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<string, string>)['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<string, string>)['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();
}
});
});