hooksSectionWorkspaceHookTrustNotice.test.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // @vitest-environment jsdom
  2. import type { Hook } from "@zcode/shared";
  3. import { createElement } from "react";
  4. import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
  5. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  6. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  7. import { HooksSection } from "@/settings/HooksSection.js";
  8. const hooksStateMock = vi.hoisted(() => ({
  9. workspacePath: "/workspace-a",
  10. workspaceIdentity: null as string | null,
  11. loadedWorkspaceKey: "/workspace-a" as string | null,
  12. hooks: [] as Hook[],
  13. loading: false,
  14. error: null as string | null,
  15. operatingHookId: null as string | null,
  16. initialize: vi.fn(async () => undefined),
  17. refresh: vi.fn(async () => undefined),
  18. addHook: vi.fn(async () => undefined),
  19. updateHook: vi.fn(async () => undefined),
  20. deleteHook: vi.fn(async () => undefined),
  21. toggleHook: vi.fn(async () => undefined),
  22. importHook: vi.fn(async () => undefined),
  23. }));
  24. const pluginStoreStateMock = vi.hoisted(() => ({
  25. plugins: [],
  26. availablePlugins: [],
  27. installedPlugins: [],
  28. marketplaceAvailabilityKnown: true,
  29. loading: false,
  30. error: null,
  31. initialize: vi.fn(async () => undefined),
  32. }));
  33. const serviceResolutionMock = vi.hoisted(() => ({
  34. rpcReady: true,
  35. }));
  36. vi.mock("@/store/hooksStore.js", () => ({
  37. useHooksStore: () => hooksStateMock,
  38. }));
  39. vi.mock("@/store/pluginManagementStore.js", () => ({
  40. usePluginManagementStore: (selector: (state: typeof pluginStoreStateMock) => unknown) =>
  41. selector(pluginStoreStateMock),
  42. }));
  43. vi.mock("@/workspace-config-catalog/index.js", () => ({
  44. useConfigWorkspaceTargets: () => [],
  45. useConfigWorkspaceServicesResolution: () => ({
  46. rpcReady: serviceResolutionMock.rpcReady,
  47. services: {
  48. hooksService: {},
  49. pluginManagementService: {},
  50. },
  51. }),
  52. }));
  53. vi.mock("@/hooks/useServices.js", () => ({
  54. useServices: () => ({}),
  55. }));
  56. vi.mock("@/hooks/useConfirmDialog.js", () => ({
  57. useConfirmDialog: () => vi.fn(async () => false),
  58. }));
  59. vi.mock("@/hooks/useZCodeSessionService.js", () => ({
  60. useZCodeSessionService: () => undefined,
  61. }));
  62. vi.mock("@/store/TabStoreProvider.js", () => ({
  63. useTabStore: (selector: (state: { tabs: [] }) => unknown) => selector({ tabs: [] }),
  64. }));
  65. vi.mock("@/store/workspaceHookReviewStore.js", () => ({
  66. useWorkspaceHookReviewStore: (
  67. selector: (state: { bindings: Record<string, never> }) => unknown,
  68. ) => selector({ bindings: {} }),
  69. }));
  70. vi.mock("@/settings/useWorkspaceHookInlineTrust.js", () => ({
  71. useWorkspaceHookInlineTrust: () => ({
  72. trustActionAvailable: false,
  73. trustingHookId: null,
  74. trustHook: vi.fn(async () => undefined),
  75. }),
  76. }));
  77. vi.mock("@/settings/SettingsSearchInput.js", () => ({
  78. SettingsSearchInput: ({
  79. onChange,
  80. placeholder,
  81. value,
  82. }: {
  83. onChange: (event: { target: { value: string } }) => void;
  84. placeholder: string;
  85. value: string;
  86. }) => createElement("input", { "aria-label": "hooks-search", onChange, placeholder, value }),
  87. }));
  88. // 只替换 React 组件,两个纯函数(getPluginWorkspaceKey / isPluginScopeWorkspaceConnected)
  89. // 走真实实现。此前手写整份 mock,导致源码新增 isPluginScopeWorkspaceConnected 后本用例直接崩
  90. // ("No export is defined on the mock");用 importOriginal 可以从结构上避免这类漂移。
  91. vi.mock("@/settings/PluginScopeMenu.js", async (importOriginal) => ({
  92. ...(await importOriginal<typeof import("@/settings/PluginScopeMenu.js")>()),
  93. PluginScopeMenu: () => null,
  94. }));
  95. vi.mock("@/settings/HookForm.js", () => ({ HookForm: () => null }));
  96. vi.mock("@/settings/SettingsHeaderBreadcrumb.js", () => ({
  97. SettingsBreadcrumbReporter: () => null,
  98. }));
  99. vi.mock("@/settings/SettingsResourceHeaderActions.js", () => ({
  100. SettingsResourceHeaderActions: () => null,
  101. }));
  102. vi.mock("@/settings/HooksList.js", () => ({ HooksList: () => null }));
  103. vi.mock("@/settings/pluginStoreListing.js", () => ({
  104. formatCanonicalPluginName: (name: string) => name,
  105. }));
  106. vi.mock("@/components/ui/button.js", () => ({
  107. Button: ({ children }: { children?: unknown }) => createElement("button", null, children),
  108. }));
  109. vi.mock("@/components/ui/toast.js", () => ({ toast: vi.fn() }));
  110. vi.mock("@/lib/zcodeDraftSkillInvalidation.js", () => ({
  111. invalidateDeferredDraftSessionForRuntimeChange: vi.fn(async () => undefined),
  112. }));
  113. vi.mock("@/settings/PluginInstallEmptyState.js", () => ({
  114. PluginLoadingState: ({ label }: { label: string }) => createElement("div", null, label),
  115. PluginSearchEmptyState: ({ label }: { label: string }) => createElement("div", null, label),
  116. }));
  117. function workspaceHook(): Hook {
  118. return {
  119. id: "workspace-pending",
  120. event: "PreToolUse",
  121. type: "command",
  122. command: "echo review",
  123. enabled: true,
  124. editable: false,
  125. location: {
  126. source: "zcode",
  127. scope: "user",
  128. directoryPath: "/workspace-a/.zcode",
  129. projectPath: "/workspace-a",
  130. },
  131. workspaceHook: {
  132. sourceRootEnabled: true,
  133. declarationEnabled: true,
  134. runtimeHooksEnabled: true,
  135. configuredEnabled: true,
  136. sourcePath: "/workspace-a/.zcode/config.json",
  137. reviewItemId: "review-pending",
  138. workspaceIdentity: "/workspace-a",
  139. bundleDigest: "a".repeat(64),
  140. hookDeclarationDigest: "b".repeat(64),
  141. sourceFileIndex: 0,
  142. trustState: "pending_trust",
  143. },
  144. };
  145. }
  146. function renderHooksSection() {
  147. return render(
  148. createElement(
  149. ZCodeIntlProvider,
  150. { initialLocale: "en-US" },
  151. createElement(HooksSection, { workspacePath: hooksStateMock.workspacePath }),
  152. ),
  153. );
  154. }
  155. describe("HooksSection workspace Hook Trust notice", () => {
  156. beforeEach(() => {
  157. hooksStateMock.workspacePath = "/workspace-a";
  158. hooksStateMock.workspaceIdentity = null;
  159. hooksStateMock.loadedWorkspaceKey = "/workspace-a";
  160. hooksStateMock.hooks = [workspaceHook()];
  161. hooksStateMock.loading = false;
  162. hooksStateMock.error = null;
  163. serviceResolutionMock.rpcReady = true;
  164. });
  165. afterEach(() => cleanup());
  166. it("keeps the scope notice visible when search filters every hook row", async () => {
  167. renderHooksSection();
  168. fireEvent.change(screen.getByLabelText("hooks-search"), {
  169. target: { value: "does-not-match" },
  170. });
  171. await waitFor(() => {
  172. expect(screen.getByTestId("workspace-hook-trust-notice")).toBeTruthy();
  173. });
  174. });
  175. it("hides the old snapshot notice while the target workspace is connecting", () => {
  176. hooksStateMock.workspacePath = "/workspace-b";
  177. hooksStateMock.loadedWorkspaceKey = "/workspace-a";
  178. serviceResolutionMock.rpcReady = false;
  179. renderHooksSection();
  180. expect(screen.queryByTestId("workspace-hook-trust-notice")).toBeNull();
  181. });
  182. });