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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { releaseControlledReplay } from "#e2e/helpers/controlled-replay";
  2. import { clearAppData } from "#e2e/helpers/desktop-app";
  3. import {
  4. assertUpstreamRequestCapture,
  5. assertUpstreamThoughtLevelCapture,
  6. waitForUpstreamNetworkCapture,
  7. } from "#e2e/helpers/upstream-capture";
  8. import {
  9. UPSTREAM_MODEL,
  10. UPSTREAM_SECONDARY_MODEL,
  11. UPSTREAM_SECONDARY_THOUGHT_LEVEL,
  12. UPSTREAM_THOUGHT_LEVEL,
  13. ensureUpstreamModelForE2E,
  14. selectUpstreamModelById,
  15. selectUpstreamThoughtLevelValue,
  16. } from "#e2e/helpers/upstream-provider";
  17. import {
  18. E2E_REPLY_TOKEN,
  19. clickV4Stop,
  20. getV4ConversationState,
  21. prepareV4ConversationE2E,
  22. sendV4Prompt,
  23. startNewV4Draft,
  24. waitForV4ConversationState,
  25. waitForV4ComposerText,
  26. } from "#e2e/helpers/v4-conversation";
  27. import {
  28. clickFirstQueueSendNow,
  29. waitForQueueContaining,
  30. waitForQueueCount,
  31. } from "#e2e/helpers/conversation-session-queue";
  32. import {
  33. expectNoUpstreamRequestForTextWithin,
  34. waitForUpstreamRequest,
  35. } from "#e2e/helpers/conversation-session-network";
  36. const MODEL_SWITCH_QUEUE_TIMEOUT_MS = 180000;
  37. describe("会话区模型切换 Queue E2E", () => {
  38. before(async function () {
  39. this.timeout(MODEL_SWITCH_QUEUE_TIMEOUT_MS);
  40. await prepareV4ConversationE2E();
  41. await ensureUpstreamModelForE2E(UPSTREAM_SECONDARY_MODEL);
  42. });
  43. beforeEach(async function () {
  44. this.timeout(MODEL_SWITCH_QUEUE_TIMEOUT_MS);
  45. await startNewV4Draft();
  46. await selectUpstreamModelById(UPSTREAM_MODEL);
  47. await selectUpstreamThoughtLevelValue(UPSTREAM_THOUGHT_LEVEL);
  48. });
  49. afterEach(async () => {
  50. await stopIfBusy();
  51. });
  52. after(async () => {
  53. await browser.electron.restoreAllMocks();
  54. await clearAppData();
  55. });
  56. it("stop 后 held queue 切模型再立即发送,保留提交时 primary 配置", async function () {
  57. this.timeout(MODEL_SWITCH_QUEUE_TIMEOUT_MS);
  58. const runId = Date.now();
  59. const runningMarker = `E2E_MODEL_SWITCH_HELD_RUNNING_${runId}`;
  60. const heldMarker = `E2E_MODEL_SWITCH_HELD_SEND_NOW_${runId}`;
  61. const runningPrompt = `E2E_SLOW_STREAM ${runningMarker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  62. await sendV4Prompt(runningPrompt);
  63. await waitForV4ComposerText("", "模型切换 held 首轮发送后输入框没有清空");
  64. await waitForV4ConversationState(
  65. (snapshot) => snapshot.state === "streaming",
  66. "模型切换 held 首轮没有进入 streaming",
  67. 30000,
  68. );
  69. // 修复原因:标题生成也会短暂进入 streaming;必须等真正的 main request
  70. // 已经进入 provider 后再发送 queued prompt,否则 queued marker 会被并入首轮请求。
  71. await waitForUpstreamRequest(
  72. { excludes: [heldMarker], includes: [runningMarker] },
  73. "模型切换 held 首轮 main request 没有进入 上游 capture",
  74. 30000,
  75. );
  76. const heldPrompt = `${heldMarker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  77. await sendV4Prompt(heldPrompt);
  78. await waitForV4ComposerText("", "模型切换 held queued text 发送后输入框没有清空");
  79. await waitForQueueContaining(heldMarker);
  80. const queuedItems = await waitForQueueCount(1);
  81. expect(queuedItems[0]?.kind).toBe("text");
  82. expect(queuedItems[0]?.content).toContain(heldMarker);
  83. await clickV4Stop();
  84. await waitForV4ConversationState(
  85. (snapshot) => snapshot.state !== "streaming" && snapshot.queueCount === 1,
  86. "stop 后没有退出 streaming 并保留 held queue",
  87. 30000,
  88. );
  89. await expectNoUpstreamRequestForTextWithin(heldMarker, 900);
  90. await selectUpstreamModelById(UPSTREAM_SECONDARY_MODEL);
  91. await selectUpstreamThoughtLevelValue(UPSTREAM_SECONDARY_THOUGHT_LEVEL);
  92. const sentNowItem = await clickFirstQueueSendNow();
  93. expect(sentNowItem.content).toContain(heldMarker);
  94. const sendNowRecord = await waitForUpstreamNetworkCapture(heldMarker);
  95. // 用户确认:立即发送只提升原 Submission,不能重新读取 Composer 的模型选择。
  96. assertUpstreamRequestCapture(sendNowRecord, {
  97. expectedText: heldMarker,
  98. model: UPSTREAM_MODEL,
  99. });
  100. assertUpstreamThoughtLevelCapture(sendNowRecord, UPSTREAM_THOUGHT_LEVEL);
  101. });
  102. it("running 中 queued text 消费前切模型,auto drain 保留提交时 primary 配置", async function () {
  103. this.timeout(MODEL_SWITCH_QUEUE_TIMEOUT_MS);
  104. const runId = Date.now();
  105. const runningMarker = `E2E_MODEL_SWITCH_QUEUE_RUNNING_${runId}`;
  106. const queuedMarker = `E2E_MODEL_SWITCH_QUEUE_AUTO_DRAIN_${runId}`;
  107. const runningPrompt = `E2E_SLOW_STREAM ${runningMarker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  108. await sendV4Prompt(runningPrompt);
  109. await waitForV4ComposerText("", "模型切换 queue 首轮发送后输入框没有清空");
  110. await waitForV4ConversationState(
  111. (snapshot) => snapshot.state === "streaming",
  112. "模型切换 queue 首轮没有进入 streaming",
  113. 30000,
  114. );
  115. await waitForUpstreamRequest(
  116. { excludes: [queuedMarker], includes: [runningMarker] },
  117. "模型切换 queue 首轮 main request 没有进入 上游 capture",
  118. 30000,
  119. );
  120. const queuedPrompt = `${queuedMarker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  121. await sendV4Prompt(queuedPrompt);
  122. await waitForV4ComposerText("", "模型切换 queued text 发送后输入框没有清空");
  123. await waitForQueueContaining(queuedMarker);
  124. const queue = await waitForQueueCount(1);
  125. expect(queue[0]?.kind).toBe("text");
  126. expect(queue[0]?.content).toContain(queuedMarker);
  127. await selectUpstreamModelById(UPSTREAM_SECONDARY_MODEL);
  128. await selectUpstreamThoughtLevelValue(UPSTREAM_SECONDARY_THOUGHT_LEVEL);
  129. // N02 无上下文 guard;模型选择完成后再放行主流,固定真实 auto-drain 边界。
  130. await releaseControlledReplay("upstream-model-switch-queue-running-slow");
  131. await waitForV4ConversationState(
  132. (snapshot) => snapshot.queueCount === 0,
  133. "切换 secondary 后 queued text 没有开始 auto drain",
  134. 120000,
  135. );
  136. const queuedRecord = await waitForUpstreamNetworkCapture(queuedMarker);
  137. // N02:消费不能回读最新 Composer 选择,必须保持提交时冻结的配置。
  138. assertUpstreamRequestCapture(queuedRecord, {
  139. expectedText: queuedMarker,
  140. model: UPSTREAM_MODEL,
  141. });
  142. assertUpstreamThoughtLevelCapture(queuedRecord, UPSTREAM_THOUGHT_LEVEL);
  143. await waitForV4ConversationState(
  144. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  145. "切换 secondary 后 queued text 没有被 auto drain 消费完成",
  146. 90000,
  147. );
  148. });
  149. });
  150. async function stopIfBusy() {
  151. const snapshot = await getV4ConversationState().catch(() => null);
  152. if (snapshot?.state !== "streaming") {
  153. return;
  154. }
  155. await clickV4Stop().catch(() => undefined);
  156. await waitForV4ConversationState(
  157. (candidate) => candidate.state !== "streaming",
  158. "afterEach stop 后没有退出 streaming",
  159. 30000,
  160. ).catch(() => undefined);
  161. }