fix: 天河链测试环境联调修正(手工 multipart + GET body)与 seed 短码格式

- upload 改手工构造 multipart(userDto 无 filename 带 application/json),修复 undici Blob 强加 filename 导致的 500
- confirm 接口 9 改用 node:https 发送带 body 的 GET,appKey 移出 URL query 避免泄漏
- seed 追溯码短码改为 12 位纯数字,与 TRACE_CODE_PATTERN 一致,修复 C 端公开溯源判 INVALID
- 单测适配:https 模块 mock、手工 multipart 断言,27 测试全过
- 问题清单更新 T1-T8 联调结论,新增 F1(已修)/F2(待查)
This commit is contained in:
Nixevol
2026-07-09 19:34:02 +08:00
parent fa4215bd52
commit e037e460e2
4 changed files with 176 additions and 71 deletions
+34 -11
View File
@@ -1,4 +1,28 @@
import { TianheProvider } from './tianhe.provider';
import { EventEmitter } from 'node:events';
// 模拟 node:https.request:接口 9 用带 body 的 GET,走 https 模块而非 fetch
const httpsRequestMock = jest.fn();
jest.mock('node:https', () => ({ request: (...args: unknown[]) => httpsRequestMock(...args) }));
function mockHttpsRequestOnce(responseJson: unknown, statusCode = 200) {
httpsRequestMock.mockImplementation((_url: unknown, _opts: unknown, cb: unknown) => {
const callback = (typeof _opts === 'function' ? _opts : cb) as (res: EventEmitter & { statusCode: number }) => void;
const req = new EventEmitter() as EventEmitter & { write: () => void; end: () => void };
req.write = () => undefined;
req.end = () => {
const res = new EventEmitter() as EventEmitter & { statusCode: number };
res.statusCode = statusCode;
callback(res);
res.emit('data', JSON.stringify(responseJson));
res.emit('end');
};
return req;
});
}
// 放在 jest.mock 之后导入,确保 provider 拿到被 mock 的 https
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { TianheProvider } = require('./tianhe.provider') as typeof import('./tianhe.provider');
describe('TianheProvider', () => {
const provider = new TianheProvider({
@@ -43,7 +67,7 @@ describe('TianheProvider', () => {
expect(result.blockHeight).toBeNull();
});
it('upload 的 userDto 分段必须为 application/json 类型', async () => {
it('upload 手工 multipart:userDto 分段无 filename 且带 application/json', async () => {
const fetchMock = jest.spyOn(globalThis, 'fetch').mockResolvedValue({
ok: true,
text: async () =>
@@ -52,10 +76,12 @@ describe('TianheProvider', () => {
await provider.attest({ dataHash: '0xhash' });
const body = fetchMock.mock.calls[0][1]?.body as FormData;
const userDto = body.get('userDto');
expect(userDto).toBeInstanceOf(Blob);
expect((userDto as Blob).type).toBe('application/json');
const init = fetchMock.mock.calls[0][1]!;
expect(String((init.headers as Record<string, string>)['Content-Type'])).toContain('multipart/form-data; boundary=');
const body = (init.body as Buffer).toString('utf8');
// userDto 分段:无 filename、带 application/json
expect(body).toContain('Content-Disposition: form-data; name="userDto"\r\nContent-Type: application/json');
expect(body).not.toContain('name="userDto"; filename');
});
it('响应缺少 transactionHash 时抛错,不用本地 dataHash 冒充', async () => {
@@ -114,7 +140,7 @@ describe('TianheProvider', () => {
expect(String(fetchMock.mock.calls[1][0])).toContain('/api/v2/user');
});
it('按交易状态接口确认链上区块信息', async () => {
it('按交易状态接口确认链上区块信息(GET+body 走 https 模块)', async () => {
const response = {
code: 0,
message: 'success',
@@ -126,10 +152,7 @@ describe('TianheProvider', () => {
tokenId: -999,
},
};
jest.spyOn(globalThis, 'fetch').mockResolvedValue({
ok: true,
text: async () => JSON.stringify(response),
} as Response);
mockHttpsRequestOnce(response);
const result = await provider.confirmTransaction(response.data.transactionHash);