workspaceHookTrustNotice.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import type { Hook, WorkspaceHookReviewTrustState } from "@zcode/shared";
  2. import { createElement } from "react";
  3. import { renderToStaticMarkup } from "react-dom/server";
  4. import { describe, expect, it } from "vitest";
  5. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  6. import {
  7. hasWorkspaceHooksRequiringReview,
  8. shouldShowWorkspaceHookTrustNotice,
  9. WorkspaceHookTrustNotice,
  10. } from "@/settings/WorkspaceHookTrustNotice.js";
  11. function workspaceHook(trustState: WorkspaceHookReviewTrustState): Hook {
  12. return {
  13. id: `workspace-${trustState}`,
  14. event: "PreToolUse",
  15. type: "command",
  16. command: "echo review",
  17. enabled: true,
  18. editable: false,
  19. location: {
  20. source: "zcode",
  21. scope: "project",
  22. directoryPath: "/workspace/.zcode",
  23. projectPath: "/workspace",
  24. },
  25. workspaceHook: {
  26. sourceRootEnabled: true,
  27. declarationEnabled: true,
  28. runtimeHooksEnabled: true,
  29. configuredEnabled: true,
  30. sourcePath: "/workspace/.zcode/config.json",
  31. reviewItemId: `review-${trustState}`,
  32. workspaceIdentity: "/workspace",
  33. bundleDigest: "a".repeat(64),
  34. hookDeclarationDigest: "b".repeat(64),
  35. sourceFileIndex: 0,
  36. trustState,
  37. },
  38. };
  39. }
  40. function renderNotice(locale: "en-US" | "zh-CN", hooks: Hook[]): string {
  41. return renderToStaticMarkup(
  42. createElement(
  43. ZCodeIntlProvider,
  44. { initialLocale: locale },
  45. createElement(WorkspaceHookTrustNotice, { hooks }),
  46. ),
  47. );
  48. }
  49. describe("Workspace Hook Trust settings notice", () => {
  50. it("shows a non-interactive warning for hooks that still expose the inline Trust action", () => {
  51. const hooks = [workspaceHook("pending_trust")];
  52. const html = renderNotice("en-US", hooks);
  53. expect(hasWorkspaceHooksRequiringReview(hooks)).toBe(true);
  54. expect(html).toContain('role="note"');
  55. expect(html).toContain('data-testid="workspace-hook-trust-notice"');
  56. expect(html).toContain("Hooks can run outside of the sandbox");
  57. expect(html).toContain("border-warning/30 bg-warning/10");
  58. expect(html).toContain("lucide-circle-alert");
  59. expect(html).not.toContain("<button");
  60. });
  61. it("uses the requested Chinese copy", () => {
  62. expect(renderNotice("zh-CN", [workspaceHook("stale_digest")])).toContain(
  63. "钩子可在沙盒外运行,因此,请审查最近安装或修改的所有钩子",
  64. );
  65. });
  66. it("hides after every Workspace Hook is persistently trusted", () => {
  67. const hooks = [workspaceHook("trusted_persistent")];
  68. expect(hasWorkspaceHooksRequiringReview(hooks)).toBe(false);
  69. expect(renderNotice("en-US", hooks)).toBe("");
  70. });
  71. it("ignores ordinary user hooks without Workspace Hook Trust metadata", () => {
  72. const hook = workspaceHook("pending_trust");
  73. delete hook.workspaceHook;
  74. expect(hasWorkspaceHooksRequiringReview([hook])).toBe(false);
  75. expect(renderNotice("en-US", [hook])).toBe("");
  76. });
  77. it("keeps the notice hidden while the target workspace is connecting", () => {
  78. const workspaceAHooks = [workspaceHook("pending_trust")];
  79. expect(
  80. shouldShowWorkspaceHookTrustNotice({
  81. hooks: workspaceAHooks,
  82. loadedWorkspaceKey: "workspace-a",
  83. rpcReady: false,
  84. targetWorkspaceKey: "workspace-b",
  85. }),
  86. ).toBe(false);
  87. expect(
  88. shouldShowWorkspaceHookTrustNotice({
  89. hooks: workspaceAHooks,
  90. loadedWorkspaceKey: "workspace-a",
  91. rpcReady: true,
  92. targetWorkspaceKey: "workspace-b",
  93. }),
  94. ).toBe(false);
  95. });
  96. it("shows only the loaded target snapshot, even when search filters every row", () => {
  97. const workspaceBHooks = [workspaceHook("pending_trust")];
  98. expect(
  99. shouldShowWorkspaceHookTrustNotice({
  100. hooks: workspaceBHooks,
  101. loadedWorkspaceKey: "workspace-b",
  102. rpcReady: true,
  103. targetWorkspaceKey: "workspace-b",
  104. }),
  105. ).toBe(true);
  106. expect(renderNotice("en-US", workspaceBHooks)).toContain(
  107. 'data-testid="workspace-hook-trust-notice"',
  108. );
  109. });
  110. });