share-not-found.mjs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // SHARE-NF01:先启动 pnpm dev:web-share:mock,再运行本文件;仅使用开发态 fixture。
  2. import assert from "node:assert/strict";
  3. import { chromium } from "playwright-core";
  4. const baseUrl = process.env.SHARE_E2E_BASE_URL || "http://localhost:5173";
  5. const browser = await chromium.launch({ channel: "chrome", headless: true });
  6. try {
  7. for (const locale of ["cn/share", "share"]) {
  8. for (const width of [320, 1280]) {
  9. for (const theme of ["zai-light", "zai-dark"]) {
  10. const context = await browser.newContext({ viewport: { width, height: 800 } });
  11. await context.addInitScript((value) => localStorage.setItem("zcode-theme", value), theme);
  12. const page = await context.newPage();
  13. const chinese = locale === "cn/share";
  14. const home = page.getByRole("link", {
  15. name: chinese ? "回到首页" : "Back to home",
  16. exact: true,
  17. });
  18. await page.goto(`${baseUrl}/${locale}/mock-not-found`);
  19. await page
  20. .getByRole("heading", {
  21. name: chinese ? "登录后查看分享" : "Sign in to view this share",
  22. exact: true,
  23. })
  24. .waitFor();
  25. assert.equal(await home.count(), 0);
  26. await page.locator('[data-share-login-provider="zai"]').click();
  27. await page
  28. .getByRole("heading", {
  29. name: chinese ? "找不到分享内容" : "Share not found",
  30. exact: true,
  31. })
  32. .waitFor();
  33. await page
  34. .getByText(
  35. chinese
  36. ? "Z.ai 与 BigModel 的账号数据不互通。"
  37. : "Z.ai and BigModel do not share account data.",
  38. { exact: false },
  39. )
  40. .waitFor();
  41. await home.waitFor();
  42. assert.equal(await home.getAttribute("href"), "https://zcode.z.ai");
  43. assert.equal(await home.getAttribute("target"), null);
  44. assert.equal(
  45. await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth),
  46. true,
  47. );
  48. const retry = page.getByRole("button", {
  49. name: chinese ? "重试" : "Try again",
  50. exact: true,
  51. });
  52. // 清除模拟登录态后重试必须回到登录页,证明按钮实际重新读取 token 并请求。
  53. await page.evaluate(() => sessionStorage.removeItem("zcode:share:mock-auth"));
  54. await retry.click();
  55. await page.locator('[data-share-login-provider="bigmodel"]').waitFor();
  56. await page.locator('[data-share-login-provider="bigmodel"]').click();
  57. await home.waitFor();
  58. await page.route("https://zcode.z.ai/**", (route) =>
  59. route.fulfill({ contentType: "text/html", body: "<h1>Home</h1>" }),
  60. );
  61. await home.click();
  62. await page.waitForURL("https://zcode.z.ai/");
  63. assert.equal(context.pages().length, 1);
  64. await page.goto(`${baseUrl}/${locale}/mock-expired`);
  65. await page
  66. .getByRole("heading", { name: chinese ? "分享已过期" : "Share expired", exact: true })
  67. .waitFor();
  68. assert.equal(await home.count(), 0);
  69. await context.close();
  70. console.log(`PASS SHARE-NF01 ${locale} ${width} ${theme}`);
  71. }
  72. }
  73. }
  74. } finally {
  75. await browser.close();
  76. }