provider-app-icon-cases.mjs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import assert from "node:assert/strict";
  2. import { mkdir } from "node:fs/promises";
  3. import { join } from "node:path";
  4. import { test } from "node:test";
  5. export function registerProviderAppIconCases(getContext) {
  6. for (const [width, locale, theme] of [
  7. [390, "zh-CN", "light"],
  8. [1200, "en-US", "dark"],
  9. ]) {
  10. test(`zai app icon ${width}/${locale}/${theme}`, async () => {
  11. const { browser, origin, artifacts } = getContext();
  12. await mkdir(artifacts, { recursive: true });
  13. const page = await browser.newPage({ viewport: { width, height: 900 } });
  14. const errors = [],
  15. mutations = [];
  16. page.on("pageerror", (error) => errors.push(String(error)));
  17. await page.route("**/mutation", async (route) => {
  18. mutations.push(route.request().postDataJSON());
  19. await route.fulfill({ status: 200, body: "{}" });
  20. });
  21. const assertAppIcon = async (image) => {
  22. await image.waitFor();
  23. assert.match(await image.getAttribute("src"), /model-provider-zai-app\.png(?:\?|$)/);
  24. await image.evaluate((node) => node.decode());
  25. assert.deepEqual(
  26. await image.evaluate((node) => ({
  27. width: node.naturalWidth,
  28. height: node.naturalHeight,
  29. filter: getComputedStyle(node).filter,
  30. })),
  31. { width: 128, height: 128, filter: "none" },
  32. );
  33. const box = await image.boundingBox();
  34. assert.equal(box.width, box.height);
  35. };
  36. try {
  37. await page.goto(`${origin}fixture?${new URLSearchParams({ locale, theme })}`);
  38. for (const id of ["zai-api", "zai-standard-api"]) {
  39. const card = page.getByTestId(`model-provider-template-item-${id}`);
  40. const name = id === "zai-api" ? "Z.ai Coding Plan" : "Z.ai API";
  41. await assertAppIcon(card.locator("img"));
  42. assert.equal((await card.innerText()).trim(), name);
  43. await card.hover();
  44. await page.getByRole("tooltip", { name, exact: true }).waitFor();
  45. await page.keyboard.press("Escape");
  46. await page.mouse.move(0, 0);
  47. await page.getByRole("tooltip", { name, exact: true }).waitFor({ state: "hidden" });
  48. }
  49. await page.screenshot({ path: join(artifacts, `zai-app-templates-${width}.png`) });
  50. assert.equal(mutations.length, 0);
  51. await page.getByTestId("model-provider-template-item-zai-api").click();
  52. await assertAppIcon(page.getByTestId("model-provider-header").locator("img"));
  53. assert.ok((await page.locator('img[src*="model-provider-zai-app.png"]').count()) >= 2);
  54. await page.screenshot({ path: join(artifacts, `zai-app-detail-${width}.png`) });
  55. assert.deepEqual(mutations, [{ action: "create", payload: "zai-api" }]);
  56. assert.deepEqual(errors, []);
  57. } finally {
  58. await page.close();
  59. }
  60. });
  61. }
  62. }