conversation-session-permission-full-access.test.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // PA158:真实审批、风险勾选、混合 Plan 队列及队列项冻结编辑。
  2. import { mkdir, readFile, rm } from "node:fs/promises";
  3. import { join } from "node:path";
  4. import { startDraftFromProject } from "#e2e/helpers/project-sidebar";
  5. import { clearAppData, DEFAULT_WORKSPACE } from "#e2e/helpers/desktop-app";
  6. import { resolveE2ERuntimePath } from "#e2e/helpers/e2e-runtime-paths";
  7. import {
  8. clickV4ComposerEditCancel,
  9. openV4QueueItemEditInComposer,
  10. waitForV4ComposerEditClosed,
  11. waitForV4QueueItemEditLock,
  12. } from "#e2e/helpers/v4-composer-edit";
  13. import {
  14. getV4ConfigProjection,
  15. getV4ModelConfig,
  16. getV4QueueItems,
  17. prepareV4ConversationE2E,
  18. sendV4Prompt,
  19. waitForV4ComposerText,
  20. waitForV4ComposerSelectionReady,
  21. switchV4Mode,
  22. waitForV4PermissionDialog,
  23. waitForV4QueueCount,
  24. waitForV4TimelineContaining,
  25. } from "#e2e/helpers/v4-conversation";
  26. const CASE = "conversation-session-permission-full-access";
  27. const DIRECTORY = resolveE2ERuntimePath(CASE);
  28. describe("PA158 审批完全访问", () => {
  29. before(async () => {
  30. await mkdir(DIRECTORY, { recursive: true });
  31. });
  32. after(async () => {
  33. await browser.electron.restoreAllMocks();
  34. await clearAppData();
  35. await rm(DIRECTORY, { recursive: true, force: true });
  36. });
  37. for (const target of ["guarded", "yolo"] as const)
  38. it(`审批切到 ${target},队列编辑与新会话继承所选权限`, async () => {
  39. await rm(join(DIRECTORY, "output.txt"), { force: true });
  40. // 新隔离 HOME 会先进入职业引导,按真实 UI 完成准备,不能伪造业务快照。
  41. await browser.waitUntil(
  42. async () =>
  43. browser.execute(() =>
  44. Boolean(
  45. document.querySelector(
  46. '[data-testid="onboarding-page"],[data-testid="login-use-api-key-button"],[data-testid^="v4-session-pane-"]',
  47. ),
  48. ),
  49. ),
  50. { timeout: 30000 },
  51. );
  52. const onboarding = await $('[data-testid="onboarding-page"]');
  53. if (await onboarding.isExisting()) {
  54. for (let step = 0; step < 3; step += 1) {
  55. await $(
  56. '//main[@data-testid="onboarding-page"]//button[normalize-space()="Skip" or normalize-space()="跳过"]',
  57. ).click();
  58. }
  59. await onboarding.waitForExist({ reverse: true });
  60. }
  61. await prepareV4ConversationE2E({ skipProvider: true });
  62. // 同目录也可以有未归类任务;从项目入口创建,确保前后两份会话确实属于同一项目。
  63. await startDraftFromProject(DEFAULT_WORKSPACE);
  64. await prepareV4ConversationE2E();
  65. await switchV4Mode("build");
  66. await sendV4Prompt("E2E_PA158_MAIN 执行需要确认的写文件操作");
  67. await waitForV4TimelineContaining("E2E_PA158_RUNNING");
  68. await switchV4Mode("plan");
  69. await sendV4Prompt("E2E_PA158_QUEUE_PLAN");
  70. await waitForV4QueueCount(1);
  71. await $('[data-testid="v4-composer-plan-marker"] button').click();
  72. // 菜单已隐藏 edit,旧步骤会在授权前超时;用可选的 guarded 保留混合权限和 Plan 场景。
  73. await switchV4Mode("guarded");
  74. expect(await getV4ConfigProjection()).toMatchObject({ mode: "guarded", planEnabled: false });
  75. await sendV4Prompt("E2E_PA158_QUEUE_GUARDED");
  76. await waitForV4QueueCount(2);
  77. const queue = await getV4QueueItems();
  78. await switchV4Mode("plan");
  79. await waitForV4PermissionDialog();
  80. const submitMode = await $('[data-testid="permission-allow-switch"]');
  81. await submitMode.waitForDisplayed();
  82. const picker = await $('[data-testid="permission-target-picker"]');
  83. expect(await picker.getAttribute("data-mode")).toBe("guarded");
  84. const beforeConfirmation = await getV4ModelConfig();
  85. const assertApprovalStillPending = async () => {
  86. expect(await submitMode.isDisplayed()).toBe(true);
  87. expect(await getV4ModelConfig()).toEqual(beforeConfirmation);
  88. expect(await getV4ConfigProjection()).toMatchObject({
  89. mode: "guarded",
  90. planEnabled: true,
  91. });
  92. expect(await getV4QueueItems()).toEqual(queue);
  93. await expect(readFile(join(DIRECTORY, "output.txt"), "utf8")).rejects.toMatchObject({
  94. code: "ENOENT",
  95. });
  96. };
  97. if (process.env.ZCODE_E2E_ARTIFACT_DIR) {
  98. await browser.saveScreenshot(
  99. join(process.env.ZCODE_E2E_ARTIFACT_DIR, "permission-full-access.png"),
  100. );
  101. }
  102. if (target === "yolo") {
  103. await picker.click();
  104. await $('[data-testid="permission-target-yolo"]').click();
  105. await assertApprovalStillPending();
  106. await submitMode.click();
  107. await $('[data-testid="yolo-confirmation-dialog"]').waitForDisplayed();
  108. expect(await $('[data-testid="yolo-confirmation-confirm"]').isEnabled()).toBe(false);
  109. await $('[data-testid="yolo-confirmation-risk"]').click();
  110. await assertApprovalStillPending();
  111. await $('[data-testid="yolo-confirmation-cancel"]').click();
  112. await $('[data-testid="yolo-confirmation-dialog"]').waitForDisplayed({ reverse: true });
  113. await assertApprovalStillPending();
  114. await submitMode.click();
  115. await $('[data-testid="yolo-confirmation-dialog"]').waitForDisplayed();
  116. expect(await $('[data-testid="yolo-confirmation-confirm"]').isEnabled()).toBe(false);
  117. await $('[data-testid="yolo-confirmation-risk"]').click();
  118. await assertApprovalStillPending();
  119. await $('[data-testid="yolo-confirmation-confirm"]').click();
  120. } else await submitMode.click();
  121. await waitForV4TimelineContaining("E2E_PA158_DONE");
  122. expect(await readFile(resolveE2ERuntimePath(CASE, "output.txt"), "utf8")).toBe(
  123. "E2E_PA158_TOOL_RESULT\n",
  124. );
  125. expect(await getV4QueueItems()).toEqual(queue);
  126. expect(await getV4ConfigProjection()).toMatchObject({ mode: target, planEnabled: true });
  127. // PA158-05:编辑队列项时原位冻结,工具栏载入该项记录的更新后权限与各自 Plan;
  128. // 取消后队列不变,工具栏恢复进入编辑态前的草稿配置。
  129. const draftConfig = await getV4ConfigProjection();
  130. await openV4QueueItemEditInComposer(queue[0]!.queueItemId);
  131. await waitForV4ComposerText("E2E_PA158_QUEUE_PLAN", "第一项编辑态没有载入原文");
  132. expect(await getV4ConfigProjection()).toMatchObject({ mode: target, planEnabled: true });
  133. expect(await getV4QueueItems()).toHaveLength(2);
  134. await clickV4ComposerEditCancel();
  135. await waitForV4ComposerEditClosed("第一项取消编辑后没有退出编辑态");
  136. await openV4QueueItemEditInComposer(queue[1]!.queueItemId);
  137. await waitForV4ComposerText("E2E_PA158_QUEUE_GUARDED", "第二项编辑态没有载入原文");
  138. expect(await getV4ConfigProjection()).toMatchObject({ mode: target, planEnabled: false });
  139. await clickV4ComposerEditCancel();
  140. await waitForV4ComposerEditClosed("第二项取消编辑后没有退出编辑态");
  141. // 取消后 releaseQueueItemEdit 异步回到投影;锁消失前该行仍显示「正在下方编辑」。
  142. await waitForV4QueueItemEditLock(
  143. queue[1]!.queueItemId,
  144. (state) => state.exists && !state.locked,
  145. "第二项取消编辑后锁没有释放",
  146. );
  147. expect(await getV4ConfigProjection()).toMatchObject({
  148. mode: draftConfig.mode,
  149. planEnabled: draftConfig.planEnabled,
  150. });
  151. expect(await getV4QueueItems()).toEqual(queue);
  152. await startDraftFromProject(DEFAULT_WORKSPACE);
  153. await waitForV4ComposerSelectionReady();
  154. expect(await getV4ConfigProjection()).toMatchObject({ mode: target, planEnabled: false });
  155. expect((await getV4ModelConfig()).model).toBe(beforeConfirmation.model);
  156. });
  157. });