conversation-session-v4-background-work.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // M4 门禁:cancelBackgroundWork(v4 后台工作面板取消)。
  2. // 证据层 L3:首轮模型返回 run_in_background 的 Bash tool_use → 批准权限 → 后台 bash 启动
  3. // → BackgroundTaskStarted 事件 → reducer 填充 backgroundWorks → 后台工作面板显示 running →
  4. // 点取消 → cancelBackgroundWork 命令 → v4-bridge cancelBackgroundTask → 进程被杀 →
  5. // BackgroundTaskCompleted(cancelled) → 面板状态 cancelled。证明后台工作全链路。
  6. import { clearAppData } from "#e2e/helpers/desktop-app";
  7. import {
  8. approveV4Permission,
  9. clickV4BackgroundWorkCancel,
  10. describeV4BackgroundFailureState,
  11. getV4ComposerBackgroundWorkCounts,
  12. getLatestV4CommandAck,
  13. getV4BackgroundWorks,
  14. openV4ComposerRunningBackgroundWorks,
  15. prepareV4ConversationE2E,
  16. sendV4Prompt,
  17. setV4ElectronWindowSize,
  18. waitForV4PermissionDialog,
  19. } from "#e2e/helpers/v4-conversation";
  20. describe("v4 M4 门禁:cancelBackgroundWork", () => {
  21. after(async () => {
  22. await browser.electron.restoreAllMocks();
  23. await clearAppData();
  24. });
  25. it("后台 bash 启动 → 面板显示 running → 取消 → cancelled", async function () {
  26. this.timeout(120000);
  27. await prepareV4ConversationE2E();
  28. // 与窄屏共用可容纳的内容高度,只改变待测的宽度断点,避免宿主屏幕截断。
  29. await setV4ElectronWindowSize(1280, 800);
  30. // 首轮触发 run_in_background 的 Bash tool_use。若出现权限弹窗则批准(best-effort)。
  31. await sendV4Prompt("E2E_V4_BGWORK 起一个后台任务");
  32. try {
  33. await waitForV4PermissionDialog(15000);
  34. await approveV4Permission();
  35. } catch {
  36. // 未出现权限弹窗(可能默认放行 bash)——直接等后台工作面板。
  37. }
  38. // Composer 入口先暴露当前 session 的 typed running count。
  39. try {
  40. await browser.waitUntil(
  41. async () => {
  42. const counts = await getV4ComposerBackgroundWorkCounts();
  43. return (
  44. counts?.bashCount === 1 &&
  45. counts.subagentCount === 0 &&
  46. counts.totalCount === 1 &&
  47. counts.typedVisible &&
  48. !counts.compactVisible
  49. );
  50. },
  51. { timeout: 60000, timeoutMsg: "Composer 没有显示 running Bash typed count" },
  52. );
  53. } catch (error) {
  54. throw new Error(
  55. `Composer 没有显示 running Bash typed count: ${await describeV4BackgroundFailureState()}`,
  56. { cause: error },
  57. );
  58. }
  59. // 窄屏按实际文字宽度收缩,不再固定隐藏单个 Activity 的文案。
  60. await setV4ElectronWindowSize(480, 800);
  61. await browser.waitUntil(
  62. async () => {
  63. const counts = await getV4ComposerBackgroundWorkCounts();
  64. return counts?.totalCount === 1 && counts.bashCount === 1;
  65. },
  66. { timeout: 15000, timeoutMsg: "窄 Composer 丢失 Activity 总数" },
  67. );
  68. await openV4ComposerRunningBackgroundWorks(60000);
  69. const narrowLayout = await browser.execute(() => ({
  70. clientWidth: document.documentElement.clientWidth,
  71. scrollWidth: document.documentElement.scrollWidth,
  72. timelineClass:
  73. document.querySelector<HTMLElement>('[data-v4-timeline-content-column="true"]')
  74. ?.className ?? "",
  75. }));
  76. expect(narrowLayout.scrollWidth).toBeLessThanOrEqual(narrowLayout.clientWidth + 1);
  77. expect(narrowLayout.timelineClass).not.toContain("-translate-x-[10.75rem]");
  78. let workId = "";
  79. await browser.waitUntil(
  80. async () => {
  81. const works = await getV4BackgroundWorks();
  82. const running = works.find((w) => w.status === "running");
  83. if (running) {
  84. workId = running.workId;
  85. return true;
  86. }
  87. return false;
  88. },
  89. { timeout: 60000, timeoutMsg: "后台工作面板没有出现 running 项" },
  90. );
  91. expect(workId).toBeTruthy();
  92. // 取消该后台工作 → command accepted/noop,running item 从 active 面板消失。
  93. const clicked = await clickV4BackgroundWorkCancel(workId);
  94. expect(clicked).toBe(true);
  95. await browser.waitUntil(
  96. async () => {
  97. const works = await getV4BackgroundWorks();
  98. return !works.some((work) => work.workId === workId);
  99. },
  100. { timeout: 60000, timeoutMsg: "取消后 running background item 没有消失" },
  101. );
  102. const ack = await getLatestV4CommandAck("cancelBackgroundWork");
  103. expect(["accepted", "noop"]).toContain(ack?.status);
  104. await browser.waitUntil(async () => (await getV4ComposerBackgroundWorkCounts()) === null, {
  105. timeout: 30000,
  106. timeoutMsg: "最后一个 running task 收口后 Composer 入口仍存在",
  107. });
  108. });
  109. });