useWorkspaceHookInlineTrustAuthority.test.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // @vitest-environment jsdom
  2. import { act, renderHook } from "@testing-library/react";
  3. import { beforeEach, describe, expect, it, vi } from "vitest";
  4. import type { Hook } from "@zcode/shared";
  5. import type { IHooksService } from "@zcode/services";
  6. import { useWorkspaceHookInlineTrust } from "@/settings/useWorkspaceHookInlineTrust.js";
  7. import { useHooksStore } from "@/store/hooksStore.js";
  8. import { useWorkspaceHookReviewStore } from "@/store/workspaceHookReviewStore.js";
  9. vi.mock("@/hooks/useServices.js", () => ({
  10. useServices: () => ({}),
  11. }));
  12. vi.mock("@/i18n/IntlProvider.js", () => ({
  13. useZCodeIntl: () => ({ intl: { formatMessage: ({ id }: { id: string }) => id } }),
  14. }));
  15. vi.mock("@/components/ui/toast.js", () => ({ toast: vi.fn() }));
  16. beforeEach(() => {
  17. useHooksStore.setState({
  18. workspacePath: null,
  19. workspaceIdentity: null,
  20. authorityKey: null,
  21. loadedWorkspaceKey: null,
  22. hooks: [],
  23. loading: false,
  24. error: null,
  25. operatingHookId: null,
  26. });
  27. useWorkspaceHookReviewStore.setState({ bindings: {}, commandBindings: {} });
  28. });
  29. describe("useWorkspaceHookInlineTrust Source authority", () => {
  30. it("refreshes the same authority bucket after granting trust", async () => {
  31. const loadHooks = vi.fn(async () => ({ hooks: [], hooksEnabled: true }));
  32. const service: IHooksService = {
  33. loadHooks,
  34. saveHooks: vi.fn(async () => undefined),
  35. grantWorkspaceHookTrust: vi.fn(async () => ({ accepted: true })),
  36. };
  37. await useHooksStore
  38. .getState()
  39. .initialize("/root", "workspace:repo", service, "source-settings:a");
  40. const { result } = renderHook(() =>
  41. useWorkspaceHookInlineTrust({
  42. workspacePath: "/root",
  43. workspaceIdentity: "workspace:repo",
  44. authorityKey: "source-settings:a",
  45. hooksService: service,
  46. rpcReady: true,
  47. }),
  48. );
  49. const hook: Hook = {
  50. id: "source-hook",
  51. event: "SessionStart",
  52. type: "command",
  53. command: "echo source",
  54. enabled: false,
  55. workspaceHook: {
  56. reviewItemId: "item",
  57. workspaceIdentity: "workspace:repo",
  58. bundleDigest: "b".repeat(64),
  59. hookDeclarationDigest: "a".repeat(64),
  60. sourceFileIndex: 0,
  61. sourceRootEnabled: true,
  62. declarationEnabled: false,
  63. runtimeHooksEnabled: true,
  64. configuredEnabled: false,
  65. sourcePath: ".zcode/config.json",
  66. trustState: "pending_trust",
  67. },
  68. };
  69. await act(async () => await result.current.trustHook(hook));
  70. expect(service.grantWorkspaceHookTrust).toHaveBeenCalledOnce();
  71. expect(loadHooks).toHaveBeenCalledTimes(2);
  72. });
  73. });