workspaceHookTrustState.test.ts 5.2 KB

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