start-plan-followup.test.mjs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import assert from "node:assert/strict";
  2. import { mkdir } from "node:fs/promises";
  3. import { join } from "node:path";
  4. import { tmpdir } from "node:os";
  5. import { after, before, test } from "node:test";
  6. import { fileURLToPath } from "node:url";
  7. import { createServer } from "vite";
  8. import react from "@vitejs/plugin-react";
  9. import tailwindcss from "@tailwindcss/vite";
  10. import { chromium } from "playwright-core";
  11. const root = fileURLToPath(new URL("../../../../../../", import.meta.url));
  12. const entry = "/packages/ui/test/browser/manual-review/pending/start-plan-followup.fixture.tsx";
  13. let server;
  14. let browser;
  15. let origin;
  16. before(async () => {
  17. server = await createServer({
  18. root,
  19. configFile: false,
  20. plugins: [
  21. react(),
  22. tailwindcss(),
  23. {
  24. name: "start-plan-followup-fixture",
  25. configureServer(vite) {
  26. vite.middlewares.use(async (req, res, next) => {
  27. if (!req.url?.startsWith("/fixture?")) return next();
  28. res.setHeader("Content-Type", "text/html");
  29. res.end(
  30. await vite.transformIndexHtml(
  31. req.url,
  32. `<!doctype html><html><head><meta name="viewport" content="width=device-width, initial-scale=1" /></head><body><div id="root"></div><script type="module" src="${entry}"></script></body></html>`,
  33. ),
  34. );
  35. });
  36. },
  37. },
  38. ],
  39. resolve: { alias: { "@": `${root}packages/ui/src` } },
  40. optimizeDeps: { entries: [entry.slice(1)] },
  41. server: { host: "127.0.0.1", port: 0 },
  42. });
  43. await server.listen();
  44. origin = server.resolvedUrls.local[0];
  45. browser = await chromium.launch(
  46. process.env.CHROME_EXECUTABLE_PATH
  47. ? { executablePath: process.env.CHROME_EXECUTABLE_PATH }
  48. : {},
  49. );
  50. });
  51. after(async () => {
  52. await browser?.close();
  53. await server?.close();
  54. });
  55. const artifacts = join(tmpdir(), "zcode-start156-browser");
  56. for (const [width, locale, theme] of [
  57. [390, "zh-CN", "light"],
  58. [390, "en-US", "dark"],
  59. [1200, "zh-CN", "dark"],
  60. [1200, "en-US", "light"],
  61. ]) {
  62. for (const family of ["bigmodel", "zai"]) {
  63. for (const state of [
  64. "disconnected",
  65. "notPurchased",
  66. "checking",
  67. "purchased",
  68. "pending",
  69. "empty",
  70. "unavailable",
  71. "expired",
  72. ]) {
  73. test(`SF-05/08/10/11 ${family}/${state}/${width}/${locale}/${theme}`, async () => {
  74. await mkdir(artifacts, { recursive: true });
  75. const page = await browser.newPage({ viewport: { width, height: 900 } });
  76. const errors = [];
  77. page.on("pageerror", (error) => errors.push(String(error)));
  78. try {
  79. const query = new URLSearchParams({ family, state, locale, theme });
  80. await page.goto(`${origin}fixture?${query}`);
  81. const trigger = page.getByRole("combobox");
  82. await trigger.waitFor();
  83. assert.equal(await trigger.getAttribute("data-placeholder"), null);
  84. assert.match(await trigger.innerText(), /InternationalResearch/);
  85. assert.ok(
  86. (await page.getByTestId("model-label").innerText()).includes(
  87. locale === "zh-CN" ? "GLM-5.3(团队套餐)" : "GLM-5.3(Team Plan)",
  88. ),
  89. );
  90. if (["disconnected", "unavailable", "expired"].includes(state)) {
  91. const name =
  92. state === "unavailable"
  93. ? locale === "zh-CN"
  94. ? "重试"
  95. : "Retry"
  96. : state === "expired"
  97. ? locale === "zh-CN"
  98. ? "重新登录"
  99. : "Sign in again"
  100. : locale === "zh-CN"
  101. ? "登录"
  102. : "Log in";
  103. await page.getByRole("button", { name, exact: true }).click();
  104. assert.equal(await page.getByTestId("actions").innerText(), "1");
  105. }
  106. if (state === "notPurchased")
  107. assert.ok(
  108. (await page.locator("main").innerText()).includes(
  109. locale === "zh-CN" ? "暂无可用体验套餐" : "No available Start Plan",
  110. ),
  111. );
  112. if (state === "pending")
  113. assert.match(
  114. await page.locator("main").innerText(),
  115. locale === "zh-CN" ? /待生效/ : /Pending/,
  116. );
  117. if (state === "empty") assert.match(await page.locator("main").innerText(), /0%/);
  118. const bounds = await page.evaluate(() => ({
  119. width: innerWidth,
  120. scroll: document.documentElement.scrollWidth,
  121. }));
  122. assert.ok(bounds.scroll <= bounds.width, JSON.stringify(bounds));
  123. await page.screenshot({
  124. path: join(artifacts, `${family}-${state}-${width}-${locale}-${theme}.png`),
  125. fullPage: true,
  126. });
  127. assert.deepEqual(errors, []);
  128. } finally {
  129. await page.close();
  130. }
  131. });
  132. }
  133. }
  134. }