conversation-session-tool-group-settings-persistence.test.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { mkdir, 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_SECTION_NAV,
  7. TID_TASK_SETTINGS_BUTTON,
  8. testId,
  9. } from "@zcode/shared";
  10. import { clearAppData, clickTestIdByDom, readSettings } from "../helpers/desktop-app.js";
  11. import { listConversationToolGroups } from "../helpers/conversation-session-tool-groups.js";
  12. import { ensureToolCrossProductFullAccessMode } from "../helpers/conversation-session-tool-cross-product.js";
  13. import { resolveE2ERuntimePath, resolveE2EToolPath } from "../helpers/e2e-runtime-paths.js";
  14. import {
  15. E2E_REPLY_TOKEN,
  16. prepareV4ConversationE2E,
  17. sendV4Prompt,
  18. switchV4Mode,
  19. waitForV4AssistantMessageContaining,
  20. waitForV4Pane,
  21. } from "../helpers/v4-conversation.js";
  22. import { expandAssistantHistoriesWithContent } from "../helpers/conversation-session-tool-diagnostics.js";
  23. const CASE_NAME = "conversation-session-tool-group-settings-persistence";
  24. const CASE_MARKER = "E2E_TOOL_GROUP_SETTINGS_PERSISTENCE";
  25. const RUNTIME_ROOT = resolveE2ERuntimePath(CASE_NAME);
  26. const READ_PATHS = [
  27. resolveE2EToolPath(CASE_NAME, "seed-a.txt"),
  28. resolveE2EToolPath(CASE_NAME, "seed-b.txt"),
  29. ] as const;
  30. const WRITE_PATHS = [
  31. resolveE2EToolPath(CASE_NAME, "result-a.ts"),
  32. resolveE2EToolPath(CASE_NAME, "result-b.ts"),
  33. ] as const;
  34. const GROUP_SWITCH_LABELS = {
  35. explore: ["Group exploration tools", "分组探索工具"],
  36. terminal: ["Group terminal commands", "分组终端命令"],
  37. changes: ["Group file changes", "分组文件更改"],
  38. } as const;
  39. type GroupSwitch = keyof typeof GROUP_SWITCH_LABELS;
  40. describe("TGE06 tool group settings defaults and persistence", () => {
  41. before(async function () {
  42. this.timeout(150000);
  43. await clearAppData();
  44. await prepareV4ConversationE2E();
  45. await switchV4Mode("yolo");
  46. });
  47. afterEach(async () => {
  48. await rm(RUNTIME_ROOT, { recursive: true, force: true });
  49. });
  50. after(async () => {
  51. await browser.electron.restoreAllMocks();
  52. await clearAppData();
  53. });
  54. it("验证默认值、独立切换、历史重分组与持久化", async function () {
  55. this.timeout(240000);
  56. await prepareFixture();
  57. await openGeneralSettings();
  58. expect(await readGroupSwitch("explore")).toBe(true);
  59. expect(await readGroupSwitch("terminal")).toBe(true);
  60. expect(await readGroupSwitch("changes")).toBe(false);
  61. await setGroupSwitch("changes", true);
  62. await leaveSettings();
  63. await ensureToolCrossProductFullAccessMode();
  64. await sendV4Prompt(
  65. [
  66. `${CASE_MARKER}: Follow these tool steps exactly in order.`,
  67. `1. Read ${READ_PATHS[0]} and ${READ_PATHS[1]}.`,
  68. `2. Bash exactly: node -e "console.log('TGE06_TERMINAL_A')" and node -e "console.log('TGE06_TERMINAL_B')".`,
  69. `3. Write ${WRITE_PATHS[0]} and ${WRITE_PATHS[1]} with exactly "export const grouped = true;\\n".`,
  70. `4. Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`,
  71. ].join(" "),
  72. );
  73. await waitForV4AssistantMessageContaining(E2E_REPLY_TOKEN, 150000);
  74. await waitForV4Pane((snapshot) => !snapshot.canStop, "TGE06 工具轮没有结束", 150000);
  75. await expandAssistantHistoriesWithContent();
  76. await waitForGroupNames(["Explore", "ExecuteGroup", "ChangesGroup"]);
  77. await openGeneralSettings();
  78. await setGroupSwitch("explore", false);
  79. await leaveSettings();
  80. await waitForGroupNames(["ExecuteGroup", "ChangesGroup"]);
  81. await openGeneralSettings();
  82. await setGroupSwitch("terminal", false);
  83. await leaveSettings();
  84. await waitForGroupNames(["ChangesGroup"]);
  85. await openGeneralSettings();
  86. expect(await readGroupSwitch("explore")).toBe(false);
  87. expect(await readGroupSwitch("terminal")).toBe(false);
  88. expect(await readGroupSwitch("changes")).toBe(true);
  89. expect(await readSettings()).toMatchObject({
  90. toolGroupingExploreEnabled: false,
  91. toolGroupingTerminalEnabled: false,
  92. toolGroupingChangesEnabled: true,
  93. });
  94. });
  95. });
  96. async function prepareFixture() {
  97. await rm(RUNTIME_ROOT, { recursive: true, force: true });
  98. await mkdir(RUNTIME_ROOT, { recursive: true });
  99. await Promise.all([
  100. writeFile(join(RUNTIME_ROOT, "seed-a.txt"), "TGE06_READ_A\n", "utf8"),
  101. writeFile(join(RUNTIME_ROOT, "seed-b.txt"), "TGE06_READ_B\n", "utf8"),
  102. ]);
  103. }
  104. async function openGeneralSettings() {
  105. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON, { timeout: 15000 });
  106. await browser.waitUntil(
  107. () =>
  108. browser.execute(
  109. (id) => Boolean(document.querySelector(`[data-testid="${id}"]`)),
  110. TID_SETTINGS_PAGE,
  111. ),
  112. { timeout: 15000, timeoutMsg: "TGE06 设置页没有打开" },
  113. );
  114. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "general"), {
  115. timeout: 15000,
  116. });
  117. await browser.waitUntil(async () => (await readGroupSwitch("explore")) !== null, {
  118. timeout: 15000,
  119. timeoutMsg: "TGE06 常规设置没有工具分组开关",
  120. });
  121. }
  122. async function leaveSettings() {
  123. await clickTestIdByDom(TID_SETTINGS_BACK_BUTTON, { timeout: 15000 });
  124. }
  125. async function readGroupSwitch(name: GroupSwitch): Promise<boolean | null> {
  126. return browser.execute(
  127. (labels) => {
  128. const control = labels
  129. .map((label) => document.querySelector<HTMLElement>(`[aria-label="${label}"]`))
  130. .find(Boolean);
  131. if (!control) return null;
  132. return (
  133. control.getAttribute("aria-checked") === "true" ||
  134. control.getAttribute("data-state") === "checked"
  135. );
  136. },
  137. [...GROUP_SWITCH_LABELS[name]],
  138. );
  139. }
  140. async function setGroupSwitch(name: GroupSwitch, enabled: boolean) {
  141. if ((await readGroupSwitch(name)) !== enabled) {
  142. const clicked = await browser.execute(
  143. (labels) => {
  144. const control = labels
  145. .map((label) => document.querySelector<HTMLElement>(`[aria-label="${label}"]`))
  146. .find(Boolean);
  147. control?.click();
  148. return Boolean(control);
  149. },
  150. [...GROUP_SWITCH_LABELS[name]],
  151. );
  152. expect(clicked).toBe(true);
  153. }
  154. await browser.waitUntil(async () => (await readGroupSwitch(name)) === enabled, {
  155. timeout: 15000,
  156. timeoutMsg: `TGE06 ${name} 开关没有变为 ${String(enabled)}`,
  157. });
  158. }
  159. async function waitForGroupNames(expected: string[]) {
  160. let latest: string[] = [];
  161. await browser.waitUntil(
  162. async () => {
  163. latest = (await listConversationToolGroups()).map((group) => group.toolName);
  164. return JSON.stringify(latest) === JSON.stringify(expected);
  165. },
  166. {
  167. timeout: 15000,
  168. timeoutMsg: `TGE06 分组未更新: ${JSON.stringify(latest)}`,
  169. },
  170. );
  171. }