conversation-session-readstate-model-isolation.test.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import { clearAppData } from "#e2e/helpers/desktop-app";
  2. import {
  3. assertUpstreamRequestCapture,
  4. assertUpstreamThoughtLevelCapture,
  5. waitForUpstreamNetworkCapture,
  6. } from "#e2e/helpers/upstream-capture";
  7. import {
  8. UPSTREAM_PROVIDER_ID,
  9. UPSTREAM_SECONDARY_MODEL,
  10. UPSTREAM_SECONDARY_THOUGHT_LEVEL,
  11. ensureUpstreamModelForE2E,
  12. getUpstreamProviderModelValues,
  13. getUpstreamThoughtLevelLabels,
  14. getSelectedUpstreamModelLabel,
  15. getSelectedUpstreamThoughtLevelLabel,
  16. selectUpstreamModelById,
  17. selectUpstreamThoughtLevelValue,
  18. } from "#e2e/helpers/upstream-provider";
  19. import {
  20. E2E_REPLY_TOKEN,
  21. getV4Messages,
  22. prepareV4ConversationE2E,
  23. selectV4TaskById,
  24. sendV4Prompt,
  25. startNewV4Draft,
  26. waitForV4ComposerText,
  27. waitForV4ConversationState,
  28. } from "#e2e/helpers/v4-conversation";
  29. const CASE_MARKER = "E2E_READSTATE_MODEL_ISOLATION";
  30. describe("会话区 readState 不覆盖历史 session 模型 E2E", () => {
  31. after(async () => {
  32. await browser.electron.restoreAllMocks();
  33. await clearAppData();
  34. });
  35. it("I67: 新建草稿水合后切回非首个模型 session,续发请求仍使用该 session 模型", async function () {
  36. this.timeout(220000);
  37. await prepareV4ConversationE2E();
  38. await ensureUpstreamModelForE2E(UPSTREAM_SECONDARY_MODEL);
  39. await waitForEmptyDraft();
  40. await selectSecondaryConfig();
  41. const runId = Date.now();
  42. const firstPrompt = buildPrompt(`${CASE_MARKER}_FIRST_${runId}`);
  43. await sendPromptAndAssertSecondaryConfig(firstPrompt, "readState 隔离首轮");
  44. const sessionId = await waitForIdleSession("readState 隔离首轮没有完成");
  45. await startNewV4Draft();
  46. await waitForEmptyDraft();
  47. await selectV4TaskById(sessionId);
  48. await waitForSessionVisible(sessionId, "切回旧 session 后没有恢复 idle");
  49. await assertSelectedSecondaryConfig();
  50. const followPrompt = buildPrompt(`${CASE_MARKER}_FOLLOW_${runId}`);
  51. await sendPromptAndAssertSecondaryConfig(followPrompt, "readState 隔离 follow-up");
  52. await waitForIdleSession("readState 隔离 follow-up 没有完成");
  53. });
  54. });
  55. function buildPrompt(marker: string) {
  56. return `${marker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  57. }
  58. async function selectSecondaryConfig() {
  59. await selectUpstreamModelById(UPSTREAM_SECONDARY_MODEL, {
  60. includePlainModelFallback: false,
  61. providerId: UPSTREAM_PROVIDER_ID,
  62. });
  63. await selectUpstreamThoughtLevelValue(UPSTREAM_SECONDARY_THOUGHT_LEVEL);
  64. await assertSelectedSecondaryConfig();
  65. }
  66. async function sendPromptAndAssertSecondaryConfig(prompt: string, label: string) {
  67. const marker = prompt.split(":")[0] ?? prompt;
  68. await sendV4Prompt(prompt);
  69. await waitForV4ComposerText("", `${label} 发送后输入框没有清空`);
  70. await waitForV4UserMessageContaining(marker);
  71. const record = await waitForUpstreamNetworkCapture(marker);
  72. assertUpstreamRequestCapture(record, {
  73. expectedText: prompt,
  74. model: UPSTREAM_SECONDARY_MODEL,
  75. });
  76. if (UPSTREAM_SECONDARY_THOUGHT_LEVEL) {
  77. assertUpstreamThoughtLevelCapture(record, UPSTREAM_SECONDARY_THOUGHT_LEVEL);
  78. }
  79. }
  80. async function assertSelectedSecondaryConfig() {
  81. const modelLabel = await getSelectedUpstreamModelLabel();
  82. const acceptedModelValues = getUpstreamProviderModelValues(UPSTREAM_SECONDARY_MODEL, {
  83. includePlainModelFallback: false,
  84. providerId: UPSTREAM_PROVIDER_ID,
  85. });
  86. const modelSelected =
  87. acceptedModelValues.includes(modelLabel.currentValue) ||
  88. modelLabel.text.includes(UPSTREAM_SECONDARY_MODEL) ||
  89. modelLabel.title.includes(UPSTREAM_SECONDARY_MODEL);
  90. if (!modelSelected) {
  91. throw new Error(
  92. `当前模型不是 readState 隔离 case 的 secondary 模型: ${JSON.stringify({
  93. acceptedModelValues,
  94. model: UPSTREAM_SECONDARY_MODEL,
  95. modelLabel,
  96. })}`,
  97. );
  98. }
  99. if (!UPSTREAM_SECONDARY_THOUGHT_LEVEL) {
  100. return;
  101. }
  102. const thoughtLabel = await getSelectedUpstreamThoughtLevelLabel();
  103. const acceptedThoughtValues = [
  104. UPSTREAM_SECONDARY_THOUGHT_LEVEL,
  105. ...getUpstreamThoughtLevelLabels(UPSTREAM_SECONDARY_THOUGHT_LEVEL),
  106. ].map((value) => value.toLowerCase());
  107. const currentThoughtCandidates = [
  108. thoughtLabel.currentValue,
  109. thoughtLabel.ariaLabel,
  110. thoughtLabel.text,
  111. thoughtLabel.title,
  112. ].map((value) => value.replace(/\s+/g, " ").trim().toLowerCase());
  113. if (!currentThoughtCandidates.some((candidate) => acceptedThoughtValues.includes(candidate))) {
  114. throw new Error(
  115. `当前思考深度不是 readState 隔离 case 的 secondary 配置: ${JSON.stringify({
  116. acceptedThoughtValues,
  117. thoughtLabel,
  118. })}`,
  119. );
  120. }
  121. }
  122. async function waitForEmptyDraft() {
  123. await waitForV4ConversationState(
  124. (snapshot) =>
  125. snapshot.state === "idle" &&
  126. (snapshot.sessionId === null || snapshot.sessionId === "draft") &&
  127. (snapshot.taskId === null || snapshot.taskId === "draft") &&
  128. snapshot.queueCount === 0 &&
  129. !snapshot.modelSwitchPending,
  130. "没有进入空草稿态",
  131. 30000,
  132. );
  133. }
  134. async function waitForSessionVisible(sessionId: string, timeoutMsg: string) {
  135. await waitForV4ConversationState(
  136. (snapshot) => snapshot.sessionId === sessionId && snapshot.state === "idle",
  137. timeoutMsg,
  138. 30000,
  139. );
  140. }
  141. async function waitForIdleSession(timeoutMsg: string) {
  142. const snapshot = await waitForV4ConversationState(
  143. (candidate) =>
  144. candidate.state === "idle" &&
  145. candidate.queueCount === 0 &&
  146. Boolean(candidate.sessionId) &&
  147. candidate.sessionId !== "draft",
  148. timeoutMsg,
  149. 90000,
  150. );
  151. if (!snapshot.sessionId || snapshot.sessionId === "draft") {
  152. throw new Error(`${timeoutMsg}: sessionId missing`);
  153. }
  154. return snapshot.sessionId;
  155. }
  156. async function waitForV4UserMessageContaining(marker: string) {
  157. await browser.waitUntil(
  158. async () => (await getV4Messages("user")).some((message) => message.text.includes(marker)),
  159. {
  160. timeout: 20000,
  161. timeoutMsg: `没有看到包含 ${marker} 的用户消息`,
  162. },
  163. );
  164. }