conversation-session-deleted-project-new-task.test.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { ServiceChannels } from "@zcode/shared";
  4. import type { ProjectTree } from "@zcode/services";
  5. import { clearAppData } from "#e2e/helpers/desktop-app";
  6. import {
  7. prepareV4ConversationE2E,
  8. sendV4Prompt,
  9. waitForV4AssistantMessageContaining,
  10. waitForV4Pane,
  11. setV4ComposerText,
  12. waitForV4ComposerText,
  13. } from "#e2e/helpers/v4-conversation";
  14. import {
  15. installHandoffRpcProbe,
  16. callHandoffHost,
  17. restoreHandoffProbes,
  18. } from "#e2e/helpers/remote-project-handoff-probe";
  19. async function navigation() {
  20. return browser.execute(() => {
  21. const target = window as Window & {
  22. __zcodeTabStoreE2E?: {
  23. getState(): { activeWorkspacePath: string | null; activeWorkspaceIdentity?: string | null };
  24. };
  25. __zcodeSessionStoreE2E?: {
  26. getState(): {
  27. getWorkspaceState(
  28. path: string,
  29. identity?: string,
  30. ): {
  31. activeTaskId: string | null;
  32. draftProjectId?: string | null;
  33. };
  34. };
  35. };
  36. };
  37. const tab = target.__zcodeTabStoreE2E!.getState();
  38. const path = tab.activeWorkspacePath!;
  39. const state = target
  40. .__zcodeSessionStoreE2E!.getState()
  41. .getWorkspaceState(path, tab.activeWorkspaceIdentity ?? undefined);
  42. return { path, taskId: state.activeTaskId, projectId: state.draftProjectId ?? null };
  43. });
  44. }
  45. describe("PDN01 deleted project new task", () => {
  46. after(async () => {
  47. await restoreHandoffProbes();
  48. await clearAppData();
  49. });
  50. for (const entry of ["menu", "editor"] as const) {
  51. it(`删除当前项目后可新建未归类任务(${entry})`, async function () {
  52. this.timeout(180_000);
  53. await prepareV4ConversationE2E();
  54. await installHandoffRpcProbe();
  55. const name = `E2E_DELETED_PROJECT_${entry}`;
  56. await $('[data-testid="virtual-project-add"]').click();
  57. await $('[data-testid="virtual-project-name"]').setValue(name);
  58. await $('[data-testid="virtual-project-save"]').click();
  59. const project = $(`[data-project-name="${name}"]`);
  60. await project.waitForDisplayed();
  61. const projectId = await project.getAttribute("data-project-id");
  62. await browser.waitUntil(async () => (await navigation()).projectId === projectId);
  63. if (entry === "menu") {
  64. await sendV4Prompt("E2E_DELETED_PROJECT_SESSION");
  65. await waitForV4AssistantMessageContaining("E2E_DELETED_PROJECT_READY");
  66. } else {
  67. await setV4ComposerText("E2E_DELETED_PROJECT_SAVED_DRAFT");
  68. }
  69. const before = await navigation();
  70. await project.$('[data-testid="virtual-project-row"]').click({ button: "right" });
  71. if (entry === "editor") {
  72. await $('[data-testid="virtual-project-edit"]').click();
  73. await $('[data-testid="virtual-project-editor-delete"]').click();
  74. } else {
  75. await $('[data-testid="virtual-project-delete"]').click();
  76. }
  77. await project.waitForExist({ reverse: true });
  78. expect(await navigation()).toEqual(before);
  79. if (entry === "menu") {
  80. await waitForV4AssistantMessageContaining("E2E_DELETED_PROJECT_READY");
  81. } else {
  82. await waitForV4ComposerText("E2E_DELETED_PROJECT_SAVED_DRAFT", "删除项目后原草稿丢失");
  83. }
  84. const tree = await callHandoffHost<ProjectTree>(ServiceChannels.Project, "readTree");
  85. expect(tree.projects.some((item) => item.id === projectId)).toBe(false);
  86. if (before.taskId) {
  87. expect(tree.tasks.find((item) => item.taskId === before.taskId)).toMatchObject({
  88. workspacePath: before.path,
  89. projectId,
  90. });
  91. }
  92. await writeFile(
  93. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, `deleted-project-${entry}-before.json`),
  94. JSON.stringify({ before, projectId, tasks: tree.tasks }, null, 2),
  95. );
  96. if (entry === "menu") {
  97. await $('[data-testid="task-new-button"]').click();
  98. } else {
  99. await browser.electron.execute((electron) => {
  100. const find = (menu: Electron.Menu): Electron.MenuItem | undefined => {
  101. for (const item of menu.items) {
  102. if (item.accelerator === "CmdOrCtrl+N") return item;
  103. if (item.submenu) {
  104. const nested = find(item.submenu);
  105. if (nested) return nested;
  106. }
  107. }
  108. };
  109. const item = find(electron.Menu.getApplicationMenu()!);
  110. if (!item) throw new Error("new task menu shortcut missing");
  111. item.click(
  112. undefined as never,
  113. electron.BrowserWindow.getFocusedWindow()!,
  114. undefined as never,
  115. );
  116. });
  117. }
  118. await browser.waitUntil(async () => (await navigation()).path !== before.path, {
  119. timeout: 15_000,
  120. timeoutMsg: "删除项目后新建任务仍停在旧目录",
  121. });
  122. await waitForV4Pane((pane) => pane.sessionId === "draft", "新建任务未显示草稿");
  123. expect((await navigation()).projectId).toBeNull();
  124. await setV4ComposerText("E2E_DELETED_PROJECT_NEW_DRAFT");
  125. await waitForV4ComposerText("E2E_DELETED_PROJECT_NEW_DRAFT", "新草稿不能输入");
  126. await browser.saveScreenshot(
  127. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, `deleted-project-${entry}-after.png`),
  128. );
  129. });
  130. }
  131. });