workspace-agent-warmup.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { TID_WORKSPACE_ITEM, formatZCodeAgentProcessName, testId } from "@zcode/shared";
  2. import {
  3. countAgentProcessRows,
  4. listAgentProcessIdentities,
  5. waitForAgentProcessCount,
  6. type AgentProcessIdentity,
  7. } from "./resource-manager.js";
  8. export function pickWorkspacePaths(workspacePaths: string[], indexes: readonly number[]): string[] {
  9. return indexes.map((index) => {
  10. const workspacePath = workspacePaths[index];
  11. if (!workspacePath) {
  12. throw new Error(`workspace fixture 缺少 index=${index}`);
  13. }
  14. return workspacePath;
  15. });
  16. }
  17. export async function waitForActiveWorkspacePath(
  18. expectedWorkspacePath: string,
  19. timeout = 30_000,
  20. ): Promise<void> {
  21. await browser.waitUntil(
  22. async () => {
  23. try {
  24. return await browser.execute((expectedPath) => {
  25. const tabStore = (
  26. window as Window & {
  27. __zcodeTabStoreE2E?: {
  28. getState?: () => { activeWorkspacePath?: string | null };
  29. };
  30. }
  31. ).__zcodeTabStoreE2E;
  32. return tabStore?.getState?.().activeWorkspacePath === expectedPath;
  33. }, expectedWorkspacePath);
  34. } catch {
  35. // renderer reload 期间 WebDriver 会短暂失去执行上下文,继续等待新页面挂载 store。
  36. return false;
  37. }
  38. },
  39. {
  40. timeout,
  41. interval: 100,
  42. timeoutMsg: `active workspace 没有收敛为 ${expectedWorkspacePath}`,
  43. },
  44. );
  45. }
  46. export async function waitForRestoredWorkspaceItems(
  47. workspacePaths: string[],
  48. timeout = 30_000,
  49. ): Promise<void> {
  50. const workspaceItemTestIds = workspacePaths.map((workspacePath) =>
  51. testId(TID_WORKSPACE_ITEM, workspacePath),
  52. );
  53. await browser.waitUntil(
  54. async () => {
  55. try {
  56. return await browser.execute((expectedTestIds) => {
  57. const actual = new Set(
  58. Array.from(document.querySelectorAll<HTMLElement>("[data-testid]"))
  59. .map((element) => element.dataset.testid)
  60. .filter((value): value is string => Boolean(value)),
  61. );
  62. return expectedTestIds.every((expected) => actual.has(expected));
  63. }, workspaceItemTestIds);
  64. } catch {
  65. return false;
  66. }
  67. },
  68. {
  69. timeout,
  70. interval: 250,
  71. timeoutMsg: `${workspacePaths.length} 个恢复 workspace 没有全部出现在侧栏`,
  72. },
  73. );
  74. }
  75. export async function openWorkspaceDraftFromSidebar(workspacePath: string): Promise<void> {
  76. const workspaceItemTestId = testId(TID_WORKSPACE_ITEM, workspacePath);
  77. await browser.waitUntil(
  78. () =>
  79. browser.execute((expectedTestId) => {
  80. return Array.from(document.querySelectorAll<HTMLElement>("[data-testid]")).some(
  81. (element) => element.dataset.testid === expectedTestId,
  82. );
  83. }, workspaceItemTestId),
  84. {
  85. timeout: 15_000,
  86. interval: 100,
  87. timeoutMsg: `侧栏没有 dormant workspace: ${workspacePath}`,
  88. },
  89. );
  90. const isExpanded = await browser.execute((expectedTestId) => {
  91. return Array.from(document.querySelectorAll<HTMLElement>("[data-testid]")).find(
  92. (element) => element.dataset.testid === expectedTestId,
  93. )?.getAttribute("aria-expanded") === "true";
  94. }, workspaceItemTestId);
  95. if (isExpanded) {
  96. // 修复原因:workspace 展开态与 active workspace 是两套状态;renderer reload 后目标行
  97. // 可能已展开但仍 dormant。先真实收起,再点击展开,才能命中产品的显式草稿导航入口。
  98. await clickWorkspaceItem(workspaceItemTestId);
  99. await browser.waitUntil(
  100. () =>
  101. browser.execute((expectedTestId) => {
  102. return Array.from(document.querySelectorAll<HTMLElement>("[data-testid]")).find(
  103. (element) => element.dataset.testid === expectedTestId,
  104. )?.getAttribute("aria-expanded") === "false";
  105. }, workspaceItemTestId),
  106. {
  107. timeout: 10_000,
  108. interval: 100,
  109. timeoutMsg: `workspace 没有先收起: ${workspacePath}`,
  110. },
  111. );
  112. }
  113. await clickWorkspaceItem(workspaceItemTestId);
  114. await waitForActiveWorkspacePath(workspacePath);
  115. }
  116. async function clickWorkspaceItem(workspaceItemTestId: string): Promise<void> {
  117. const clicked = await browser.execute((expectedTestId) => {
  118. const item = Array.from(document.querySelectorAll<HTMLElement>("[data-testid]")).find(
  119. (element) => element.dataset.testid === expectedTestId,
  120. );
  121. item?.click();
  122. return Boolean(item);
  123. }, workspaceItemTestId);
  124. if (!clicked) {
  125. throw new Error(`侧栏 workspace 在点击前消失: ${workspaceItemTestId}`);
  126. }
  127. }
  128. export async function selectTaskOrganizeMode(labels: string[]): Promise<void> {
  129. await browser.waitUntil(
  130. () =>
  131. browser.execute((expectedLabels) => {
  132. const trigger = Array.from(
  133. document.querySelectorAll<HTMLElement>('[role="tab"]'),
  134. ).find((element) => {
  135. const text = element.textContent?.replace(/\s+/gu, " ").trim() ?? "";
  136. return expectedLabels.some((label) => text.includes(label));
  137. });
  138. if (!trigger) return false;
  139. if (
  140. trigger.hasAttribute("data-active") ||
  141. trigger.getAttribute("data-state") === "active" ||
  142. trigger.getAttribute("aria-selected") === "true"
  143. ) {
  144. return true;
  145. }
  146. trigger.dispatchEvent(
  147. new MouseEvent("mousedown", {
  148. bubbles: true,
  149. cancelable: true,
  150. button: 0,
  151. }),
  152. );
  153. return false;
  154. }, labels),
  155. {
  156. timeout: 15_000,
  157. interval: 100,
  158. timeoutMsg: `侧栏没有切换到任务组织视图: ${labels.join("/")}`,
  159. },
  160. );
  161. }
  162. export async function assertStableAgentSelection(
  163. allWorkspacePaths: string[],
  164. expectedWorkspacePaths: string[],
  165. ): Promise<AgentProcessIdentity[]> {
  166. await waitForAgentProcessCount(expectedWorkspacePaths.length);
  167. // 观察窗口跨过 Process Monitor 的至少一次刷新周期,避免只命中启动风暴前的瞬时值。
  168. await browser.pause(1_500);
  169. const identities = await listAgentProcessIdentities();
  170. expect(identities).toHaveLength(expectedWorkspacePaths.length);
  171. const expected = new Set(expectedWorkspacePaths);
  172. const placeholderPrefix = formatZCodeAgentProcessName("placeholder");
  173. for (const workspacePath of allWorkspacePaths) {
  174. const formatted = formatZCodeAgentProcessName("placeholder", workspacePath);
  175. const workspaceNameSuffix = formatted.slice(placeholderPrefix.length);
  176. const actual = await countAgentProcessRows({ workspaceNameSuffix });
  177. expect(actual).toBe(expected.has(workspacePath) ? 1 : 0);
  178. }
  179. return identities;
  180. }