From 5530add712f4283cbe71571cc658bb452456e978 Mon Sep 17 00:00:00 2001 From: zhangxiang Date: Tue, 20 Oct 2026 05:46:05 +0800 Subject: [PATCH] =?UTF-8?q?fix(e2e,api):=20=E8=81=94=E7=B3=BB=E8=A1=A8?= =?UTF-8?q?=E5=8D=95=E9=99=90=E6=B5=81=E5=90=8C=E7=99=BB=E5=BD=95=E4=B8=80?= =?UTF-8?q?=E6=A0=B7=E5=8F=AF=E8=A6=86=E7=9B=96=EF=BC=9B=E4=BF=AE=20uj-08?= =?UTF-8?q?=20=E9=9D=A2=E6=9D=BF=E5=8A=A8=E7=94=BB=E4=B8=8E=E5=AF=BC?= =?UTF-8?q?=E8=88=AA=E7=82=B9=E5=87=BB=E9=94=99=E4=BD=8D=E7=9A=84=E4=B8=A4?= =?UTF-8?q?=E5=A4=84=E7=9C=9F=E5=9B=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 全量功能 E2E 在「修好数据」后的干净读数:1066 passed / 2 failed,两条都是 website-acceptance 的「导航菜单功能正常」在 firefox/webkit 上 15s 超时。 按「能复现就修根因」处理,不记成墙钟抖动: 1) 登录/联系表单限流同族:contact 也是进程内内存桶 5 次/小时/IP,而 e2e server 跨整轮用例存活 ⇒ 多次提交必然 429。抽出 src/lib/rate-limit-config.ts 的 readRateLimitMax(未设⇒默认、非法值⇒默认且打 [rate-limit-config] warn、 合法值⇒取整),login 与 contact 都改用它;默认值都不动(10/5), 仅在 e2e harness 抬到 500/200。3 例判据覆盖上述三条路径。 2) uj-08:helper hover 的是顶栏里第一个 a[href=/services](实测命中工具行那条), 且面板正在播 280ms 入场动画 ⇒ Playwright 判 not stable 重试到 15s。 改为语义锚点「查看全部能力」+ 等 320ms(> token 的 280ms), 并断言其 href 就是导航项自己声明的 /services。firefox+chromium 复跑 2 passed。 3) website-acceptance「导航菜单功能正常」:日志显示点击后 navigated to / ⇒ 点在了 logo 上。①补 expectHydrated(nav)(相邻用例早有,这条漏了); ②选择器排除 href=/ ⇒ 测的真是菜单项。firefox+webkit 连跑 **5/5 绿**(4.7s, 此前 15s 超时)。 复算:tsc 0、type-check:e2e 0、rate-limit-config + login + seed-target 测试全绿。 --- e2e/playwright.config.ts | 2 ++ e2e/uj-08-service-journey.spec.ts | 14 +++++++++++-- e2e/website-acceptance.spec.ts | 11 ++++++++++- src/app/api/auth/login/route.ts | 3 ++- src/app/api/contact/route.ts | 7 ++++++- src/lib/rate-limit-config.test.ts | 33 +++++++++++++++++++++++++++++++ src/lib/rate-limit-config.ts | 27 +++++++++++++++++++++++++ 7 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 src/lib/rate-limit-config.test.ts create mode 100644 src/lib/rate-limit-config.ts diff --git a/e2e/playwright.config.ts b/e2e/playwright.config.ts index cfdba47..c047eea 100644 --- a/e2e/playwright.config.ts +++ b/e2e/playwright.config.ts @@ -163,6 +163,8 @@ export default defineConfig({ // 只在 E2E harness 抬高额度;生产与默认环境不经过这里,仍是 10 次/15 分钟/IP // (默认值与覆盖行为由 src/app/api/auth/login/route.test.ts 的双向用例钉住)。 LOGIN_RATE_LIMIT_MAX: '500', + // 同上:contact 是 5 次/小时/IP 的进程内桶,全量跑里多次提交会 429。 + CONTACT_RATE_LIMIT_MAX: '200', }, }, }); diff --git a/e2e/uj-08-service-journey.spec.ts b/e2e/uj-08-service-journey.spec.ts index 14ed08b..abcda4d 100644 --- a/e2e/uj-08-service-journey.spec.ts +++ b/e2e/uj-08-service-journey.spec.ts @@ -43,9 +43,19 @@ async function navigateFromServiceNav(page: Page) { await expect(capabilitiesTrigger).toBeVisible({ timeout: 5000 }); await capabilitiesTrigger.hover(); - const servicesLink = desktopNav.locator('a[href="/services"]').first(); + // 2026-10-17(§14.4):两处脆性一起修,断言不减。 + // ① 原用 `a[href="/services"]` 的 `.first()` —— 在顶栏里它会先命中工具行那条 + // `text-xs text-text-muted` 的链接(实测解析出来的就是它),hover 时面板正在播 + // 280ms 入场动画 ⇒ Playwright 判 "element is not stable" 一直重试到 15s(firefox 稳定红)。 + // ② 改用语义锚点「查看全部能力」并把面板动画等掉(320ms > --transition-normal 的 280ms)。 + // 目的地仍由导航项自己声明的 href 断言,不靠中文标签反推路由(同 e9bd9644 的纪律)。 + await page.waitForTimeout(320); + const servicesLink = desktopNav.getByRole('link', { name: /查看全部能力/ }).first(); await expect(servicesLink, '能力面板里没有指向 /services 的入口').toBeVisible({ timeout: 5000 }); - await servicesLink.hover(); // 防止 mouseleave 收起面板 + await expect(servicesLink, '能力面板入口的 href 必须是它自己声明的 /services').toHaveAttribute( + 'href', + '/services', + ); await servicesLink.click(); } diff --git a/e2e/website-acceptance.spec.ts b/e2e/website-acceptance.spec.ts index 734243d..882fc1e 100644 --- a/e2e/website-acceptance.spec.ts +++ b/e2e/website-acceptance.spec.ts @@ -71,8 +71,17 @@ test.describe('网站全面测试验收', { tag: '@regression' }, () => { // 桌面走顶栏、移动走抽屉;原因见 primary-nav.ts。 // N-27:原实现零断言,仅靠 `waitForURL(/\/.+/)` 隐式放行(任何路径变化都算过)。 // 改为断言「点击的链接确实落到它自己声明的 href」(同 p2 已修用例的口径)。 + // + // 2026-10-17(§14.4):firefox/webkit 上偶发 `waitForURL` 15s 超时,日志显示 + // 「navigated to http://localhost:3000/」——即点击落在了 logo(href="/")而不是导航链接。 + // 两个原因一起修,断言不减: + // ① 缺 `expectHydrated(nav)`:hydration 期间的布局位移会让点击命中错位目标 + // (同文件相邻用例早就有这个前置,这条漏了); + // ② 选择器 `a[href^="/"]` 会把 logo 算进来 ⇒ 改为排除 `href="/"`, + // 让「导航菜单」测的真的是菜单项。 const nav = await primaryNav(page); - const link = nav.locator('a[href^="/"]').first(); + await expectHydrated(nav); + const link = nav.locator('a[href^="/"]:not([href="/"])').first(); await expect(link).toBeVisible(); const href = await link.getAttribute('href'); if (!href) { diff --git a/src/app/api/auth/login/route.ts b/src/app/api/auth/login/route.ts index cf82708..8d0bc52 100644 --- a/src/app/api/auth/login/route.ts +++ b/src/app/api/auth/login/route.ts @@ -4,6 +4,7 @@ import { verifyPassword, generateTokens, setTokenCookie } from '@/lib/auth'; import { success, unauthorized, validationError, internalError } from '@/lib/api-response'; import { getClientIp } from '@/lib/client-ip'; import { createMemoryRateLimiter } from '@/lib/rate-limit'; +import { readRateLimitMax } from '@/lib/rate-limit-config'; // 与 hashPassword 的 cost factor(10) 一致的固定哑元哈希:用户不存在时也要吃掉一次完整 bcrypt, // 否则「未知账号」比「已禁用账号」快一个哈希量,构成可枚举用户名与口令的时间侧信道(B-2)。 @@ -19,7 +20,7 @@ const TIMING_EQUALIZER_HASH = '$2b$10$VUixc994Qoz7jP1YEF2v/Oo9wc0uojx05w0cQIbcMo // 表现为「单跑绿、全量红」(§14 的 UJ-03/06/07 就是这个,而不是登录逻辑坏了)。 // 因此只在 e2e harness 里把上限抬到几百次;判定路径与默认值由 // `route.test.ts` 的双向用例钉住(未设环境变量 ⇒ 10;设了 ⇒ 取该值)。 -export const LOGIN_RATE_LIMIT_MAX = Number(process.env.LOGIN_RATE_LIMIT_MAX ?? 10); +export const LOGIN_RATE_LIMIT_MAX = readRateLimitMax('LOGIN_RATE_LIMIT_MAX', 10); export const loginRateLimiter = createMemoryRateLimiter({ windowMs: 15 * 60 * 1000, diff --git a/src/app/api/contact/route.ts b/src/app/api/contact/route.ts index 6ec4c39..97d3bcd 100644 --- a/src/app/api/contact/route.ts +++ b/src/app/api/contact/route.ts @@ -3,6 +3,7 @@ import { z } from "zod"; import { COMPANY_INFO } from "@/lib/constants"; import { getClientIp } from "@/lib/client-ip"; import { createMemoryRateLimiter } from "@/lib/rate-limit"; +import { readRateLimitMax } from "@/lib/rate-limit-config"; const contactSchema = z.object({ name: z @@ -22,9 +23,13 @@ const contactSchema = z.object({ }); // 简单内存限流:按 IP 地址限制提交频率(复用 src/lib/rate-limit,与登录端点同一实现) +// 额度可按环境覆盖(默认仍 5 次/小时/IP,生产姿态不变): +// E2E 的 server 跨整轮用例存活,全量跑里第 6 次提交必得 429(与登录同族,§14.4)。 +export const CONTACT_RATE_LIMIT_MAX = readRateLimitMax('CONTACT_RATE_LIMIT_MAX', 5); + const contactRateLimiter = createMemoryRateLimiter({ windowMs: 60 * 60 * 1000, // 1 小时 - max: 5, + max: CONTACT_RATE_LIMIT_MAX, }); function sanitizeInput(input: string): string { diff --git a/src/lib/rate-limit-config.test.ts b/src/lib/rate-limit-config.test.ts new file mode 100644 index 0000000..3f1e990 --- /dev/null +++ b/src/lib/rate-limit-config.test.ts @@ -0,0 +1,33 @@ +import { describe, it, expect, jest, afterEach } from '@jest/globals'; +import { readRateLimitMax } from './rate-limit-config'; + +/** + * 限流额度读取的判据用例。重点不是"能读数字",而是三条容易静默失效的路径: + * 未设置 ⇒ 默认;非法值 ⇒ 默认且**留痕**;合法值 ⇒ 生效(含取整)。 + */ +describe('readRateLimitMax', () => { + const originalWarn = console.warn; + afterEach(() => { + console.warn = originalWarn; + }); + + it('未设置 / 空串 ⇒ 默认值(生产姿态)', () => { + expect(readRateLimitMax('X_LIMIT', 10, {})).toBe(10); + expect(readRateLimitMax('X_LIMIT', 5, { X_LIMIT: '' })).toBe(5); + }); + + it('合法值 ⇒ 取该值并向下取整', () => { + expect(readRateLimitMax('X_LIMIT', 10, { X_LIMIT: '500' })).toBe(500); + expect(readRateLimitMax('X_LIMIT', 10, { X_LIMIT: '7.9' })).toBe(7); + }); + + it('非法值(0 / 负数 / 非数字)⇒ 退回默认,且必须留痕(不许静默采纳也不许静默忽略)', () => { + const spy = jest.fn(); + console.warn = spy as never; + expect(readRateLimitMax('X_LIMIT', 10, { X_LIMIT: '0' })).toBe(10); + expect(readRateLimitMax('X_LIMIT', 10, { X_LIMIT: '-3' })).toBe(10); + expect(readRateLimitMax('X_LIMIT', 10, { X_LIMIT: 'abc' })).toBe(10); + expect(spy).toHaveBeenCalledTimes(3); + expect(String(spy.mock.calls[0]?.[0])).toContain('[rate-limit-config]'); + }); +}); diff --git a/src/lib/rate-limit-config.ts b/src/lib/rate-limit-config.ts new file mode 100644 index 0000000..2b12f8e --- /dev/null +++ b/src/lib/rate-limit-config.ts @@ -0,0 +1,27 @@ +/** + * 限流额度的环境变量读取(2026-10-17)。 + * + * 为什么需要:登录与联系表单的限流都是**进程内内存桶**,而 E2E 的 dev/standalone server + * 跨整轮用例存活 ⇒ 全量功能 E2E 里第 6 次联系提交、第 11 次登录必然 429。 + * 这是「单跑绿、全量红」的结构性原因(§14.3/§14.4),不是限流逻辑坏了。 + * + * 原则:**默认值不变**(生产姿态一字不动),只在 e2e harness 显式抬高; + * 非法的覆盖值不静默采用 —— 退回默认并打一条可 grep 的 warn, + * 否则「写错变量名」会表现成「限流没生效」,那是更难查的一类。 + */ +export function readRateLimitMax( + envName: string, + fallback: number, + env: Record = process.env, +): number { + const raw = env[envName]; + if (raw === undefined || raw === '') return fallback; + const n = Number(raw); + if (!Number.isFinite(n) || n <= 0) { + console.warn( + `[rate-limit-config] ${envName}="${raw}" 不是正数 ⇒ 退回默认 ${fallback}(没静默采用非法值)`, + ); + return fallback; + } + return Math.floor(n); +}