conversation-session-remote-data-sync-hook-push.test.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { mkdir, readFile, writeFile } from "node:fs/promises";
  2. import { dirname, join } from "node:path";
  3. import {
  4. TID_SETTINGS_PAGE,
  5. TID_SETTINGS_SECTION_NAV,
  6. TID_TASK_SETTINGS_BUTTON,
  7. testId,
  8. } from "@zcode/shared/test-ids";
  9. import {
  10. clearAppData,
  11. clickTestIdByDom,
  12. getE2EAppDataPaths,
  13. waitForTestIdByDom,
  14. } from "#e2e/helpers/desktop-app";
  15. import {
  16. readRemoteContainerFile,
  17. readRemoteDockerContainer,
  18. remoteContainerPathExists,
  19. writeRemoteContainerFile,
  20. } from "#e2e/helpers/remote-docker-target";
  21. import {
  22. connectSSHWorkspace,
  23. prepareSSHConnectionPage,
  24. readSSHRuntimeConfig,
  25. } from "#e2e/helpers/ssh-remote-p0";
  26. import { prepareV4ConversationE2E } from "#e2e/helpers/v4-conversation";
  27. // DSYNC-11:设置 → 数据同步 → 设备 → 推送钩子(docs/remote/data-sync.md「钩子」)。
  28. // 只同步本机 ~/.zcode/cli/config.json 的 hooks;写入时合并到远端 events[事件] 中 matcher 相同的分组,
  29. // 远端已有钩子和其他配置原样保留,根级 enabled 只开不关。远端断言用 docker exec 读容器内 config.json。
  30. const CASE_MARKER = "E2E_DSYNC_11_HOOK_PUSH";
  31. const REMOTE_CLI_CONFIG = "/home/dev/.zcode/cli/config.json";
  32. const PUSHED_HOOK = {
  33. type: "command",
  34. command: `echo ${CASE_MARKER} post-bash`,
  35. timeout: 30,
  36. statusMessage: "E2E hook",
  37. };
  38. const REMOTE_EXISTING_BASH = { type: "command", command: `echo ${CASE_MARKER} remote-bash` };
  39. const REMOTE_EXISTING_EDIT = { type: "command", command: `echo ${CASE_MARKER} remote-edit` };
  40. const REMOTE_PRE_TOOL = { type: "command", command: `echo ${CASE_MARKER} remote-pre` };
  41. type HookConfigFile = Record<string, unknown> & {
  42. hooks?: Record<string, unknown> & { enabled?: boolean; events?: Record<string, unknown> };
  43. };
  44. /** 钩子行的 key 是规范化哈希:按行内显示的完整命令找到该行。 */
  45. async function readHookRow(command: string) {
  46. return await browser.execute((text) => {
  47. const row = [
  48. ...document.querySelectorAll<HTMLButtonElement>('[data-testid^="remote-sync-row-"]'),
  49. ].find((candidate) => candidate.textContent?.includes(text));
  50. return row
  51. ? {
  52. key: row.dataset.syncKey ?? null,
  53. status: row.dataset.syncStatus ?? null,
  54. checked: row.getAttribute("aria-checked"),
  55. text: row.textContent ?? "",
  56. }
  57. : null;
  58. }, command);
  59. }
  60. async function waitForHookRowStatus(command: string, status: string, timeoutMsg: string) {
  61. await browser.waitUntil(async () => (await readHookRow(command))?.status === status, {
  62. timeout: 60_000,
  63. timeoutMsg: `${CASE_MARKER}: ${timeoutMsg}`,
  64. });
  65. return (await readHookRow(command))!;
  66. }
  67. /** 本机 config.json 读改写:只写 hooks,保留 E2E 预置的其他配置。 */
  68. async function writeLocalHooks(homeDir: string) {
  69. const configPath = join(homeDir, ".zcode", "cli", "config.json");
  70. let config: Record<string, unknown> = {};
  71. try {
  72. config = JSON.parse(await readFile(configPath, "utf-8")) as Record<string, unknown>;
  73. } catch {
  74. config = {};
  75. }
  76. await mkdir(dirname(configPath), { recursive: true });
  77. await writeFile(
  78. configPath,
  79. JSON.stringify(
  80. {
  81. ...config,
  82. hooks: {
  83. enabled: true,
  84. events: { PostToolUse: [{ matcher: "Bash", hooks: [PUSHED_HOOK] }] },
  85. },
  86. },
  87. null,
  88. 2,
  89. ),
  90. );
  91. }
  92. /** 远端 config.json 读改写:保留连接过程已写入的字段,预置远端自己的钩子(根级关闭)。 */
  93. async function seedRemoteHooks(container: string): Promise<HookConfigFile> {
  94. const existing = (await remoteContainerPathExists(container, REMOTE_CLI_CONFIG))
  95. ? (JSON.parse(await readRemoteContainerFile(container, REMOTE_CLI_CONFIG)) as HookConfigFile)
  96. : {};
  97. const seeded: HookConfigFile = {
  98. ...existing,
  99. hooks: {
  100. enabled: false,
  101. timeoutMs: 45_000,
  102. events: {
  103. PostToolUse: [
  104. { matcher: "Edit", hooks: [REMOTE_EXISTING_EDIT] },
  105. { matcher: "Bash", hooks: [REMOTE_EXISTING_BASH] },
  106. ],
  107. PreToolUse: [{ hooks: [REMOTE_PRE_TOOL] }],
  108. },
  109. },
  110. };
  111. await writeRemoteContainerFile(container, REMOTE_CLI_CONFIG, JSON.stringify(seeded, null, 2));
  112. return seeded;
  113. }
  114. describe(`${CASE_MARKER}: 数据同步页推送钩子`, () => {
  115. after(async () => {
  116. await browser.electron.restoreAllMocks();
  117. await clearAppData();
  118. });
  119. it("DSYNC-11-01 推送一条 PostToolUse 钩子,合并进远端同 matcher 分组,远端原有钩子与配置保留,根级开关被打开", async function () {
  120. this.timeout(20 * 60_000);
  121. const config = readSSHRuntimeConfig(CASE_MARKER);
  122. const container = readRemoteDockerContainer(CASE_MARKER);
  123. const workspacePath = config.workspacePaths[0];
  124. const { homeDir } = getE2EAppDataPaths();
  125. await prepareSSHConnectionPage(CASE_MARKER);
  126. await prepareV4ConversationE2E({ skipProvider: true });
  127. await connectSSHWorkspace({ caseMarker: CASE_MARKER, config, workspacePath });
  128. await writeLocalHooks(homeDir);
  129. const seeded = await seedRemoteHooks(container);
  130. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON, { timeout: 30_000 });
  131. await waitForTestIdByDom(TID_SETTINGS_PAGE, { timeout: 30_000 });
  132. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "dataSync"), {
  133. timeout: 30_000,
  134. timeoutMsg: `${CASE_MARKER}: 设置页没有数据同步分区`,
  135. });
  136. await waitForTestIdByDom("remote-sync-device-list", {
  137. timeout: 60_000,
  138. timeoutMsg: `${CASE_MARKER}: 数据同步页没有在线设备`,
  139. });
  140. await clickTestIdByDom("remote-sync-device-action");
  141. await waitForTestIdByDom("remote-sync-dialog", { timeout: 30_000 });
  142. // 概览有钩子一行;分类页该钩子为「推送」且默认勾选,行内显示事件、matcher 与完整命令。
  143. await waitForTestIdByDom("remote-sync-overview-hooks", {
  144. timeout: 60_000,
  145. timeoutMsg: `${CASE_MARKER}: 概览没有钩子一行`,
  146. });
  147. await clickTestIdByDom("remote-sync-tab-hooks");
  148. const row = await waitForHookRowStatus(PUSHED_HOOK.command, "push", "钩子没有显示为「推送」");
  149. expect(row.checked).toBe("true");
  150. expect(row.text).toContain("PostToolUse · Bash");
  151. expect(row.text).toContain(PUSHED_HOOK.command);
  152. await clickTestIdByDom("remote-sync-push", {
  153. timeout: 30_000,
  154. timeoutMsg: `${CASE_MARKER}: 推送钩子按钮不可用`,
  155. });
  156. await waitForHookRowStatus(PUSHED_HOOK.command, "synced", "推送后钩子没有变为「已同步」");
  157. const remoteConfig = JSON.parse(
  158. await readRemoteContainerFile(container, REMOTE_CLI_CONFIG),
  159. ) as HookConfigFile;
  160. // 其他配置字段原样保留。
  161. const { hooks: _seededHooks, ...seededRest } = seeded;
  162. const { hooks: remoteHooks, ...remoteRest } = remoteConfig;
  163. expect(remoteRest).toEqual(seededRest);
  164. expect(remoteHooks).toEqual({
  165. enabled: true,
  166. timeoutMs: 45_000,
  167. events: {
  168. PostToolUse: [
  169. { matcher: "Edit", hooks: [REMOTE_EXISTING_EDIT] },
  170. { matcher: "Bash", hooks: [REMOTE_EXISTING_BASH, PUSHED_HOOK] },
  171. ],
  172. PreToolUse: [{ hooks: [REMOTE_PRE_TOOL] }],
  173. },
  174. });
  175. });
  176. });