usePluginReferenceCatalog.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // @vitest-environment jsdom
  2. import { act, renderHook, waitFor } from "@testing-library/react";
  3. import type {
  4. ZCodePluginReferenceCatalogEntry,
  5. ZCodePluginsReferenceCatalogResult,
  6. } from "@zcode/shared";
  7. import { beforeEach, describe, expect, it, vi } from "vitest";
  8. const getPluginReferenceCatalog =
  9. vi.fn<(params: unknown) => Promise<ZCodePluginsReferenceCatalogResult>>();
  10. let runtimeRestartListener: ((event: { workspaceKey: string }) => void) | undefined;
  11. const runtimeRestartSubscription = { dispose: vi.fn() };
  12. const onAgentRuntimeRestarted = vi.fn((listener: (event: { workspaceKey: string }) => void) => {
  13. runtimeRestartListener = listener;
  14. return runtimeRestartSubscription;
  15. });
  16. const resolution = {
  17. services: {
  18. pluginManagementService: {
  19. getPluginReferenceCatalog,
  20. },
  21. zcodeAgentService: {
  22. onAgentRuntimeRestarted,
  23. },
  24. },
  25. remoteSessionId: null as string | null,
  26. isRemoteTarget: false,
  27. connectionKind: "local-ready" as const,
  28. rpcReady: true,
  29. };
  30. const useWorkspaceServicesResolution = vi.fn(() => resolution);
  31. vi.mock("@/hooks/useWorkspaceServices.js", () => ({
  32. useWorkspaceServicesResolution,
  33. }));
  34. vi.mock("@/logger.js", () => ({
  35. logger: {
  36. warn: vi.fn(),
  37. },
  38. }));
  39. function plugin(pluginId: string): ZCodePluginReferenceCatalogEntry {
  40. const [name = pluginId, marketplace = "market"] = pluginId.split("@");
  41. return {
  42. pluginId,
  43. name,
  44. marketplace,
  45. enabled: true,
  46. conflictingPluginIds: [],
  47. skillQualifiedNames: [`${name}:search`],
  48. mcpServerNames: [],
  49. subagentNames: [],
  50. };
  51. }
  52. function deferred<T>() {
  53. let resolve!: (value: T) => void;
  54. const promise = new Promise<T>((settle) => {
  55. resolve = settle;
  56. });
  57. return { promise, resolve };
  58. }
  59. describe("usePluginReferenceCatalog", () => {
  60. beforeEach(() => {
  61. vi.clearAllMocks();
  62. getPluginReferenceCatalog.mockReset();
  63. runtimeRestartListener = undefined;
  64. resolution.remoteSessionId = null;
  65. resolution.rpcReady = true;
  66. });
  67. it("deduplicates only in-flight Session requests and refetches after completion", async () => {
  68. const firstRequest = deferred<ZCodePluginsReferenceCatalogResult>();
  69. getPluginReferenceCatalog.mockReturnValueOnce(firstRequest.promise).mockResolvedValueOnce({
  70. authority: "session",
  71. plugins: [plugin("fresh@market")],
  72. });
  73. const { clearPluginReferenceCatalogCacheForTest, usePluginReferenceCatalog } =
  74. await import("@/hooks/usePluginReferenceCatalog.js");
  75. clearPluginReferenceCatalogCacheForTest();
  76. const renderCatalog = () =>
  77. renderHook(() =>
  78. usePluginReferenceCatalog("/remote", "ssh://host/remote", "session-a", true, {
  79. dedupeSessionRequest: true,
  80. preferredRemoteSessionId: "remote-runtime-explicit",
  81. suppressErrorLog: true,
  82. }),
  83. );
  84. const first = renderCatalog();
  85. const concurrent = renderCatalog();
  86. await waitFor(() => expect(getPluginReferenceCatalog).toHaveBeenCalledTimes(1));
  87. await act(async () => {
  88. firstRequest.resolve({
  89. authority: "session",
  90. plugins: [plugin("in-flight@market")],
  91. });
  92. await firstRequest.promise;
  93. });
  94. await waitFor(() => expect(first.result.current.entries).toHaveLength(1));
  95. await waitFor(() => expect(concurrent.result.current.entries).toHaveLength(1));
  96. first.unmount();
  97. concurrent.unmount();
  98. const second = renderCatalog();
  99. await waitFor(() => expect(second.result.current.entries[0]?.pluginId).toBe("fresh@market"));
  100. expect(getPluginReferenceCatalog).toHaveBeenCalledTimes(2);
  101. expect(useWorkspaceServicesResolution).toHaveBeenLastCalledWith(
  102. "/remote",
  103. "remote-runtime-explicit",
  104. "ssh://host/remote",
  105. );
  106. second.unmount();
  107. });
  108. it("does not reuse a cached Session catalog across remote attachments", async () => {
  109. getPluginReferenceCatalog.mockResolvedValue({
  110. authority: "session",
  111. plugins: [plugin("remote@market")],
  112. });
  113. const { clearPluginReferenceCatalogCacheForTest, usePluginReferenceCatalog } =
  114. await import("@/hooks/usePluginReferenceCatalog.js");
  115. clearPluginReferenceCatalogCacheForTest();
  116. const hook = renderHook(
  117. (props: { remoteSessionId: string }) =>
  118. usePluginReferenceCatalog("/remote", "ssh://host/remote", "session-a", true, {
  119. dedupeSessionRequest: true,
  120. preferredRemoteSessionId: props.remoteSessionId,
  121. }),
  122. { initialProps: { remoteSessionId: "remote-runtime-a" } },
  123. );
  124. await waitFor(() => expect(hook.result.current.entries).toHaveLength(1));
  125. hook.rerender({ remoteSessionId: "remote-runtime-b" });
  126. await waitFor(() => expect(getPluginReferenceCatalog).toHaveBeenCalledTimes(2));
  127. expect(getPluginReferenceCatalog).toHaveBeenLastCalledWith({
  128. workspacePath: "/remote",
  129. workspaceIdentity: "ssh://host/remote",
  130. remoteSessionId: "remote-runtime-b",
  131. sessionId: "session-a",
  132. });
  133. hook.unmount();
  134. });
  135. it("refetches the same Session authority after its Agent runtime restarts", async () => {
  136. getPluginReferenceCatalog
  137. .mockResolvedValueOnce({
  138. authority: "session",
  139. plugins: [plugin("runtime-a@market")],
  140. })
  141. .mockResolvedValueOnce({
  142. authority: "session",
  143. plugins: [plugin("runtime-b@market")],
  144. });
  145. const { clearPluginReferenceCatalogCacheForTest, usePluginReferenceCatalog } =
  146. await import("@/hooks/usePluginReferenceCatalog.js");
  147. clearPluginReferenceCatalogCacheForTest();
  148. const hook = renderHook(() =>
  149. usePluginReferenceCatalog("/remote", "ssh://host/remote", "session-a", true, {
  150. dedupeSessionRequest: true,
  151. preferredRemoteSessionId: "remote-runtime-stable",
  152. }),
  153. );
  154. await waitFor(() => expect(hook.result.current.entries[0]?.pluginId).toBe("runtime-a@market"));
  155. act(() => {
  156. runtimeRestartListener?.({ workspaceKey: "ssh://host/remote" });
  157. });
  158. await waitFor(() => expect(getPluginReferenceCatalog).toHaveBeenCalledTimes(2));
  159. await waitFor(() => expect(hook.result.current.entries[0]?.pluginId).toBe("runtime-b@market"));
  160. expect(getPluginReferenceCatalog).toHaveBeenLastCalledWith({
  161. workspacePath: "/remote",
  162. workspaceIdentity: "ssh://host/remote",
  163. remoteSessionId: "remote-runtime-stable",
  164. sessionId: "session-a",
  165. });
  166. hook.unmount();
  167. expect(runtimeRestartSubscription.dispose).toHaveBeenCalled();
  168. });
  169. it("drops the previous runtime result when it arrives after the restarted runtime", async () => {
  170. const runtimeA = deferred<ZCodePluginsReferenceCatalogResult>();
  171. const runtimeB = deferred<ZCodePluginsReferenceCatalogResult>();
  172. getPluginReferenceCatalog
  173. .mockReturnValueOnce(runtimeA.promise)
  174. .mockReturnValueOnce(runtimeB.promise);
  175. const { usePluginReferenceCatalog } = await import("@/hooks/usePluginReferenceCatalog.js");
  176. const hook = renderHook(() =>
  177. usePluginReferenceCatalog("/remote", "ssh://host/remote", "session-a", true, {
  178. dedupeSessionRequest: true,
  179. }),
  180. );
  181. await waitFor(() => expect(getPluginReferenceCatalog).toHaveBeenCalledTimes(1));
  182. act(() => {
  183. runtimeRestartListener?.({ workspaceKey: "ssh://host/remote" });
  184. });
  185. await waitFor(() => expect(getPluginReferenceCatalog).toHaveBeenCalledTimes(2));
  186. await act(async () => {
  187. runtimeB.resolve({
  188. authority: "session",
  189. plugins: [plugin("runtime-b@market")],
  190. });
  191. await runtimeB.promise;
  192. });
  193. await waitFor(() => expect(hook.result.current.entries[0]?.pluginId).toBe("runtime-b@market"));
  194. await act(async () => {
  195. runtimeA.resolve({
  196. authority: "session",
  197. plugins: [plugin("runtime-a-stale@market")],
  198. });
  199. await runtimeA.promise;
  200. });
  201. expect(hook.result.current.entries[0]?.pluginId).toBe("runtime-b@market");
  202. hook.unmount();
  203. });
  204. it("hides the previous authority immediately when workspace/session request identity changes", async () => {
  205. const first = deferred<ZCodePluginsReferenceCatalogResult>();
  206. const second = deferred<ZCodePluginsReferenceCatalogResult>();
  207. getPluginReferenceCatalog
  208. .mockReturnValueOnce(first.promise)
  209. .mockReturnValueOnce(second.promise);
  210. const { usePluginReferenceCatalog } = await import("@/hooks/usePluginReferenceCatalog.js");
  211. const hook = renderHook(
  212. (props: { sessionId: string }) =>
  213. usePluginReferenceCatalog("/workspace", "ssh://host/workspace", props.sessionId, true),
  214. { initialProps: { sessionId: "session-a" } },
  215. );
  216. await waitFor(() => expect(getPluginReferenceCatalog).toHaveBeenCalledTimes(1));
  217. await act(async () => {
  218. first.resolve({
  219. authority: "session",
  220. plugins: [plugin("alpha@market")],
  221. });
  222. await first.promise;
  223. });
  224. await waitFor(() => expect(hook.result.current.entries[0]?.pluginId).toBe("alpha@market"));
  225. hook.rerender({ sessionId: "session-b" });
  226. // 新 effect 发请求前的首帧也必须 fail closed,不能闪现 session-a catalog。
  227. expect(hook.result.current.entries).toEqual([]);
  228. expect(hook.result.current.authority).toBeNull();
  229. await waitFor(() => expect(getPluginReferenceCatalog).toHaveBeenCalledTimes(2));
  230. await act(async () => {
  231. second.resolve({
  232. authority: "session",
  233. plugins: [plugin("beta@market")],
  234. });
  235. await second.promise;
  236. });
  237. await waitFor(() => expect(hook.result.current.entries[0]?.pluginId).toBe("beta@market"));
  238. hook.unmount();
  239. });
  240. it("drops late results and clears cached entries while a remote attachment is unavailable", async () => {
  241. const first = deferred<ZCodePluginsReferenceCatalogResult>();
  242. const second = deferred<ZCodePluginsReferenceCatalogResult>();
  243. getPluginReferenceCatalog
  244. .mockReturnValueOnce(first.promise)
  245. .mockReturnValueOnce(second.promise);
  246. const { usePluginReferenceCatalog } = await import("@/hooks/usePluginReferenceCatalog.js");
  247. const hook = renderHook(
  248. (props: { revision: number }) => {
  249. void props.revision;
  250. return usePluginReferenceCatalog("/remote", "ssh://host/remote", "session-a", true);
  251. },
  252. { initialProps: { revision: 0 } },
  253. );
  254. await waitFor(() => expect(getPluginReferenceCatalog).toHaveBeenCalledTimes(1));
  255. resolution.rpcReady = false;
  256. hook.rerender({ revision: 1 });
  257. expect(hook.result.current).toMatchObject({
  258. entries: [],
  259. authority: null,
  260. loading: false,
  261. error: null,
  262. });
  263. await act(async () => {
  264. first.resolve({
  265. authority: "session",
  266. plugins: [plugin("stale@market")],
  267. });
  268. await first.promise;
  269. });
  270. expect(hook.result.current.entries).toEqual([]);
  271. resolution.rpcReady = true;
  272. resolution.remoteSessionId = "remote-runtime-2";
  273. hook.rerender({ revision: 2 });
  274. await waitFor(() => expect(getPluginReferenceCatalog).toHaveBeenCalledTimes(2));
  275. expect(getPluginReferenceCatalog).toHaveBeenLastCalledWith({
  276. workspacePath: "/remote",
  277. workspaceIdentity: "ssh://host/remote",
  278. remoteSessionId: "remote-runtime-2",
  279. sessionId: "session-a",
  280. });
  281. await act(async () => {
  282. second.resolve({
  283. authority: "session",
  284. plugins: [plugin("fresh@market")],
  285. });
  286. await second.promise;
  287. });
  288. await waitFor(() => expect(hook.result.current.entries[0]?.pluginId).toBe("fresh@market"));
  289. hook.unmount();
  290. });
  291. });