workspace-agent-warmup.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { formatZCodeAgentProcessName } from "@zcode/shared";
  2. import { startDraftFromProject, waitForProjectRow } from "./project-sidebar.js";
  3. import {
  4. countAgentProcessRows,
  5. listAgentProcessIdentities,
  6. waitForAgentProcessCount,
  7. type AgentProcessIdentity,
  8. } from "./resource-manager.js";
  9. export function pickWorkspacePaths(workspacePaths: string[], indexes: readonly number[]): string[] {
  10. return indexes.map((index) => {
  11. const workspacePath = workspacePaths[index];
  12. if (!workspacePath) {
  13. throw new Error(`workspace fixture 缺少 index=${index}`);
  14. }
  15. return workspacePath;
  16. });
  17. }
  18. export async function waitForActiveWorkspacePath(
  19. expectedWorkspacePath: string,
  20. timeout = 30_000,
  21. ): Promise<void> {
  22. await browser.waitUntil(
  23. async () => {
  24. try {
  25. return await browser.execute((expectedPath) => {
  26. const tabStore = (
  27. window as Window & {
  28. __zcodeTabStoreE2E?: {
  29. getState?: () => { activeWorkspacePath?: string | null };
  30. };
  31. }
  32. ).__zcodeTabStoreE2E;
  33. return tabStore?.getState?.().activeWorkspacePath === expectedPath;
  34. }, expectedWorkspacePath);
  35. } catch {
  36. // renderer reload 期间 WebDriver 会短暂失去执行上下文,继续等待新页面挂载 store。
  37. return false;
  38. }
  39. },
  40. {
  41. timeout,
  42. interval: 100,
  43. timeoutMsg: `active workspace 没有收敛为 ${expectedWorkspacePath}`,
  44. },
  45. );
  46. }
  47. export { waitForRestoredProjects } from "./project-sidebar.js";
  48. /**
  49. * 从侧栏为指定 workspace 打开新任务草稿。虚拟项目重构后 workspace 不再是可点击的目录
  50. * 节点,而是同名项目行;项目行的「新建任务」按钮就是显式草稿导航入口(cwd = 主要目录),
  51. * 不再依赖旧的「先收起再展开」手势。
  52. */
  53. export async function openWorkspaceDraftFromSidebar(workspacePath: string): Promise<void> {
  54. await waitForProjectRow(workspacePath, {
  55. timeout: 15_000,
  56. timeoutMsg: `侧栏没有 dormant workspace 对应的项目: ${workspacePath}`,
  57. });
  58. await startDraftFromProject(workspacePath);
  59. await waitForActiveWorkspacePath(workspacePath);
  60. }
  61. export async function assertStableAgentSelection(
  62. allWorkspacePaths: string[],
  63. expectedWorkspacePaths: string[],
  64. ): Promise<AgentProcessIdentity[]> {
  65. await waitForAgentProcessCount(expectedWorkspacePaths.length);
  66. // 观察窗口跨过 Process Monitor 的至少一次刷新周期,避免只命中启动风暴前的瞬时值。
  67. await browser.pause(1_500);
  68. const identities = await listAgentProcessIdentities();
  69. expect(identities).toHaveLength(expectedWorkspacePaths.length);
  70. const expected = new Set(expectedWorkspacePaths);
  71. const placeholderPrefix = formatZCodeAgentProcessName("placeholder");
  72. for (const workspacePath of allWorkspacePaths) {
  73. const formatted = formatZCodeAgentProcessName("placeholder", workspacePath);
  74. const workspaceNameSuffix = formatted.slice(placeholderPrefix.length);
  75. const actual = await countAgentProcessRows({ workspaceNameSuffix });
  76. expect(actual).toBe(expected.has(workspacePath) ? 1 : 0);
  77. }
  78. return identities;
  79. }
  80. /** 旧组织模式入口退场后,所有项目型用例统一选择项目视图。 */
  81. export async function selectTaskOrganizeMode(_labels: string[]): Promise<void> {
  82. // 断开后紧接重连曾在菜单关闭动画中再次点开关,导致菜单被反向关闭;已是项目视图无需重复选择。
  83. if (await $('[data-testid="project-section"]').isDisplayed()) return;
  84. await $('[data-testid="sidebar-view-menu"]').click();
  85. const item = $('[role="menuitemradio"][data-testid="sidebar-view-projects"]');
  86. // 菜单状态属性属于组件实现;等待真实项目选项可操作,避免依赖已移除的 data-state。
  87. await item.waitForClickable();
  88. await item.click();
  89. await item.waitForExist({ reverse: true });
  90. await $('[data-testid="project-section"]').waitForDisplayed();
  91. }