conversation-session-draft-model-switch-first-send.test.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import {
  2. DEFAULT_WORKSPACE,
  3. clearAppData,
  4. waitForDefaultWorkspaceReady,
  5. waitForWorkspaceApp,
  6. } from "../../../helpers/desktop-app.js";
  7. import { reloadElectronSessionPreservingBrowserProfile } from "../../../helpers/e2e-electron-reload.js";
  8. import { getV4ComposerDraftScopeSnapshot } from "../../../helpers/conversation-session-store.js";
  9. import {
  10. assertUpstreamRequestCapture,
  11. assertUpstreamThoughtLevelCapture,
  12. waitForUpstreamNetworkCapture,
  13. } from "../../../helpers/upstream-capture.js";
  14. import {
  15. UPSTREAM_PROVIDER_ID,
  16. UPSTREAM_MODEL,
  17. UPSTREAM_SECONDARY_MODEL,
  18. UPSTREAM_SECONDARY_THOUGHT_LEVEL,
  19. ensureUpstreamModelForE2E,
  20. getUpstreamProviderModelValues,
  21. getUpstreamThoughtLevelLabels,
  22. getSelectedUpstreamModelLabel,
  23. getSelectedUpstreamThoughtLevelLabel,
  24. selectUpstreamModelById,
  25. selectUpstreamThoughtLevelValue,
  26. } from "../../../helpers/upstream-provider.js";
  27. import {
  28. E2E_REPLY_TOKEN,
  29. prepareConversationE2E,
  30. sendPrompt,
  31. waitForAssistantMessageContaining,
  32. waitForChatState,
  33. waitForComposerText,
  34. waitForUserMessageContaining,
  35. } from "../../../helpers/conversation-session.js";
  36. import {
  37. getV4ModelConfig,
  38. selectV4TaskById,
  39. switchV4Mode,
  40. } from "../../../helpers/v4-conversation.js";
  41. describe("会话区草稿模型切换首发 E2E", () => {
  42. after(async () => {
  43. await browser.electron.restoreAllMocks();
  44. await clearAppData();
  45. });
  46. it("I10/I47/I52: 首发冻结 Composer 选择,接纳后及冷重启继续保留", async function () {
  47. this.timeout(260000);
  48. await prepareConversationE2E();
  49. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 30000);
  50. await ensureUpstreamModelForE2E(UPSTREAM_SECONDARY_MODEL);
  51. await waitForChatState(
  52. (snapshot) =>
  53. snapshot.state === "idle" &&
  54. snapshot.sessionId === null &&
  55. snapshot.taskId === null &&
  56. snapshot.queueCount === 0,
  57. "草稿切模型 case 没有处于空草稿态",
  58. 30000,
  59. );
  60. await selectUpstreamModelById(UPSTREAM_SECONDARY_MODEL, {
  61. includePlainModelFallback: false,
  62. providerId: UPSTREAM_PROVIDER_ID,
  63. });
  64. await selectUpstreamThoughtLevelValue(UPSTREAM_SECONDARY_THOUGHT_LEVEL);
  65. await assertSelectedSecondaryConfig();
  66. const runId = Date.now();
  67. const marker = `E2E_DRAFT_MODEL_SWITCH_FIRST_SEND_${runId}`;
  68. const prompt = `${marker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  69. await sendPrompt(prompt);
  70. await waitForComposerText("", "草稿切模型 case 首发后输入框没有清空");
  71. await waitForUserMessageContaining(marker);
  72. const captureRecord = await waitForUpstreamNetworkCapture(marker);
  73. assertUpstreamRequestCapture(captureRecord, {
  74. expectedText: prompt,
  75. model: UPSTREAM_SECONDARY_MODEL,
  76. });
  77. if (UPSTREAM_SECONDARY_THOUGHT_LEVEL) {
  78. assertUpstreamThoughtLevelCapture(captureRecord, UPSTREAM_SECONDARY_THOUGHT_LEVEL);
  79. }
  80. await waitForAssistantMessageContaining(E2E_REPLY_TOKEN);
  81. const completed = await waitForChatState(
  82. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  83. "草稿切模型 case 首轮完成后没有回到 idle",
  84. 90000,
  85. );
  86. const sessionId = completed.sessionId ?? completed.taskId;
  87. if (!sessionId) throw new Error("草稿切模型 case 首轮完成后缺少 sessionId");
  88. // 已接纳的执行模型仍是 secondary;此后只修改 Composer,不能提前改写 Session。
  89. await assertSelectedSecondaryConfig();
  90. await selectUpstreamModelById(UPSTREAM_MODEL, {
  91. includePlainModelFallback: false,
  92. providerId: UPSTREAM_PROVIDER_ID,
  93. });
  94. await switchV4Mode("edit");
  95. await waitForComposerConfig(UPSTREAM_MODEL, "edit");
  96. const beforeRestartDraft = await getV4ComposerDraftScopeSnapshot(DEFAULT_WORKSPACE, sessionId);
  97. expect(beforeRestartDraft).toMatchObject({
  98. exists: true,
  99. mode: "edit",
  100. modelSelection: {
  101. modelId: UPSTREAM_MODEL,
  102. providerId: UPSTREAM_PROVIDER_ID,
  103. },
  104. });
  105. // 完整重启 Electron/Host/CLI 后,未发送的 Session-scope Composer Draft 仍是唯一恢复源。
  106. await reloadElectronSessionPreservingBrowserProfile(browser);
  107. await waitForRendererAfterReloadSession();
  108. await waitForDefaultWorkspaceReady(60000);
  109. await selectV4TaskById(sessionId);
  110. expect(await getV4ComposerDraftScopeSnapshot(DEFAULT_WORKSPACE, sessionId)).toMatchObject({
  111. exists: true,
  112. mode: "edit",
  113. modelSelection: {
  114. modelId: UPSTREAM_MODEL,
  115. providerId: UPSTREAM_PROVIDER_ID,
  116. },
  117. });
  118. await waitForComposerConfig(UPSTREAM_MODEL, "edit");
  119. });
  120. });
  121. async function waitForComposerConfig(model: string, mode: string) {
  122. let latest = await getV4ModelConfig();
  123. await browser.waitUntil(
  124. async () => {
  125. latest = await getV4ModelConfig();
  126. return (
  127. latest.source === "composer" &&
  128. latest.provider === UPSTREAM_PROVIDER_ID &&
  129. latest.model === model &&
  130. latest.mode === mode
  131. );
  132. },
  133. {
  134. timeout: 30000,
  135. timeoutMsg: `Composer 配置没有恢复: ${JSON.stringify({ expected: { model, mode }, latest })}`,
  136. },
  137. );
  138. }
  139. async function assertSelectedSecondaryConfig() {
  140. const modelLabel = await getSelectedUpstreamModelLabel();
  141. const acceptedModelValues = getUpstreamProviderModelValues(UPSTREAM_SECONDARY_MODEL, {
  142. includePlainModelFallback: false,
  143. providerId: UPSTREAM_PROVIDER_ID,
  144. });
  145. const modelSelected =
  146. acceptedModelValues.includes(modelLabel.currentValue) ||
  147. modelLabel.text.includes(UPSTREAM_SECONDARY_MODEL) ||
  148. modelLabel.title.includes(UPSTREAM_SECONDARY_MODEL);
  149. if (!modelSelected) {
  150. throw new Error(
  151. `草稿当前模型不是 secondary 上游 模型: ${JSON.stringify({
  152. acceptedModelValues,
  153. model: UPSTREAM_SECONDARY_MODEL,
  154. modelLabel,
  155. })}`,
  156. );
  157. }
  158. if (!UPSTREAM_SECONDARY_THOUGHT_LEVEL) {
  159. return;
  160. }
  161. const thoughtLabel = await getSelectedUpstreamThoughtLevelLabel();
  162. const acceptedThoughtValues = [
  163. UPSTREAM_SECONDARY_THOUGHT_LEVEL,
  164. ...getUpstreamThoughtLevelLabels(UPSTREAM_SECONDARY_THOUGHT_LEVEL),
  165. ].map((value) => value.toLowerCase());
  166. const currentThoughtCandidates = [
  167. thoughtLabel.currentValue,
  168. thoughtLabel.ariaLabel,
  169. thoughtLabel.text,
  170. thoughtLabel.title,
  171. ].map((value) => value.replace(/\s+/g, " ").trim().toLowerCase());
  172. if (
  173. !currentThoughtCandidates.some((candidate) => acceptedThoughtValues.includes(candidate))
  174. ) {
  175. throw new Error(
  176. `草稿当前思考深度不是 secondary 配置: ${JSON.stringify({
  177. acceptedThoughtValues,
  178. thoughtLabel,
  179. })}`,
  180. );
  181. }
  182. }
  183. async function waitForRendererAfterReloadSession() {
  184. await browser.waitUntil(
  185. async () => {
  186. try {
  187. const puppeteer = await browser.getPuppeteer();
  188. const target = puppeteer
  189. .targets()
  190. .filter((candidate) => isRendererUrl(candidate.url()))
  191. .at(-1) as ({ _targetId?: string } & object) | undefined;
  192. if (!target?._targetId) return false;
  193. await browser.switchToWindow(target._targetId);
  194. return true;
  195. } catch {
  196. return false;
  197. }
  198. },
  199. {
  200. timeout: 30000,
  201. interval: 250,
  202. timeoutMsg: "Composer Draft 冷重启后没有找到 renderer target",
  203. },
  204. );
  205. }
  206. function isRendererUrl(url: string) {
  207. try {
  208. return new URL(url).pathname.endsWith("/renderer/index.html");
  209. } catch {
  210. return false;
  211. }
  212. }