conversation-session-model-switch-queue.test.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import { clearAppData } from "../../../helpers/desktop-app.js";
  2. import {
  3. assertUpstreamRequestCapture,
  4. assertUpstreamThoughtLevelCapture,
  5. waitForUpstreamNetworkCapture,
  6. } from "../../../helpers/upstream-capture.js";
  7. import {
  8. UPSTREAM_MODEL,
  9. UPSTREAM_SECONDARY_MODEL,
  10. UPSTREAM_SECONDARY_THOUGHT_LEVEL,
  11. UPSTREAM_THOUGHT_LEVEL,
  12. ensureUpstreamModelForE2E,
  13. selectUpstreamModelById,
  14. selectUpstreamThoughtLevelValue,
  15. waitForUpstreamModelSelected,
  16. } from "../../../helpers/upstream-provider.js";
  17. import {
  18. E2E_REPLY_TOKEN,
  19. clickChatStop,
  20. clickFirstQueueSendNow,
  21. expectNoUpstreamRequestForTextWithin,
  22. getChatRootSnapshot,
  23. prepareConversationE2E,
  24. sendPrompt,
  25. startNewTask,
  26. waitForChatState,
  27. waitForComposerText,
  28. waitForUpstreamRequest,
  29. waitForQueueContaining,
  30. waitForQueueCount,
  31. } from "../../../helpers/conversation-session.js";
  32. const MODEL_SWITCH_QUEUE_TIMEOUT_MS = 180000;
  33. const MODEL_SWITCH_GUARD_TITLE_ZH = "需要压缩上下文后再切换模型";
  34. const MODEL_SWITCH_GUARD_TITLE_EN = "Compress context before switching models";
  35. describe("会话区模型切换 Queue E2E", () => {
  36. before(async function () {
  37. this.timeout(MODEL_SWITCH_QUEUE_TIMEOUT_MS);
  38. await prepareConversationE2E();
  39. await ensureUpstreamModelForE2E(UPSTREAM_SECONDARY_MODEL);
  40. });
  41. beforeEach(async function () {
  42. this.timeout(MODEL_SWITCH_QUEUE_TIMEOUT_MS);
  43. await startNewTask();
  44. await selectUpstreamModelByIdWithGuardConfirm(UPSTREAM_MODEL);
  45. await selectUpstreamThoughtLevelValue(UPSTREAM_THOUGHT_LEVEL);
  46. });
  47. afterEach(async () => {
  48. await stopIfBusy();
  49. });
  50. after(async () => {
  51. await browser.electron.restoreAllMocks();
  52. await clearAppData();
  53. });
  54. it("stop 后 held queue 切模型再立即发送,应使用 secondary 配置", async function () {
  55. this.timeout(MODEL_SWITCH_QUEUE_TIMEOUT_MS);
  56. const runId = Date.now();
  57. const runningMarker = `E2E_MODEL_SWITCH_HELD_RUNNING_${runId}`;
  58. const heldMarker = `E2E_MODEL_SWITCH_HELD_SEND_NOW_${runId}`;
  59. const runningPrompt = `E2E_SLOW_STREAM ${runningMarker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  60. await sendPrompt(runningPrompt);
  61. await waitForComposerText("", "模型切换 held 首轮发送后输入框没有清空");
  62. await waitForChatState(
  63. (snapshot) => snapshot.state === "streaming",
  64. "模型切换 held 首轮没有进入 streaming",
  65. 30000,
  66. );
  67. // 修复原因:标题生成也会短暂进入 streaming;必须等真正的 main request
  68. // 已经进入 provider 后再发送 queued prompt,否则 queued marker 会被并入首轮请求。
  69. await waitForUpstreamRequest(
  70. { excludes: [heldMarker], includes: [runningMarker] },
  71. "模型切换 held 首轮 main request 没有进入 上游 capture",
  72. 30000,
  73. );
  74. const heldPrompt = `${heldMarker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  75. await sendPrompt(heldPrompt);
  76. await waitForComposerText("", "模型切换 held queued text 发送后输入框没有清空");
  77. await waitForQueueContaining(heldMarker);
  78. const queuedItems = await waitForQueueCount(1);
  79. expect(queuedItems[0]?.kind).toBe("text");
  80. expect(queuedItems[0]?.content).toContain(heldMarker);
  81. await clickChatStop();
  82. await waitForChatState(
  83. (snapshot) => snapshot.state !== "streaming" && snapshot.queueCount === 1,
  84. "stop 后没有退出 streaming 并保留 held queue",
  85. 30000,
  86. );
  87. await expectNoUpstreamRequestForTextWithin(heldMarker, 900);
  88. await selectUpstreamModelByIdWithGuardConfirm(UPSTREAM_SECONDARY_MODEL);
  89. await selectUpstreamThoughtLevelValue(UPSTREAM_SECONDARY_THOUGHT_LEVEL);
  90. const sentNowItem = await clickFirstQueueSendNow();
  91. expect(sentNowItem.content).toContain(heldMarker);
  92. const sendNowRecord = await waitForUpstreamNetworkCapture(heldMarker);
  93. assertSecondaryUpstreamConfig(sendNowRecord, heldMarker);
  94. });
  95. it("running 中 queued text 消费前切模型,auto drain 应使用 secondary 配置", async function () {
  96. this.timeout(MODEL_SWITCH_QUEUE_TIMEOUT_MS);
  97. const runId = Date.now();
  98. const runningMarker = `E2E_MODEL_SWITCH_QUEUE_RUNNING_${runId}`;
  99. const queuedMarker = `E2E_MODEL_SWITCH_QUEUE_AUTO_DRAIN_${runId}`;
  100. const runningPrompt = `E2E_SLOW_STREAM ${runningMarker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  101. await sendPrompt(runningPrompt);
  102. await waitForComposerText("", "模型切换 queue 首轮发送后输入框没有清空");
  103. await waitForChatState(
  104. (snapshot) => snapshot.state === "streaming",
  105. "模型切换 queue 首轮没有进入 streaming",
  106. 30000,
  107. );
  108. await waitForUpstreamRequest(
  109. { excludes: [queuedMarker], includes: [runningMarker] },
  110. "模型切换 queue 首轮 main request 没有进入 上游 capture",
  111. 30000,
  112. );
  113. const queuedPrompt = `${queuedMarker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  114. await sendPrompt(queuedPrompt);
  115. await waitForComposerText("", "模型切换 queued text 发送后输入框没有清空");
  116. await waitForQueueContaining(queuedMarker);
  117. const queue = await waitForQueueCount(1);
  118. expect(queue[0]?.kind).toBe("text");
  119. expect(queue[0]?.content).toContain(queuedMarker);
  120. await selectUpstreamModelByIdWithGuardConfirm(UPSTREAM_SECONDARY_MODEL);
  121. await selectUpstreamThoughtLevelValue(UPSTREAM_SECONDARY_THOUGHT_LEVEL);
  122. await waitForChatState(
  123. (snapshot) => snapshot.queueCount === 0,
  124. "切换 secondary 后 queued text 没有开始 auto drain",
  125. 120000,
  126. );
  127. const queuedRecord = await waitForUpstreamNetworkCapture(queuedMarker);
  128. assertSecondaryUpstreamConfig(queuedRecord, queuedMarker);
  129. await waitForChatState(
  130. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  131. "切换 secondary 后 queued text 没有被 auto drain 消费完成",
  132. 90000,
  133. );
  134. });
  135. });
  136. function assertSecondaryUpstreamConfig(
  137. record: Awaited<ReturnType<typeof waitForUpstreamNetworkCapture>>,
  138. expectedText: string,
  139. ) {
  140. assertUpstreamRequestCapture(record, {
  141. expectedText,
  142. model: UPSTREAM_SECONDARY_MODEL,
  143. });
  144. assertUpstreamThoughtLevelCapture(record, UPSTREAM_SECONDARY_THOUGHT_LEVEL);
  145. }
  146. async function selectUpstreamModelByIdWithGuardConfirm(modelId: string) {
  147. await selectUpstreamModelById(modelId, {
  148. afterModelItemClick: () => confirmModelSwitchCompressionDialogIfPresent(modelId),
  149. });
  150. }
  151. async function confirmModelSwitchCompressionDialogIfPresent(modelId: string) {
  152. // Bugfix: 切模型 guard 是异步弹出的;短探测只在真实出现时接管,避免无 guard 场景被额外等待放大 running queue 自动消费竞态。
  153. const hasDialog = await waitForModelSwitchCompressionDialog(250).catch(() => false);
  154. if (!hasDialog) {
  155. return false;
  156. }
  157. await confirmModelSwitchCompressionDialog();
  158. await waitForChatState(
  159. (snapshot) => !snapshot.modelSwitchPending,
  160. `模型 ${modelId} guard 压缩后切换 pending 状态没有结束`,
  161. 120000,
  162. );
  163. await waitForUpstreamModelSelected(modelId);
  164. return true;
  165. }
  166. async function confirmModelSwitchCompressionDialog() {
  167. await waitForModelSwitchCompressionDialog(15000);
  168. const clicked = await browser.execute(() => {
  169. const buttons = Array.from(document.querySelectorAll<HTMLButtonElement>("button"));
  170. const confirmButton = buttons.find((button) => {
  171. const text = button.innerText.replace(/\s+/g, " ").trim();
  172. return text === "压缩" || text === "Compress" || text.includes("压缩");
  173. });
  174. confirmButton?.click();
  175. return Boolean(confirmButton);
  176. });
  177. expect(clicked).toBe(true);
  178. }
  179. async function waitForModelSwitchCompressionDialog(timeout: number) {
  180. await browser.waitUntil(async () => hasModelSwitchCompressionDialog(), {
  181. timeout,
  182. timeoutMsg: "没有出现模型切换前的上下文压缩确认弹窗",
  183. });
  184. return true;
  185. }
  186. async function hasModelSwitchCompressionDialog() {
  187. return browser.execute(
  188. (titleZh, titleEn) => {
  189. const bodyText = document.body.innerText;
  190. return bodyText.includes(titleZh) || bodyText.includes(titleEn);
  191. },
  192. MODEL_SWITCH_GUARD_TITLE_ZH,
  193. MODEL_SWITCH_GUARD_TITLE_EN,
  194. );
  195. }
  196. async function stopIfBusy() {
  197. const snapshot = await getChatRootSnapshot().catch(() => null);
  198. if (snapshot?.state !== "streaming") {
  199. return;
  200. }
  201. await clickChatStop().catch(() => undefined);
  202. await waitForChatState(
  203. (candidate) => candidate.state !== "streaming",
  204. "afterEach stop 后没有退出 streaming",
  205. 30000,
  206. ).catch(() => undefined);
  207. }