conversation-session-goal.test.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import { clearAppData } from "#e2e/helpers/desktop-app";
  2. import {
  3. E2E_REPLY_TOKEN,
  4. assertVisibleV4UserMessagesNotContaining,
  5. clickV4Stop,
  6. getV4ConversationState,
  7. prepareV4ConversationE2E,
  8. sendV4Prompt,
  9. startNewV4Draft,
  10. waitForV4ConversationState,
  11. waitForV4ComposerText,
  12. waitForV4UserMessageContaining,
  13. } from "#e2e/helpers/v4-conversation";
  14. import {
  15. countUpstreamRequestsContaining,
  16. expectNoUpstreamRequestForTextWithin,
  17. } from "#e2e/helpers/conversation-session-network";
  18. import { waitForQueueContaining, waitForQueueCount } from "#e2e/helpers/conversation-session-queue";
  19. describe("会话区 Goal E2E", () => {
  20. after(async () => {
  21. await browser.electron.restoreAllMocks();
  22. await clearAppData();
  23. });
  24. it("completed 下 /goal 设置和更新;running 入队,held 显式选择后保留队列发送", async function () {
  25. this.timeout(180000);
  26. await prepareV4ConversationE2E();
  27. const runId = Date.now();
  28. const firstGoalObjective = `E2E_GOAL_SET_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  29. const firstGoal = `/goal ${firstGoalObjective}`;
  30. await sendV4Prompt(firstGoal);
  31. await waitForV4ComposerText("", "首个 /goal 发送后输入框没有清空");
  32. await waitForV4UserMessageContaining(firstGoalObjective);
  33. await waitForV4ConversationState(
  34. (snapshot) =>
  35. snapshot.queueCount === 0 &&
  36. snapshot.targetStatus === "active" &&
  37. snapshot.targetObjective?.includes(`E2E_GOAL_SET_${runId}`) === true,
  38. "首个 /goal 没有设置 active target",
  39. 90000,
  40. );
  41. const firstGoalStreaming = await waitForV4ConversationState(
  42. (snapshot) => snapshot.state === "streaming",
  43. "首个 /goal 设置后没有进入目标验证/继续运行",
  44. 30000,
  45. );
  46. expect(firstGoalStreaming.targetStatus).toBe("active");
  47. expect(firstGoalStreaming.targetStatus).not.toBe("validating");
  48. await expectNoGoalValidatingLockForWindow(firstGoalStreaming);
  49. await assertVisibleV4UserMessagesNotContaining("goal-continuation");
  50. await assertVisibleV4UserMessagesNotContaining("Current session goal state");
  51. const firstGoalCompletedSnapshot = await waitForV4ConversationState(
  52. (snapshot) =>
  53. snapshot.state !== "streaming" &&
  54. snapshot.activeInputId === null &&
  55. !snapshot.stopRequested &&
  56. snapshot.queueCount === 0 &&
  57. snapshot.targetObjective?.includes(`E2E_GOAL_SET_${runId}`) === true &&
  58. isCompletedTargetStatus(snapshot.targetStatus),
  59. // 修复原因:GL/H02 验证 completed 下已有 goal 再更新;旧 case 通过 stop
  60. // 制造空闲态,但 stop 后 runtime activeInputId 会短暂保留,下一条 /goal 容易被当成 queue。
  61. "首个 /goal 没有通过 verifier 自然进入完成态",
  62. 60000,
  63. );
  64. if (!firstGoalCompletedSnapshot.taskId) {
  65. throw new Error("首个 /goal 完成后没有可用 taskId");
  66. }
  67. // V4 的目标与执行状态来自 conversation 投影;legacy task meta 不再同步这些字段。
  68. // 上面的 activeInputId/queue/verified 联合条件才是下一次提交的完成屏障。
  69. const updatedGoalObjective = `E2E_GOAL_UPDATE_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  70. const updatedGoal = `/goal ${updatedGoalObjective}`;
  71. await sendV4Prompt(updatedGoal);
  72. await waitForV4ComposerText("", "更新 /goal 发送后输入框没有清空");
  73. await waitForV4UserMessageContaining(updatedGoalObjective);
  74. await waitForV4ConversationState(
  75. (snapshot) =>
  76. snapshot.queueCount === 0 &&
  77. snapshot.targetStatus === "active" &&
  78. snapshot.targetObjective?.includes(`E2E_GOAL_UPDATE_${runId}`) === true,
  79. "更新 /goal 没有替换 active target",
  80. 90000,
  81. );
  82. const updatedGoalStreaming = await waitForV4ConversationState(
  83. (snapshot) => snapshot.state === "streaming",
  84. "更新 /goal 后没有进入目标验证/继续运行",
  85. 30000,
  86. );
  87. expect(updatedGoalStreaming.targetStatus).toBe("active");
  88. expect(updatedGoalStreaming.targetStatus).not.toBe("validating");
  89. await expectNoGoalValidatingLockForWindow(updatedGoalStreaming);
  90. await waitForV4ConversationState(
  91. (snapshot) =>
  92. snapshot.state !== "streaming" &&
  93. !snapshot.stopRequested &&
  94. snapshot.queueCount === 0 &&
  95. snapshot.targetObjective?.includes(`E2E_GOAL_UPDATE_${runId}`) === true &&
  96. isCompletedTargetStatus(snapshot.targetStatus),
  97. "更新 /goal 没有通过 verifier 自然进入完成态",
  98. 60000,
  99. );
  100. await startNewV4Draft();
  101. await waitForV4ConversationState(
  102. (snapshot) => snapshot.taskId === null && snapshot.queueCount === 0,
  103. "新建任务后没有进入空会话草稿态",
  104. 15000,
  105. );
  106. const slowPrompt = `E2E_SLOW_STREAM E2E_GOAL_RUNNING_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  107. await sendV4Prompt(slowPrompt);
  108. await waitForV4ComposerText("", "goal 已设置后普通消息发送没有清空输入框");
  109. await waitForV4ConversationState(
  110. (snapshot) => snapshot.state === "streaming",
  111. "goal 已设置后普通消息没有进入 streaming",
  112. 30000,
  113. );
  114. const runningGoal = `/goal E2E_GOAL_RUNNING_QUEUE_${runId}`;
  115. await sendV4Prompt(runningGoal);
  116. await waitForQueueContaining(`E2E_GOAL_RUNNING_QUEUE_${runId}`);
  117. let queue = await waitForQueueCount(1);
  118. expect(queue[0]?.kind).toBe("goal");
  119. await expectNoUpstreamRequestForTextWithin(`E2E_GOAL_RUNNING_QUEUE_${runId}`, 900);
  120. await clickV4Stop();
  121. await waitForV4ConversationState(
  122. (snapshot) => snapshot.state !== "streaming" && snapshot.queueCount === 1,
  123. "stop 后没有保留 running 中追加的 /goal queue",
  124. 30000,
  125. );
  126. const heldGoal = `/goal E2E_GOAL_HELD_QUEUE_${runId}`;
  127. const heldRequestsBefore = await countUpstreamRequestsContaining(
  128. `E2E_GOAL_HELD_QUEUE_${runId}`,
  129. );
  130. await sendV4Prompt(heldGoal);
  131. // held 输入已按协议改为显式选择;旧用例忽略确认框,误把正确门禁当作未入队。
  132. const keepQueueSend = await $('[data-testid="v4-composer-keep-queue-send"]');
  133. await keepQueueSend.waitForClickable();
  134. queue = await waitForQueueCount(1);
  135. expect(queue[0]?.content).toContain(`E2E_GOAL_RUNNING_QUEUE_${runId}`);
  136. await expectNoUpstreamRequestForTextWithin(`E2E_GOAL_HELD_QUEUE_${runId}`, 900);
  137. expect(await countUpstreamRequestsContaining(`E2E_GOAL_HELD_QUEUE_${runId}`)).toBe(
  138. heldRequestsBefore,
  139. );
  140. await keepQueueSend.click();
  141. await waitForV4ComposerText("", "保留队列发送 goal 后输入框没有清空");
  142. await waitForV4ConversationState(
  143. (snapshot) =>
  144. snapshot.targetObjective?.includes(`E2E_GOAL_HELD_QUEUE_${runId}`) === true &&
  145. snapshot.targetStatus === "verified" &&
  146. snapshot.activeInputId === null &&
  147. snapshot.queueCount === 1,
  148. "保留队列发送的新 goal 没有完成或原队列被改变",
  149. 60000,
  150. );
  151. expect((await waitForQueueCount(1))[0]?.content).toContain(`E2E_GOAL_RUNNING_QUEUE_${runId}`);
  152. });
  153. });
  154. function isCompletedTargetStatus(status: string | null) {
  155. return status === "verified";
  156. }
  157. async function expectNoGoalValidatingLockForWindow(
  158. initialSnapshot: Awaited<ReturnType<typeof getV4ConversationState>>,
  159. durationMs = 800,
  160. ) {
  161. const deadline = Date.now() + durationMs;
  162. let sawStreaming = initialSnapshot.state === "streaming";
  163. let sawActiveTarget = initialSnapshot.targetStatus === "active";
  164. const statuses: Array<{ state: string | null; targetStatus: string | null }> = [
  165. {
  166. state: initialSnapshot.state,
  167. targetStatus: initialSnapshot.targetStatus,
  168. },
  169. ];
  170. while (Date.now() < deadline) {
  171. const snapshot = await getV4ConversationState();
  172. statuses.push({
  173. state: snapshot.state,
  174. targetStatus: snapshot.targetStatus,
  175. });
  176. sawStreaming ||= snapshot.state === "streaming";
  177. sawActiveTarget ||= snapshot.targetStatus === "active";
  178. expect(snapshot.targetStatus).not.toBe("validating");
  179. await browser.pause(100);
  180. }
  181. expect({ sawActiveTarget, sawStreaming, statuses }).toEqual(
  182. expect.objectContaining({
  183. sawActiveTarget: true,
  184. sawStreaming: true,
  185. }),
  186. );
  187. }