conversation-session-plugin-builtin-workspace-disable.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { access, readFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { clearAppData, getE2EAppDataPaths } from "#e2e/helpers/desktop-app";
  4. import {
  5. getPluginList,
  6. getPluginReferenceCatalog,
  7. pluginDataPath,
  8. pluginPathExists,
  9. pluginStoragePath,
  10. } from "#e2e/helpers/plugin-management-lifecycle";
  11. const CASE_MARKER = "E2E_WPL_013_BUILTIN_WORKSPACE_DISABLE";
  12. const BUILTIN_ID = "skill-creator@zcode-plugins-official";
  13. describe("WPL-013 builtin Workspace disable E2E", () => {
  14. after(async () => {
  15. await browser.electron.restoreAllMocks();
  16. await clearAppData();
  17. });
  18. it("disables the builtin only in Workspace A while preserving Host user state", async function () {
  19. this.timeout(120_000);
  20. const { homeDir, storageRoot, workspace: workspaceA } = getE2EAppDataPaths();
  21. const workspaceB = join(homeDir, "WorkspaceB");
  22. const workspaceAConfigPath = join(workspaceA, ".zcode", "config.json");
  23. const workspaceBConfigPath = join(workspaceB, ".zcode", "config.json");
  24. const userConfigPath = join(storageRoot, "cli", "config.json");
  25. const installedPluginsPath = pluginStoragePath("installed_plugins.json");
  26. const dataPath = join(pluginDataPath(BUILTIN_ID), "state.json");
  27. const workspaceAConfigBefore = await readFile(workspaceAConfigPath, "utf8");
  28. const userConfigBefore = await readFile(userConfigPath, "utf8");
  29. const installedPluginsBefore = await readOptionalFile(installedPluginsPath);
  30. const pluginDataBefore = await readFile(dataPath, "utf8");
  31. expect(await pathExists(workspaceBConfigPath)).toBe(false);
  32. const pluginA = (await getPluginList(workspaceA)).plugins.find(
  33. (plugin) => plugin.id === BUILTIN_ID,
  34. );
  35. const pluginB = (await getPluginList(workspaceB)).plugins.find(
  36. (plugin) => plugin.id === BUILTIN_ID,
  37. );
  38. if (!pluginA || !pluginB) {
  39. throw new Error(`${CASE_MARKER}: builtin Plugin 没有在 A/B resolver 中出现`);
  40. }
  41. expect(pluginA).toMatchObject({
  42. enabled: false,
  43. enabledSource: "workspace",
  44. });
  45. expect(pluginB).toMatchObject({
  46. enabled: true,
  47. enabledSource: "user",
  48. });
  49. expect(pluginA.rootPath).toBe(pluginB.rootPath);
  50. expect(pluginPathExists(pluginA.rootPath)).toBe(true);
  51. const catalogA = await getPluginReferenceCatalog({
  52. workspacePath: workspaceA,
  53. });
  54. const catalogB = await getPluginReferenceCatalog({
  55. workspacePath: workspaceB,
  56. });
  57. expect(catalogA.authority).toBe("workspace");
  58. expect(catalogB.authority).toBe("workspace");
  59. expect(catalogA.plugins.find((plugin) => plugin.pluginId === BUILTIN_ID)).toMatchObject({
  60. enabled: false,
  61. });
  62. expect(catalogB.plugins.find((plugin) => plugin.pluginId === BUILTIN_ID)).toMatchObject({
  63. enabled: true,
  64. });
  65. expect(await readFile(workspaceAConfigPath, "utf8")).toBe(workspaceAConfigBefore);
  66. expect(await pathExists(workspaceBConfigPath)).toBe(false);
  67. expect(await readFile(userConfigPath, "utf8")).toBe(userConfigBefore);
  68. expect(await readOptionalFile(installedPluginsPath)).toBe(installedPluginsBefore);
  69. expect(await readFile(dataPath, "utf8")).toBe(pluginDataBefore);
  70. expect(pluginPathExists(pluginA.rootPath)).toBe(true);
  71. });
  72. });
  73. async function readOptionalFile(path: string): Promise<string | null> {
  74. try {
  75. return await readFile(path, "utf8");
  76. } catch (error) {
  77. if (error instanceof Error && "code" in error && error.code === "ENOENT") {
  78. return null;
  79. }
  80. throw error;
  81. }
  82. }
  83. async function pathExists(path: string): Promise<boolean> {
  84. try {
  85. await access(path);
  86. return true;
  87. } catch {
  88. return false;
  89. }
  90. }