jwt-expiry-reauthentication.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { readFile } from "node:fs/promises";
  2. import { ZAI_PROVIDER_ID } from "@zcode/shared";
  3. import { TID_OAUTH_LOGIN_BUTTON, testId } 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(testId(TID_OAUTH_LOGIN_BUTTON, ZAI_PROVIDER_ID), {
  26. timeout: 20000,
  27. timeoutMsg: "确认登录过期提示后没有展示 Z.ai 登录入口",
  28. });
  29. await waitForTestIdByDom(TID_OAUTH_LOGIN_BUTTON, {
  30. timeout: 20000,
  31. timeoutMsg: "确认登录过期提示后没有展示 BigModel 登录入口",
  32. });
  33. await expectBigModelLoginButton();
  34. await expectExpiredAlertClosed();
  35. });
  36. });
  37. async function waitForExpiredLoginAlert(): Promise<{ title: string; description: string }> {
  38. await browser.waitUntil(
  39. async () => {
  40. const snapshot = await readAlertSnapshot();
  41. return /登录已过期|Your session has expired/u.test(snapshot.title);
  42. },
  43. {
  44. timeout: 60000,
  45. timeoutMsg: "已过期 JWT 冷启动后没有展示登录过期提示",
  46. },
  47. );
  48. return readAlertSnapshot();
  49. }
  50. async function readAlertSnapshot(): Promise<{ title: string; description: string }> {
  51. return (await browser.execute(() => {
  52. const content = document.querySelector<HTMLElement>('[data-slot="alert-dialog-content"]');
  53. return {
  54. title:
  55. content
  56. ?.querySelector<HTMLElement>('[data-slot="alert-dialog-title"]')
  57. ?.textContent?.trim() ?? "",
  58. description:
  59. content
  60. ?.querySelector<HTMLElement>('[data-slot="alert-dialog-description"]')
  61. ?.textContent?.trim() ?? "",
  62. };
  63. })) as { title: string; description: string };
  64. }
  65. async function clickExpiredLoginAlertAction() {
  66. await browser.execute(() => {
  67. const button = document.querySelector<HTMLButtonElement>(
  68. '[data-slot="alert-dialog-content"] button',
  69. );
  70. if (!button) {
  71. throw new Error("登录过期提示缺少重新登录按钮");
  72. }
  73. button.click();
  74. });
  75. }
  76. async function waitForExpiredSessionCredentialsCleanup() {
  77. try {
  78. await browser.waitUntil(
  79. async () => {
  80. const credentials = await readCredentials();
  81. return EXPIRED_SESSION_CREDENTIAL_KEYS.every((key) => !(key in credentials));
  82. },
  83. {
  84. timeout: 10000,
  85. timeoutMsg: "登录过期提示确认前认证凭据没有完成清理",
  86. },
  87. );
  88. } catch (error) {
  89. const credentials = await readCredentials().catch(() => null);
  90. const keys = credentials ? Object.keys(credentials).sort().join(", ") : "<missing>";
  91. throw new Error(`过期认证凭据没有被清理,当前凭据 key: ${keys}`, {
  92. cause: error,
  93. });
  94. }
  95. }
  96. async function expectBigModelLoginButton() {
  97. const text = (await browser.execute((buttonId) => {
  98. return (
  99. document.querySelector<HTMLElement>(`[data-testid="${buttonId}"]`)?.textContent?.trim() ?? ""
  100. );
  101. }, TID_OAUTH_LOGIN_BUTTON)) as string;
  102. expect(text).toContain("BigModel");
  103. }
  104. async function expectExpiredAlertClosed() {
  105. await browser.waitUntil(
  106. async () => {
  107. return (await browser.execute(() => {
  108. return !document.querySelector('[data-slot="alert-dialog-content"]');
  109. })) as boolean;
  110. },
  111. {
  112. timeout: 5000,
  113. timeoutMsg: "进入登录页后登录过期提示没有关闭",
  114. },
  115. );
  116. }
  117. async function readCredentials(): Promise<Record<string, string>> {
  118. const { credentialsFile } = getE2EAppDataPaths();
  119. return JSON.parse(await readFile(credentialsFile, "utf-8")) as Record<string, string>;
  120. }