conversation-session-plugin-marketplace-installed-roundtrip.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import { existsSync } from "node:fs";
  2. import { readFile } from "node:fs/promises";
  3. import { join } from "node:path";
  4. import { TID_SETTINGS_BACK_BUTTON } from "@zcode/shared/test-ids";
  5. import {
  6. clearAppData,
  7. clickTestIdByDom,
  8. getE2EAppDataPaths,
  9. waitForDefaultWorkspaceReady,
  10. } from "#e2e/helpers/desktop-app";
  11. import {
  12. addMarketplaceSourceThroughAgent,
  13. clickPluginControl,
  14. createPluginLifecycleFixture,
  15. getPluginList,
  16. hasPluginControl,
  17. installPluginThroughUi,
  18. setPluginEnabledThroughUi,
  19. openInstalledPluginSettingsDetail,
  20. openPluginCard,
  21. openPluginSettings,
  22. openPluginManagementSettings,
  23. PLUGIN_LIFECYCLE_CONFIG_KEY,
  24. PLUGIN_LIFECYCLE_INHERITED_CONFIG_KEY,
  25. PLUGIN_LIFECYCLE_CONFIG_VALUE,
  26. PLUGIN_LIFECYCLE_ID,
  27. waitForPluginCard,
  28. waitForPluginControl,
  29. waitForPluginSettingsScope,
  30. waitUntilPluginOverview,
  31. selectPluginSettingsScope,
  32. } from "#e2e/helpers/plugin-management-lifecycle";
  33. describe("WPL-015 Marketplace installed roundtrip E2E", () => {
  34. after(async () => {
  35. await browser.electron.restoreAllMocks();
  36. await clearAppData();
  37. });
  38. it("returns to chat after global install, then manages User and Workspace configuration", async function () {
  39. this.timeout(240000);
  40. const fixture = await createPluginLifecycleFixture("1.0.0");
  41. const { storageRoot, workspace } = getE2EAppDataPaths();
  42. const userConfigPath = join(storageRoot, "cli", "config.json");
  43. const workspaceConfigPath = join(workspace, ".zcode", "config.json");
  44. await waitForDefaultWorkspaceReady();
  45. await addMarketplaceSourceThroughAgent(fixture.marketplaceRoot);
  46. await waitUntilPluginOverview(
  47. (overview) => overview.availablePlugins.some((plugin) => plugin.id === PLUGIN_LIFECYCLE_ID),
  48. "WPL-015 marketplace fixture did not become available",
  49. 60000,
  50. );
  51. await openPluginSettings();
  52. expect(await hasPluginControl("plugin-store-scope-trigger")).toBe(false);
  53. expect(await hasPluginControl("plugin-store-config")).toBe(false);
  54. await clickPluginControl("plugin-store-segment-personal");
  55. await waitForPluginCard(PLUGIN_LIFECYCLE_ID);
  56. expect(
  57. await hasPluginControl("plugin-store-enabled-switch", { pluginId: PLUGIN_LIFECYCLE_ID }),
  58. ).toBe(false);
  59. await openPluginCard(PLUGIN_LIFECYCLE_ID);
  60. await installPluginThroughUi(PLUGIN_LIFECYCLE_ID);
  61. await clickPluginControl("plugin-store-detail-back");
  62. await waitForPluginControl("plugin-store-enabled-switch", { pluginId: PLUGIN_LIFECYCLE_ID });
  63. await setPluginEnabledThroughUi(PLUGIN_LIFECYCLE_ID, false, "market");
  64. await setPluginEnabledThroughUi(PLUGIN_LIFECYCLE_ID, true, "market");
  65. await clickTestIdByDom("desktop-top-nav-back");
  66. await openPluginManagementSettings();
  67. await waitForPluginSettingsScope("user");
  68. await waitForPluginControl("plugin-settings-plugin-row", {
  69. pluginId: PLUGIN_LIFECYCLE_ID,
  70. });
  71. expect(await readInstalledPluginRowState(PLUGIN_LIFECYCLE_ID)).toEqual({
  72. checked: true,
  73. source: null,
  74. });
  75. const userConfigAfterInstall = await readJsonFile(userConfigPath);
  76. expect(readEnabledPlugins(userConfigAfterInstall)[PLUGIN_LIFECYCLE_ID]).toBe(true);
  77. // 先在 User 视图保存一个值,Workspace 视图应显示它,但不能把其它 default
  78. // effective 值一起当作 Workspace 配置写回。
  79. await openInstalledPluginSettingsDetail(PLUGIN_LIFECYCLE_ID);
  80. await clickPluginControl("plugin-store-advanced");
  81. await setPluginControlValueForRoundtrip(
  82. PLUGIN_LIFECYCLE_CONFIG_KEY,
  83. PLUGIN_LIFECYCLE_CONFIG_VALUE,
  84. );
  85. await clickPluginControl("plugin-store-config-save", {
  86. pluginId: PLUGIN_LIFECYCLE_ID,
  87. });
  88. await browser.waitUntil(
  89. async () =>
  90. readPluginOptions(await readJsonFile(userConfigPath))[PLUGIN_LIFECYCLE_CONFIG_KEY] ===
  91. PLUGIN_LIFECYCLE_CONFIG_VALUE,
  92. {
  93. timeout: 30000,
  94. timeoutMsg: "User Plugin option did not persist before Workspace roundtrip",
  95. },
  96. );
  97. await clickPluginControl("plugin-settings-detail-back");
  98. await selectPluginSettingsScope("workspace");
  99. expect(await hasPluginControl("plugin-store-browse")).toBe(false);
  100. await waitForPluginControl("plugin-settings-plugin-row", {
  101. pluginId: PLUGIN_LIFECYCLE_ID,
  102. });
  103. expect(await readInstalledPluginRowState(PLUGIN_LIFECYCLE_ID)).toEqual({
  104. checked: true,
  105. source: null,
  106. });
  107. await openInstalledPluginSettingsDetail(PLUGIN_LIFECYCLE_ID);
  108. await clickPluginControl("plugin-store-advanced");
  109. expect(await readPluginOptionSource(PLUGIN_LIFECYCLE_INHERITED_CONFIG_KEY)).toBe("default");
  110. await setPluginControlValueForRoundtrip(
  111. PLUGIN_LIFECYCLE_CONFIG_KEY,
  112. `${PLUGIN_LIFECYCLE_CONFIG_VALUE}-workspace`,
  113. );
  114. await clickPluginControl("plugin-store-config-save", {
  115. pluginId: PLUGIN_LIFECYCLE_ID,
  116. });
  117. await browser.waitUntil(
  118. async () =>
  119. readPluginOptions(await readJsonFile(workspaceConfigPath))[PLUGIN_LIFECYCLE_CONFIG_KEY] ===
  120. `${PLUGIN_LIFECYCLE_CONFIG_VALUE}-workspace`,
  121. {
  122. timeout: 30000,
  123. timeoutMsg: "Workspace Plugin option did not persist",
  124. },
  125. );
  126. expect(readPluginOptions(await readJsonFile(workspaceConfigPath))).toEqual({
  127. [PLUGIN_LIFECYCLE_CONFIG_KEY]: `${PLUGIN_LIFECYCLE_CONFIG_VALUE}-workspace`,
  128. });
  129. await waitForPluginOptionSource(PLUGIN_LIFECYCLE_CONFIG_KEY, "workspace");
  130. expect(await readPluginOptionSource(PLUGIN_LIFECYCLE_INHERITED_CONFIG_KEY)).toBe("default");
  131. await clickPluginControl("plugin-store-config-clear", {
  132. pluginId: PLUGIN_LIFECYCLE_ID,
  133. configKey: PLUGIN_LIFECYCLE_CONFIG_KEY,
  134. });
  135. await clickPluginControl("plugin-store-config-save", {
  136. pluginId: PLUGIN_LIFECYCLE_ID,
  137. });
  138. await browser.waitUntil(
  139. async () =>
  140. !Object.hasOwn(
  141. readPluginOptions(await readJsonFile(workspaceConfigPath)),
  142. PLUGIN_LIFECYCLE_CONFIG_KEY,
  143. ),
  144. {
  145. timeout: 30000,
  146. timeoutMsg: "Workspace Plugin option did not restore inheritance",
  147. },
  148. );
  149. await waitForPluginOptionSource(PLUGIN_LIFECYCLE_CONFIG_KEY, "user");
  150. // 已安装详情通过页内返回按钮回到配置列表。
  151. await clickPluginControl("plugin-settings-detail-back");
  152. await waitForPluginControl("plugin-settings-plugin-row", {
  153. pluginId: PLUGIN_LIFECYCLE_ID,
  154. });
  155. await clickPluginControl("plugin-settings-enabled-switch", {
  156. pluginId: PLUGIN_LIFECYCLE_ID,
  157. });
  158. await browser.waitUntil(
  159. async () => {
  160. const plugin = (await getPluginList(workspace, undefined, "workspace")).plugins.find(
  161. (item) => item.id === PLUGIN_LIFECYCLE_ID,
  162. );
  163. return plugin?.enabled === false && plugin.enabledSource === "workspace";
  164. },
  165. {
  166. timeout: 30000,
  167. timeoutMsg: "Workspace explicit disable did not reach the Agent projection",
  168. },
  169. );
  170. expect(readEnabledPlugins(await readJsonFile(workspaceConfigPath))[PLUGIN_LIFECYCLE_ID]).toBe(
  171. false,
  172. );
  173. await openInstalledPluginSettingsDetail(PLUGIN_LIFECYCLE_ID);
  174. await clickPluginControl("plugin-store-reset-config");
  175. await browser.waitUntil(
  176. async () => {
  177. const plugin = (await getPluginList(workspace, undefined, "workspace")).plugins.find(
  178. (item) => item.id === PLUGIN_LIFECYCLE_ID,
  179. );
  180. return plugin?.enabled === true && plugin.enabledSource === "user";
  181. },
  182. {
  183. timeout: 30000,
  184. timeoutMsg: "Workspace restore-inheritance did not return to User",
  185. },
  186. );
  187. expect(
  188. readEnabledPlugins(await readJsonFile(workspaceConfigPath))[PLUGIN_LIFECYCLE_ID],
  189. ).toBeUndefined();
  190. await clickTestIdByDom(TID_SETTINGS_BACK_BUTTON);
  191. await clickTestIdByDom("plugin-store-sidebar-open");
  192. await waitForPluginControl("plugin-store-root", {}, 30000);
  193. await clickTestIdByDom("desktop-top-nav-back");
  194. await browser.waitUntil(async () => !(await hasPluginControl("plugin-store-root")));
  195. expect(await hasPluginControl("plugin-settings-scope-trigger")).toBe(false);
  196. });
  197. });
  198. async function readInstalledPluginRowState(pluginId: string): Promise<{
  199. checked: boolean;
  200. source: string | null;
  201. }> {
  202. return browser.execute((id) => {
  203. const row = Array.from(
  204. document.querySelectorAll<HTMLElement>('[data-testid="plugin-settings-plugin-row"]'),
  205. ).find((candidate) => candidate.dataset.pluginId === id);
  206. return {
  207. checked:
  208. row?.querySelector<HTMLElement>('[data-testid="plugin-settings-enabled-switch"]')?.dataset
  209. .state === "checked",
  210. source:
  211. row
  212. ?.querySelector<HTMLElement>("[data-settings-scope]")
  213. ?.getAttribute("data-settings-scope") ?? null,
  214. };
  215. }, pluginId);
  216. }
  217. async function readJsonFile(path: string): Promise<Record<string, unknown>> {
  218. if (!existsSync(path)) return {};
  219. return JSON.parse(await readFile(path, "utf8")) as Record<string, unknown>;
  220. }
  221. function readEnabledPlugins(config: Record<string, unknown>): Record<string, boolean> {
  222. const plugins =
  223. typeof config.plugins === "object" && config.plugins !== null
  224. ? (config.plugins as Record<string, unknown>)
  225. : {};
  226. return typeof plugins.enabledPlugins === "object" && plugins.enabledPlugins !== null
  227. ? (plugins.enabledPlugins as Record<string, boolean>)
  228. : {};
  229. }
  230. function readPluginOptions(config: Record<string, unknown>): Record<string, unknown> {
  231. const plugins =
  232. typeof config.plugins === "object" && config.plugins !== null
  233. ? (config.plugins as Record<string, unknown>)
  234. : {};
  235. return typeof plugins.options === "object" && plugins.options !== null
  236. ? (((plugins.options as Record<string, unknown>)[PLUGIN_LIFECYCLE_ID] as Record<
  237. string,
  238. unknown
  239. >) ?? {})
  240. : {};
  241. }
  242. async function setPluginControlValueForRoundtrip(key: string, value: string): Promise<void> {
  243. await waitForPluginControl("plugin-store-config-input", { configKey: key });
  244. const state = await browser.execute(
  245. (configKey, nextValue) => {
  246. const input = document.querySelector<HTMLInputElement>(
  247. `[data-testid="plugin-store-config-input"][data-config-key="${configKey}"]`,
  248. );
  249. if (!input) return { updated: false, reason: "missing" };
  250. if (input.disabled || input.readOnly) {
  251. return {
  252. updated: false,
  253. reason: "not-editable",
  254. disabled: input.disabled,
  255. readOnly: input.readOnly,
  256. outerHTML: input.outerHTML,
  257. };
  258. }
  259. const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")?.set;
  260. setter?.call(input, nextValue);
  261. input.dispatchEvent(new InputEvent("input", { bubbles: true, data: nextValue }));
  262. input.dispatchEvent(new Event("change", { bubbles: true }));
  263. return { updated: true };
  264. },
  265. key,
  266. value,
  267. );
  268. if (!state.updated)
  269. throw new Error(`Could not set Plugin option input: ${JSON.stringify(state)}`);
  270. }
  271. async function readPluginOptionSource(key: string): Promise<string | null> {
  272. return browser.execute((configKey) => {
  273. const input = document.querySelector<HTMLElement>(
  274. `[data-testid="plugin-store-config-input"][data-config-key="${configKey}"]`,
  275. );
  276. return (
  277. input?.closest("label")?.querySelector<HTMLElement>("[data-settings-scope]")?.dataset
  278. .settingsScope ?? null
  279. );
  280. }, key);
  281. }
  282. async function waitForPluginOptionSource(
  283. key: string,
  284. expected: "default" | "user" | "workspace",
  285. ): Promise<void> {
  286. await browser.waitUntil(async () => (await readPluginOptionSource(key)) === expected, {
  287. timeout: 30000,
  288. timeoutMsg: `Plugin option source did not become ${expected}: ${key}`,
  289. });
  290. }