conversation-session-hook-trust.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. * 正式 E2E —— Workspace Hook Trust 软门禁与逐条持久信任(HK05–HK07)。
  3. * 已通过人工 review 并由 e2e:promote 从 manual-review/pending 晋级,
  4. * 随默认 spec 发现进入常规回归门禁;fixture 走 case-local provider replay。
  5. *
  6. * 证据分层:
  7. * - UI/交互:pending banner、Settings 钩子行内“信任”按钮、锁定 Switch
  8. * - Runtime:`.hook-trust-e2e/executions.jsonl` 执行证据(未信任零执行、不补跑、
  9. * 信任后自然事件执行)
  10. * - 持久化:`<E2E_HOME>/.zcode/security/workspace-hook-trust-v1.json` 精确落盘
  11. *
  12. * fixture 由 wdio.conf.ts 在 beforeSession 阶段(resetE2EHome 内)落盘,
  13. * 首个 agent 进程冷启动即加载 workspace hooks;spec 内不重复安装。
  14. */
  15. import {
  16. TID_SETTINGS_BACK_BUTTON,
  17. TID_SETTINGS_PAGE,
  18. TID_SETTINGS_SECTION_NAV,
  19. TID_TASK_SETTINGS_BUTTON,
  20. TID_V4_WORKSPACE_HOOK_PENDING_BANNER,
  21. TID_V4_WORKSPACE_HOOK_PENDING_DISMISS,
  22. TID_V4_WORKSPACE_HOOK_PENDING_REVIEW,
  23. testId,
  24. } from "@zcode/shared";
  25. import {
  26. DEFAULT_WORKSPACE,
  27. clearAppData,
  28. clickTestIdByDom,
  29. waitForWorkspaceApp,
  30. } from "../helpers/desktop-app.js";
  31. import { sel } from "../helpers/selectors.js";
  32. import {
  33. E2E_HOOK_TRUST_LABELS,
  34. readHookTrustExecutions,
  35. readHookTrustStore,
  36. waitForHookTrustExecution,
  37. waitForHookTrustStoreRecords,
  38. } from "../helpers/hook-trust-fixture.js";
  39. import {
  40. prepareV4ConversationE2E,
  41. sendV4Prompt,
  42. startNewV4Draft,
  43. waitForV4Pane,
  44. } from "../helpers/v4-conversation.js";
  45. const HOOKS_SECTION_SELECTOR = '[data-testid="hooks-settings-section"]';
  46. const HOOK_ROW_SELECTOR = '//div[@data-testid="configured-hook-row"]';
  47. // 信任按钮通过 ShieldCheck 图标定位(lucide class),不依赖 locale 文案。
  48. const TRUST_BUTTON_IN_ROW =
  49. './/button[.//*[contains(@class, "lucide-shield-check")]]';
  50. describe("Workspace Hook Trust (HK05–HK07)", () => {
  51. before(async function () {
  52. this.timeout(120000);
  53. await prepareV4ConversationE2E();
  54. });
  55. after(async () => {
  56. await browser.electron.restoreAllMocks();
  57. await clearAppData();
  58. });
  59. it("HK05: 未信任 Hook 被软门禁跳过,banner 引导进设置,行内只提供信任与锁定开关", async function () {
  60. this.timeout(180000);
  61. await sendV4Prompt("E2E_HOOK_TRUST: reply with ok");
  62. await waitForIdle("Soft-gated turn did not return to idle");
  63. // 软门禁不阻塞对话,且未信任 Hook 零执行。
  64. await $(sel(TID_V4_WORKSPACE_HOOK_PENDING_BANNER)).waitForDisplayed({
  65. timeout: 30000,
  66. });
  67. expect(await readHookTrustExecutions()).toHaveLength(0);
  68. // 从 pending banner 进入 设置 → 钩子。
  69. await clickTestIdByDom(TID_V4_WORKSPACE_HOOK_PENDING_REVIEW, {
  70. timeout: 15000,
  71. });
  72. await $(sel(TID_SETTINGS_PAGE)).waitForDisplayed({ timeout: 15000 });
  73. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "hooks"), {
  74. timeout: 15000,
  75. });
  76. await $(HOOKS_SECTION_SELECTOR).waitForDisplayed({ timeout: 15000 });
  77. // 三条 workspace Hook 声明全部未信任:只有“信任”按钮 + 锁定关闭的开关,
  78. // 没有其他审核动作(Allow once / Trust all / Keep blocked 均不存在)。
  79. await waitForHookTrustRowCount(3);
  80. for (const label of Object.values(E2E_HOOK_TRUST_LABELS)) {
  81. await assertUntrustedHookRow(label);
  82. }
  83. // 忽略 banner 是 renderer 本地行为,不产生 Trust mutation。
  84. await clickTestIdByDom(TID_SETTINGS_BACK_BUTTON, {
  85. timeout: 15000,
  86. });
  87. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 30000);
  88. await clickTestIdByDom(TID_V4_WORKSPACE_HOOK_PENDING_DISMISS, {
  89. timeout: 15000,
  90. });
  91. await $(sel(TID_V4_WORKSPACE_HOOK_PENDING_BANNER)).waitForDisplayed({
  92. timeout: 15000,
  93. reverse: true,
  94. });
  95. expect(await readHookTrustStore()).toBeNull();
  96. });
  97. it("HK06: 行内信任单条声明,只对该条精确持久落盘,其余行保持可审核", async function () {
  98. this.timeout(180000);
  99. await openHooksSettings();
  100. const trustButton = await findTrustButton(E2E_HOOK_TRUST_LABELS.sessionStartA);
  101. await trustButton.waitForDisplayed({ timeout: 15000 });
  102. await trustButton.click();
  103. const records = await waitForHookTrustStoreRecords(
  104. (items) => items.length === 1,
  105. "Trusting session-start-a did not persist exactly one store record",
  106. );
  107. expect(records[0]!.decision).toBe("trusted");
  108. expect(records[0]!.eventAtGrant).toBe("SessionStart");
  109. // 被信任的行按钮消失;其余两条仍显示“信任”。
  110. await assertNoTrustButton(E2E_HOOK_TRUST_LABELS.sessionStartA);
  111. for (const label of [
  112. E2E_HOOK_TRUST_LABELS.sessionStartB,
  113. E2E_HOOK_TRUST_LABELS.promptSubmit,
  114. ]) {
  115. await (await findTrustButton(label)).waitForDisplayed({ timeout: 15000 });
  116. }
  117. await closeHooksSettings();
  118. });
  119. it("HK07: 信任不补跑已跳过的 SessionStart,只作用于未来自然事件", async function () {
  120. this.timeout(240000);
  121. // 第一段(同 task):信任 UserPromptSubmit 后再发一条 prompt,
  122. // 出现 prompt-submit 记录 = 信任只作用于未来自然事件;
  123. // 全程无 SessionStart 记录 = 已信任的 session-start-a 不补跑。
  124. await openHooksSettings();
  125. await clickTrustButton(E2E_HOOK_TRUST_LABELS.promptSubmit);
  126. await waitForHookTrustStoreRecords(
  127. (items) => items.length === 2,
  128. "Trusting prompt-submit did not persist a second store record",
  129. );
  130. await closeHooksSettings();
  131. await sendV4Prompt("E2E_HOOK_TRUST second prompt: reply with ok");
  132. await waitForHookTrustExecution(
  133. E2E_HOOK_TRUST_LABELS.promptSubmit,
  134. "Trusted UserPromptSubmit hook did not run on next natural prompt",
  135. );
  136. await waitForIdle("Second turn did not return to idle");
  137. const inTaskExecutions = await readHookTrustExecutions();
  138. expect(
  139. inTaskExecutions.filter((item) => item.event === "SessionStart"),
  140. ).toHaveLength(0);
  141. expect(
  142. inTaskExecutions.some(
  143. (item) => item.label === E2E_HOOK_TRUST_LABELS.promptSubmit,
  144. ),
  145. ).toBe(true);
  146. // 第二段(新 task):catalog HK07 字面语义——新 task 的自然 SessionStart
  147. // 按 Trust 执行。该路径与 UserPromptSubmit 不同:覆盖新 session 冷启动时
  148. // 的配置发现、trust store 读取与 admission 评估全链路,不以"语义等价"省略。
  149. await startNewV4Draft();
  150. await sendV4Prompt("E2E_HOOK_TRUST new task: reply with ok");
  151. await waitForHookTrustExecution(
  152. E2E_HOOK_TRUST_LABELS.sessionStartA,
  153. "Trusted SessionStart hook did not run on the next natural session start",
  154. );
  155. await waitForIdle("New-task turn did not return to idle");
  156. const executions = await readHookTrustExecutions();
  157. // 已信任的 a 在新 task 自然 SessionStart 执行且仅执行一次;
  158. // 未信任的 b 仍被软门禁跳过。
  159. expect(
  160. executions.filter((item) => item.label === E2E_HOOK_TRUST_LABELS.sessionStartA),
  161. ).toHaveLength(1);
  162. expect(
  163. executions.some(
  164. (item) => item.label === E2E_HOOK_TRUST_LABELS.sessionStartB,
  165. ),
  166. ).toBe(false);
  167. });
  168. });
  169. async function waitForIdle(timeoutMsg: string): Promise<void> {
  170. await waitForV4Pane(
  171. (snapshot) =>
  172. snapshot.sessionId !== null &&
  173. snapshot.sessionId !== "draft" &&
  174. !snapshot.canStop,
  175. timeoutMsg,
  176. 60000,
  177. );
  178. }
  179. async function openHooksSettings(): Promise<void> {
  180. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON, {
  181. timeout: 15000,
  182. timeoutMsg: "Hook trust E2E could not open Settings",
  183. });
  184. await $(sel(TID_SETTINGS_PAGE)).waitForDisplayed({ timeout: 15000 });
  185. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "hooks"), {
  186. timeout: 15000,
  187. });
  188. await $(HOOKS_SECTION_SELECTOR).waitForDisplayed({ timeout: 15000 });
  189. }
  190. async function closeHooksSettings(): Promise<void> {
  191. await clickTestIdByDom(TID_SETTINGS_BACK_BUTTON, {
  192. timeout: 15000,
  193. timeoutMsg: "Hook trust E2E could not leave Settings",
  194. });
  195. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 30000);
  196. }
  197. async function waitForHookTrustRowCount(count: number): Promise<void> {
  198. await browser.waitUntil(
  199. () =>
  200. browser.execute(
  201. (expected: number) =>
  202. Array.from(
  203. document.querySelectorAll<HTMLElement>(
  204. '[data-testid="configured-hook-row"]',
  205. ),
  206. ).filter((row) => row.innerText.includes("hook-trust-")).length ===
  207. expected,
  208. count,
  209. ),
  210. {
  211. timeout: 15000,
  212. timeoutMsg: `Hooks settings did not render ${count} workspace hook rows`,
  213. },
  214. );
  215. }
  216. function hookRowSelector(label: string): string {
  217. return `${HOOK_ROW_SELECTOR}[contains(., "${label}")]`;
  218. }
  219. async function findTrustButton(label: string) {
  220. const row = await $(hookRowSelector(label));
  221. await row.waitForDisplayed({ timeout: 15000 });
  222. return await row.$(TRUST_BUTTON_IN_ROW);
  223. }
  224. async function clickTrustButton(label: string): Promise<void> {
  225. await (await findTrustButton(label)).click();
  226. }
  227. async function assertNoTrustButton(label: string): Promise<void> {
  228. const row = await $(hookRowSelector(label));
  229. await row.waitForDisplayed({ timeout: 15000 });
  230. await (await row.$(TRUST_BUTTON_IN_ROW)).waitForExist({
  231. timeout: 15000,
  232. reverse: true,
  233. });
  234. }
  235. async function assertUntrustedHookRow(label: string): Promise<void> {
  236. const row = await $(hookRowSelector(label));
  237. await row.waitForDisplayed({ timeout: 15000 });
  238. const buttons = await row.$$("button");
  239. // 行内动作只有“信任”按钮和 Switch 两个 button;不存在任何额外审核动作或徽章入口。
  240. expect(buttons).toHaveLength(2);
  241. const trustButton = await row.$(TRUST_BUTTON_IN_ROW);
  242. await trustButton.waitForDisplayed({ timeout: 15000 });
  243. expect(await trustButton.isEnabled()).toBe(true);
  244. // 未信任行 Switch 强制关闭并锁定。
  245. const switchControl = await row.$('button[role="switch"]');
  246. await switchControl.waitForDisplayed({ timeout: 15000 });
  247. expect(await switchControl.getAttribute("aria-checked")).toBe("false");
  248. expect(await switchControl.getAttribute("disabled")).not.toBeNull();
  249. }