usePluginCreator.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // @vitest-environment jsdom
  2. import { act, createElement } from "react";
  3. import { createRoot } from "react-dom/client";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. const mocks = vi.hoisted(() => {
  6. const tabs = { activeWorkspacePath: "/workspace", activeWorkspaceIdentity: "host-a" };
  7. const pane = { focusedPaneId: "primary", panes: {} };
  8. const group = { activeGroupId: null, groups: {} };
  9. const list = vi.fn();
  10. const resolution = {
  11. rpcReady: true,
  12. services: { skillsService: { list } },
  13. remoteSessionId: "attachment-a",
  14. };
  15. return { tabs, pane, group, list, resolution, toast: vi.fn() };
  16. });
  17. vi.mock("@/hooks/useWorkspaceServices.js", () => ({
  18. useWorkspaceServicesResolution: () => mocks.resolution,
  19. }));
  20. vi.mock("@/store/TabStoreProvider.js", () => ({
  21. useTabStore: (select: (s: unknown) => unknown) => select(mocks.tabs),
  22. useTabStoreApi: () => ({ getState: () => mocks.tabs }),
  23. }));
  24. vi.mock("@/store/tabStore.js", () => ({ isWorkspaceReadOnly: () => false }));
  25. vi.mock("@/v4/paneLayoutStore.js", () => ({
  26. INITIAL_PANE_LAYOUT: mocks.pane,
  27. usePaneLayoutStore: Object.assign((select: (s: unknown) => unknown) => select(mocks.pane), {
  28. getState: () => mocks.pane,
  29. }),
  30. }));
  31. vi.mock("@/v4/workbenchGroupStore.js", () => ({
  32. useWorkbenchGroupStore: Object.assign((select: (s: unknown) => unknown) => select(mocks.group), {
  33. getState: () => mocks.group,
  34. }),
  35. }));
  36. vi.mock("@/v4/workbenchNewTaskTarget.js", () => ({
  37. resolveWorkbenchNewTaskTarget: (s: {
  38. activeWorkspacePath: string;
  39. activeWorkspaceIdentity: string;
  40. }) => ({ workspacePath: s.activeWorkspacePath, workspaceIdentity: s.activeWorkspaceIdentity }),
  41. }));
  42. vi.mock("@/components/ui/toast.js", () => ({ toast: mocks.toast }));
  43. vi.mock("@/i18n/IntlProvider.js", () => ({
  44. useZCodeIntl: () => ({ intl: { formatMessage: ({ id }: { id: string }) => id } }),
  45. }));
  46. vi.mock("@/logger.js", () => ({ logger: { info: vi.fn(), warn: vi.fn() } }));
  47. import { usePluginCreator } from "@/hooks/usePluginCreator.js";
  48. const skill = {
  49. id: "creator",
  50. name: "plugin-creator",
  51. scope: "plugin",
  52. enabled: true,
  53. path: "/workspace/creator/SKILL.md",
  54. pluginId: "plugin-creator@zcode-plugins-official",
  55. description: "Create",
  56. body: "",
  57. };
  58. let root: ReturnType<typeof createRoot> | undefined;
  59. afterEach(async () => {
  60. if (root) await act(() => root?.unmount());
  61. root = undefined;
  62. vi.clearAllMocks();
  63. mocks.tabs.activeWorkspaceIdentity = "host-a";
  64. mocks.resolution.remoteSessionId = "attachment-a";
  65. mocks.resolution.rpcReady = true;
  66. });
  67. async function mount(onCreate: () => void) {
  68. let api!: ReturnType<typeof usePluginCreator>;
  69. function Component() {
  70. api = usePluginCreator(onCreate);
  71. return null;
  72. }
  73. root = createRoot(document.createElement("div"));
  74. const render = () => act(() => root?.render(createElement(Component)));
  75. await render();
  76. return { get: () => api, render };
  77. }
  78. describe("creator action admission", () => {
  79. it("coalesces duplicate clicks and creates one unsent draft with target identity", async () => {
  80. let finish!: (r: unknown) => void;
  81. mocks.list.mockImplementation(
  82. () =>
  83. new Promise((resolve) => {
  84. finish = resolve;
  85. }),
  86. );
  87. const create = vi.fn();
  88. const app = await mount(create);
  89. let pending!: Promise<void>;
  90. await act(async () => {
  91. pending = app.get().create();
  92. void app.get().create();
  93. });
  94. expect(mocks.list).toHaveBeenCalledTimes(1);
  95. expect(mocks.list).toHaveBeenCalledWith(
  96. expect.objectContaining({ workspaceIdentity: "host-a", workspacePath: "/workspace" }),
  97. );
  98. await act(async () => {
  99. finish({ skills: [skill] });
  100. await pending;
  101. });
  102. expect(create).toHaveBeenCalledTimes(1);
  103. expect(create).toHaveBeenCalledWith(
  104. expect.objectContaining({
  105. expectedWorkspaceKey: "host-a",
  106. initialPrompt: "[$plugin-creator](/workspace/creator/SKILL.md) ",
  107. }),
  108. );
  109. });
  110. it.each(["identity", "attachment"])(
  111. "discards a pending lookup after %s changes",
  112. async (kind) => {
  113. let finish!: (r: unknown) => void;
  114. mocks.list.mockImplementation(
  115. () =>
  116. new Promise((resolve) => {
  117. finish = resolve;
  118. }),
  119. );
  120. const create = vi.fn();
  121. const app = await mount(create);
  122. let pending!: Promise<void>;
  123. await act(async () => {
  124. pending = app.get().create();
  125. });
  126. if (kind === "identity") mocks.tabs.activeWorkspaceIdentity = "host-b";
  127. else mocks.resolution.remoteSessionId = "attachment-b";
  128. await app.render();
  129. await act(async () => {
  130. finish({ skills: [skill] });
  131. await pending;
  132. });
  133. expect(create).not.toHaveBeenCalled();
  134. },
  135. );
  136. it("reports disabled creator without creating or enabling anything", async () => {
  137. mocks.list.mockResolvedValue({ skills: [{ ...skill, enabled: false }] });
  138. const create = vi.fn();
  139. const app = await mount(create);
  140. await act(() => app.get().create());
  141. expect(create).not.toHaveBeenCalled();
  142. expect(mocks.toast).toHaveBeenCalled();
  143. });
  144. it.each(["missing", "failure", "disconnected"])(
  145. "reports %s without inserting a reference",
  146. async (kind) => {
  147. if (kind === "failure") mocks.list.mockRejectedValue(new Error("unavailable"));
  148. else mocks.list.mockResolvedValue({ skills: [] });
  149. if (kind === "disconnected") mocks.resolution.rpcReady = false;
  150. const create = vi.fn();
  151. const app = await mount(create);
  152. await act(() => app.get().create());
  153. expect(create).not.toHaveBeenCalled();
  154. expect(mocks.toast).toHaveBeenCalled();
  155. if (kind === "disconnected") expect(mocks.list).not.toHaveBeenCalled();
  156. },
  157. );
  158. });