conversation-session-model-provider-session-isolation.test.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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_MODEL,
  9. UPSTREAM_PROVIDER_ID,
  10. UPSTREAM_SECONDARY_MODEL,
  11. UPSTREAM_SECONDARY_THOUGHT_LEVEL,
  12. UPSTREAM_THOUGHT_LEVEL,
  13. ensureUpstreamModelForE2E,
  14. getUpstreamProviderModelValues,
  15. getUpstreamThoughtLevelLabels,
  16. getSelectedUpstreamModelLabel,
  17. getSelectedUpstreamThoughtLevelLabel,
  18. selectUpstreamModelById,
  19. selectUpstreamThoughtLevelValue,
  20. } from "#e2e/helpers/upstream-provider";
  21. import {
  22. E2E_REPLY_TOKEN,
  23. prepareV4ConversationE2E,
  24. selectV4TaskById,
  25. sendV4Prompt,
  26. startNewV4Draft,
  27. waitForV4ConversationState,
  28. waitForV4ComposerText,
  29. waitForV4UserMessageContaining,
  30. } from "#e2e/helpers/v4-conversation";
  31. interface ExpectedConfig {
  32. model: string;
  33. thoughtLevel: string;
  34. }
  35. const PRIMARY_CONFIG: ExpectedConfig = {
  36. model: UPSTREAM_MODEL,
  37. thoughtLevel: UPSTREAM_THOUGHT_LEVEL,
  38. };
  39. const SECONDARY_CONFIG: ExpectedConfig = {
  40. model: UPSTREAM_SECONDARY_MODEL,
  41. thoughtLevel: UPSTREAM_SECONDARY_THOUGHT_LEVEL,
  42. };
  43. describe("会话区模型供应商 session 隔离 E2E", () => {
  44. after(async () => {
  45. await browser.electron.restoreAllMocks();
  46. await clearAppData();
  47. });
  48. it("I12: A/B session 应分别保留 provider/model/thought 配置", async function () {
  49. this.timeout(260000);
  50. await prepareV4ConversationE2E();
  51. await ensureUpstreamModelForE2E(UPSTREAM_SECONDARY_MODEL);
  52. await waitForEmptyDraft();
  53. await selectExpectedConfig(PRIMARY_CONFIG);
  54. const runId = Date.now();
  55. const promptA = buildPrompt(`E2E_MODEL_PROVIDER_SESSION_ISOLATION_A_${runId}`);
  56. await sendPromptAndAssertConfig(promptA, PRIMARY_CONFIG, "A 首轮");
  57. const sessionA = await waitForIdleSession("A 首轮没有完成");
  58. await startNewV4Draft();
  59. await waitForEmptyDraft();
  60. await selectExpectedConfig(SECONDARY_CONFIG);
  61. const promptB = buildPrompt(`E2E_MODEL_PROVIDER_SESSION_ISOLATION_B_${runId}`);
  62. await sendPromptAndAssertConfig(promptB, SECONDARY_CONFIG, "B 首轮");
  63. const sessionB = await waitForIdleSession("B 首轮没有完成");
  64. expect(sessionB).not.toBe(sessionA);
  65. await selectV4TaskById(sessionA);
  66. await waitForSessionVisible(sessionA, "切回 A 后没有恢复 A session");
  67. await assertSelectedExpectedConfig(PRIMARY_CONFIG);
  68. const promptAFollow = buildPrompt(`E2E_MODEL_PROVIDER_SESSION_ISOLATION_A_FOLLOW_${runId}`);
  69. await sendPromptAndAssertConfig(promptAFollow, PRIMARY_CONFIG, "A follow-up");
  70. await waitForIdleSession("A follow-up 没有完成");
  71. await selectV4TaskById(sessionB);
  72. await waitForSessionVisible(sessionB, "切回 B 后没有恢复 B session");
  73. await assertSelectedExpectedConfig(SECONDARY_CONFIG);
  74. const promptBFollow = buildPrompt(`E2E_MODEL_PROVIDER_SESSION_ISOLATION_B_FOLLOW_${runId}`);
  75. await sendPromptAndAssertConfig(promptBFollow, SECONDARY_CONFIG, "B follow-up");
  76. await waitForIdleSession("B follow-up 没有完成");
  77. });
  78. });
  79. function buildPrompt(marker: string) {
  80. return `${marker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  81. }
  82. async function selectExpectedConfig(config: ExpectedConfig) {
  83. await selectUpstreamModelById(config.model, {
  84. includePlainModelFallback: false,
  85. providerId: UPSTREAM_PROVIDER_ID,
  86. });
  87. await selectUpstreamThoughtLevelValue(config.thoughtLevel);
  88. await assertSelectedExpectedConfig(config);
  89. }
  90. async function sendPromptAndAssertConfig(prompt: string, config: ExpectedConfig, label: string) {
  91. const marker = prompt.split(":")[0] ?? prompt;
  92. await sendV4Prompt(prompt);
  93. await waitForV4ComposerText("", `${label} 发送后输入框没有清空`);
  94. await waitForV4UserMessageContaining(marker);
  95. const record = await waitForUpstreamNetworkCapture(marker);
  96. assertUpstreamRequestCapture(record, {
  97. expectedText: prompt,
  98. model: config.model,
  99. });
  100. if (config.thoughtLevel) {
  101. assertUpstreamThoughtLevelCapture(record, config.thoughtLevel);
  102. }
  103. }
  104. async function assertSelectedExpectedConfig(config: ExpectedConfig) {
  105. const modelLabel = await getSelectedUpstreamModelLabel();
  106. const acceptedModelValues = getUpstreamProviderModelValues(config.model, {
  107. includePlainModelFallback: false,
  108. providerId: UPSTREAM_PROVIDER_ID,
  109. });
  110. const modelSelected =
  111. acceptedModelValues.includes(modelLabel.currentValue) ||
  112. modelLabel.text.includes(config.model) ||
  113. modelLabel.title.includes(config.model);
  114. if (!modelSelected) {
  115. throw new Error(
  116. `当前模型不是期望配置: ${JSON.stringify({
  117. acceptedModelValues,
  118. expected: config,
  119. modelLabel,
  120. })}`,
  121. );
  122. }
  123. if (!config.thoughtLevel) {
  124. return;
  125. }
  126. const thoughtLabel = await getSelectedUpstreamThoughtLevelLabel();
  127. const acceptedThoughtValues = [
  128. config.thoughtLevel,
  129. ...getUpstreamThoughtLevelLabels(config.thoughtLevel),
  130. ].map((value) => value.toLowerCase());
  131. const currentThoughtCandidates = [
  132. thoughtLabel.currentValue,
  133. thoughtLabel.ariaLabel,
  134. thoughtLabel.text,
  135. thoughtLabel.title,
  136. ].map((value) => value.replace(/\s+/g, " ").trim().toLowerCase());
  137. if (!currentThoughtCandidates.some((candidate) => acceptedThoughtValues.includes(candidate))) {
  138. throw new Error(
  139. `当前思考深度不是期望配置: ${JSON.stringify({
  140. acceptedThoughtValues,
  141. expected: config,
  142. thoughtLabel,
  143. })}`,
  144. );
  145. }
  146. }
  147. async function waitForEmptyDraft() {
  148. await waitForV4ConversationState(
  149. (snapshot) =>
  150. snapshot.state === "idle" &&
  151. snapshot.sessionId === null &&
  152. snapshot.taskId === null &&
  153. snapshot.queueCount === 0 &&
  154. !snapshot.modelSwitchPending,
  155. "没有进入空草稿态",
  156. 30000,
  157. );
  158. }
  159. async function waitForSessionVisible(sessionId: string, timeoutMsg: string) {
  160. await waitForV4ConversationState(
  161. (snapshot) => snapshot.sessionId === sessionId && snapshot.state === "idle",
  162. timeoutMsg,
  163. 30000,
  164. );
  165. }
  166. async function waitForIdleSession(timeoutMsg: string) {
  167. const snapshot = await waitForV4ConversationState(
  168. (candidate) =>
  169. candidate.state === "idle" && candidate.queueCount === 0 && Boolean(candidate.sessionId),
  170. timeoutMsg,
  171. 90000,
  172. );
  173. if (!snapshot.sessionId) {
  174. throw new Error(`${timeoutMsg}: sessionId missing`);
  175. }
  176. return snapshot.sessionId;
  177. }