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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 {
  7. clearAppData,
  8. } from "../helpers/desktop-app.js";
  9. import {
  10. approveV4Permission,
  11. clickV4BackgroundWorkCancel,
  12. getV4ComposerBackgroundWorkCounts,
  13. getLatestV4CommandAck,
  14. getV4BackgroundWorks,
  15. openV4ComposerRunningBackgroundWorks,
  16. prepareV4ConversationE2E,
  17. sendV4Prompt,
  18. setV4ElectronWindowSize,
  19. waitForV4PermissionDialog,
  20. } from "../helpers/v4-conversation.js";
  21. describe("v4 M4 门禁:cancelBackgroundWork", () => {
  22. after(async () => {
  23. await browser.electron.restoreAllMocks();
  24. await clearAppData();
  25. });
  26. it("后台 bash 启动 → 面板显示 running → 取消 → cancelled", async function () {
  27. this.timeout(120000);
  28. await prepareV4ConversationE2E();
  29. await setV4ElectronWindowSize(1280, 900);
  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. await browser.waitUntil(
  40. async () => {
  41. const counts = await getV4ComposerBackgroundWorkCounts();
  42. return (
  43. counts?.bashCount === 1 &&
  44. counts.subagentCount === 0 &&
  45. counts.totalCount === 1 &&
  46. counts.typedVisible &&
  47. !counts.compactVisible
  48. );
  49. },
  50. { timeout: 60000, timeoutMsg: "Composer 没有显示 running Bash typed count" },
  51. );
  52. // 窄 Composer 合并为 Activity 总数;点击后 panel 使用 overlay 并直达 Running。
  53. await setV4ElectronWindowSize(480, 800);
  54. await browser.waitUntil(
  55. async () => {
  56. const counts = await getV4ComposerBackgroundWorkCounts();
  57. return counts?.totalCount === 1 && counts.compactVisible && !counts.typedVisible;
  58. },
  59. { timeout: 15000, timeoutMsg: "窄 Composer 没有合并为 Activity 总数" },
  60. );
  61. await openV4ComposerRunningBackgroundWorks(60000);
  62. const narrowLayout = await browser.execute(() => ({
  63. clientWidth: document.documentElement.clientWidth,
  64. scrollWidth: document.documentElement.scrollWidth,
  65. timelineClass:
  66. document.querySelector<HTMLElement>('[data-v4-timeline-content-column="true"]')
  67. ?.className ?? "",
  68. }));
  69. expect(narrowLayout.scrollWidth).toBeLessThanOrEqual(narrowLayout.clientWidth + 1);
  70. expect(narrowLayout.timelineClass).not.toContain("-translate-x-[10.75rem]");
  71. let workId = "";
  72. await browser.waitUntil(
  73. async () => {
  74. const works = await getV4BackgroundWorks();
  75. const running = works.find((w) => w.status === "running");
  76. if (running) {
  77. workId = running.workId;
  78. return true;
  79. }
  80. return false;
  81. },
  82. { timeout: 60000, timeoutMsg: "后台工作面板没有出现 running 项" },
  83. );
  84. expect(workId).toBeTruthy();
  85. // 取消该后台工作 → command accepted/noop,running item 从 active 面板消失。
  86. const clicked = await clickV4BackgroundWorkCancel(workId);
  87. expect(clicked).toBe(true);
  88. await browser.waitUntil(
  89. async () => {
  90. const works = await getV4BackgroundWorks();
  91. return !works.some((work) => work.workId === workId);
  92. },
  93. { timeout: 60000, timeoutMsg: "取消后 running background item 没有消失" },
  94. );
  95. const ack = await getLatestV4CommandAck("cancelBackgroundWork");
  96. expect(["accepted", "noop"]).toContain(ack?.status);
  97. await browser.waitUntil(
  98. async () => (await getV4ComposerBackgroundWorkCounts()) === null,
  99. { timeout: 30000, timeoutMsg: "最后一个 running task 收口后 Composer 入口仍存在" },
  100. );
  101. });
  102. });