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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. // 修复原因:a54857749d 起 Workspace option 存到用户私有文件,项目 .zcode/config.json 里的
  118. // plugins.options 不再生效也不再写入;改读 Agent 投影的 configuredOptions/optionSources,并断言项目文件不含该 option。
  119. await browser.waitUntil(
  120. async () => {
  121. const plugin = await readWorkspacePlugin();
  122. return (
  123. plugin?.configuredOptions?.[PLUGIN_LIFECYCLE_CONFIG_KEY] ===
  124. `${PLUGIN_LIFECYCLE_CONFIG_VALUE}-workspace` &&
  125. plugin.optionSources?.[PLUGIN_LIFECYCLE_CONFIG_KEY] === "workspace"
  126. );
  127. },
  128. {
  129. timeout: 30000,
  130. timeoutMsg: "Workspace Plugin option did not persist",
  131. },
  132. );
  133. expect(readPluginOptions(await readJsonFile(workspaceConfigPath))).toEqual({});
  134. await waitForPluginOptionSource(PLUGIN_LIFECYCLE_CONFIG_KEY, "workspace");
  135. expect(await readPluginOptionSource(PLUGIN_LIFECYCLE_INHERITED_CONFIG_KEY)).toBe("default");
  136. await clickPluginControl("plugin-store-config-clear", {
  137. pluginId: PLUGIN_LIFECYCLE_ID,
  138. configKey: PLUGIN_LIFECYCLE_CONFIG_KEY,
  139. });
  140. await clickPluginControl("plugin-store-config-save", {
  141. pluginId: PLUGIN_LIFECYCLE_ID,
  142. });
  143. await browser.waitUntil(
  144. async () =>
  145. (await readWorkspacePlugin())?.optionSources?.[PLUGIN_LIFECYCLE_CONFIG_KEY] === "user",
  146. {
  147. timeout: 30000,
  148. timeoutMsg: "Workspace Plugin option did not restore inheritance",
  149. },
  150. );
  151. await waitForPluginOptionSource(PLUGIN_LIFECYCLE_CONFIG_KEY, "user");
  152. // 已安装详情通过页内返回按钮回到配置列表。
  153. await clickPluginControl("plugin-settings-detail-back");
  154. await waitForPluginControl("plugin-settings-plugin-row", {
  155. pluginId: PLUGIN_LIFECYCLE_ID,
  156. });
  157. await clickPluginControl("plugin-settings-enabled-switch", {
  158. pluginId: PLUGIN_LIFECYCLE_ID,
  159. });
  160. await browser.waitUntil(
  161. async () => {
  162. const plugin = (await getPluginList(workspace, undefined, "workspace")).plugins.find(
  163. (item) => item.id === PLUGIN_LIFECYCLE_ID,
  164. );
  165. return plugin?.enabled === false && plugin.enabledSource === "workspace";
  166. },
  167. {
  168. timeout: 30000,
  169. timeoutMsg: "Workspace explicit disable did not reach the Agent projection",
  170. },
  171. );
  172. expect(readEnabledPlugins(await readJsonFile(workspaceConfigPath))[PLUGIN_LIFECYCLE_ID]).toBe(
  173. false,
  174. );
  175. await openInstalledPluginSettingsDetail(PLUGIN_LIFECYCLE_ID);
  176. await clickPluginControl("plugin-store-reset-config");
  177. await browser.waitUntil(
  178. async () => {
  179. const plugin = (await getPluginList(workspace, undefined, "workspace")).plugins.find(
  180. (item) => item.id === PLUGIN_LIFECYCLE_ID,
  181. );
  182. return plugin?.enabled === true && plugin.enabledSource === "user";
  183. },
  184. {
  185. timeout: 30000,
  186. timeoutMsg: "Workspace restore-inheritance did not return to User",
  187. },
  188. );
  189. expect(
  190. readEnabledPlugins(await readJsonFile(workspaceConfigPath))[PLUGIN_LIFECYCLE_ID],
  191. ).toBeUndefined();
  192. await clickTestIdByDom(TID_SETTINGS_BACK_BUTTON);
  193. await clickTestIdByDom("plugin-store-sidebar-open");
  194. await waitForPluginControl("plugin-store-root", {}, 30000);
  195. await clickTestIdByDom("desktop-top-nav-back");
  196. await browser.waitUntil(async () => !(await hasPluginControl("plugin-store-root")));
  197. expect(await hasPluginControl("plugin-settings-scope-trigger")).toBe(false);
  198. });
  199. });
  200. async function readWorkspacePlugin() {
  201. const { workspace } = getE2EAppDataPaths();
  202. return (await getPluginList(workspace, undefined, "workspace")).plugins.find(
  203. (item) => item.id === PLUGIN_LIFECYCLE_ID,
  204. );
  205. }
  206. async function readInstalledPluginRowState(pluginId: string): Promise<{
  207. checked: boolean;
  208. source: string | null;
  209. }> {
  210. return browser.execute((id) => {
  211. const row = Array.from(
  212. document.querySelectorAll<HTMLElement>('[data-testid="plugin-settings-plugin-row"]'),
  213. ).find((candidate) => candidate.dataset.pluginId === id);
  214. return {
  215. checked:
  216. row?.querySelector<HTMLElement>('[data-testid="plugin-settings-enabled-switch"]')?.dataset
  217. .state === "checked",
  218. source:
  219. row
  220. ?.querySelector<HTMLElement>("[data-settings-scope]")
  221. ?.getAttribute("data-settings-scope") ?? null,
  222. };
  223. }, pluginId);
  224. }
  225. async function readJsonFile(path: string): Promise<Record<string, unknown>> {
  226. if (!existsSync(path)) return {};
  227. return JSON.parse(await readFile(path, "utf8")) as Record<string, unknown>;
  228. }
  229. function readEnabledPlugins(config: Record<string, unknown>): Record<string, boolean> {
  230. const plugins =
  231. typeof config.plugins === "object" && config.plugins !== null
  232. ? (config.plugins as Record<string, unknown>)
  233. : {};
  234. return typeof plugins.enabledPlugins === "object" && plugins.enabledPlugins !== null
  235. ? (plugins.enabledPlugins as Record<string, boolean>)
  236. : {};
  237. }
  238. function readPluginOptions(config: Record<string, unknown>): Record<string, unknown> {
  239. const plugins =
  240. typeof config.plugins === "object" && config.plugins !== null
  241. ? (config.plugins as Record<string, unknown>)
  242. : {};
  243. return typeof plugins.options === "object" && plugins.options !== null
  244. ? (((plugins.options as Record<string, unknown>)[PLUGIN_LIFECYCLE_ID] as Record<
  245. string,
  246. unknown
  247. >) ?? {})
  248. : {};
  249. }
  250. async function setPluginControlValueForRoundtrip(key: string, value: string): Promise<void> {
  251. await waitForPluginControl("plugin-store-config-input", { configKey: key });
  252. const state = await browser.execute(
  253. (configKey, nextValue) => {
  254. const input = document.querySelector<HTMLInputElement>(
  255. `[data-testid="plugin-store-config-input"][data-config-key="${configKey}"]`,
  256. );
  257. if (!input) return { updated: false, reason: "missing" };
  258. if (input.disabled || input.readOnly) {
  259. return {
  260. updated: false,
  261. reason: "not-editable",
  262. disabled: input.disabled,
  263. readOnly: input.readOnly,
  264. outerHTML: input.outerHTML,
  265. };
  266. }
  267. const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")?.set;
  268. setter?.call(input, nextValue);
  269. input.dispatchEvent(new InputEvent("input", { bubbles: true, data: nextValue }));
  270. input.dispatchEvent(new Event("change", { bubbles: true }));
  271. return { updated: true };
  272. },
  273. key,
  274. value,
  275. );
  276. if (!state.updated)
  277. throw new Error(`Could not set Plugin option input: ${JSON.stringify(state)}`);
  278. }
  279. async function readPluginOptionSource(key: string): Promise<string | null> {
  280. return browser.execute((configKey) => {
  281. const input = document.querySelector<HTMLElement>(
  282. `[data-testid="plugin-store-config-input"][data-config-key="${configKey}"]`,
  283. );
  284. return (
  285. input?.closest("label")?.querySelector<HTMLElement>("[data-settings-scope]")?.dataset
  286. .settingsScope ?? null
  287. );
  288. }, key);
  289. }
  290. async function waitForPluginOptionSource(
  291. key: string,
  292. expected: "default" | "user" | "workspace",
  293. ): Promise<void> {
  294. await browser.waitUntil(async () => (await readPluginOptionSource(key)) === expected, {
  295. timeout: 30000,
  296. timeoutMsg: `Plugin option source did not become ${expected}: ${key}`,
  297. });
  298. }