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

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