conversation-session-draft-create-singleflight.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { join } from "node:path";
  2. import { writeFile } from "node:fs/promises";
  3. import { TID_V4_COMPOSER_SEND, TID_V4_SESSION_PANE, testId } from "@zcode/shared/test-ids";
  4. import {
  5. getV4PaneSnapshot,
  6. prepareV4ConversationE2E,
  7. sendV4Prompt,
  8. setV4ComposerText,
  9. startNewV4Draft,
  10. V4_MAIN_PANE_ID,
  11. waitForV4Pane,
  12. waitForV4TimelineContaining,
  13. waitForV4ComposerText,
  14. } from "#e2e/helpers/v4-conversation";
  15. interface CreateGate {
  16. commands: Array<{ type: string; sessionId: string | null }>;
  17. release(): void;
  18. restore(): void;
  19. }
  20. declare global {
  21. interface Window {
  22. __draftCreateGate?: CreateGate;
  23. }
  24. }
  25. describe("A01 草稿首发与预热共享一次创建", () => {
  26. afterEach(async () => {
  27. await browser.execute(() => window.__draftCreateGate?.restore());
  28. });
  29. it("预热创建在途点击发送,最终只提升同一会话", async function () {
  30. this.timeout(150_000);
  31. await prepareV4ConversationE2E();
  32. // 先进入已完成任务,再从真实新任务入口进入新草稿,避免复用前置草稿的预热。
  33. await sendV4Prompt("E2E_DRAFT_SINGLEFLIGHT_SETUP reply briefly");
  34. await waitForV4TimelineContaining("draft-singleflight-ok", 45_000);
  35. await waitForV4Pane((pane) => !pane.canStop && pane.sessionId !== "draft", "前置任务未完成");
  36. await browser.execute(() => {
  37. const original = MessagePort.prototype.postMessage;
  38. const held: Array<() => void> = [];
  39. let holding = true;
  40. const gate: CreateGate = {
  41. commands: [],
  42. release() {
  43. holding = false;
  44. held.splice(0).forEach((send) => send());
  45. },
  46. restore() {
  47. gate.release();
  48. MessagePort.prototype.postMessage = original;
  49. delete window.__draftCreateGate;
  50. },
  51. };
  52. // 只暂停真实 RPC 的上行时序,不伪造 createSession/模型结果,也不增加产品测试分支。
  53. MessagePort.prototype.postMessage = function (
  54. this: MessagePort,
  55. ...args: [unknown, (Transferable[] | StructuredSerializeOptions)?]
  56. ) {
  57. const forward = () => Reflect.apply(original, this, args);
  58. const data: unknown = args[0];
  59. const bytes =
  60. data instanceof ArrayBuffer
  61. ? new Uint8Array(data)
  62. : ArrayBuffer.isView(data)
  63. ? new Uint8Array(data.buffer, data.byteOffset, data.byteLength)
  64. : null;
  65. const wire = bytes ? new TextDecoder().decode(bytes) : "";
  66. const type = wire.match(/"type":"(createSession|sendText)"/)?.[1];
  67. if (wire.includes("sendConversationCommandV4") && type) {
  68. // 信封目标可以是 null;不能跳过它而误读 payload 中预分配的新会话 ID。
  69. const sessionId = wire.match(/"sessionId":(?:null|"([^"]+)")/)?.[1] ?? null;
  70. gate.commands.push({ type, sessionId });
  71. if (type === "createSession" && holding) {
  72. held.push(forward);
  73. return;
  74. }
  75. }
  76. forward();
  77. };
  78. window.__draftCreateGate = gate;
  79. });
  80. await startNewV4Draft();
  81. await browser.waitUntil(
  82. () => browser.execute(() => Boolean(window.__draftCreateGate?.commands.length)),
  83. { timeout: 15_000, timeoutMsg: "未截获草稿预热 createSession" },
  84. );
  85. await sendV4Prompt("E2E_DRAFT_SINGLEFLIGHT_FIRST reply briefly");
  86. const pending = await browser.execute(
  87. (paneId, sendId) => ({
  88. commands: window.__draftCreateGate!.commands,
  89. sending: document.querySelector<HTMLButtonElement>(
  90. `[data-testid="${paneId}"] [data-testid="${sendId}"]`,
  91. )?.disabled,
  92. }),
  93. testId(TID_V4_SESSION_PANE, V4_MAIN_PANE_ID),
  94. TID_V4_COMPOSER_SEND,
  95. );
  96. expect(pending.sending).toBe(true);
  97. expect(pending.commands).toEqual([{ type: "createSession", sessionId: null }]);
  98. expect((await getV4PaneSnapshot()).sessionId).toBe("draft");
  99. // PD08:首发在途继续输入,接纳交接必须保留下一条正文。
  100. await setV4ComposerText("E2E_PD08_NEXT_INPUT");
  101. await browser.execute(() => window.__draftCreateGate!.release());
  102. await waitForV4TimelineContaining("draft-singleflight-ok", 45_000);
  103. const pane = await waitForV4Pane(
  104. (value) => value.sessionId !== "draft" && value.sessionId !== null && !value.canStop,
  105. "首发没有进入已完成详情页",
  106. );
  107. const commands = await browser.execute(() => window.__draftCreateGate!.commands);
  108. expect(commands).toEqual([
  109. { type: "createSession", sessionId: null },
  110. { type: "sendText", sessionId: pane.sessionId },
  111. ]);
  112. expect(pane.timelineText).toContain("E2E_DRAFT_SINGLEFLIGHT_FIRST");
  113. await waitForV4ComposerText("E2E_PD08_NEXT_INPUT", "首发交接丢失下一条正文");
  114. await writeFile(
  115. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, "draft-create-commands.json"),
  116. JSON.stringify({ pending, commands, pane }, null, 2),
  117. );
  118. await browser.saveScreenshot(join(process.env.ZCODE_E2E_ARTIFACT_DIR!, "draft-first-send.png"));
  119. });
  120. });