hooksSectionWorkspaceHookTrustNotice.test.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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("@/hooks/useWorkspaceServices.js", () => ({
  44. useWorkspaceServicesResolution: () => ({
  45. rpcReady: serviceResolutionMock.rpcReady,
  46. services: {
  47. hooksService: {},
  48. pluginManagementService: {},
  49. },
  50. }),
  51. }));
  52. vi.mock("@/hooks/useConfirmDialog.js", () => ({
  53. useConfirmDialog: () => vi.fn(async () => false),
  54. }));
  55. vi.mock("@/hooks/useZCodeSessionService.js", () => ({
  56. useZCodeSessionService: () => undefined,
  57. }));
  58. vi.mock("@/store/TabStoreProvider.js", () => ({
  59. useTabStore: (selector: (state: { tabs: [] }) => unknown) => selector({ tabs: [] }),
  60. }));
  61. vi.mock("@/store/workspaceHookReviewStore.js", () => ({
  62. useWorkspaceHookReviewStore: (selector: (state: { bindings: Record<string, never> }) => unknown) =>
  63. selector({ bindings: {} }),
  64. }));
  65. vi.mock("@/settings/useWorkspaceHookInlineTrust.js", () => ({
  66. useWorkspaceHookInlineTrust: () => ({
  67. trustActionAvailable: false,
  68. trustingHookId: null,
  69. trustHook: vi.fn(async () => undefined),
  70. }),
  71. }));
  72. vi.mock("@/settings/SettingsSearchInput.js", () => ({
  73. SettingsSearchInput: ({
  74. onChange,
  75. placeholder,
  76. value,
  77. }: {
  78. onChange: (event: { target: { value: string } }) => void;
  79. placeholder: string;
  80. value: string;
  81. }) => createElement("input", { "aria-label": "hooks-search", onChange, placeholder, value }),
  82. }));
  83. // 只替换 React 组件,两个纯函数(getPluginWorkspaceKey / isPluginScopeWorkspaceConnected)
  84. // 走真实实现。此前手写整份 mock,导致源码新增 isPluginScopeWorkspaceConnected 后本用例直接崩
  85. // ("No export is defined on the mock");用 importOriginal 可以从结构上避免这类漂移。
  86. vi.mock("@/settings/PluginScopeMenu.js", async (importOriginal) => ({
  87. ...(await importOriginal<typeof import("@/settings/PluginScopeMenu.js")>()),
  88. PluginScopeMenu: () => null,
  89. }));
  90. vi.mock("@/settings/HookForm.js", () => ({ HookForm: () => null }));
  91. vi.mock("@/settings/SettingsHeaderBreadcrumb.js", () => ({ SettingsBreadcrumbReporter: () => null }));
  92. vi.mock("@/settings/SettingsResourceHeaderActions.js", () => ({ SettingsResourceHeaderActions: () => null }));
  93. vi.mock("@/settings/HooksList.js", () => ({ HooksList: () => null }));
  94. vi.mock("@/settings/pluginStoreListing.js", () => ({ formatCanonicalPluginName: (name: string) => name }));
  95. vi.mock("@/components/ui/button.js", () => ({
  96. Button: ({ children }: { children?: unknown }) => createElement("button", null, children),
  97. }));
  98. vi.mock("@/components/ui/toast.js", () => ({ toast: vi.fn() }));
  99. vi.mock("@/lib/zcodeDraftSkillInvalidation.js", () => ({
  100. invalidateDeferredDraftSessionForRuntimeChange: vi.fn(async () => undefined),
  101. }));
  102. vi.mock("@/settings/PluginInstallEmptyState.js", () => ({
  103. PluginLoadingState: ({ label }: { label: string }) => createElement("div", null, label),
  104. PluginSearchEmptyState: ({ label }: { label: string }) => createElement("div", null, label),
  105. }));
  106. function workspaceHook(): Hook {
  107. return {
  108. id: "workspace-pending",
  109. event: "PreToolUse",
  110. type: "command",
  111. command: "echo review",
  112. enabled: true,
  113. editable: false,
  114. location: {
  115. source: "zcode",
  116. scope: "user",
  117. directoryPath: "/workspace-a/.zcode",
  118. projectPath: "/workspace-a",
  119. },
  120. workspaceHook: {
  121. sourceRootEnabled: true,
  122. declarationEnabled: true,
  123. runtimeHooksEnabled: true,
  124. configuredEnabled: true,
  125. sourcePath: "/workspace-a/.zcode/config.json",
  126. reviewItemId: "review-pending",
  127. workspaceIdentity: "/workspace-a",
  128. bundleDigest: "a".repeat(64),
  129. hookDeclarationDigest: "b".repeat(64),
  130. sourceFileIndex: 0,
  131. trustState: "pending_trust",
  132. },
  133. };
  134. }
  135. function renderHooksSection() {
  136. return render(
  137. createElement(
  138. ZCodeIntlProvider,
  139. { initialLocale: "en-US" },
  140. createElement(HooksSection, { workspacePath: hooksStateMock.workspacePath }),
  141. ),
  142. );
  143. }
  144. describe("HooksSection workspace Hook Trust notice", () => {
  145. beforeEach(() => {
  146. hooksStateMock.workspacePath = "/workspace-a";
  147. hooksStateMock.workspaceIdentity = null;
  148. hooksStateMock.loadedWorkspaceKey = "/workspace-a";
  149. hooksStateMock.hooks = [workspaceHook()];
  150. hooksStateMock.loading = false;
  151. hooksStateMock.error = null;
  152. serviceResolutionMock.rpcReady = true;
  153. });
  154. afterEach(() => cleanup());
  155. it("keeps the scope notice visible when search filters every hook row", async () => {
  156. renderHooksSection();
  157. fireEvent.change(screen.getByLabelText("hooks-search"), {
  158. target: { value: "does-not-match" },
  159. });
  160. await waitFor(() => {
  161. expect(screen.getByTestId("workspace-hook-trust-notice")).toBeTruthy();
  162. });
  163. });
  164. it("hides the old snapshot notice while the target workspace is connecting", () => {
  165. hooksStateMock.workspacePath = "/workspace-b";
  166. hooksStateMock.loadedWorkspaceKey = "/workspace-a";
  167. serviceResolutionMock.rpcReady = false;
  168. renderHooksSection();
  169. expect(screen.queryByTestId("workspace-hook-trust-notice")).toBeNull();
  170. });
  171. });