settings-personalization.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import {
  4. TID_SETTINGS_BACK_BUTTON,
  5. TID_SETTINGS_PAGE,
  6. TID_SETTINGS_PERSONALIZATION_CANCEL,
  7. TID_SETTINGS_PERSONALIZATION_EDITOR,
  8. TID_SETTINGS_PERSONALIZATION_SAVE,
  9. TID_SETTINGS_PERSONALIZATION_SIZE_HINT,
  10. TID_SETTINGS_SECTION_NAV,
  11. TID_TASK_SETTINGS_BUTTON,
  12. testId,
  13. } from "@zcode/shared/test-ids";
  14. import {
  15. DEFAULT_WORKSPACE,
  16. clearAppData,
  17. clickTestIdByDom,
  18. getE2EAppDataPaths,
  19. waitForWorkspaceApp,
  20. } from "#e2e/helpers/desktop-app";
  21. import { sel } from "#e2e/helpers/selectors";
  22. import { prepareV4ConversationE2E } from "#e2e/helpers/v4-conversation";
  23. const PERSONALIZATION_INSTRUCTIONS = "E2E_PZ_INSTRUCTIONS 永远用中文回复,先给结论。";
  24. /** 个性化编辑的文件就是 Agent 读取的 ~/.zcode/AGENTS.md;e2e 的 HOME 已指向隔离目录。 */
  25. function personalizationAgentsFile(): string {
  26. return join(getE2EAppDataPaths().storageRoot, "AGENTS.md");
  27. }
  28. async function readAgentsFile(): Promise<string | null> {
  29. try {
  30. return await readFile(personalizationAgentsFile(), "utf8");
  31. } catch (error) {
  32. if ((error as NodeJS.ErrnoException).code === "ENOENT") return null;
  33. throw error;
  34. }
  35. }
  36. /**
  37. * 用测试进程自己持有 AGENTS.md 的文件锁(owner pid 存活,宿主无法回收),
  38. * 让桌面 Host 的 saveUserInstructions 阻塞到锁超时(默认 8s)后失败。
  39. * 用于构造「保存 in-flight → 切走分区 → 写入失败」的确定性时序。
  40. */
  41. async function holdAgentsFileLock(): Promise<() => Promise<void>> {
  42. const lockDir = `${personalizationAgentsFile()}.lock`;
  43. await mkdir(lockDir);
  44. await writeFile(
  45. join(lockDir, "owner-e2e-holder.json"),
  46. `${JSON.stringify({ pid: process.pid, createdAt: Date.now(), token: "e2e-holder" })}\n`,
  47. );
  48. return async () => {
  49. await rm(lockDir, { recursive: true, force: true });
  50. };
  51. }
  52. async function openSettingsSection(sectionId: string) {
  53. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON, { timeout: 15000 });
  54. await $(sel(TID_SETTINGS_PAGE)).waitForDisplayed({ timeout: 15000 });
  55. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, sectionId), {
  56. timeout: 15000,
  57. timeoutMsg: `设置页没有出现分区入口:${sectionId}`,
  58. });
  59. }
  60. describe("设置页个性化(自定义指令)E2E", () => {
  61. before(async () => {
  62. await rm(personalizationAgentsFile(), { force: true });
  63. });
  64. after(async () => {
  65. await clearAppData();
  66. });
  67. beforeEach(async () => {
  68. // 上一个用例可能停在设置页,先真实关闭遮挡层再进入本用例。
  69. const back = $(sel(TID_SETTINGS_BACK_BUTTON));
  70. if (await back.isDisplayed()) await back.click();
  71. await prepareV4ConversationE2E({ skipProvider: true });
  72. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 30000);
  73. });
  74. it("PZ-00: 个性化是「Agent 能力」组的第一项", async () => {
  75. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON, { timeout: 15000 });
  76. await $(sel(TID_SETTINGS_PAGE)).waitForDisplayed({ timeout: 15000 });
  77. const groupSections = await browser.execute(() => {
  78. const group = document.querySelector(
  79. '[aria-labelledby="settings-sidebar-group-agentCapabilities"]',
  80. );
  81. if (!group) return null;
  82. return Array.from(group.querySelectorAll<HTMLElement>("[data-settings-section-nav]")).map(
  83. (item) => item.dataset.settingsSectionNav ?? "",
  84. );
  85. });
  86. expect(groupSections).not.toBeNull();
  87. expect(groupSections?.[0]).toBe("personalization");
  88. expect(groupSections).toContain("memory");
  89. });
  90. it("PZ-01: 点击个性化直达编辑器,保存后落盘 AGENTS.md", async () => {
  91. await openSettingsSection("personalization");
  92. const editor = $(sel(TID_SETTINGS_PERSONALIZATION_EDITOR));
  93. await editor.waitForDisplayed({
  94. timeout: 15000,
  95. timeoutMsg: "点击个性化分区后没有直接出现编辑器",
  96. });
  97. // 单层交互:不经过任何二级入口,编辑器直接铺在内容区。
  98. expect(await readAgentsFile()).toBeNull();
  99. expect(await editor.getValue()).toBe("");
  100. expect(await editor.getAttribute("placeholder")).toMatch(
  101. /添加自定义指令|Add custom instructions/u,
  102. );
  103. await editor.setValue(PERSONALIZATION_INSTRUCTIONS);
  104. expect(await readAgentsFile()).toBeNull();
  105. await clickTestIdByDom(TID_SETTINGS_PERSONALIZATION_SAVE, {
  106. timeout: 15000,
  107. timeoutMsg: "个性化保存按钮没有出现",
  108. });
  109. await browser.waitUntil(async () => (await readAgentsFile()) === PERSONALIZATION_INSTRUCTIONS, {
  110. timeout: 15000,
  111. timeoutMsg: "保存后没有把内容写入 ~/.zcode/AGENTS.md",
  112. });
  113. });
  114. it("PZ-02: 文件是唯一事实源,重新进入回显且取消丢弃修改", async () => {
  115. const seeded = "E2E_PZ_SEEDED 回显校验";
  116. await writeFile(personalizationAgentsFile(), seeded);
  117. await openSettingsSection("personalization");
  118. const editor = $(sel(TID_SETTINGS_PERSONALIZATION_EDITOR));
  119. await editor.waitForDisplayed({ timeout: 15000 });
  120. await browser.waitUntil(async () => (await editor.getValue()) === seeded, {
  121. timeout: 15000,
  122. timeoutMsg: "进入分区后没有回显 AGENTS.md 的既有内容",
  123. });
  124. // 未修改时保存/取消均不可用。
  125. expect(await $(sel(TID_SETTINGS_PERSONALIZATION_SAVE)).isEnabled()).toBe(false);
  126. expect(await $(sel(TID_SETTINGS_PERSONALIZATION_CANCEL)).isEnabled()).toBe(false);
  127. // 取消丢弃修改:编辑器回到基线,文件不变。
  128. await editor.setValue("E2E_PZ_DISCARDED");
  129. await clickTestIdByDom(TID_SETTINGS_PERSONALIZATION_CANCEL, { timeout: 15000 });
  130. await browser.waitUntil(async () => (await editor.getValue()) === seeded, {
  131. timeout: 15000,
  132. timeoutMsg: "取消后没有恢复到已保存内容",
  133. });
  134. expect(await readAgentsFile()).toBe(seeded);
  135. // 切走再切回,内容仍来自文件。
  136. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "general"), { timeout: 15000 });
  137. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "personalization"), {
  138. timeout: 15000,
  139. });
  140. await editor.waitForDisplayed({ timeout: 15000 });
  141. await browser.waitUntil(async () => (await editor.getValue()) === seeded, {
  142. timeout: 15000,
  143. timeoutMsg: "重新进入分区后没有回显 AGENTS.md 的内容",
  144. });
  145. });
  146. it("PZ-03: 清空并保存后文件内容被清空", async () => {
  147. await writeFile(personalizationAgentsFile(), "E2E_PZ_TO_CLEAR");
  148. await openSettingsSection("personalization");
  149. const editor = $(sel(TID_SETTINGS_PERSONALIZATION_EDITOR));
  150. await editor.waitForDisplayed({ timeout: 15000 });
  151. await browser.waitUntil(async () => (await editor.getValue()) === "E2E_PZ_TO_CLEAR", {
  152. timeout: 15000,
  153. });
  154. // 不能用 setValue(""):chromedriver 的 clear 路径不触发 React 可见的 input 事件,
  155. // 受控组件的草稿不更新、脏态门控不会放行保存。改用真实键盘全选删除。
  156. await editor.click();
  157. await browser.keys([process.platform === "darwin" ? "Meta" : "Control", "a"]);
  158. await browser.keys("Backspace");
  159. await clickTestIdByDom(TID_SETTINGS_PERSONALIZATION_SAVE, { timeout: 15000 });
  160. await browser.waitUntil(async () => (await readAgentsFile()) === "", {
  161. timeout: 15000,
  162. timeoutMsg: "清空保存后 AGENTS.md 内容没有被清空",
  163. });
  164. });
  165. it("PZ-04: 超过 Agent 读取上限(100KB)的内容被阻断保存并提示", async () => {
  166. await writeFile(personalizationAgentsFile(), "E2E_PZ_SIZE_BASE");
  167. await openSettingsSection("personalization");
  168. const editor = $(sel(TID_SETTINGS_PERSONALIZATION_EDITOR));
  169. await editor.waitForDisplayed({ timeout: 15000 });
  170. await browser.waitUntil(async () => (await editor.getValue()) === "E2E_PZ_SIZE_BASE", {
  171. timeout: 15000,
  172. });
  173. // Agent 只读前 100KB(UTF-8 字节);超限内容必须被 UI 阻断,不能保存成功后静默截断。
  174. // 128KB 文本走 chromedriver 的 element/value 会超时;改用原生 value setter +
  175. // input 事件注入,React 受控组件可感知且瞬时完成。
  176. const oversized = `${"z".repeat(50 * 1024)}${"\u4e2d".repeat(25 * 1025)}`;
  177. await browser.execute(
  178. (editorTestId: string, text: string) => {
  179. const textarea = document.querySelector<HTMLTextAreaElement>(
  180. `[data-testid="${editorTestId}"]`,
  181. )!;
  182. const setter = Object.getOwnPropertyDescriptor(
  183. window.HTMLTextAreaElement.prototype,
  184. "value",
  185. )!.set!;
  186. setter.call(textarea, text);
  187. textarea.dispatchEvent(new Event("input", { bubbles: true }));
  188. },
  189. TID_SETTINGS_PERSONALIZATION_EDITOR,
  190. oversized,
  191. );
  192. await $(sel(TID_SETTINGS_PERSONALIZATION_SIZE_HINT)).waitForDisplayed({
  193. timeout: 15000,
  194. timeoutMsg: "超限提示没有出现",
  195. });
  196. // 超限时保存按钮不可用,文件保持基线内容。
  197. expect(await $(sel(TID_SETTINGS_PERSONALIZATION_SAVE)).isEnabled()).toBe(false);
  198. expect(await readAgentsFile()).toBe("E2E_PZ_SIZE_BASE");
  199. // 删回限内后保存恢复可用。
  200. await editor.click();
  201. await browser.keys([process.platform === "darwin" ? "Meta" : "Control", "a"]);
  202. await browser.keys("Backspace");
  203. await editor.setValue("E2E_PZ_SIZE_OK");
  204. await clickTestIdByDom(TID_SETTINGS_PERSONALIZATION_SAVE, { timeout: 15000 });
  205. await browser.waitUntil(async () => (await readAgentsFile()) === "E2E_PZ_SIZE_OK", {
  206. timeout: 15000,
  207. timeoutMsg: "回到限内后保存没有恢复",
  208. });
  209. });
  210. it("PZ-05: 未保存草稿跨分区切换存活", async () => {
  211. await writeFile(personalizationAgentsFile(), "E2E_PZ5_BASE");
  212. await openSettingsSection("personalization");
  213. const editor = $(sel(TID_SETTINGS_PERSONALIZATION_EDITOR));
  214. await editor.waitForDisplayed({ timeout: 15000 });
  215. await browser.waitUntil(async () => (await editor.getValue()) === "E2E_PZ5_BASE", {
  216. timeout: 15000,
  217. });
  218. await editor.setValue("E2E_PZ5_DRAFT");
  219. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "general"), { timeout: 15000 });
  220. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "personalization"), {
  221. timeout: 15000,
  222. });
  223. await editor.waitForDisplayed({ timeout: 15000 });
  224. await browser.waitUntil(async () => (await editor.getValue()) === "E2E_PZ5_DRAFT", {
  225. timeout: 15000,
  226. timeoutMsg: "切走再切回后未保存草稿没有存活",
  227. });
  228. // 草稿只是存活,未被自动写入文件。
  229. expect(await readAgentsFile()).toBe("E2E_PZ5_BASE");
  230. });
  231. it("PZ-06: 保存中切走分区且写入失败后,草稿仍可恢复重试", async function () {
  232. this.timeout(60000);
  233. await writeFile(personalizationAgentsFile(), "E2E_PZ6_BASE");
  234. await openSettingsSection("personalization");
  235. const editor = $(sel(TID_SETTINGS_PERSONALIZATION_EDITOR));
  236. await editor.waitForDisplayed({ timeout: 15000 });
  237. await browser.waitUntil(async () => (await editor.getValue()) === "E2E_PZ6_BASE", {
  238. timeout: 15000,
  239. });
  240. await editor.setValue("E2E_PZ6_DRAFT");
  241. // 持锁令宿主保存阻塞到超时(默认 8s);点保存后立刻切走分区,
  242. // 构造「保存 in-flight → 卸载 → 写入失败」的确定性时序。
  243. const releaseLock = await holdAgentsFileLock();
  244. try {
  245. await clickTestIdByDom(TID_SETTINGS_PERSONALIZATION_SAVE, { timeout: 15000 });
  246. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "general"), { timeout: 15000 });
  247. // 等待锁超时失败落到已卸载组件的 store 收口(无 UI 可断言,按超时上限等)。
  248. await browser.pause(9000);
  249. } finally {
  250. await releaseLock();
  251. }
  252. // 重新进入:草稿仍在(模块级 store 存活),可再次保存成功。
  253. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "personalization"), {
  254. timeout: 15000,
  255. });
  256. await editor.waitForDisplayed({ timeout: 15000 });
  257. await browser.waitUntil(async () => (await editor.getValue()) === "E2E_PZ6_DRAFT", {
  258. timeout: 15000,
  259. timeoutMsg: "保存失败于卸载态后,草稿没有被恢复",
  260. });
  261. await clickTestIdByDom(TID_SETTINGS_PERSONALIZATION_SAVE, { timeout: 15000 });
  262. await browser.waitUntil(async () => (await readAgentsFile()) === "E2E_PZ6_DRAFT", {
  263. timeout: 15000,
  264. timeoutMsg: "恢复后的草稿重试保存没有落盘",
  265. });
  266. });
  267. });