workspaceHookTrustNotice.test.ts 4.3 KB

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