jwt-expiry-reauthentication.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { readFile } from "node:fs/promises";
  2. import { ZAI_PROVIDER_ID } from "@zcode/shared";
  3. import { TID_LOGIN_REGION_SWITCH, TID_OAUTH_LOGIN_BUTTON } from "@zcode/shared/test-ids";
  4. import { clearAppData, getE2EAppDataPaths, waitForTestIdByDom } from "#e2e/helpers/desktop-app";
  5. const EXPIRED_SESSION_CREDENTIAL_KEYS = [
  6. "oauth:active_provider",
  7. `oauth:${ZAI_PROVIDER_ID}:access_token`,
  8. `oauth:${ZAI_PROVIDER_ID}:refresh_token`,
  9. `oauth:${ZAI_PROVIDER_ID}:user_info`,
  10. "zcodejwttoken",
  11. ] as const;
  12. describe("ZCode JWT 过期后重新认证", () => {
  13. after(async () => {
  14. await browser.electron.restoreAllMocks();
  15. await clearAppData();
  16. });
  17. it("冷启动先清理过期登录态,提示用户后进入完整登录流程", async () => {
  18. const alert = await waitForExpiredLoginAlert();
  19. expect(alert.title).toMatch(/登录已过期|Your session has expired/u);
  20. expect(alert.description).toMatch(/请重新登录|sign in again/u);
  21. // Bug 回归边界:认证事实必须在提示框确认前清理,否则用户停留在弹窗时仍会
  22. // 携带过期 JWT 发请求,复现“看似登录但无法恢复”的分裂状态。
  23. await waitForExpiredSessionCredentialsCleanup();
  24. await clickExpiredLoginAlertAction();
  25. await waitForTestIdByDom(TID_OAUTH_LOGIN_BUTTON, {
  26. timeout: 20000,
  27. timeoutMsg: "确认登录过期提示后没有展示登录入口",
  28. });
  29. await waitForTestIdByDom(TID_LOGIN_REGION_SWITCH, {
  30. timeout: 20000,
  31. timeoutMsg: "确认登录过期提示后没有展示中国/全球切换入口",
  32. });
  33. await expectExpiredAlertClosed();
  34. });
  35. });
  36. async function waitForExpiredLoginAlert(): Promise<{ title: string; description: string }> {
  37. await browser.waitUntil(
  38. async () => {
  39. const snapshot = await readAlertSnapshot();
  40. return /登录已过期|Your session has expired/u.test(snapshot.title);
  41. },
  42. {
  43. timeout: 60000,
  44. timeoutMsg: "已过期 JWT 冷启动后没有展示登录过期提示",
  45. },
  46. );
  47. return readAlertSnapshot();
  48. }
  49. async function readAlertSnapshot(): Promise<{ title: string; description: string }> {
  50. return (await browser.execute(() => {
  51. const content = document.querySelector<HTMLElement>('[data-slot="alert-dialog-content"]');
  52. return {
  53. title:
  54. content
  55. ?.querySelector<HTMLElement>('[data-slot="alert-dialog-title"]')
  56. ?.textContent?.trim() ?? "",
  57. description:
  58. content
  59. ?.querySelector<HTMLElement>('[data-slot="alert-dialog-description"]')
  60. ?.textContent?.trim() ?? "",
  61. };
  62. })) as { title: string; description: string };
  63. }
  64. async function clickExpiredLoginAlertAction() {
  65. await browser.execute(() => {
  66. const button = document.querySelector<HTMLButtonElement>(
  67. '[data-slot="alert-dialog-content"] button',
  68. );
  69. if (!button) {
  70. throw new Error("登录过期提示缺少重新登录按钮");
  71. }
  72. button.click();
  73. });
  74. }
  75. async function waitForExpiredSessionCredentialsCleanup() {
  76. try {
  77. await browser.waitUntil(
  78. async () => {
  79. const credentials = await readCredentials();
  80. return EXPIRED_SESSION_CREDENTIAL_KEYS.every((key) => !(key in credentials));
  81. },
  82. {
  83. timeout: 10000,
  84. timeoutMsg: "登录过期提示确认前认证凭据没有完成清理",
  85. },
  86. );
  87. } catch (error) {
  88. const credentials = await readCredentials().catch(() => null);
  89. const keys = credentials ? Object.keys(credentials).sort().join(", ") : "<missing>";
  90. throw new Error(`过期认证凭据没有被清理,当前凭据 key: ${keys}`, {
  91. cause: error,
  92. });
  93. }
  94. }
  95. async function expectExpiredAlertClosed() {
  96. await browser.waitUntil(
  97. async () => {
  98. return (await browser.execute(() => {
  99. return !document.querySelector('[data-slot="alert-dialog-content"]');
  100. })) as boolean;
  101. },
  102. {
  103. timeout: 5000,
  104. timeoutMsg: "进入登录页后登录过期提示没有关闭",
  105. },
  106. );
  107. }
  108. async function readCredentials(): Promise<Record<string, string>> {
  109. const { credentialsFile } = getE2EAppDataPaths();
  110. return JSON.parse(await readFile(credentialsFile, "utf-8")) as Record<string, string>;
  111. }