conversation-session-composer-edit-mode.test.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. // D21–D28 composer 编辑态(docs/ui/composer-edit-mode.md):编辑 latest user query 复用底部 composer。
  2. // 证据层 L3:
  3. // 1) 进入编辑态前暂存原草稿;取消、提交成功、失效后普通发送成功都恢复原草稿;取消恢复工具栏配置,
  4. // 提交成功保留提交的模型与 mode、Plan 回到草稿值;
  5. // 2) 原消息位置显示「正在下方编辑」,点击只聚焦 composer,不滚动;Esc 不取消编辑态;
  6. // 3) 编辑态能力与普通 composer 完全一致:新增附件、工具栏配置随 editUserQuery 提交;
  7. // 4) 目标失效只依据投影 canEdit:队列续跑后发送走普通 sendText;
  8. // 5) 切换 task 保持编辑态,刷新只恢复原草稿;切换编辑目标时有改动先确认。
  9. import { TID_V4_EDIT, testId } from "@zcode/shared/test-ids";
  10. import {
  11. DEFAULT_WORKSPACE,
  12. clearAppData,
  13. clickTestIdByDom,
  14. waitForWorkspaceApp,
  15. } from "#e2e/helpers/desktop-app";
  16. import { releaseControlledReplay } from "#e2e/helpers/controlled-replay";
  17. import {
  18. countUpstreamRequestsContaining,
  19. getUpstreamRequestEvidence,
  20. waitForUpstreamRequest,
  21. } from "#e2e/helpers/conversation-session-network";
  22. import {
  23. ensureUpstreamModelForE2E,
  24. selectUpstreamModelById,
  25. UPSTREAM_PROVIDER_ID,
  26. UPSTREAM_SECONDARY_MODEL,
  27. } from "#e2e/helpers/upstream-provider";
  28. import {
  29. clickV4ComposerEditCancel,
  30. clickV4UserRowEditHint,
  31. confirmV4ComposerEditSwitch,
  32. getV4ComposerEditState,
  33. getV4QueueItemEditLock,
  34. waitForV4QueueItemEditLock,
  35. getV4UserRowEditHint,
  36. hasV4ComposerEditSwitchDialog,
  37. openLatestV4UserQueryEditInComposer,
  38. openV4QueueItemEditInComposer,
  39. waitForV4ComposerEditClosed,
  40. waitForV4ComposerEditState,
  41. } from "#e2e/helpers/v4-composer-edit";
  42. import {
  43. clickV4Send,
  44. clickV4Stop,
  45. getV4ComposerAttachments,
  46. getV4ComposerText,
  47. getV4ConfigProjection,
  48. getV4ModelConfig,
  49. getV4PaneSnapshot,
  50. getV4QueueItems,
  51. getV4TimelineScrollState,
  52. isV4ComposerFocused,
  53. pasteV4ComposerFileAttachment,
  54. prepareV4ConversationE2E,
  55. selectV4TaskById,
  56. sendV4Prompt,
  57. setV4ComposerText,
  58. startNewV4Draft,
  59. switchV4Mode,
  60. waitForV4ComposerText,
  61. waitForV4Pane,
  62. waitForV4QueueCount,
  63. waitForV4TimelineContaining,
  64. } from "#e2e/helpers/v4-conversation";
  65. const DRAFT_TEXT = "E2E_CEM_DRAFT 保留的原草稿";
  66. const DRAFT_FILE = "e2e-cem-draft.txt";
  67. const ADDED_FILE = "e2e-cem-added.txt";
  68. const ADDED_FILE_CONTENT = "E2E_CEM_ADDED_ATTACHMENT_CONTENT";
  69. describe("D21–D28 composer 编辑态:编辑 latest user query", () => {
  70. afterEach(async () => {
  71. const pane = await getV4PaneSnapshot();
  72. if (!pane.canStop) return;
  73. // controlled stream 跨 case 存活时会与下一条 case 并行;主动 stop 并等待收口。
  74. await clickV4Stop();
  75. await waitForV4Pane((next) => !next.canStop, "composer 编辑态 case 收尾 stop 没有完成", 30000);
  76. });
  77. after(async () => {
  78. await browser.electron.restoreAllMocks();
  79. await clearAppData();
  80. });
  81. it("D21/D26:有草稿时编辑 → 占位与状态条 → Esc 不取消、点占位只聚焦 → 取消恢复原草稿", async () => {
  82. await prepareV4ConversationE2E();
  83. await startNewV4Draft();
  84. await sendV4Prompt("E2E_CEM_ORIGINAL D21 原始问题");
  85. await waitForV4TimelineContaining("CEM_ORIGINAL_REPLY", 45000);
  86. await waitForV4Pane((s) => !s.canStop, "D21 首轮没有收口", 45000);
  87. await writeDraftWithAttachment();
  88. const rowId = await openLatestV4UserQueryEditInComposer();
  89. const editing = await getV4ComposerEditState();
  90. expect(editing).toMatchObject({ kind: "userQuery", state: "editing", hasCancel: true });
  91. expect(editing?.text.length).toBeGreaterThan(0);
  92. await waitForV4ComposerText("E2E_CEM_ORIGINAL D21 原始问题", "编辑态没有载入原消息正文");
  93. expect(await getV4ComposerAttachments()).toHaveLength(0);
  94. // 原位置显示占位并隐藏编辑入口。
  95. await browser.waitUntil(async () => (await getV4UserRowEditHint(rowId)).hint, {
  96. timeout: 15000,
  97. timeoutMsg: "原消息位置没有出现「正在下方编辑」占位",
  98. });
  99. const hint = await getV4UserRowEditHint(rowId);
  100. expect(hint.hintText.length).toBeGreaterThan(0);
  101. expect(hint.editAction).toBe(false);
  102. // D26:Esc 不取消编辑态。
  103. await browser.keys(["Escape"]);
  104. expect(await getV4ComposerEditState()).toMatchObject({ kind: "userQuery", state: "editing" });
  105. // D26:点击占位只聚焦 composer,不滚动时间线。
  106. await browser.execute(() => (document.activeElement as HTMLElement | null)?.blur());
  107. const scrollBefore = await getV4TimelineScrollState();
  108. await clickV4UserRowEditHint(rowId);
  109. await browser.waitUntil(() => isV4ComposerFocused(), {
  110. timeout: 5000,
  111. timeoutMsg: "点击占位后 composer 没有获得焦点",
  112. });
  113. expect((await getV4TimelineScrollState()).scrollTop).toBe(scrollBefore.scrollTop);
  114. await setV4ComposerText("E2E_CEM_ORIGINAL 改了但取消");
  115. const editRequestsBefore = await countUpstreamRequestsContaining("改了但取消");
  116. await clickV4ComposerEditCancel();
  117. await waitForV4ComposerEditClosed("取消后没有退出编辑态");
  118. await assertDraftRestored();
  119. expect((await getV4UserRowEditHint(rowId)).hint).toBe(false);
  120. expect(await countUpstreamRequestsContaining("改了但取消")).toBe(editRequestsBefore);
  121. });
  122. it("D27/D28/C07:编辑态载入原配置 → 新增附件后发送 → 请求携带新附件与编辑态模型 → 退出后恢复原草稿、保留模型与 mode → 下一条沿用", async () => {
  123. await prepareV4ConversationE2E();
  124. await ensureUpstreamModelForE2E(UPSTREAM_SECONDARY_MODEL);
  125. await startNewV4Draft();
  126. await sendV4Prompt("E2E_CEM_ORIGINAL D27 原始问题");
  127. await waitForV4TimelineContaining("CEM_ORIGINAL_REPLY", 45000);
  128. await waitForV4Pane((s) => !s.canStop, "D27 首轮没有收口", 45000);
  129. // 原消息以模型 A、原 mode、Plan 关闭提交;之后草稿工具栏切到模型 B、另一个 mode 并打开 Plan。
  130. const submittedConfig = await getV4ModelConfig();
  131. const submittedMode = await getV4ConfigProjection();
  132. expect(submittedConfig.model).not.toBe(UPSTREAM_SECONDARY_MODEL);
  133. expect(submittedMode.planEnabled).toBe(false);
  134. await writeDraftWithAttachment();
  135. await selectUpstreamModelById(UPSTREAM_SECONDARY_MODEL, {
  136. includePlainModelFallback: false,
  137. providerId: UPSTREAM_PROVIDER_ID,
  138. });
  139. const draftMode = submittedMode.mode === "guarded" ? "build" : "guarded";
  140. await switchV4Mode(draftMode);
  141. await switchV4Mode("plan");
  142. const configBefore = await getV4ModelConfig();
  143. const draftModeBefore = await getV4ConfigProjection();
  144. expect(configBefore.model).toBe(UPSTREAM_SECONDARY_MODEL);
  145. expect(draftModeBefore).toMatchObject({ mode: draftMode, planEnabled: true });
  146. await openLatestV4UserQueryEditInComposer();
  147. // D28:编辑态载入该消息提交时的配置(模型 A),与排队消息编辑同一规则。
  148. await browser.waitUntil(
  149. async () => (await getV4ModelConfig()).model === submittedConfig.model,
  150. { timeout: 15000, timeoutMsg: "编辑态没有载入原消息提交时的模型" },
  151. );
  152. // mode 与 Plan 同样载入原提交值,不沿用草稿的 guarded/build 与 Plan。
  153. expect(await getV4ConfigProjection()).toMatchObject({
  154. mode: submittedMode.mode,
  155. planEnabled: false,
  156. });
  157. await setV4ComposerText("E2E_CEM_EDITED 编辑后的问题");
  158. // D27:编辑态新增附件与普通 composer 走同一上传事务。
  159. await pasteV4ComposerFileAttachment(ADDED_FILE, {
  160. base64: Buffer.from(ADDED_FILE_CONTENT).toString("base64"),
  161. mimeType: "text/plain",
  162. });
  163. await browser.waitUntil(
  164. async () => {
  165. const attachments = await getV4ComposerAttachments();
  166. return attachments.length === 1 && attachments[0]?.uploadStatus === "ready";
  167. },
  168. { timeout: 30000, timeoutMsg: "编辑态新增附件没有上传完成" },
  169. );
  170. // D28:编辑态保持载入的模型 A 提交;草稿是模型 B,重跑用 A 才说明提交的是编辑态配置。
  171. expect((await getV4ModelConfig()).model).toBe(submittedConfig.model);
  172. await clickV4Send();
  173. await waitForUpstreamRequest(
  174. { includes: ["E2E_CEM_EDITED", ADDED_FILE_CONTENT] },
  175. "编辑态提交的重跑请求没有携带新附件",
  176. 45000,
  177. );
  178. const [rerun] = await getUpstreamRequestEvidence({ includes: ["E2E_CEM_EDITED"] });
  179. expect(rerun?.requestJson).toMatchObject({ model: submittedConfig.model });
  180. await waitForV4TimelineContaining("CEM_EDITED_REPLY", 45000);
  181. const snapshot = await waitForV4Pane(
  182. (s) => !s.canStop && s.timelineText.includes("CEM_EDITED_REPLY"),
  183. "编辑重跑没有完成",
  184. 45000,
  185. );
  186. expect(snapshot.timelineText).not.toContain("D27 原始问题");
  187. // C07/D28:提交成功后退出编辑态,恢复原草稿正文与附件;工具栏保留提交的模型 A 与原 mode,
  188. // Plan 回到草稿值(开)。三项都与草稿不同,能区分「保留」与「恢复」。
  189. await waitForV4ComposerEditClosed("editUserQuery 成功后没有退出编辑态", 30000);
  190. await assertDraftRestored();
  191. expect(await getV4ModelConfig()).toMatchObject({
  192. provider: submittedConfig.provider,
  193. model: submittedConfig.model,
  194. });
  195. expect(submittedConfig.model).not.toBe(configBefore.model);
  196. expect(await getV4ConfigProjection()).toMatchObject({
  197. mode: submittedMode.mode,
  198. planEnabled: true,
  199. });
  200. // 下一条消息沿用保留的模型,不悄悄回到草稿里的模型 B。
  201. await setV4ComposerText("E2E_CEM_NEXT 编辑后的下一条");
  202. await clickV4Send();
  203. await waitForUpstreamRequest(
  204. { includes: ["E2E_CEM_NEXT"] },
  205. "编辑提交后的下一条消息没有发出",
  206. 45000,
  207. );
  208. const [next] = await getUpstreamRequestEvidence({ includes: ["E2E_CEM_NEXT"] });
  209. expect(next?.requestJson).toMatchObject({ model: submittedConfig.model });
  210. await waitForV4TimelineContaining("CEM_NEXT_REPLY", 45000);
  211. });
  212. it("D23:编辑中队列续跑使原消息不再是最后一条 → 失效态发送作为普通新消息", async () => {
  213. await prepareV4ConversationE2E();
  214. await startNewV4Draft();
  215. await sendV4Prompt("E2E_CEM_SLOW_D22 慢慢回答");
  216. await waitForV4TimelineContaining("CEM_D22_STREAMING", 45000);
  217. await waitForV4Pane((s) => s.canStop, "D23 首轮没有进入 running", 45000);
  218. await sendV4Prompt("E2E_CEM_QUEUED_D22 排队的问题");
  219. await waitForV4QueueCount(1, 30000);
  220. await setV4ComposerText(DRAFT_TEXT);
  221. const rowId = await openLatestV4UserQueryEditInComposer();
  222. await waitForV4ComposerText("E2E_CEM_SLOW_D22 慢慢回答", "编辑态没有载入 running 中的原消息");
  223. // 当前轮结束后队列续跑,产生新的 real-user 输入,原 row canEdit=false。
  224. await releaseControlledReplay("cem-slow-d22");
  225. await waitForV4TimelineContaining("CEM_D22_QUEUED_REPLY", 45000);
  226. const invalid = await waitForV4ComposerEditState(
  227. (state) =>
  228. state?.kind === "userQuery" &&
  229. state.targetId === rowId &&
  230. state.state === "invalid" &&
  231. state.invalidReason === "notLatest",
  232. "原消息不再是最后一条后,编辑态没有进入失效态",
  233. 30000,
  234. );
  235. expect(invalid?.hasCancel).toBe(true);
  236. // 失效态发送走普通 sendText:原消息与新消息都保留。
  237. await setV4ComposerText("E2E_CEM_AS_NEW 作为新消息发送");
  238. await clickV4Send();
  239. await waitForV4TimelineContaining("CEM_AS_NEW_REPLY", 45000);
  240. const snapshot = await waitForV4Pane(
  241. (s) => !s.canStop && s.timelineText.includes("CEM_AS_NEW_REPLY"),
  242. "失效态发送的新消息没有完成",
  243. 45000,
  244. );
  245. expect(snapshot.timelineText).toContain("E2E_CEM_SLOW_D22 慢慢回答");
  246. expect(snapshot.timelineText).toContain("E2E_CEM_QUEUED_D22 排队的问题");
  247. expect(snapshot.timelineText).toContain("E2E_CEM_AS_NEW 作为新消息发送");
  248. await waitForV4ComposerEditClosed("失效态普通发送成功后没有退出编辑态", 30000);
  249. await waitForV4ComposerText(DRAFT_TEXT, "失效态普通发送成功后没有恢复原草稿", 15000);
  250. });
  251. it("D25:切换编辑目标:未改动直接切换(先释放旧锁),已改动先确认;暂存的始终是原草稿", async () => {
  252. await prepareV4ConversationE2E();
  253. await startNewV4Draft();
  254. await sendV4Prompt("E2E_CEM_SLOW_D24 慢慢回答");
  255. await waitForV4TimelineContaining("CEM_D24_STREAMING", 45000);
  256. await waitForV4Pane((s) => s.canStop, "D25 首轮没有进入 running", 45000);
  257. await sendV4Prompt("E2E_CEM_QUEUED_D24 排队的问题");
  258. await waitForV4QueueCount(1, 30000);
  259. const [queued] = await getV4QueueItems();
  260. if (!queued) throw new Error("D25 队列项不存在");
  261. await setV4ComposerText(DRAFT_TEXT);
  262. const rowId = await openLatestV4UserQueryEditInComposer();
  263. // 未改动:直接切到排队消息,不弹确认。
  264. await openV4QueueItemEditInComposer(queued.queueItemId);
  265. expect(await hasV4ComposerEditSwitchDialog()).toBe(false);
  266. await waitForV4ComposerText("E2E_CEM_QUEUED_D24 排队的问题", "切换后没有载入排队消息");
  267. // 编辑态在 lock ACK 后进入;editLock 随后经投影到达。
  268. await waitForV4QueueItemEditLock(
  269. queued.queueItemId,
  270. (state) => state.locked && state.owner === "self",
  271. "切换后排队消息没有显示本端编辑锁",
  272. );
  273. // 已改动:切回 latest query 先确认,确认后释放排队消息的锁。
  274. await setV4ComposerText("E2E_CEM_QUEUED_D24 改动未保存");
  275. await clickTestIdByDom(testId(TID_V4_EDIT, rowId), {
  276. timeout: 15000,
  277. timeoutMsg: "latest query 编辑入口不可点击",
  278. });
  279. await browser.waitUntil(() => hasV4ComposerEditSwitchDialog(), {
  280. timeout: 15000,
  281. timeoutMsg: "已改动时切换编辑目标没有弹出确认",
  282. });
  283. expect(await getV4ComposerEditState()).toMatchObject({
  284. kind: "queueItem",
  285. targetId: queued.queueItemId,
  286. });
  287. await confirmV4ComposerEditSwitch();
  288. await waitForV4ComposerEditState(
  289. (state) => state?.kind === "userQuery" && state.targetId === rowId,
  290. "确认后没有切换到 latest query 编辑态",
  291. );
  292. await waitForV4ComposerText("E2E_CEM_SLOW_D24 慢慢回答", "切换后没有载入 latest query");
  293. await browser.waitUntil(
  294. async () => !(await getV4QueueItemEditLock(queued.queueItemId)).locked,
  295. { timeout: 30000, timeoutMsg: "切换目标后排队消息的锁没有释放" },
  296. );
  297. expect((await getV4QueueItems())[0]?.text).toContain("排队的问题");
  298. await clickV4ComposerEditCancel();
  299. await waitForV4ComposerEditClosed("取消后没有退出编辑态");
  300. await waitForV4ComposerText(DRAFT_TEXT, "多次切换目标后没有恢复进入编辑态前的原草稿", 15000);
  301. });
  302. it("D24:切换 task 后切回保持编辑态;刷新后丢弃编辑态、只恢复原草稿", async () => {
  303. await prepareV4ConversationE2E();
  304. await startNewV4Draft();
  305. await sendV4Prompt("E2E_CEM_ORIGINAL D24 原始问题");
  306. await waitForV4TimelineContaining("CEM_ORIGINAL_REPLY", 45000);
  307. const settled = await waitForV4Pane(
  308. (s) => !s.canStop && s.sessionId !== null && s.sessionId !== "draft",
  309. "D24 首轮没有收口",
  310. 45000,
  311. );
  312. const sessionId = settled.sessionId!;
  313. await setV4ComposerText(DRAFT_TEXT);
  314. const rowId = await openLatestV4UserQueryEditInComposer();
  315. await setV4ComposerText("E2E_CEM_ORIGINAL D24 编辑中的内容");
  316. await startNewV4Draft();
  317. expect(await getV4ComposerEditState()).toBeNull();
  318. await selectV4TaskById(sessionId);
  319. await waitForV4ComposerEditState(
  320. (state) =>
  321. state?.kind === "userQuery" && state.targetId === rowId && state.state === "editing",
  322. "切回 task 后编辑态没有保持",
  323. );
  324. await waitForV4ComposerText("E2E_CEM_ORIGINAL D24 编辑中的内容", "切回 task 后编辑内容丢失");
  325. await browser.execute(() => window.location.reload());
  326. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 30000);
  327. await selectV4TaskById(sessionId);
  328. await waitForV4Pane((s) => s.sessionId === sessionId, "刷新后没有恢复 session", 30000);
  329. await browser.waitUntil(async () => (await getV4ComposerText()) !== null, {
  330. timeout: 15000,
  331. timeoutMsg: "刷新后 composer 没有挂载",
  332. });
  333. // 编辑内容不写持久化草稿:刷新后只剩进入编辑态前的原草稿。
  334. await waitForV4ComposerText(DRAFT_TEXT, "刷新后没有恢复进入编辑态前的原草稿", 15000);
  335. expect(await getV4ComposerEditState()).toBeNull();
  336. expect((await getV4UserRowEditHint(rowId)).hint).toBe(false);
  337. });
  338. });
  339. async function writeDraftWithAttachment() {
  340. await setV4ComposerText(DRAFT_TEXT);
  341. await pasteV4ComposerFileAttachment(DRAFT_FILE, {
  342. base64: Buffer.from("E2E_CEM_DRAFT_ATTACHMENT").toString("base64"),
  343. mimeType: "text/plain",
  344. });
  345. await browser.waitUntil(
  346. async () => {
  347. const attachments = await getV4ComposerAttachments();
  348. return attachments.length === 1 && attachments[0]?.uploadStatus === "ready";
  349. },
  350. { timeout: 30000, timeoutMsg: "原草稿附件没有上传完成" },
  351. );
  352. }
  353. async function assertDraftRestored() {
  354. await waitForV4ComposerText(DRAFT_TEXT, "退出编辑态后没有恢复原草稿正文", 15000);
  355. const attachments = await getV4ComposerAttachments();
  356. expect(attachments).toHaveLength(1);
  357. expect(attachments[0]?.text).toContain(DRAFT_FILE);
  358. expect(attachments[0]?.uploadStatus).toBe("ready");
  359. }