workspaceHookTrustState.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. didWorkspaceHookReviewSettle,
  4. resolveWorkspaceHookReasonCodeMessageId,
  5. shouldSilenceStaleRejection,
  6. } from "../src/settings/workspaceHookTrustState.js";
  7. import enUS from "../src/i18n/locales/en-US.js";
  8. import zhCN from "../src/i18n/locales/zh-CN.js";
  9. describe("workspace Hook trust state 展示语义(D19)", () => {
  10. it("只在「有 review → 无 review」跃变时判定审核终结", () => {
  11. // 授权走行内 Trust、不经 revoke 的 refresh,设置页需据此补刷新,
  12. // 否则按钮会滞留到组件重挂载(2026-08-11 UAT)。
  13. expect(
  14. didWorkspaceHookReviewSettle({
  15. previousInteractionId: "interaction-1",
  16. currentInteractionId: undefined,
  17. }),
  18. ).toBe(true);
  19. // 初次挂载:从来没有过 review,不得触发——否则会紧随 initialize 多加载一次。
  20. expect(
  21. didWorkspaceHookReviewSettle({
  22. previousInteractionId: undefined,
  23. currentInteractionId: undefined,
  24. }),
  25. ).toBe(false);
  26. // 审核刚打开、以及 supersede 换代(仍有 review)都不算终结。
  27. expect(
  28. didWorkspaceHookReviewSettle({
  29. previousInteractionId: undefined,
  30. currentInteractionId: "interaction-1",
  31. }),
  32. ).toBe(false);
  33. expect(
  34. didWorkspaceHookReviewSettle({
  35. previousInteractionId: "interaction-1",
  36. currentInteractionId: "interaction-2",
  37. }),
  38. ).toBe(false);
  39. });
  40. it("reasonCode 映射到非空 i18n key,未知码回退到通用拒绝文案而非原始码", () => {
  41. // Bug(2026-08-11 UAT):命令被拒绝时直接把原始 reasonCode 渲染给用户,
  42. // 中英文用户都不可读。映射后未知码必须回退到通用文案,不暴露内部枚举字面量。
  43. const knownCodes = [
  44. "workspace_hooks_review_superseded",
  45. "workspace_hooks_snapshot_mismatch",
  46. "workspace_hooks_bundle_changed",
  47. "workspace_hooks_config_unreadable",
  48. "workspace_hooks_config_write_failed",
  49. "workspace_hooks_config_rebuild_failed",
  50. "workspace_hooks_trust_store_corrupt",
  51. "workspace_hooks_blocked_by_policy",
  52. "workspace_hooks_policy_requires_pretrust",
  53. "workspace_hooks_interaction_timeout",
  54. "workspace_hooks_require_trust_capable_host",
  55. ];
  56. for (const code of knownCodes) {
  57. const id = resolveWorkspaceHookReasonCodeMessageId(code);
  58. expect(id, `${code} 应映射到非空 key`).toBeTruthy();
  59. expect(id, `${code} 不得回退为原始码`).not.toBe(code);
  60. // 每个 key 在 zh/en 两侧都必须存在,缺 key 会把原始 id 渲染到界面。
  61. expect(zhCN[id], `zh-CN 缺 ${id}`).toBeTruthy();
  62. expect(enUS[id], `en-US 缺 ${id}`).toBeTruthy();
  63. }
  64. // 未知码回退到通用拒绝文案。
  65. const fallback = resolveWorkspaceHookReasonCodeMessageId("totally_unknown_code");
  66. expect(fallback).toBe("settings.hooks.review.reason.rejected");
  67. expect(fallback).not.toBe("totally_unknown_code");
  68. expect(zhCN[fallback]).toBeTruthy();
  69. expect(enUS[fallback]).toBeTruthy();
  70. // undefined / 空值同样回退。
  71. expect(resolveWorkspaceHookReasonCodeMessageId(undefined)).toBe(
  72. "settings.hooks.review.reason.rejected",
  73. );
  74. });
  75. });
  76. describe("双击 stale superseded 静默收敛(#9)", () => {
  77. it("superseded + 本地已无 live pending binding(双击第二次)⇒ 静默", () => {
  78. // Bugfix(#9):双击 Trust 按钮时第一次点击成功并终结审核(store clear binding),
  79. // 第二次点击的 commandId 被验证为已终结的 flow,返回 superseded。此刻本地已无
  80. // pending binding,说明用户意图已达成——不应展示错误。
  81. expect(
  82. shouldSilenceStaleRejection({
  83. reasonCode: "workspace_hooks_review_superseded",
  84. hasLivePendingBinding: false,
  85. }),
  86. ).toBe(true);
  87. });
  88. it("superseded + 本地仍有 live pending binding(bundle 变更等真正 supersede)⇒ 展示", () => {
  89. // 真正的 superseded:审核期间 bundle 变更,Runtime 推翻了当前 flow,但新的 review
  90. // binding 已建立(仍 pending)。用户必须重新审核——此错误必须展示。
  91. expect(
  92. shouldSilenceStaleRejection({
  93. reasonCode: "workspace_hooks_review_superseded",
  94. hasLivePendingBinding: true,
  95. }),
  96. ).toBe(false);
  97. });
  98. it("非 superseded 的 rejection 一律展示,不论 binding 状态", () => {
  99. // snapshot_mismatch、blocked_by_policy 等 rejection 与双击无关,必须始终展示。
  100. for (const reasonCode of [
  101. "workspace_hooks_snapshot_mismatch",
  102. "workspace_hooks_blocked_by_policy",
  103. "workspace_hooks_bundle_changed",
  104. undefined,
  105. ]) {
  106. expect(
  107. shouldSilenceStaleRejection({
  108. reasonCode,
  109. hasLivePendingBinding: false,
  110. }),
  111. ).toBe(false);
  112. expect(
  113. shouldSilenceStaleRejection({
  114. reasonCode,
  115. hasLivePendingBinding: true,
  116. }),
  117. ).toBe(false);
  118. }
  119. });
  120. });