plugin-localization-creator.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import { setRendererViewport, clearRendererViewport } from "#e2e/helpers/renderer-viewport";
  2. import { clearAppData, setInputValueByTestIdDom } from "#e2e/helpers/desktop-app";
  3. import { resolveE2EToolPath } from "#e2e/helpers/e2e-runtime-paths";
  4. import {
  5. getV4ComposerText,
  6. isV4ComposerFocused,
  7. waitForV4Pane,
  8. } from "#e2e/helpers/v4-conversation";
  9. import {
  10. PLUGIN_LIFECYCLE_ID,
  11. PLUGIN_LIFECYCLE_MARKETPLACE_ID,
  12. addLifecycleMarketplaceThroughUi,
  13. assertPluginStoreFitsViewport,
  14. capturePluginManualReview,
  15. clickPluginControl,
  16. createPluginLifecycleFixture,
  17. getPluginControlText,
  18. getPluginOverview,
  19. hasPluginControl,
  20. installPluginThroughUi,
  21. openPluginManagementSettings,
  22. openPluginSettings,
  23. requirePluginManualReview,
  24. setPluginPresentation,
  25. waitForPluginControl,
  26. waitUntilPluginOverview,
  27. } from "#e2e/helpers/plugin-management-lifecycle";
  28. describe("PLM-LC-018/019/020 本地化与插件创建入口", () => {
  29. after(async () => {
  30. await clearRendererViewport();
  31. await browser.electron.restoreAllMocks();
  32. await clearAppData();
  33. });
  34. it("两处添加菜单分别创建真实 skill 草稿并保留来源添加与中文管理搜索", async function () {
  35. this.timeout(300000);
  36. requirePluginManualReview();
  37. const fixture = await createPluginLifecycleFixture();
  38. await openPluginSettings();
  39. await setPluginPresentation({ locale: "zh-CN", theme: "zai-light", width: 1440, height: 800 });
  40. await setRendererViewport(1440, 1000);
  41. await addLifecycleMarketplaceThroughUi(fixture);
  42. const countMarketCards = () =>
  43. browser.execute(
  44. (marketplace) =>
  45. [...document.querySelectorAll<HTMLElement>('[data-testid="plugin-store-card"]')].filter(
  46. (card) => card.dataset.pluginId?.endsWith(`@${marketplace}`),
  47. ).length,
  48. PLUGIN_LIFECYCLE_MARKETPLACE_ID,
  49. );
  50. expect(await countMarketCards()).toBe(8);
  51. await capturePluginManualReview("PLM-LC-001", "all-plugins-expanded");
  52. const groupKey = `marketplace:${PLUGIN_LIFECYCLE_MARKETPLACE_ID}`;
  53. await clickPluginControl("plugin-store-group-toggle", { groupKey });
  54. expect(await countMarketCards()).toBe(6);
  55. await clickPluginControl("plugin-store-group-toggle", { groupKey });
  56. expect(await countMarketCards()).toBe(8);
  57. await installPluginThroughUi(PLUGIN_LIFECYCLE_ID);
  58. for (const query of ["shengmingzhouqi", "SMZQ"]) {
  59. await setInputValueByTestIdDom("plugin-store-search", query);
  60. await waitForPluginControl("plugin-store-card", { pluginId: PLUGIN_LIFECYCLE_ID });
  61. }
  62. await setInputValueByTestIdDom("plugin-store-search", "");
  63. await openAddMenu("plugin-store-create");
  64. expect(await getPluginControlText("plugin-add-menu")).toContain("创建插件");
  65. expect(await getPluginControlText("plugin-add-menu")).not.toContain("录制");
  66. await clickPluginControl("plugin-create-menu-item");
  67. await expectCreatorDraft();
  68. await capturePluginManualReview("PLM-LC-019", "store-creator-draft");
  69. await openPluginManagementSettings();
  70. await clickPluginControl("settings-back-button");
  71. await expectCreatorDraft(false);
  72. await openPluginManagementSettings();
  73. await waitForPluginControl("plugin-settings-plugin-row", { pluginId: PLUGIN_LIFECYCLE_ID });
  74. expect(
  75. await getPluginControlText("plugin-settings-plugin-row", { pluginId: PLUGIN_LIFECYCLE_ID }),
  76. ).toContain("生命周期测试插件");
  77. for (const query of ["生命周期测试插件", "shengmingzhouqi", "smzq"]) {
  78. await setInputValueByTestIdDom("plugin-settings-search", query);
  79. await waitForPluginControl("plugin-settings-plugin-row", { pluginId: PLUGIN_LIFECYCLE_ID });
  80. }
  81. await setInputValueByTestIdDom("plugin-settings-search", "");
  82. await openAddMenu("plugin-settings-add");
  83. await clickPluginControl("plugin-create-menu-item");
  84. await expectCreatorDraft();
  85. await openPluginManagementSettings();
  86. await openAddMenu("plugin-settings-add");
  87. await clickPluginControl("plugin-store-add-source-menu-item");
  88. await waitForPluginControl("plugin-store-add-source-dialog");
  89. expect(
  90. await browser.execute(() => document.querySelector('[data-testid="settings-page"]') === null),
  91. ).toBe(true);
  92. const invalidSource = resolveE2EToolPath("plugin-creator", "missingmarketplace".repeat(8));
  93. await setInputValueByTestIdDom("plugin-store-add-source-input", invalidSource);
  94. await clickPluginControl("plugin-store-add-source-risk-acknowledgement");
  95. await clickPluginControl("plugin-store-add-source-submit");
  96. await waitForPluginControl("plugin-store-add-source-error");
  97. expect(
  98. await browser.execute(
  99. () =>
  100. document.querySelector<HTMLInputElement>('[data-testid="plugin-store-add-source-input"]')
  101. ?.value,
  102. ),
  103. ).toBe(invalidSource);
  104. await browser.keys("Escape");
  105. await clearRendererViewport();
  106. await setPluginPresentation({ locale: "en-US", theme: "zai-dark", width: 480, height: 800 });
  107. await setRendererViewport(390, 844);
  108. // 添加来源失败后仍停留在默认公开分段;本地 fixture 属于个人来源,搜索只覆盖当前分段。
  109. await clickPluginControl("plugin-store-segment-personal");
  110. await setInputValueByTestIdDom("plugin-store-search", "smzq");
  111. await waitForPluginControl("plugin-store-card", { pluginId: PLUGIN_LIFECYCLE_ID });
  112. await openAddMenu("plugin-store-create");
  113. const text = await getPluginControlText("plugin-add-menu");
  114. expect(text).toContain("Create plugin");
  115. expect(text).toContain("Add plugin marketplace");
  116. await capturePluginManualReview("PLM-LC-020", "en-dark-narrow-add-menu");
  117. await assertPluginStoreFitsViewport();
  118. await browser.keys("Escape");
  119. for (const presentation of [
  120. { locale: "zh-CN" as const, theme: "zai-light" as const, width: 390 },
  121. { locale: "en-US" as const, theme: "zai-dark" as const, width: 1280 },
  122. { locale: "zh-CN" as const, theme: "zai-light" as const, width: 1280 },
  123. ]) {
  124. await clearRendererViewport();
  125. await setPluginPresentation({
  126. ...presentation,
  127. width: Math.max(480, presentation.width),
  128. height: 844,
  129. });
  130. await setRendererViewport(presentation.width, 844);
  131. // 长路径必须完整保留并换行,不能用截断或隐藏溢出来通过窄屏验收。
  132. expect(await getPluginControlText("plugin-store-error")).toContain(invalidSource);
  133. await assertPluginStoreFitsViewport();
  134. await capturePluginManualReview(
  135. "PLM-LC-020",
  136. `${presentation.locale}-${presentation.theme}-${presentation.width}-error`,
  137. );
  138. }
  139. });
  140. it("非官方市场必须确认风险才能添加,关闭后重新确认", async function () {
  141. this.timeout(120000);
  142. requirePluginManualReview();
  143. const fixture = await createPluginLifecycleFixture();
  144. await openPluginSettings();
  145. await setPluginPresentation({ locale: "zh-CN", theme: "zai-light", width: 1200, height: 800 });
  146. await openAddMenu("plugin-store-create");
  147. await clickPluginControl("plugin-store-add-source-menu-item");
  148. await waitForPluginControl("plugin-store-add-source-dialog");
  149. await setInputValueByTestIdDom("plugin-store-add-source-input", fixture.marketplaceRoot);
  150. const acknowledgement = browser.$(
  151. '[data-testid="plugin-store-add-source-risk-acknowledgement"]',
  152. );
  153. const submit = browser.$('[data-testid="plugin-store-add-source-submit"]');
  154. expect(await acknowledgement.getAttribute("aria-checked")).toBe("false");
  155. expect(await submit.isEnabled()).toBe(false);
  156. expect(await getPluginControlText("plugin-store-add-source-risk-warning")).toContain(
  157. "恶意插件",
  158. );
  159. await browser.$('[data-testid="plugin-store-add-source-input"]').click();
  160. await browser.keys("Enter");
  161. expect(
  162. (await getPluginOverview()).marketplaces.some(
  163. (marketplace) => marketplace.id === PLUGIN_LIFECYCLE_MARKETPLACE_ID,
  164. ),
  165. ).toBe(false);
  166. await clickPluginControl("plugin-store-add-source-risk-acknowledgement");
  167. expect(await submit.isEnabled()).toBe(true);
  168. await capturePluginManualReview("PLM-LC-020", "zh-light-risk-confirmation");
  169. await clickPluginControl("plugin-store-add-source-submit");
  170. await waitUntilPluginOverview(
  171. (overview) =>
  172. overview.marketplaces.some(
  173. (marketplace) => marketplace.id === PLUGIN_LIFECYCLE_MARKETPLACE_ID,
  174. ),
  175. "确认风险后未添加市场",
  176. );
  177. await browser.waitUntil(
  178. async () => !(await hasPluginControl("plugin-store-add-source-dialog")),
  179. );
  180. await openAddMenu("plugin-store-create");
  181. await clickPluginControl("plugin-store-add-source-menu-item");
  182. await waitForPluginControl("plugin-store-add-source-dialog");
  183. expect(await acknowledgement.getAttribute("aria-checked")).toBe("false");
  184. expect(await submit.isEnabled()).toBe(false);
  185. await clickPluginControl("plugin-store-add-source-risk-acknowledgement");
  186. await browser.keys("Escape");
  187. await openAddMenu("plugin-store-create");
  188. await clickPluginControl("plugin-store-add-source-menu-item");
  189. await waitForPluginControl("plugin-store-add-source-dialog");
  190. expect(await acknowledgement.getAttribute("aria-checked")).toBe("false");
  191. });
  192. });
  193. async function openAddMenu(testId: string) {
  194. await waitForPluginControl(testId);
  195. await browser.execute((id) => {
  196. document
  197. .querySelector<HTMLElement>(`[data-testid="${id}"]`)
  198. ?.dispatchEvent(
  199. new PointerEvent("pointerdown", { bubbles: true, button: 0, pointerType: "mouse" }),
  200. );
  201. }, testId);
  202. await waitForPluginControl("plugin-add-menu");
  203. expect(
  204. await browser.execute(
  205. () => document.querySelectorAll('[data-testid="plugin-add-menu"] [role="menuitem"]').length,
  206. ),
  207. ).toBe(2);
  208. }
  209. async function expectCreatorDraft(checkFocus = true) {
  210. // 旧草稿仍挂载在设置页背后,必须先等创建请求真正关闭设置,避免读到上次草稿。
  211. await browser.waitUntil(
  212. async () => browser.execute(() => !document.querySelector('[data-testid="settings-page"]')),
  213. { timeout: 30000 },
  214. );
  215. await waitForV4Pane((s) => s.sessionId === "draft", "创建插件未进入新草稿");
  216. await browser.waitUntil(
  217. async () => (await getV4ComposerText())?.includes("$plugin-creator") === true,
  218. { timeout: 30000 },
  219. );
  220. const text = await getV4ComposerText();
  221. expect(text).toContain("/SKILL.md)");
  222. expect(text?.endsWith(" ")).toBe(true);
  223. if (checkFocus)
  224. await browser.waitUntil(isV4ComposerFocused, {
  225. timeout: 15000,
  226. timeoutMsg: "创建插件后输入框未获得焦点",
  227. });
  228. await waitForV4Pane(
  229. (snapshot) => snapshot.sessionId === "draft" && snapshot.rowCount === 0 && !snapshot.canStop,
  230. "创建入口自动发送了任务",
  231. );
  232. }