|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import enMessages from "../messages/en.json"; |
| 3 | +import zhMessages from "../messages/zh.json"; |
| 4 | +import { resolveErrorKey } from "../app/components/LoginErrorNotice"; |
| 5 | + |
| 6 | +// 后端 OAuthController 实际会重定向的全部 error code。新增 code 时两边一起加, |
| 7 | +// 否则用户只会看到通用的"请重试"(对 oauth_provider 这类重试无用的错误是误导)。 |
| 8 | +const BACKEND_ERROR_CODES = [ |
| 9 | + "discord_canary", |
| 10 | + "oauth_failed", |
| 11 | + "oauth_state", |
| 12 | + "oauth_provider", |
| 13 | +]; |
| 14 | + |
| 15 | +describe("resolveErrorKey", () => { |
| 16 | + it("没有 error 参数时不显示提示", () => { |
| 17 | + expect(resolveErrorKey(null)).toBeNull(); |
| 18 | + expect(resolveErrorKey("")).toBeNull(); |
| 19 | + }); |
| 20 | + |
| 21 | + it("后端每个 error code 都有专属文案,不塌缩成通用提示", () => { |
| 22 | + for (const code of BACKEND_ERROR_CODES) { |
| 23 | + const key = resolveErrorKey(code); |
| 24 | + expect(key, `${code} 应有映射`).not.toBeNull(); |
| 25 | + if (code !== "oauth_failed") { |
| 26 | + expect(key, `${code} 不该塌缩成通用文案`).not.toBe("errorGeneric"); |
| 27 | + } |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + // 回归:直接用 error 索引对象会命中原型链,?error=__proto__ 取到 Object.prototype |
| 32 | + // (非 undefined,?? 兜底不触发),渲染非字符串会让整个登录页崩掉。 |
| 33 | + it("原型链上的键不会逃逸,一律落到通用文案", () => { |
| 34 | + for (const key of [ |
| 35 | + "__proto__", |
| 36 | + "constructor", |
| 37 | + "toString", |
| 38 | + "valueOf", |
| 39 | + "hasOwnProperty", |
| 40 | + ]) { |
| 41 | + expect(resolveErrorKey(key), `${key} 必须被兜住`).toBe("errorGeneric"); |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + it("未知 code 落到通用文案", () => { |
| 46 | + expect(resolveErrorKey("wat")).toBe("errorGeneric"); |
| 47 | + }); |
| 48 | + |
| 49 | + it("解析出的 key 在 zh/en 里都存在", () => { |
| 50 | + for (const code of [...BACKEND_ERROR_CODES, "__proto__", "wat"]) { |
| 51 | + const key = resolveErrorKey(code)!; |
| 52 | + expect(zhMessages.login, `zh 缺 ${key}`).toHaveProperty(key); |
| 53 | + expect(enMessages.login, `en 缺 ${key}`).toHaveProperty(key); |
| 54 | + } |
| 55 | + }); |
| 56 | +}); |
0 commit comments