hooksSection.test.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. import type { IHooksService } from "@zcode/services";
  2. import type { Hook } from "@zcode/shared";
  3. import { createElement } from "react";
  4. import { renderToStaticMarkup } from "react-dom/server";
  5. import { afterEach, describe, expect, it } from "vitest";
  6. import { TooltipProvider } from "@/components/ui/tooltip.js";
  7. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  8. import { HooksList, groupHookSections } from "@/settings/HooksList.js";
  9. import {
  10. buildPluginHookRows,
  11. filterPluginHooksByScope,
  12. isReadOnlyZCodeHook,
  13. resolveHookScopeTarget,
  14. resolveLatestReviewScopeTarget,
  15. } from "@/settings/HooksSection.js";
  16. import { useHooksStore } from "@/store/hooksStore.js";
  17. afterEach(() => {
  18. useHooksStore.setState({
  19. workspacePath: null,
  20. workspaceIdentity: null,
  21. authorityKey: null,
  22. loadedWorkspaceKey: null,
  23. hooks: [],
  24. loading: false,
  25. error: null,
  26. operatingHookId: null,
  27. });
  28. });
  29. describe("Hooks settings page", () => {
  30. it("keeps same-name plugin hooks in separate marketplace groups", () => {
  31. const sections = groupHookSections(
  32. [],
  33. [],
  34. [
  35. {
  36. pluginId: "computer-use@zcode-plugins-official",
  37. pluginName: "computer-use",
  38. detail: { event: "PreToolUse", type: "command", command: "official" },
  39. pluginEnabled: true,
  40. },
  41. {
  42. pluginId: "zcode-cua@custom-marketplace",
  43. pluginName: "zcode-cua",
  44. detail: { event: "PreToolUse", type: "command", command: "custom" },
  45. pluginEnabled: true,
  46. },
  47. ],
  48. );
  49. expect(sections.filter((section) => section.kind === "plugin")).toHaveLength(2);
  50. expect(
  51. sections
  52. .filter((section) => section.kind === "plugin")
  53. .flatMap((section) => section.hooks.map((hook) => hook.pluginId)),
  54. ).toEqual(["computer-use@zcode-plugins-official", "zcode-cua@custom-marketplace"]);
  55. });
  56. it("keeps a selected local workspace scope on the base host while a remote workspace is active", () => {
  57. const activePath = "/home/dev/project";
  58. const activeIdentity = "remote:server:source-1:/home/dev/project";
  59. // 选中的本地项目没有 identity,不能借用活动远端工作区的 identity 把本机路径发往远端。
  60. expect(
  61. resolveHookScopeTarget(
  62. { workspacePath: "/Users/dev/local", workspaceIdentity: undefined },
  63. activePath,
  64. activeIdentity,
  65. ),
  66. ).toEqual({ workspacePath: "/Users/dev/local", workspaceIdentity: undefined });
  67. // 选中远端项目时沿用其自身 identity。
  68. expect(
  69. resolveHookScopeTarget(
  70. { workspacePath: "/srv/other", workspaceIdentity: "remote:server:source-2:/srv/other" },
  71. activePath,
  72. activeIdentity,
  73. ),
  74. ).toEqual({
  75. workspacePath: "/srv/other",
  76. workspaceIdentity: "remote:server:source-2:/srv/other",
  77. });
  78. // User scope 未选项目:仍跟随当前 workspace 路由。
  79. expect(resolveHookScopeTarget(undefined, activePath, activeIdentity)).toEqual({
  80. workspacePath: activePath,
  81. workspaceIdentity: activeIdentity,
  82. });
  83. });
  84. it("does not classify plugin hooks as user when overview scope is unavailable", () => {
  85. const pluginRows = buildPluginHookRows(
  86. [
  87. {
  88. id: "quality@official",
  89. name: "quality",
  90. enabled: true,
  91. hookDetails: [
  92. {
  93. event: "PreToolUse",
  94. type: "command",
  95. command: "./check.sh",
  96. sourcePath: "/plugins/quality/hooks/hooks.json",
  97. runnable: true,
  98. },
  99. ],
  100. },
  101. ],
  102. [],
  103. false,
  104. );
  105. expect(pluginRows).toHaveLength(1);
  106. expect(pluginRows[0]?.pluginScope).toBeUndefined();
  107. expect(filterPluginHooksByScope(pluginRows, "user")).toEqual(pluginRows);
  108. expect(filterPluginHooksByScope(pluginRows, "project")).toEqual(pluginRows);
  109. const workspaceRows = buildPluginHookRows(
  110. [
  111. {
  112. id: "quality@official",
  113. name: "quality",
  114. enabled: true,
  115. hookDetails: pluginRows.map((row) => row.detail),
  116. },
  117. ],
  118. [{ id: "quality@official", scope: "workspace" }],
  119. true,
  120. );
  121. expect(filterPluginHooksByScope(workspaceRows, "user")).toEqual([]);
  122. expect(filterPluginHooksByScope(workspaceRows, "project")).toEqual(workspaceRows);
  123. });
  124. it("persists per-hook enabled state through saveHooks with workspace identity", async () => {
  125. const saved: Array<Parameters<IHooksService["saveHooks"]>[0]> = [];
  126. let persistedHooks = [configuredHook()];
  127. const service: IHooksService = {
  128. async loadHooks() {
  129. return {
  130. hooks: persistedHooks,
  131. hooksEnabled: persistedHooks.some((hook) => hook.enabled),
  132. };
  133. },
  134. async saveHooks(params) {
  135. saved.push(params);
  136. persistedHooks = params.hooks;
  137. },
  138. };
  139. useHooksStore.setState({
  140. workspacePath: "/workspace",
  141. workspaceIdentity: "ssh://dev/workspace",
  142. hooks: persistedHooks,
  143. });
  144. await useHooksStore.getState().toggleHook("configured", false, service);
  145. expect(saved).toHaveLength(1);
  146. expect(saved[0]).toMatchObject({
  147. workspacePath: "/workspace",
  148. workspaceIdentity: "ssh://dev/workspace",
  149. });
  150. expect(saved[0]?.hooks[0]).toMatchObject({
  151. id: "configured",
  152. enabled: false,
  153. });
  154. });
  155. // Bugfix 回归:store 是单例,慢 workspace 的加载结果后到时曾无条件覆盖当前投影,
  156. // 之后任一写操作会把上一个 workspace 的整组 hooks 落盘到当前 workspace。
  157. it("discards a stale hooks load after the workspace scope changed", async () => {
  158. const hookA: Hook = { ...configuredHook(), id: "hook-a", command: "echo a" };
  159. const hookB: Hook = { ...configuredHook(), id: "hook-b", command: "echo b" };
  160. let releaseSlowLoad: (() => void) | undefined;
  161. const slowLoad = new Promise<void>((resolve) => {
  162. releaseSlowLoad = resolve;
  163. });
  164. const saved: Array<Parameters<IHooksService["saveHooks"]>[0]> = [];
  165. const service: IHooksService = {
  166. async loadHooks(params) {
  167. if (params.workspacePath === "/workspace-a") {
  168. await slowLoad;
  169. return { hooks: [hookA], hooksEnabled: true };
  170. }
  171. return { hooks: [hookB], hooksEnabled: true };
  172. },
  173. async saveHooks(params) {
  174. saved.push(params);
  175. },
  176. };
  177. const slowInitialize = useHooksStore.getState().initialize("/workspace-a", undefined, service);
  178. await useHooksStore.getState().initialize("/workspace-b", undefined, service);
  179. releaseSlowLoad?.();
  180. await slowInitialize;
  181. expect(useHooksStore.getState().loadedWorkspaceKey).toBe("/workspace-b");
  182. expect(useHooksStore.getState().hooks.map((hook) => hook.id)).toEqual(["hook-b"]);
  183. await useHooksStore.getState().toggleHook("hook-b", false, service);
  184. expect(saved).toHaveLength(1);
  185. expect(saved[0]?.workspacePath).toBe("/workspace-b");
  186. expect(saved[0]?.hooks.map((hook) => hook.id)).toEqual(["hook-b"]);
  187. });
  188. it("imports a compatibility hook as an enabled ZCode hook", async () => {
  189. const legacy: Hook = {
  190. id: "legacy",
  191. event: "Stop",
  192. type: "command",
  193. command: "echo legacy",
  194. enabled: false,
  195. location: {
  196. source: "claude",
  197. scope: "project",
  198. directoryPath: "/workspace/.claude",
  199. projectPath: "/workspace",
  200. },
  201. };
  202. let persistedHooks = [legacy];
  203. const service: IHooksService = {
  204. async loadHooks() {
  205. return {
  206. hooks: persistedHooks,
  207. hooksEnabled: persistedHooks.some((hook) => hook.enabled),
  208. };
  209. },
  210. async saveHooks(params) {
  211. persistedHooks = params.hooks;
  212. },
  213. };
  214. useHooksStore.setState({
  215. workspacePath: "/workspace",
  216. hooks: persistedHooks,
  217. });
  218. await useHooksStore.getState().importHook("legacy", service);
  219. expect(persistedHooks).toContainEqual(
  220. expect.objectContaining({
  221. event: "Stop",
  222. command: "echo legacy",
  223. enabled: true,
  224. location: expect.objectContaining({
  225. source: "zcode",
  226. scope: "project",
  227. }),
  228. }),
  229. );
  230. });
  231. it("groups direct, plugin, and compatibility hooks by source", () => {
  232. const legacy: Hook = {
  233. id: "legacy",
  234. event: "Stop",
  235. type: "command",
  236. command: "echo legacy",
  237. enabled: false,
  238. location: {
  239. source: "claude",
  240. scope: "project",
  241. directoryPath: "/workspace/.claude",
  242. },
  243. };
  244. const sections = groupHookSections(
  245. [configuredHook(), { ...configuredHook(), id: "configured-2", event: "Stop" }],
  246. [legacy],
  247. [
  248. {
  249. pluginId: "quality@official",
  250. pluginName: "quality",
  251. pluginEnabled: true,
  252. pluginScope: undefined,
  253. detail: {
  254. event: "PostToolUse",
  255. matcher: "Write",
  256. type: "command",
  257. command: "./check.sh",
  258. sourcePath: "/plugins/quality/hooks/hooks.json",
  259. runnable: true,
  260. },
  261. },
  262. ],
  263. );
  264. expect(sections.map((section) => section.kind)).toEqual(["installed", "plugin", "legacy"]);
  265. expect(sections[1]).toMatchObject({ title: "quality", count: 1 });
  266. const html = renderToStaticMarkup(
  267. createElement(
  268. ZCodeIntlProvider,
  269. { initialLocale: "en-US" },
  270. createElement(
  271. TooltipProvider,
  272. null,
  273. createElement(HooksList, {
  274. compatibilityHooks: [legacy],
  275. editableHooks: [
  276. configuredHook(),
  277. { ...configuredHook(), id: "configured-2", event: "Stop" },
  278. ],
  279. operatingHookId: null,
  280. pluginHooks: [
  281. {
  282. pluginId: "quality@official",
  283. pluginName: "quality",
  284. pluginEnabled: true,
  285. pluginScope: undefined,
  286. pluginIconItem: { name: "quality", listing: undefined },
  287. detail: {
  288. event: "PostToolUse",
  289. matcher: "Write",
  290. type: "command",
  291. command: "./check.sh",
  292. sourcePath: "/plugins/quality/hooks/hooks.json",
  293. runnable: true,
  294. },
  295. },
  296. ],
  297. onDelete: async () => {},
  298. onEdit: () => {},
  299. onImport: async () => {},
  300. onToggle: async () => {},
  301. installedAction: createElement("button", null, "New"),
  302. showInstalledSection: true,
  303. }),
  304. ),
  305. ),
  306. );
  307. expect(html).toContain("PreToolUse");
  308. expect(html).toContain("Stop");
  309. expect(html).toContain("Installed");
  310. expect(html).toContain("New");
  311. expect(html).toContain("Legacy");
  312. expect(html).toContain("PostToolUse");
  313. expect(html).toContain("quality");
  314. expect(html).toContain("Scope unknown");
  315. expect(html).toContain("plugin-hook-row");
  316. });
  317. it("omits empty groups after filtering", () => {
  318. expect(groupHookSections([], [], [])).toEqual([]);
  319. });
  320. // workspace-hook-trust:未信任的工作区 Hook 在任何 review 状态下都显示行内 Trust 按钮;
  321. // 信任状态不通过徽章展示,避免与运行时权威投影产生第二事实来源。
  322. it("无需活跃 review 也给未信任 Workspace Hook 显示行内信任按钮", () => {
  323. const untrusted = {
  324. ...workspaceConfiguredHook("pending_trust"),
  325. id: "untrusted-disabled-row",
  326. enabled: false,
  327. workspaceHook: {
  328. ...workspaceConfiguredHook("pending_trust").workspaceHook!,
  329. configuredEnabled: false,
  330. reviewItemId: "untrusted-review-item",
  331. },
  332. };
  333. const trusted = {
  334. ...workspaceConfiguredHook("trusted_persistent"),
  335. id: "trusted-hook",
  336. workspaceHook: {
  337. ...workspaceConfiguredHook("trusted_persistent").workspaceHook!,
  338. reviewItemId: "trusted-hook",
  339. },
  340. };
  341. const html = renderToStaticMarkup(
  342. createElement(
  343. ZCodeIntlProvider,
  344. { initialLocale: "en-US" },
  345. createElement(
  346. TooltipProvider,
  347. null,
  348. createElement(HooksList, {
  349. compatibilityHooks: [],
  350. editableHooks: [untrusted, trusted],
  351. operatingHookId: null,
  352. pluginHooks: [],
  353. trustActionAvailable: true,
  354. trustingHookId: null,
  355. onEdit: () => {},
  356. onImport: async () => {},
  357. onToggle: async () => {},
  358. onTrust: async () => {},
  359. }),
  360. ),
  361. ),
  362. );
  363. expect(html.match(/>Trust</gu)).toHaveLength(1);
  364. });
  365. it("未信任 Hook 的有效运行开关固定关闭并锁定,且不修改传入配置状态", () => {
  366. const untrustedConfiguredOn = workspaceConfiguredHook("pending_trust");
  367. const html = renderToStaticMarkup(
  368. createElement(
  369. ZCodeIntlProvider,
  370. { initialLocale: "en-US" },
  371. createElement(
  372. TooltipProvider,
  373. null,
  374. createElement(HooksList, {
  375. compatibilityHooks: [],
  376. editableHooks: [untrustedConfiguredOn],
  377. operatingHookId: null,
  378. pluginHooks: [],
  379. trustActionAvailable: false,
  380. trustingHookId: null,
  381. onEdit: () => {},
  382. onImport: async () => {},
  383. onToggle: async () => {},
  384. onTrust: async () => {},
  385. }),
  386. ),
  387. ),
  388. );
  389. expect(untrustedConfiguredOn.enabled).toBe(true);
  390. expect(html).toContain(">Trust<");
  391. expect(html).toMatch(/<button[^>]*disabled=""[^>]*>[^<]*<svg[^>]*lucide-shield-check/su);
  392. expect(html).toMatch(/role="switch"[^>]*aria-checked="false"[^>]*disabled=""/u);
  393. });
  394. it("持久信任后隐藏 Trust,并恢复配置开关的真实值与可操作性", () => {
  395. const html = renderToStaticMarkup(
  396. createElement(
  397. ZCodeIntlProvider,
  398. { initialLocale: "en-US" },
  399. createElement(
  400. TooltipProvider,
  401. null,
  402. createElement(HooksList, {
  403. compatibilityHooks: [],
  404. editableHooks: [workspaceConfiguredHook("trusted_persistent")],
  405. operatingHookId: null,
  406. pluginHooks: [],
  407. trustActionAvailable: true,
  408. trustingHookId: null,
  409. onEdit: () => {},
  410. onImport: async () => {},
  411. onToggle: async () => {},
  412. onTrust: async () => {},
  413. }),
  414. ),
  415. ),
  416. );
  417. expect(html).not.toContain(">Trust<");
  418. expect(html).toMatch(/role="switch"[^>]*aria-checked="true"/u);
  419. expect(html).not.toMatch(/role="switch"[^>]*disabled=""/u);
  420. });
  421. // P3 回归:editable=false 的上游/祖先 zcode.json Hook 是只读工作区 Hook,必须留在
  422. // Installed 分组走行内 Trust,而不是进 Legacy 显示必然失败的 Import。
  423. it("readonly zcode workspace hook stays in installed section with trust, not legacy import", () => {
  424. const readOnlyHook: Hook = {
  425. ...workspaceConfiguredHook("pending_trust"),
  426. editable: false,
  427. location: {
  428. source: "zcode",
  429. scope: "project",
  430. directoryPath: "/ancestor/.zcode",
  431. projectPath: "/workspace",
  432. },
  433. };
  434. expect(isReadOnlyZCodeHook(readOnlyHook)).toBe(true);
  435. expect(isReadOnlyZCodeHook(workspaceConfiguredHook("pending_trust"))).toBe(false);
  436. const html = renderToStaticMarkup(
  437. createElement(
  438. ZCodeIntlProvider,
  439. { initialLocale: "en-US" },
  440. createElement(
  441. TooltipProvider,
  442. null,
  443. createElement(HooksList, {
  444. compatibilityHooks: [],
  445. editableHooks: [readOnlyHook],
  446. operatingHookId: null,
  447. pluginHooks: [],
  448. trustActionAvailable: true,
  449. trustingHookId: null,
  450. onEdit: () => {},
  451. onImport: async () => {},
  452. onToggle: async () => {},
  453. onTrust: async () => {},
  454. }),
  455. ),
  456. ),
  457. );
  458. expect(html).toContain("Installed");
  459. expect(html).not.toContain("Legacy");
  460. expect(html).toContain(">Trust<");
  461. expect(html).not.toContain(">Import<");
  462. // 只读行未信任:Switch 强制关闭并禁用(readOnly 且 requiresTrust 双重锁定)。
  463. expect(html).toMatch(/role="switch"[^>]*aria-checked="false"[^>]*disabled=""/u);
  464. });
  465. // P2 回归:refresh 发起时 target 已不是当前 workspace,必须整体放弃——
  466. // 不得 set({loading:true}),否则结果因 key 不匹配被丢弃后 loading 永远不复位。
  467. it("discards a stale refresh before it starts and never claims loading", async () => {
  468. let releaseLoad: (() => void) | undefined;
  469. const gate = new Promise<void>((resolve) => {
  470. releaseLoad = resolve;
  471. });
  472. let loadCalls = 0;
  473. const service: IHooksService = {
  474. async loadHooks() {
  475. loadCalls += 1;
  476. await gate;
  477. return { hooks: [], hooksEnabled: false };
  478. },
  479. async saveHooks() {},
  480. };
  481. // store 当前停在 workspace B;refresh 携带的却是 A 的三元组(Trust 等待期间切走)。
  482. useHooksStore.setState({ workspacePath: "/workspace-b", workspaceIdentity: null });
  483. const pending = useHooksStore.getState().refresh(service, { workspacePath: "/workspace-a" });
  484. // 同步段过后 loading 必须仍为 false:过期请求无权占用全局 loading。
  485. expect(useHooksStore.getState().loading).toBe(false);
  486. releaseLoad?.();
  487. await pending;
  488. expect(loadCalls).toBe(0);
  489. expect(useHooksStore.getState().loading).toBe(false);
  490. });
  491. // P2 回归:refresh 等待期间切走 → 结果丢弃,但 loading 归新 workspace 的
  492. // initialize 所有,丢弃路径不得把 loading 复位成影响新投影的状态。
  493. it("leaves loading ownership to the new workspace when refresh target drifts mid-flight", async () => {
  494. let releaseLoad: (() => void) | undefined;
  495. const gate = new Promise<void>((resolve) => {
  496. releaseLoad = resolve;
  497. });
  498. const service: IHooksService = {
  499. async loadHooks(params) {
  500. if (params.workspacePath === "/workspace-a") {
  501. await gate;
  502. return { hooks: [], hooksEnabled: false };
  503. }
  504. return { hooks: [configuredHook()], hooksEnabled: true };
  505. },
  506. async saveHooks() {},
  507. };
  508. useHooksStore.setState({ workspacePath: "/workspace-a", workspaceIdentity: null });
  509. const refreshA = useHooksStore.getState().refresh(service, { workspacePath: "/workspace-a" });
  510. // A 的 load 挂起期间切到 B 并完成 B 的 initialize(loading=true 属于 B 的加载)。
  511. const initB = useHooksStore.getState().initialize("/workspace-b", undefined, service);
  512. releaseLoad?.();
  513. await Promise.all([refreshA, initB]);
  514. // B 的投影完好,A 的过期结果没有污染;loading 最终由 B 的生命周期复位。
  515. expect(useHooksStore.getState().loadedWorkspaceKey).toBe("/workspace-b");
  516. expect(useHooksStore.getState().loading).toBe(false);
  517. });
  518. });
  519. describe("resolveLatestReviewScopeTarget", () => {
  520. const tabs = [
  521. { workspacePath: "/local/repo", workspaceIdentity: undefined },
  522. { workspacePath: "/remote/repo", workspaceIdentity: "ssh://dev/remote/repo" },
  523. ];
  524. it("maps the latest binding to a known workspace scope key by identity", () => {
  525. const target = resolveLatestReviewScopeTarget(
  526. {
  527. s1: {
  528. request: {
  529. interactionId: "i-1",
  530. createdAt: 1,
  531. workspaceIdentity: "ssh://dev/remote/repo",
  532. },
  533. workspacePath: "/remote/repo",
  534. },
  535. },
  536. tabs,
  537. );
  538. expect(target).toEqual({ interactionId: "i-1", scopeKey: "ssh://dev/remote/repo" });
  539. });
  540. it("falls back to workspacePath matching only for identity-less legacy bindings", () => {
  541. const target = resolveLatestReviewScopeTarget(
  542. {
  543. s1: {
  544. request: { interactionId: "i-2", createdAt: 2, workspaceIdentity: undefined },
  545. workspacePath: "/local/repo",
  546. },
  547. },
  548. tabs,
  549. );
  550. expect(target).toEqual({ interactionId: "i-2", scopeKey: "/local/repo" });
  551. });
  552. it("picks the newest interaction when multiple bindings exist", () => {
  553. const target = resolveLatestReviewScopeTarget(
  554. {
  555. s1: {
  556. request: {
  557. interactionId: "i-old",
  558. createdAt: 1,
  559. workspaceIdentity: "ssh://dev/remote/repo",
  560. },
  561. workspacePath: "/remote/repo",
  562. },
  563. s2: {
  564. request: { interactionId: "i-new", createdAt: 5, workspaceIdentity: undefined },
  565. workspacePath: "/local/repo",
  566. },
  567. },
  568. tabs,
  569. );
  570. expect(target).toEqual({ interactionId: "i-new", scopeKey: "/local/repo" });
  571. });
  572. // P2 回归:binding 指向已关闭/未知的 workspace 时返回 null——调用方不得
  573. // 把 scope 切过去(否则设置页会持续停在 Connecting,且与失效 scope 重置 user
  574. // 的 effect 形成乒乓循环)。
  575. it("returns null when the binding workspace no longer exists in tabs", () => {
  576. const target = resolveLatestReviewScopeTarget(
  577. {
  578. s1: {
  579. request: { interactionId: "i-gone", createdAt: 3, workspaceIdentity: "ssh://gone/repo" },
  580. workspacePath: "/gone/repo",
  581. },
  582. },
  583. tabs,
  584. );
  585. expect(target).toBeNull();
  586. });
  587. it("returns null when there are no bindings", () => {
  588. expect(resolveLatestReviewScopeTarget({}, tabs)).toBeNull();
  589. });
  590. });
  591. function configuredHook(): Hook {
  592. return {
  593. id: "configured",
  594. event: "PreToolUse",
  595. matcher: "Write",
  596. type: "process",
  597. command: "node",
  598. args: ["hook.mjs"],
  599. enabled: true,
  600. location: {
  601. source: "zcode",
  602. scope: "project",
  603. directoryPath: "/workspace/.zcode",
  604. projectPath: "/workspace",
  605. },
  606. };
  607. }
  608. function workspaceConfiguredHook(
  609. trustState: NonNullable<NonNullable<Hook["workspaceHook"]>["trustState"]>,
  610. ): Hook {
  611. return {
  612. ...configuredHook(),
  613. workspaceHook: {
  614. reviewItemId: "configured",
  615. workspaceIdentity: "local:/workspace",
  616. bundleDigest: "b".repeat(64),
  617. hookDeclarationDigest: "a".repeat(64),
  618. sourceFileIndex: 0,
  619. sourceRootEnabled: true,
  620. declarationEnabled: true,
  621. runtimeHooksEnabled: true,
  622. configuredEnabled: true,
  623. sourcePath: ".zcode/config.json",
  624. trustState,
  625. },
  626. };
  627. }