pluginStoreInstallEffects.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { renderToStaticMarkup } from "react-dom/server";
  4. import { beforeEach, expect, it, vi } from "vitest";
  5. import type { IPluginManagementService } from "@zcode/services";
  6. import { createPluginManagementStore } from "@/store/pluginManagementStore.js";
  7. import type { PluginStoreActions } from "@/settings/PluginStoreCard.js";
  8. import { TooltipProvider } from "@/components/ui/tooltip.js";
  9. import { PluginStorePage } from "@/settings/PluginStorePage.js";
  10. let store: ReturnType<typeof createPluginManagementStore>;
  11. let service: IPluginManagementService;
  12. let actions: PluginStoreActions;
  13. const effects = vi.hoisted(() => ({
  14. invalidate: vi.fn(async () => {}),
  15. skills: vi.fn(async () => {}),
  16. }));
  17. vi.mock("@/store/pluginManagementStore.js", async (original) => ({
  18. ...(await original<typeof import("@/store/pluginManagementStore.js")>()),
  19. useScopedPluginManagementStore: () => store,
  20. }));
  21. vi.mock("@/hooks/usePlatform.js", () => ({
  22. usePlatform: () => ({ reportTelemetryEvent: vi.fn() }),
  23. }));
  24. vi.mock("@/hooks/useServices.js", () => ({
  25. useServices: () => ({ pluginManagementService: service, skillsService: {} }),
  26. }));
  27. vi.mock("@/hooks/usePluginStoreOrder.js", () => ({
  28. usePluginStoreOrder: () => ({ order: [], refresh: vi.fn() }),
  29. }));
  30. vi.mock("@/store/TabStoreProvider.js", () => ({ useOptionalTabStore: () => undefined }));
  31. vi.mock("@/plugin-ui/index.js", () => ({
  32. useMarketPluginUi: () => ({ surfaces: [] }),
  33. resolvePluginUiSurfaceTitle: () => "Panel",
  34. }));
  35. vi.mock("@/i18n/IntlProvider.js", () => ({
  36. useZCodeIntl: () => ({
  37. intl: { formatMessage: ({ id }: { id: string }) => id },
  38. locale: "en-US",
  39. }),
  40. }));
  41. vi.mock("@/lib/zcodeDraftSkillInvalidation.js", () => ({
  42. invalidateDeferredDraftSessionForSkillChange: effects.invalidate,
  43. }));
  44. vi.mock("@/lib/skillStoreRefresh.js", () => ({
  45. refreshSharedSkillStoreForWorkspace: effects.skills,
  46. }));
  47. vi.mock("@/conversation-workspace-target/PluginAddMenu.js", () => ({ PluginAddMenu: () => null }));
  48. vi.mock("@/settings/PluginStoreListView.js", () => ({
  49. PluginStoreListView: (props: { actions: PluginStoreActions }) => {
  50. actions = props.actions;
  51. return null;
  52. },
  53. }));
  54. vi.mock("@/settings/PluginUninstallConfirmDialog.js", () => ({
  55. PluginUninstallConfirmDialog: () => null,
  56. }));
  57. vi.mock("@/settings/PluginStoreSourcesDialog.js", () => ({ PluginStoreSourcesDialog: () => null }));
  58. vi.mock("@/settings/AddMarketplaceSourceDialog.js", () => ({
  59. AddMarketplaceSourceDialog: () => null,
  60. }));
  61. beforeEach(async () => {
  62. vi.clearAllMocks();
  63. service = {
  64. listPlugins: vi.fn(async () => ({ plugins: [], diagnostics: [] })),
  65. getPluginsOverview: vi.fn(async () => ({
  66. marketplaces: [],
  67. availablePlugins: [],
  68. installedPlugins: [],
  69. restorableBuiltins: [],
  70. diagnostics: [],
  71. })),
  72. installPlugin: vi.fn(async () => ({
  73. installedPlugins: [],
  74. dependencyClosure: [],
  75. diagnostics: [],
  76. })),
  77. restoreBuiltinPlugin: vi.fn(async () => ({ pluginId: "panel@example", diagnostics: [] })),
  78. } as unknown as IPluginManagementService;
  79. store = createPluginManagementStore(service);
  80. await store.getState().initialize({ workspacePath: "/project/a", pluginService: service });
  81. });
  82. it.each([false, true])(
  83. "does not refresh capabilities after a failed install or restore (restore=%s)",
  84. async (restorable) => {
  85. const diagnostics = [
  86. { code: "test_failure", message: "installation failed", severity: "error" as const },
  87. ];
  88. vi.mocked(service.installPlugin).mockResolvedValue({
  89. installedPlugins: [],
  90. dependencyClosure: [],
  91. diagnostics,
  92. });
  93. vi.mocked(service.restoreBuiltinPlugin).mockResolvedValue({
  94. pluginId: "panel@example",
  95. diagnostics,
  96. });
  97. renderToStaticMarkup(
  98. createElement(
  99. TooltipProvider,
  100. null,
  101. createElement(PluginStorePage, { workspacePath: "/project/a", embedded: true }),
  102. ),
  103. );
  104. actions.onInstall({
  105. id: "panel@example",
  106. name: "panel",
  107. marketplace: "example",
  108. restorable,
  109. } as never);
  110. await vi.waitFor(() => expect(store.getState().activeOperations).toEqual([]));
  111. expect(store.getState().error).toBe("installation failed");
  112. expect(effects.invalidate).not.toHaveBeenCalled();
  113. expect(effects.skills).not.toHaveBeenCalled();
  114. },
  115. );
  116. it.each([false, true])(
  117. "refreshes capabilities after a successful install or restore (restore=%s)",
  118. async (restorable) => {
  119. renderToStaticMarkup(
  120. createElement(
  121. TooltipProvider,
  122. null,
  123. createElement(PluginStorePage, { workspacePath: "/project/a", embedded: true }),
  124. ),
  125. );
  126. actions.onInstall({
  127. id: "panel@example",
  128. name: "panel",
  129. marketplace: "example",
  130. restorable,
  131. } as never);
  132. await vi.waitFor(() => expect(store.getState().activeOperations).toEqual([]));
  133. expect(effects.invalidate).toHaveBeenCalledTimes(1);
  134. expect(effects.skills).toHaveBeenCalledTimes(1);
  135. },
  136. );