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

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