conversation-session-r3-desktop-settings-authority.test.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
  2. import { join, resolve } from "node:path";
  3. import {
  4. TID_SETTINGS_BACK_BUTTON,
  5. TID_SETTINGS_DATA_BASE_DIR_BROWSE,
  6. TID_SETTINGS_DATA_BASE_DIR_INPUT,
  7. TID_SETTINGS_DATA_BASE_DIR_SAVE,
  8. TID_SETTINGS_DATA_BASE_DIR_STATUS,
  9. TID_SETTINGS_PAGE,
  10. TID_SETTINGS_SECTION_NAV,
  11. TID_TASK_SETTINGS_BUTTON,
  12. testId,
  13. } from "@zcode/shared/test-ids";
  14. import {
  15. clearAppData,
  16. clickTestIdByDom,
  17. getE2EAppDataPaths,
  18. waitForTestIdByDom,
  19. } from "#e2e/helpers/desktop-app";
  20. import { resolveE2ERuntimePath } from "#e2e/helpers/e2e-runtime-paths";
  21. import { reloadElectronSessionPreservingBrowserProfile } from "#e2e/helpers/e2e-electron-reload";
  22. import { waitForRendererAfterColdRestart } from "#e2e/helpers/ssh-remote-lifecycle";
  23. import { prepareV4ConversationE2E } from "#e2e/helpers/v4-conversation";
  24. import {
  25. clickLastEnabledSSHDialogButton,
  26. fillSSHPasswordSettings,
  27. openSSHRemoteDialog,
  28. readSSHRuntimeConfig,
  29. runSSHCommand,
  30. completeSSHProjectConnection,
  31. } from "#e2e/helpers/ssh-remote-p0";
  32. const CASE_MARKER = "E2E_R3_DESKTOP_SETTINGS_AUTHORITY";
  33. const paths = getE2EAppDataPaths();
  34. const customRoot = resolveE2ERuntimePath("r3-desktop-settings-authority", "data");
  35. const artifactDir = resolve(
  36. process.env.ZCODE_E2E_ARTIFACT_DIR ?? ".e2e-artifacts/r3-desktop-settings-authority",
  37. );
  38. const hardwareSelector =
  39. '[role="switch"][aria-label="Chrome 硬件加速"], [role="switch"][aria-label="Chrome hardware acceleration"]';
  40. const certificateSelector =
  41. '[role="switch"][aria-label="忽略证书校验"], [role="switch"][aria-label="Ignore certificate errors"]';
  42. const canary = "r3-desktop-settings-canary.txt";
  43. async function openSection(section: string) {
  44. if (!(await $(`[data-testid="${TID_SETTINGS_PAGE}"]`).isExisting())) {
  45. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON, { timeout: 15_000 });
  46. await waitForTestIdByDom(TID_SETTINGS_PAGE, { timeout: 15_000 });
  47. }
  48. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, section), { timeout: 15_000 });
  49. }
  50. async function localSettings(): Promise<Record<string, unknown>> {
  51. return JSON.parse(await readFile(join(paths.appDataDir, "setting.json"), "utf8"));
  52. }
  53. async function setSwitch(selector: string, checked: boolean) {
  54. const control = $(selector);
  55. await control.waitForExist({ timeout: 15_000 });
  56. if ((await control.getAttribute("aria-checked")) !== String(checked)) {
  57. await control.scrollIntoView();
  58. await control.click();
  59. }
  60. await browser.waitUntil(
  61. async () => (await control.getAttribute("aria-checked")) === String(checked),
  62. { timeout: 15_000 },
  63. );
  64. }
  65. describe(`${CASE_MARKER}: 远端激活时桌面设置仍属于本机`, () => {
  66. const facts: Record<string, unknown> = {};
  67. after(async () => {
  68. await mkdir(artifactDir, { recursive: true });
  69. await writeFile(join(artifactDir, "settings-authority.json"), JSON.stringify(facts, null, 2));
  70. await browser.electron.restoreAllMocks();
  71. // 冷重启后的 Main/Host 仍会写 customRoot,提前 rm 会与写入竞争并报 ENOTEMPTY。
  72. // 测试数据均在隔离 HOME 内,交给 runner 在全部进程退出后统一清理。
  73. await clearAppData();
  74. });
  75. it("R3A-DEV-06-desktop-authority 保存本机设置与数据目录,Source 不被污染,冷重启读取本机值", async function () {
  76. this.timeout(8 * 60_000);
  77. const config = readSSHRuntimeConfig(CASE_MARKER);
  78. await prepareV4ConversationE2E({ skipProvider: true });
  79. await openSSHRemoteDialog(CASE_MARKER);
  80. await fillSSHPasswordSettings(config);
  81. await clickLastEnabledSSHDialogButton(CASE_MARKER);
  82. const conversation = await completeSSHProjectConnection(config, CASE_MARKER);
  83. facts.workspaceIdentity = conversation.workspaceIdentity;
  84. const readSourceSettings = async (): Promise<Record<string, unknown>> => {
  85. const result = await runSSHCommand(config, 'cat "$HOME/.zcode/v2/setting.json"');
  86. expect(result.code).toBe(0);
  87. return JSON.parse(result.stdout);
  88. };
  89. const sourceBefore = await readSourceSettings();
  90. const localBefore = await localSettings();
  91. await openSection("general");
  92. const hardwareEnabled = !(localBefore.desktopChromiumHardwareAccelerationEnabled ?? true);
  93. await setSwitch(hardwareSelector, hardwareEnabled);
  94. await browser.waitUntil(
  95. async () =>
  96. (await localSettings()).desktopChromiumHardwareAccelerationEnabled === hardwareEnabled,
  97. { timeout: 15_000, timeoutMsg: "硬件加速未保存到 Desktop" },
  98. );
  99. // 反向对照:任务属于 Source,归档设置继续保存到 Source,不能把整个页面改成本机。
  100. const archiveEnabled = !(sourceBefore.taskAutoArchiveEnabled ?? false);
  101. // 归档偏好已移到“归档”页;控件与写入 owner 未变,继续验证 Source 反向对照。
  102. await openSection("archives");
  103. const archiveSelector = '[data-testid="archive-auto-archive-switch"]';
  104. await setSwitch(archiveSelector, archiveEnabled);
  105. await browser.waitUntil(
  106. async () => (await readSourceSettings()).taskAutoArchiveEnabled === archiveEnabled,
  107. { timeout: 15_000 },
  108. );
  109. expect((await localSettings()).taskAutoArchiveEnabled).toEqual(
  110. localBefore.taskAutoArchiveEnabled,
  111. );
  112. facts.sourceArchiveUpdated = true;
  113. await openSection("browser");
  114. const certificatesEnabled = !(localBefore.embeddedBrowserAllowInsecureCertificates ?? false);
  115. await setSwitch(certificateSelector, certificatesEnabled);
  116. await browser.waitUntil(
  117. async () =>
  118. (await localSettings()).embeddedBrowserAllowInsecureCertificates === certificatesEnabled,
  119. { timeout: 15_000, timeoutMsg: "浏览器证书策略未保存到 Desktop" },
  120. );
  121. await clickTestIdByDom(TID_SETTINGS_BACK_BUTTON, { timeout: 15_000 });
  122. // 重挂设置页:Source 值仍不同,UI 必须重新读取本机。
  123. await openSection("general");
  124. await browser.waitUntil(
  125. async () =>
  126. (await $(hardwareSelector).getAttribute("aria-checked")) === String(hardwareEnabled),
  127. { timeout: 15_000 },
  128. );
  129. await openSection("browser");
  130. await browser.waitUntil(
  131. async () =>
  132. (await $(certificateSelector).getAttribute("aria-checked")) === String(certificatesEnabled),
  133. { timeout: 15_000 },
  134. );
  135. facts.localReadAfterRemount = true;
  136. await openSection("general");
  137. await mkdir(customRoot, { recursive: true });
  138. await writeFile(join(paths.appDataDir, canary), CASE_MARKER);
  139. const picker = await browser.electron.mock("dialog", "showOpenDialog");
  140. await picker.mockResolvedValueOnce({ canceled: false, filePaths: [customRoot] });
  141. await clickTestIdByDom(TID_SETTINGS_DATA_BASE_DIR_BROWSE, { timeout: 15_000 });
  142. await browser.waitUntil(
  143. async () =>
  144. (await $(`[data-testid="${TID_SETTINGS_DATA_BASE_DIR_INPUT}"]`).getValue()) === customRoot,
  145. { timeout: 15_000 },
  146. );
  147. const oldPid = await browser.electron.execute(() => process.pid);
  148. await clickTestIdByDom(TID_SETTINGS_DATA_BASE_DIR_SAVE, { timeout: 15_000 });
  149. await browser.waitUntil(
  150. async () =>
  151. (await $(`[data-testid="${TID_SETTINGS_DATA_BASE_DIR_STATUS}"]`).getAttribute(
  152. "data-state",
  153. )) === "saved",
  154. { timeout: 60_000 },
  155. );
  156. expect((await localSettings()).dataBaseDir).toBe(customRoot);
  157. expect(await readFile(join(customRoot, ".zcode/v2", canary), "utf8")).toBe(CASE_MARKER);
  158. const sourceAfter = await readSourceSettings();
  159. for (const field of [
  160. "dataBaseDir",
  161. "desktopChromiumHardwareAccelerationEnabled",
  162. "embeddedBrowserAllowInsecureCertificates",
  163. ]) {
  164. expect(sourceAfter[field]).toEqual(sourceBefore[field]);
  165. }
  166. facts.sourceDesktopSettingsUnchanged = true;
  167. facts.localCanaryCopied = true;
  168. const startupCwd = join(customRoot, ".zcode", "workspace", "default");
  169. // 根因回归:复制 v2 不会创建新根下的存储 cwd,必须由启动 owner 在启动 Worker 前准备。
  170. await expect(stat(startupCwd)).rejects.toMatchObject({ code: "ENOENT" });
  171. facts.startupCwdAbsentBeforeRestart = true;
  172. // 保持已有数据目录合同:保存后不再发起业务操作,立即完整冷重启。
  173. // 此处验证重启后的 Desktop 设置,不要求导航到无关的默认项目。
  174. await reloadElectronSessionPreservingBrowserProfile(browser);
  175. await waitForRendererAfterColdRestart(CASE_MARKER);
  176. expect(await browser.electron.execute(() => process.pid)).not.toBe(oldPid);
  177. expect((await stat(startupCwd)).isDirectory()).toBe(true);
  178. facts.startupCwdReadyAfterRestart = true;
  179. await openSection("general");
  180. await browser.waitUntil(
  181. async () =>
  182. (await $(`[data-testid="${TID_SETTINGS_DATA_BASE_DIR_INPUT}"]`).getValue()) === customRoot,
  183. { timeout: 15_000 },
  184. );
  185. expect(await $(hardwareSelector).getAttribute("aria-checked")).toBe(String(hardwareEnabled));
  186. await openSection("browser");
  187. expect(await $(certificateSelector).getAttribute("aria-checked")).toBe(
  188. String(certificatesEnabled),
  189. );
  190. facts.coldRestartRetainedDesktopSettings = true;
  191. });
  192. });