provider-plan-navigation-labels.test.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { readFile, writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { BUILTIN_MODEL_PROVIDER_IDS } from "@zcode/shared";
  4. import {
  5. TID_MODEL_PROVIDER_NAV_ITEM,
  6. TID_SETTINGS_SECTION_NAV,
  7. TID_TASK_SETTINGS_BUTTON,
  8. testId,
  9. } from "@zcode/shared/test-ids";
  10. import {
  11. clearAppData,
  12. DEFAULT_WORKSPACE,
  13. clickTestIdByDom,
  14. getE2EAppDataPaths,
  15. setCurrentElectronRendererContentSize,
  16. restoreElectronRendererContentSize,
  17. } from "#e2e/helpers/desktop-app";
  18. import { restartIntoWorkspaceRestoringPreferences } from "#e2e/helpers/model-provider-restart-runtime";
  19. import { prepareV4ConversationE2E } from "#e2e/helpers/v4-conversation";
  20. import { skipOccupationOnboardingIfPresent } from "#e2e/helpers/occupation-onboarding";
  21. import { sel } from "#e2e/helpers/selectors";
  22. async function visibleNav(key: string) {
  23. const selector = sel(testId(TID_MODEL_PROVIDER_NAV_ITEM, key));
  24. await browser.waitUntil(
  25. async () => {
  26. for (const item of await $$(selector)) {
  27. if (await item.isDisplayed()) return true;
  28. }
  29. return false;
  30. },
  31. { timeout: 15000, timeoutMsg: `导航未出现:${key}` },
  32. );
  33. // 响应式布局保留隐藏副本,只操作当前可见导航。
  34. for (const item of await $$(selector)) {
  35. if (await item.isDisplayed()) return item;
  36. }
  37. throw new Error(`可见导航已消失:${key}`);
  38. }
  39. describe("套餐导航名称与身份回归", () => {
  40. let originalSize: Awaited<ReturnType<typeof setCurrentElectronRendererContentSize>> | undefined;
  41. afterEach(async () => {
  42. if (originalSize) await restoreElectronRendererContentSize(originalSize);
  43. originalSize = undefined;
  44. });
  45. after(async () => {
  46. await clearAppData();
  47. });
  48. for (const family of ["bigmodel", "zai"] as const) {
  49. for (const locale of ["zh-CN", "en-US"] as const) {
  50. it(`PN-01/02: ${family} ${locale} 导航、详情及窄屏名称一致`, async function () {
  51. this.timeout(180000);
  52. await skipOccupationOnboardingIfPresent();
  53. await prepareV4ConversationE2E({ skipProvider: true });
  54. await restartIntoWorkspaceRestoringPreferences(DEFAULT_WORKSPACE, {
  55. afterElectronProcessExit: async () => {
  56. const file = join(getE2EAppDataPaths().appDataDir, "setting.json");
  57. const settings = JSON.parse(await readFile(file, "utf8"));
  58. await writeFile(
  59. file,
  60. JSON.stringify({
  61. ...settings,
  62. locale,
  63. localePreference: locale,
  64. providerFamilyDomain: family,
  65. providerFamilyDomainMigrated: true,
  66. providerFamilyConnectionSelections: {},
  67. }),
  68. );
  69. },
  70. });
  71. originalSize = await setCurrentElectronRendererContentSize(1280, 800);
  72. await browser.execute(
  73. (theme) => {
  74. const actions = (
  75. window as typeof window & { __testActions?: { setTheme?: (value: string) => void } }
  76. ).__testActions;
  77. if (!actions?.setTheme) throw new Error("missing setTheme action");
  78. actions.setTheme(theme);
  79. },
  80. locale === "zh-CN" ? "dark" : "light",
  81. );
  82. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON);
  83. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "modelProvider"));
  84. const id =
  85. family === "bigmodel"
  86. ? BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan
  87. : BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan;
  88. const entries = [
  89. {
  90. key: `preset:${id}`,
  91. label: family === "bigmodel" ? "BigModel" : "Z.ai",
  92. title: family === "bigmodel" ? "BigModel" : "Z.ai",
  93. },
  94. {
  95. key: `coding-plan:${id}`,
  96. label: "Start Plan",
  97. title: "Start Plan",
  98. },
  99. ];
  100. for (const entry of entries) {
  101. const button = await visibleNav(entry.key);
  102. await expect(button).toHaveAttribute("aria-label", entry.label);
  103. await expect(button).toHaveText(entry.label);
  104. await button.click();
  105. await expect(await visibleNav(entry.key)).toHaveAttribute("aria-selected", "true");
  106. await browser.waitUntil(
  107. async () =>
  108. browser.execute(
  109. (title) =>
  110. Array.from(document.querySelectorAll("h3")).some(
  111. (node) =>
  112. node.textContent?.trim() === title && node.getBoundingClientRect().width > 0,
  113. ),
  114. entry.title,
  115. ),
  116. { timeout: 15000, timeoutMsg: `详情标题错误:${entry.title}` },
  117. );
  118. }
  119. if (process.env.ZCODE_E2E_ARTIFACT_DIR) {
  120. await browser.saveScreenshot(
  121. join(process.env.ZCODE_E2E_ARTIFACT_DIR, `plan-nav-${family}-${locale}-wide.png`),
  122. );
  123. }
  124. await setCurrentElectronRendererContentSize(600, 800);
  125. for (const entry of entries) {
  126. const button = await visibleNav(entry.key);
  127. await expect(button).toHaveAttribute("aria-label", entry.label);
  128. await browser.electron.execute((electron) => {
  129. electron.BrowserWindow.getAllWindows()
  130. .find((window) => window.isVisible())
  131. ?.focus();
  132. });
  133. await $("body").moveTo({ xOffset: -250, yOffset: -350 });
  134. await button.moveTo();
  135. await $('[data-slot="tooltip-content"]').waitForDisplayed({ timeout: 10000 });
  136. await expect($('[data-slot="tooltip-content"]')).toHaveText(entry.label);
  137. await browser.keys("Escape");
  138. }
  139. if (process.env.ZCODE_E2E_ARTIFACT_DIR) {
  140. await browser.saveScreenshot(
  141. join(process.env.ZCODE_E2E_ARTIFACT_DIR, `plan-nav-${family}-${locale}-narrow.png`),
  142. );
  143. }
  144. });
  145. }
  146. }
  147. });