修复:完善上传契约与自动化闭环
统一 multipart 请求透传并补齐接口契约和边界校验;新增 AI 自动改价、暂时性失败重试、擦亮与确认发货幂等处理;修复聊天已读回执跨端同步误报警,并更新前端静态资源与回归测试。
This commit is contained in:
1 parent
44f0b5c559
commit
50eca2fca6
77 files changed
+2138
-274
No files matched your search
@@ -0,0 +1,100 @@
|
||||
import { createServer, type Server } from 'node:http';
|
||||
import type { AddressInfo } from 'node:net';
|
||||
|
||||
import { afterEach, expect, test } from 'vitest';
|
||||
|
||||
import { contractFetch } from './client';
|
||||
|
||||
// multipartRecord 保存本地 HTTP 服务实际收到的请求路径、声明边界和原始表单字节,防止测试替身掩盖浏览器传输差异。
|
||||
type multipartRecord = {
|
||||
// path 是当前 multipart 调用到达的版本化 API 路径。
|
||||
path: string;
|
||||
// contentType 是客户端实际发送的 multipart Content-Type 响应头。
|
||||
contentType: string;
|
||||
// rawBody 是服务端未解析前读取的完整请求字节。
|
||||
rawBody: Buffer;
|
||||
};
|
||||
|
||||
// multipartServer 是当前测试启动的本地 HTTP 服务;afterEach 必须关闭它避免占用测试进程端口。
|
||||
let multipartServer: Server | undefined;
|
||||
|
||||
afterEach(async () => {
|
||||
// server 保存当前测试可能仍在监听的本地 HTTP 服务。
|
||||
const server = multipartServer;
|
||||
multipartServer = undefined;
|
||||
if (server) {
|
||||
await new Promise<void>(/* resolve 在服务停止并释放端口后继续测试清理。 */ resolve => server.close(() => resolve()));
|
||||
}
|
||||
} /* 测试回调确保 multipart 本地 HTTP 服务不会泄漏到其他测试。 */);
|
||||
|
||||
test('contractFetch keeps multipart boundary, file bytes, filenames, and repeated image fields over real HTTP', async () => {
|
||||
// records 收集服务端从原始网络请求读取的 multipart 内容,用于校验声明 boundary 与请求体一致。
|
||||
const records: multipartRecord[] = [];
|
||||
// server 模拟浏览器实际请求到达的 HTTP 服务,故意不通过 fetch mock 重建 FormData。
|
||||
const server = createServer(
|
||||
// HTTP 回调按原始字节采集契约客户端的网络请求,再返回空成功响应。
|
||||
(request, response) => {
|
||||
// chunks 以字节块保存当前请求体,避免 UTF-8 转换损坏二进制文件内容。
|
||||
const chunks: Buffer[] = [];
|
||||
request.on('data', /* chunk 是当前网络读取到的原始请求字节。 */ chunk => chunks.push(Buffer.from(chunk)));
|
||||
request.on('end',
|
||||
// end 回调在请求体读取完成后记录原始 multipart 字节。
|
||||
() => {
|
||||
// rawBody 合并当前请求的全部原始字节,后续断言直接检查 multipart framing。
|
||||
const rawBody = Buffer.concat(chunks);
|
||||
records.push({
|
||||
path: request.url || '',
|
||||
contentType: request.headers['content-type'] || '',
|
||||
rawBody,
|
||||
});
|
||||
response.writeHead(204);
|
||||
response.end();
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
multipartServer = server;
|
||||
await new Promise<void>(/* resolve 在操作系统分配 loopback 端口后继续发送请求。 */ resolve => server.listen(0, '127.0.0.1', () => resolve()));
|
||||
// address 是本地服务监听地址,端口由操作系统动态分配以支持测试并发运行。
|
||||
const address = server.address() as AddressInfo;
|
||||
// paths 覆盖商品发布、批量预检、卡密导入、聊天图片、订单导入和旧版订单刷新这六条 multipart 传输路径。
|
||||
const paths = [
|
||||
'/api/v1/items/publish',
|
||||
'/api/v1/items/publish-batches/preview',
|
||||
'/api/v1/cards/batch',
|
||||
'/api/v1/chat/images',
|
||||
'/api/v1/orders/import',
|
||||
'/api/v1/orders/refresh',
|
||||
];
|
||||
|
||||
for (const /* path 是当前覆盖的 multipart 版本化 API 路径。 */ path of paths) {
|
||||
// form 是调用方构造的原生 FormData,必须由 contractFetch 连同浏览器生成的 boundary 原样发送。
|
||||
const form = new FormData();
|
||||
form.append('title', '测试商品');
|
||||
form.append('images', new Blob(['image-one'], { type: 'image/png' }), 'first image.png');
|
||||
form.append('images', new Blob(['image-two'], { type: 'image/png' }), 'second-image.png');
|
||||
// request 是 openapi-fetch 最终会交给共享 fetch 的请求形态;此处直接覆盖其网络层行为。
|
||||
const request = new Request(`http://127.0.0.1:${address.port}${path}`, { method: 'POST', body: form });
|
||||
// response 证明请求已通过实际 HTTP 往返,而非仅检查客户端构造参数。
|
||||
const response = await contractFetch(request);
|
||||
expect(response.status).toBe(204);
|
||||
}
|
||||
|
||||
expect(records).toHaveLength(paths.length);
|
||||
for (const /* record 是当前服务端记录的 multipart 原始请求。 */ record of records) {
|
||||
// boundaryMatch 提取请求头声明的 boundary;缺失时说明浏览器或共享客户端错误覆盖了 Content-Type。
|
||||
const boundaryMatch = /boundary=(?:"([^"]+)"|([^;\s]+))/.exec(record.contentType);
|
||||
expect(record.contentType).toContain('multipart/form-data');
|
||||
expect(boundaryMatch).not.toBeNull();
|
||||
// boundary 是 multipart framing 必须使用的分隔符,原始 body 的首段必须包含它。
|
||||
const boundary = boundaryMatch?.[1] || boundaryMatch?.[2] || '';
|
||||
// rawText 使用 latin1 保留任意二进制字节的一对一映射,适合检查 multipart header、文件名和示例内容。
|
||||
const rawText = record.rawBody.toString('latin1');
|
||||
expect(rawText).toContain(`--${boundary}`);
|
||||
expect(rawText).toContain('filename="first image.png"');
|
||||
expect(rawText).toContain('filename="second-image.png"');
|
||||
expect((rawText.match(/name="images"/g) || []).length).toBe(2);
|
||||
expect(rawText).toContain('image-one');
|
||||
expect(rawText).toContain('image-two');
|
||||
}
|
||||
} /* 真实 HTTP 回归验证:共享客户端绝不重建 FormData 或保留过期 boundary。 */);
|
||||
@@ -23,32 +23,8 @@ type ContractResult<T> = {
|
||||
response: Response;
|
||||
};
|
||||
|
||||
// contractFetch 为 openapi-fetch 注入现有浏览器 Cookie 与请求地址兼容语义。
|
||||
const contractFetch: typeof fetch = async (input, init) => {
|
||||
// response 是使用既有 Cookie 策略发出的 HTTP 响应。
|
||||
// requestURL 是 openapi-fetch 根据 baseUrl 生成的绝对地址;测试环境需还原为相对路径以复用既有 fetch fixture。
|
||||
const requestURL = new URL(typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url);
|
||||
// request 是合并 openapi-fetch 已构造请求与可选 init 后的完整请求对象。
|
||||
const request = input instanceof Request ? input : new Request(input, init);
|
||||
// body 是按内容类型恢复的请求载荷;GET/HEAD 无请求体,multipart 必须保留原始 FormData 边界。
|
||||
const contentType = request.headers.get('content-type') || '';
|
||||
const body: BodyInit | undefined = request.method === 'GET' || request.method === 'HEAD'
|
||||
? undefined
|
||||
: contentType.includes('multipart/form-data')
|
||||
? await request.clone().formData()
|
||||
: await request.clone().text();
|
||||
// fetchInput 是测试环境还原后的相对 API 地址,浏览器环境继续保留绝对 origin。
|
||||
const fetchInput = requestURL.origin === 'http://localhost' ? `${requestURL.pathname}${requestURL.search}` : input;
|
||||
// response 是使用旧 client 等价方法、请求体、取消信号和 Cookie 策略发出的 HTTP 响应。
|
||||
const response = await fetch(fetchInput, {
|
||||
method: request.method,
|
||||
headers: request.headers,
|
||||
body,
|
||||
signal: request.signal,
|
||||
credentials: 'include',
|
||||
});
|
||||
return response;
|
||||
};
|
||||
// contractFetch 为 openapi-fetch 注入浏览器 Cookie;必须原样转发 Request,避免重新编码 FormData 后令 body boundary 与请求头失配。
|
||||
export const contractFetch: typeof fetch = (input, init) => fetch(input, { ...init, credentials: 'include' });
|
||||
|
||||
// contractClient 是唯一持有生成 paths 类型并执行版本化 HTTP operation 的共享实例。
|
||||
// contractBaseUrl 是浏览器生产环境的当前 origin;Node 测试使用可被 fetch fixture 还原的占位 origin。
|
||||
|
||||
@@ -1988,6 +1988,15 @@ export interface components {
|
||||
AIReplySettingsResponse: {
|
||||
cookie_id?: string;
|
||||
ai_enabled: boolean;
|
||||
auto_adjust_price_enabled: boolean;
|
||||
max_discount_percent: number;
|
||||
max_discount_amount: number;
|
||||
max_bargain_rounds: number;
|
||||
custom_prompts: string;
|
||||
};
|
||||
AIReplySettingsUpdateRequest: {
|
||||
ai_enabled: boolean;
|
||||
auto_adjust_price_enabled: boolean;
|
||||
max_discount_percent: number;
|
||||
max_discount_amount: number;
|
||||
max_bargain_rounds: number;
|
||||
@@ -2039,6 +2048,18 @@ export interface components {
|
||||
poi_name?: string;
|
||||
province?: string;
|
||||
};
|
||||
ItemPublishRequest: {
|
||||
cookie_id: string;
|
||||
title: string;
|
||||
description: string;
|
||||
price: string;
|
||||
original_price: string;
|
||||
quantity: number;
|
||||
postage_mode: string;
|
||||
postage: string;
|
||||
location?: string;
|
||||
images: string[];
|
||||
};
|
||||
ItemPublishBatchPreviewRow: {
|
||||
row_no: number;
|
||||
valid: boolean;
|
||||
@@ -2554,6 +2575,10 @@ export interface components {
|
||||
system_shipped?: boolean;
|
||||
item_title?: string;
|
||||
};
|
||||
OrderRefreshRequest: {
|
||||
cookie_id?: string;
|
||||
status?: string;
|
||||
};
|
||||
OrderRefreshDetail: {
|
||||
quantity: string;
|
||||
spec_name: string;
|
||||
@@ -5206,6 +5231,15 @@ export interface operations {
|
||||
"application/json": components["schemas"]["ErrorResponse"];
|
||||
};
|
||||
};
|
||||
/** @description AI 议价与固定规则改价配置冲突 */
|
||||
409: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["ErrorResponse"];
|
||||
};
|
||||
};
|
||||
/** @description 统一错误响应 */
|
||||
500: {
|
||||
headers: {
|
||||
@@ -5273,6 +5307,15 @@ export interface operations {
|
||||
"application/json": components["schemas"]["ErrorResponse"];
|
||||
};
|
||||
};
|
||||
/** @description AI 议价与固定规则改价配置冲突 */
|
||||
409: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["ErrorResponse"];
|
||||
};
|
||||
};
|
||||
/** @description 统一错误响应 */
|
||||
500: {
|
||||
headers: {
|
||||
@@ -7464,7 +7507,11 @@ export interface operations {
|
||||
path?: never;
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody?: never;
|
||||
requestBody: {
|
||||
content: {
|
||||
"multipart/form-data": components["schemas"]["ItemPublishRequest"];
|
||||
};
|
||||
};
|
||||
responses: {
|
||||
/** @description 成功 */
|
||||
200: {
|
||||
@@ -9366,10 +9413,8 @@ export interface operations {
|
||||
};
|
||||
requestBody: {
|
||||
content: {
|
||||
"multipart/form-data": {
|
||||
cookie_id?: string;
|
||||
status?: string;
|
||||
};
|
||||
"application/json": components["schemas"]["OrderRefreshRequest"];
|
||||
"multipart/form-data": components["schemas"]["OrderRefreshRequest"];
|
||||
};
|
||||
};
|
||||
responses: {
|
||||
@@ -11464,7 +11509,11 @@ export interface operations {
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody?: never;
|
||||
requestBody: {
|
||||
content: {
|
||||
"application/json": components["schemas"]["AIReplySettingsUpdateRequest"];
|
||||
};
|
||||
};
|
||||
responses: {
|
||||
/** @description 成功 */
|
||||
200: {
|
||||
@@ -11511,6 +11560,15 @@ export interface operations {
|
||||
"application/json": components["schemas"]["ErrorResponse"];
|
||||
};
|
||||
};
|
||||
/** @description AI 议价与固定规则改价配置冲突 */
|
||||
409: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["ErrorResponse"];
|
||||
};
|
||||
};
|
||||
/** @description 统一错误响应 */
|
||||
500: {
|
||||
headers: {
|
||||
|
||||
Reference in new issue
Block a user