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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 "../helpers/desktop-app.js";
  7. import {
  8. approveV4Permission,
  9. clickV4BackgroundWorkCancel,
  10. getV4ComposerBackgroundWorkCounts,
  11. getLatestV4CommandAck,
  12. getV4BackgroundWorks,
  13. openV4ComposerRunningBackgroundWorks,
  14. prepareV4ConversationE2E,
  15. sendV4Prompt,
  16. setV4ElectronWindowSize,
  17. waitForV4PermissionDialog,
  18. } from "../helpers/v4-conversation.js";
  19. describe("v4 M4 门禁:cancelBackgroundWork", () => {
  20. after(async () => {
  21. await browser.electron.restoreAllMocks();
  22. await clearAppData();
  23. });
  24. it("后台 bash 启动 → 面板显示 running → 取消 → cancelled", async function () {
  25. this.timeout(120000);
  26. await prepareV4ConversationE2E();
  27. // 与窄屏共用可容纳的内容高度,只改变待测的宽度断点,避免宿主屏幕截断。
  28. await setV4ElectronWindowSize(1280, 800);
  29. // 首轮触发 run_in_background 的 Bash tool_use。若出现权限弹窗则批准(best-effort)。
  30. await sendV4Prompt("E2E_V4_BGWORK 起一个后台任务");
  31. try {
  32. await waitForV4PermissionDialog(15000);
  33. await approveV4Permission();
  34. } catch {
  35. // 未出现权限弹窗(可能默认放行 bash)——直接等后台工作面板。
  36. }
  37. // Composer 入口先暴露当前 session 的 typed running count。
  38. await browser.waitUntil(
  39. async () => {
  40. const counts = await getV4ComposerBackgroundWorkCounts();
  41. return (
  42. counts?.bashCount === 1 &&
  43. counts.subagentCount === 0 &&
  44. counts.totalCount === 1 &&
  45. counts.typedVisible &&
  46. !counts.compactVisible
  47. );
  48. },
  49. { timeout: 60000, timeoutMsg: "Composer 没有显示 running Bash typed count" },
  50. );
  51. // 窄 Composer 合并为 Activity 总数;点击后 panel 使用 overlay 并直达 Running。
  52. await setV4ElectronWindowSize(480, 800);
  53. await browser.waitUntil(
  54. async () => {
  55. const counts = await getV4ComposerBackgroundWorkCounts();
  56. return counts?.totalCount === 1 && counts.compactVisible && !counts.typedVisible;
  57. },
  58. { timeout: 15000, timeoutMsg: "窄 Composer 没有合并为 Activity 总数" },
  59. );
  60. await openV4ComposerRunningBackgroundWorks(60000);
  61. const narrowLayout = await browser.execute(() => ({
  62. clientWidth: document.documentElement.clientWidth,
  63. scrollWidth: document.documentElement.scrollWidth,
  64. timelineClass:
  65. document.querySelector<HTMLElement>('[data-v4-timeline-content-column="true"]')
  66. ?.className ?? "",
  67. }));
  68. expect(narrowLayout.scrollWidth).toBeLessThanOrEqual(narrowLayout.clientWidth + 1);
  69. expect(narrowLayout.timelineClass).not.toContain("-translate-x-[10.75rem]");
  70. let workId = "";
  71. await browser.waitUntil(
  72. async () => {
  73. const works = await getV4BackgroundWorks();
  74. const running = works.find((w) => w.status === "running");
  75. if (running) {
  76. workId = running.workId;
  77. return true;
  78. }
  79. return false;
  80. },
  81. { timeout: 60000, timeoutMsg: "后台工作面板没有出现 running 项" },
  82. );
  83. expect(workId).toBeTruthy();
  84. // 取消该后台工作 → command accepted/noop,running item 从 active 面板消失。
  85. const clicked = await clickV4BackgroundWorkCancel(workId);
  86. expect(clicked).toBe(true);
  87. await browser.waitUntil(
  88. async () => {
  89. const works = await getV4BackgroundWorks();
  90. return !works.some((work) => work.workId === workId);
  91. },
  92. { timeout: 60000, timeoutMsg: "取消后 running background item 没有消失" },
  93. );
  94. const ack = await getLatestV4CommandAck("cancelBackgroundWork");
  95. expect(["accepted", "noop"]).toContain(ack?.status);
  96. await browser.waitUntil(async () => (await getV4ComposerBackgroundWorkCounts()) === null, {
  97. timeout: 30000,
  98. timeoutMsg: "最后一个 running task 收口后 Composer 入口仍存在",
  99. });
  100. });
  101. });