conversation-session-model-provider-regression-gaps.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import { dirname } from "node:path";
  2. import { TID_V4_COMPOSER_INPUT } from "@zcode/shared/test-ids";
  3. import {
  4. DEFAULT_WORKSPACE,
  5. clearAppData,
  6. setInputValueByTestIdDom,
  7. } from "../helpers/desktop-app.js";
  8. import {
  9. assertUpstreamRequestCapture,
  10. waitForUpstreamNetworkCapture,
  11. } from "../helpers/upstream-capture.js";
  12. import {
  13. UPSTREAM_MODEL,
  14. UPSTREAM_SECONDARY_MODEL,
  15. UPSTREAM_SECONDARY_THOUGHT_LEVEL,
  16. UPSTREAM_THOUGHT_LEVEL,
  17. ensureUpstreamModelForE2E,
  18. selectUpstreamModelById,
  19. selectUpstreamThoughtLevelValue,
  20. waitForUpstreamModelSelected,
  21. } from "../helpers/upstream-provider.js";
  22. import {
  23. E2E_REPLY_TOKEN,
  24. clickV4Stop,
  25. getV4Messages,
  26. prepareV4ConversationE2E,
  27. selectV4TaskById,
  28. sendV4Prompt,
  29. startNewV4Draft,
  30. waitForV4ConversationState,
  31. waitForV4ComposerText,
  32. waitForV4UserMessageContaining,
  33. } from "#e2e/helpers/v4-conversation";
  34. import {
  35. clickQueueEdit,
  36. getQueueItems,
  37. waitForQueueContaining,
  38. waitForQueueCount,
  39. } from "#e2e/helpers/conversation-session-queue";
  40. import { waitForUpstreamRequest } from "#e2e/helpers/conversation-session-network";
  41. import { getPassiveAgentStartupWorkspacePaths } from "../helpers/passive-agent-startup-fixture.js";
  42. import {
  43. openWorkspaceDraftFromSidebar,
  44. waitForActiveWorkspacePath,
  45. waitForRestoredProjects,
  46. } from "../helpers/workspace-agent-warmup.js";
  47. import { restartIntoWorkspacePreservingProfile } from "../helpers/model-provider-restart.js";
  48. /**
  49. * 目录里没有被既有正式用例完整覆盖的组合。
  50. *
  51. * 每个测试都走真实的选择器、composer、Queue 和重启路径;provider fixture 只负责把
  52. * 在途窗口和确定性回复固定下来。双主窗口场景仍保持 pending,因为产品没有用户入口,
  53. * 不能用 Electron 内部造窗冒充真实 E2E。
  54. */
  55. describe("Model Provider 回归目录缺口 E2E", () => {
  56. afterEach(async () => {
  57. await stopIfBusy();
  58. });
  59. after(async () => {
  60. await browser.electron.restoreAllMocks();
  61. await clearAppData();
  62. });
  63. it("F-APP-002/F-APP-005/F-HISTORY-001: 切换 workspace 后模型选择与历史不串", async function () {
  64. this.timeout(240000);
  65. await prepareV4ConversationE2E();
  66. await ensureUpstreamModelForE2E(UPSTREAM_SECONDARY_MODEL);
  67. const workspacePaths = getPassiveAgentStartupWorkspacePaths(dirname(DEFAULT_WORKSPACE), 2);
  68. await waitForRestoredProjects(workspacePaths, 60000);
  69. const workspaceA = DEFAULT_WORKSPACE;
  70. const workspaceB = workspacePaths[0];
  71. if (!workspaceB) throw new Error("workspace fixture 不完整");
  72. await openWorkspaceDraftFromSidebar(workspaceA);
  73. await waitForActiveWorkspacePath(workspaceA);
  74. await selectUpstreamModelById(UPSTREAM_MODEL);
  75. await selectUpstreamThoughtLevelValue(UPSTREAM_THOUGHT_LEVEL);
  76. const markerA = `E2E_CATALOG_WORKSPACE_A_${Date.now()}`;
  77. const promptA = `${markerA}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  78. await sendV4Prompt(promptA);
  79. await waitForV4UserMessageContaining(markerA);
  80. const taskA = await waitForV4ConversationState(
  81. (snapshot) => Boolean(snapshot.taskId),
  82. "workspace A 首发后没有绑定持久化 task",
  83. );
  84. if (!taskA.taskId) throw new Error("workspace A 首发后 taskId 为空");
  85. assertUpstreamRequestCapture(await waitForUpstreamNetworkCapture(markerA), {
  86. expectedText: promptA,
  87. model: UPSTREAM_MODEL,
  88. });
  89. await openWorkspaceDraftFromSidebar(workspaceB);
  90. await waitForActiveWorkspacePath(workspaceB);
  91. await selectUpstreamModelById(UPSTREAM_SECONDARY_MODEL);
  92. await selectUpstreamThoughtLevelValue(UPSTREAM_SECONDARY_THOUGHT_LEVEL);
  93. const markerB = `E2E_CATALOG_WORKSPACE_B_${Date.now()}`;
  94. const promptB = `${markerB}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  95. await sendV4Prompt(promptB);
  96. await waitForV4UserMessageContaining(markerB);
  97. assertUpstreamRequestCapture(await waitForUpstreamNetworkCapture(markerB), {
  98. expectedText: promptB,
  99. model: UPSTREAM_SECONDARY_MODEL,
  100. });
  101. await openWorkspaceDraftFromSidebar(workspaceA);
  102. await waitForActiveWorkspacePath(workspaceA);
  103. await selectV4TaskById(taskA.taskId, 30000);
  104. await waitForV4UserMessageContaining(markerA);
  105. const messagesA = await getV4Messages("user");
  106. expect(messagesA.map((message) => message.text).join("\n")).toContain(markerA);
  107. expect(messagesA.map((message) => message.text).join("\n")).not.toContain(markerB);
  108. });
  109. it("F-DRAFT-002/F-DRAFT-004: 预热和页面往返期间草稿选择以最后一次选择为准", async function () {
  110. this.timeout(220000);
  111. await prepareV4ConversationE2E();
  112. await ensureUpstreamModelForE2E(UPSTREAM_SECONDARY_MODEL);
  113. await startNewV4Draft();
  114. await Promise.allSettled([
  115. selectUpstreamModelById(UPSTREAM_MODEL),
  116. selectUpstreamModelById(UPSTREAM_SECONDARY_MODEL),
  117. ]);
  118. await selectUpstreamModelById(UPSTREAM_SECONDARY_MODEL);
  119. await selectUpstreamThoughtLevelValue(UPSTREAM_SECONDARY_THOUGHT_LEVEL);
  120. await waitForUpstreamModelSelected(UPSTREAM_SECONDARY_MODEL);
  121. const settingsButton = await $(`[data-testid="task-settings-button"]`);
  122. if (await settingsButton.isExisting()) {
  123. await settingsButton.click();
  124. await browser.waitUntil(async () => await $(`[data-testid="settings-page"]`).isDisplayed(), {
  125. timeout: 15000,
  126. timeoutMsg: "模型草稿 case 设置页没有打开",
  127. });
  128. const back = await $(`[data-testid="settings-back-button"]`);
  129. if (await back.isExisting()) await back.click();
  130. }
  131. await waitForUpstreamModelSelected(UPSTREAM_SECONDARY_MODEL);
  132. const marker = `E2E_CATALOG_DRAFT_PREWARM_${Date.now()}`;
  133. const prompt = `${marker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  134. await sendV4Prompt(prompt);
  135. await waitForV4ComposerText("", "草稿预热首发后输入框没有清空");
  136. assertUpstreamRequestCapture(await waitForUpstreamNetworkCapture(marker), {
  137. expectedText: prompt,
  138. model: UPSTREAM_SECONDARY_MODEL,
  139. });
  140. });
  141. it("F-SESSION-004/F-SESSION-006: 快速切换和重复选择收敛到最后一次模型", async function () {
  142. this.timeout(220000);
  143. // 连续 case 可能把上一个测试的 active session 留在当前 workspace;先进入
  144. // 空草稿,再初始化 provider,确保本次模型切换作用于同一个新 session。
  145. await prepareV4ConversationE2E({ resetDraftBeforeProvider: true });
  146. await ensureUpstreamModelForE2E(UPSTREAM_SECONDARY_MODEL);
  147. // 三次连续的真实下拉选择模拟用户快速点 A → B → A;每次都等待 UI
  148. // 把已提交的选择投影回来,避免并发 WebDriver 点击本身制造非用户行为的
  149. // 菜单竞争。
  150. await selectUpstreamModelById(UPSTREAM_MODEL, { includePlainModelFallback: false });
  151. await selectUpstreamModelById(UPSTREAM_SECONDARY_MODEL, { includePlainModelFallback: false });
  152. await selectUpstreamModelById(UPSTREAM_MODEL, { includePlainModelFallback: false });
  153. await selectUpstreamModelById(UPSTREAM_SECONDARY_MODEL);
  154. await selectUpstreamModelById(UPSTREAM_SECONDARY_MODEL);
  155. // 思考深度与模型选择分别由 F-SESSION-003 和其他正式用例覆盖;本 case
  156. // 只验证快速模型选择的最终身份,避免把两个独立异步状态合并后掩盖竞态。
  157. const marker = `E2E_CATALOG_SELECTION_CONVERGENCE_${Date.now()}`;
  158. const prompt = `${marker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  159. await sendV4Prompt(prompt);
  160. const capture = await waitForUpstreamNetworkCapture(marker);
  161. assertUpstreamRequestCapture(capture, {
  162. expectedText: prompt,
  163. model: UPSTREAM_SECONDARY_MODEL,
  164. });
  165. });
  166. it("F-APP-004/F-SESSION-002/F-USAGE-001: 生成中退出不会重发,恢复后保留明确会话", async function () {
  167. this.timeout(260000);
  168. await prepareV4ConversationE2E();
  169. await ensureUpstreamModelForE2E(UPSTREAM_SECONDARY_MODEL);
  170. await startNewV4Draft();
  171. await selectUpstreamModelById(UPSTREAM_MODEL);
  172. const marker = `E2E_CATALOG_EXIT_DURING_STREAM_${Date.now()}`;
  173. const prompt = `E2E_SLOW_STREAM ${marker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  174. await sendV4Prompt(prompt);
  175. await waitForUpstreamRequest({ includes: [marker] }, "退出前没有进入 provider 请求");
  176. await waitForV4ConversationState(
  177. (snapshot) => snapshot.state === "streaming",
  178. "退出前没有进入 streaming",
  179. );
  180. await waitForV4UserMessageContaining(marker);
  181. const beforeMessages = await getV4Messages("user");
  182. const beforeRestart = await waitForV4ConversationState(
  183. (snapshot) => Boolean(snapshot.taskId),
  184. "退出前没有绑定可恢复的会话",
  185. );
  186. if (!beforeRestart.taskId) throw new Error("退出前 taskId 为空");
  187. await restartIntoWorkspacePreservingProfile();
  188. // 重启 helper 已等待原 workspace 恢复;这里恢复原 task,不能重新初始化空草稿。
  189. await selectV4TaskById(beforeRestart.taskId, 60000);
  190. await waitForV4ConversationState(
  191. (snapshot) =>
  192. snapshot.taskId === beforeRestart.taskId &&
  193. snapshot.state !== "submitting" &&
  194. snapshot.state !== "streaming",
  195. "重启后会话没有恢复到稳定状态",
  196. 60000,
  197. );
  198. await waitForV4UserMessageContaining(marker);
  199. const afterRestart = await getV4Messages("user");
  200. expect(afterRestart.filter((message) => message.text.includes(marker))).toHaveLength(1);
  201. expect(afterRestart.length).toBeGreaterThanOrEqual(beforeMessages.length);
  202. });
  203. it("F-QUEUE-006: 多条 Queue 维持顺序,编辑动作只删除目标项并保留另一条", async function () {
  204. this.timeout(260000);
  205. await prepareV4ConversationE2E();
  206. await ensureUpstreamModelForE2E(UPSTREAM_SECONDARY_MODEL);
  207. await startNewV4Draft();
  208. await selectUpstreamModelById(UPSTREAM_MODEL);
  209. const runId = Date.now();
  210. const running = `E2E_QUEUE_CATALOG_RUNNING_${runId}`;
  211. await sendV4Prompt(
  212. `E2E_SLOW_STREAM ${running}: keep streaming before replying ${E2E_REPLY_TOKEN}`,
  213. );
  214. await waitForV4ConversationState(
  215. (snapshot) => snapshot.state === "streaming",
  216. "Queue running turn 没有开始",
  217. );
  218. await waitForUpstreamRequest({ includes: [running] }, "Queue running turn 没有进入 provider");
  219. const first = `E2E_QUEUE_CATALOG_FIRST_${runId}`;
  220. const second = `E2E_QUEUE_CATALOG_SECOND_${runId}`;
  221. const firstPrompt = `${first}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  222. const secondPrompt = `${second}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  223. await sendV4Prompt(firstPrompt);
  224. await sendV4Prompt(secondPrompt);
  225. await waitForQueueContaining(first);
  226. await waitForQueueContaining(second);
  227. expect(await waitForQueueCount(2)).toHaveLength(2);
  228. await clickV4Stop();
  229. await waitForV4ConversationState(
  230. (snapshot) => snapshot.state !== "streaming" && snapshot.queueCount === 2,
  231. "Queue running turn stop 后没有保留两条待消费消息",
  232. );
  233. const queueBeforeEdit = await getQueueItems();
  234. const firstItem = queueBeforeEdit.find((item) => item.content.includes(first));
  235. if (!firstItem) throw new Error("Queue 中没有找到待编辑的第一条消息");
  236. await clickQueueEdit(firstItem.id);
  237. const edited = `${first}_EDITED`;
  238. // V4 的编辑动作会把 queue 内容恢复到 composer,而不是在 queue row 内嵌输入框;
  239. // 这里先断言目标项已从 Queue 移除,再把新文本写回真实 composer。重新发送的
  240. // queueItemId 与请求断言由正式的 v4 queue spec 覆盖,避免把“停止后立即发送”
  241. // 与“运行中重新入队”两种产品状态混在一个不稳定 case 里。
  242. await waitForV4ComposerText(firstPrompt, "Queue 编辑后 composer 没有恢复原消息");
  243. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, edited);
  244. await waitForV4ComposerText(edited, "Queue 编辑后的 composer 没有写入新文本");
  245. const queueAfterEdit = await getQueueItems();
  246. expect(queueAfterEdit.some((item) => item.content.includes(first))).toBe(false);
  247. expect(queueAfterEdit.some((item) => item.content.includes(second))).toBe(true);
  248. });
  249. });
  250. async function stopIfBusy() {
  251. const snapshot = await waitForV4ConversationState(() => true, "读取 chat 状态失败", 5000).catch(
  252. () => null,
  253. );
  254. if (snapshot?.state === "streaming" || snapshot?.state === "submitting") {
  255. await clickV4Stop().catch(() => undefined);
  256. await waitForV4ConversationState(
  257. (candidate) => candidate.state !== "streaming" && candidate.state !== "submitting",
  258. "清理 running session 失败",
  259. 30000,
  260. ).catch(() => undefined);
  261. }
  262. }