Skip to content

Commit cb897b1

Browse files
authored
⚡️ 加速 GM_xhr E2E 测试 (#1749)
1 parent 249e2fc commit cb897b1

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

‎e2e/gm-api.spec.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,11 @@ async function startGMApiMockServer(): Promise<GMApiMockServer> {
451451
return;
452452
}
453453

454+
if (url.pathname === "/hang") {
455+
// 超时和中止只要求请求保持未完成,不需要等待真实服务端的秒级延迟。
456+
return;
457+
}
458+
454459
const delayMatch = url.pathname.match(/^\/delay\/(\d+)$/);
455460
if (delayMatch) {
456461
const delayMs = Number(delayMatch[1]) * 1000;
@@ -545,6 +550,12 @@ function patchGMApiTestCode(code: string, mockOrigin: string): string {
545550
// @connect 127.0.0.1,而重复的 @connect 值会让脚本完全不执行。
546551
.replace(/https:\/\/raw\.githubusercontent\.com\/\S*?\/([\w.-]+)\?/g, `${mockOrigin}/raw/$1?`)
547552
.replace(/https:\/\/translate\.googleapis\.com/g, mockOrigin)
553+
// 慢端点只验证超时/中止和 progress 的相对顺序;在本地 mock 中压缩等待,避免 E2E 为真实秒级延迟买单。
554+
.replace(/\$\{HB\}\/delay\/(?:3|5|10)/g, "${HB}/hang")
555+
.replace("timeout: 1000,", "timeout: 250,")
556+
.replace("timeout: 2000,", "timeout: 250,")
557+
.replace("{ abortAfterMs: 4000 }", "{ abortAfterMs: 300 }")
558+
.replace(/duration=2&delay=1&numbytes=2048/g, "duration=0.5&delay=0.05&numbytes=2048")
548559
);
549560
}
550561

0 commit comments

Comments
 (0)