conversation-session-attachment-resume.test.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { mkdir, rm, writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import {
  4. clearAppData,
  5. DEFAULT_WORKSPACE,
  6. waitForDefaultWorkspaceReady,
  7. } from "#e2e/helpers/desktop-app";
  8. import {
  9. restartIntoWorkspacePreservingProfile,
  10. seedTurboAgentStartupModel,
  11. } from "#e2e/helpers/model-provider-restart";
  12. import {
  13. ensureUpstreamProviderForE2E,
  14. UPSTREAM_MODEL,
  15. UPSTREAM_PROVIDER_ID,
  16. } from "#e2e/helpers/upstream-provider";
  17. import { waitForUpstreamNetworkCapture } from "#e2e/helpers/upstream-capture";
  18. import { reloadElectronSessionSafely } from "#e2e/helpers/e2e-electron-reload";
  19. import {
  20. resolveSeededReplayBaseUrl,
  21. seedCustomAnthropicMessagesProvider,
  22. } from "#e2e/helpers/custom-openai-provider-store";
  23. import {
  24. clickV4Send,
  25. getV4ComposerAttachments,
  26. pickV4ComposerFiles,
  27. prepareV4ConversationE2E,
  28. selectV4TaskById,
  29. sendV4Prompt,
  30. setV4ComposerText,
  31. waitForV4Pane,
  32. waitForV4RowAttachments,
  33. waitForV4TimelineContaining,
  34. } from "#e2e/helpers/v4-conversation";
  35. const ROOT = join(DEFAULT_WORKSPACE, ".zcode-e2e", "conversation-session-attachment-resume");
  36. const FIRST = "E2E_ATTACHMENT_RESUME_FIRST";
  37. const HOT = "E2E_ATTACHMENT_RESUME_HOT";
  38. const COLD = "E2E_ATTACHMENT_RESUME_COLD";
  39. describe("AR01 attachment cold resume", () => {
  40. afterEach(async function () {
  41. if (this.currentTest?.state !== "failed") return;
  42. const artifacts = process.env.ZCODE_E2E_ARTIFACT_DIR ?? join(process.cwd(), ".e2e-artifacts");
  43. await browser.saveScreenshot(join(artifacts, "attachment-resume-failure.png"));
  44. console.info(
  45. "AR01 failure UI",
  46. await browser.execute(() => document.body.innerText.slice(-5000)),
  47. );
  48. });
  49. after(async () => {
  50. await browser.electron.restoreAllMocks();
  51. await rm(ROOT, { force: true, recursive: true });
  52. await clearAppData();
  53. });
  54. it("重启后保留首轮正文顺序与三行路径说明", async function () {
  55. this.timeout(240_000);
  56. await waitForDefaultWorkspaceReady();
  57. // 此用例不测模型选择;通过现有 fixture 设置启动模型,避免依赖已移除的旧 toolbar。
  58. await ensureUpstreamProviderForE2E({ skipToolbarSelection: true });
  59. // 通用 fixture 的 240 token 窗口用于 compact 场景;本用例需要保留未经压缩的原始消息。
  60. await seedCustomAnthropicMessagesProvider({
  61. baseURL: await resolveSeededReplayBaseUrl(),
  62. modelId: UPSTREAM_MODEL,
  63. providerId: UPSTREAM_PROVIDER_ID,
  64. providerName: "Attachment Resume Fixture",
  65. });
  66. await seedTurboAgentStartupModel(UPSTREAM_PROVIDER_ID, UPSTREAM_MODEL);
  67. await restartIntoWorkspacePreservingProfile();
  68. await prepareV4ConversationE2E({ skipProvider: true });
  69. await mkdir(ROOT, { recursive: true });
  70. const path = join(ROOT, "resume-bundle.zip");
  71. await writeFile(path, "binary fixture");
  72. await pickV4ComposerFiles([path]);
  73. await browser.waitUntil(
  74. async () => {
  75. const attachments = await getV4ComposerAttachments();
  76. return attachments.length === 1 && attachments[0]?.uploadStatus === "ready";
  77. },
  78. { timeout: 30_000, timeoutMsg: "AR01 文件附件未 ready" },
  79. );
  80. await setV4ComposerText(FIRST);
  81. await clickV4Send();
  82. await waitForV4TimelineContaining("ATTACHMENT_RESUME_FIRST_OK");
  83. const first = await waitForV4Pane(
  84. (s) => Boolean(s.sessionId && s.sessionId !== "draft") && !s.canStop,
  85. "AR01 首轮未完成",
  86. );
  87. expect(await waitForV4RowAttachments()).toContain("resume-bundle.zip");
  88. await sendV4Prompt(HOT);
  89. await waitForV4TimelineContaining("ATTACHMENT_RESUME_HOT_OK");
  90. await waitForV4Pane((s) => !s.canStop, "AR01 热态续聊未完成");
  91. const hot = firstUserContent((await waitForUpstreamNetworkCapture(HOT)).requestJson);
  92. expect(hot[0]).toMatchObject({ type: "text", text: FIRST });
  93. expect(hot[1]).toMatchObject({
  94. type: "text",
  95. text: expect.stringContaining("The file was sent by local path because"),
  96. });
  97. expect(String(hot[1]?.text).split("\n")).toHaveLength(3);
  98. // 原说明必须来自持久化事实;删除原文件后冷恢复也不应重新解析或丢失文案。
  99. await rm(path);
  100. // 任务已绑定自己的目录;按 task 身份恢复,不等待初始化阶段的默认项目路径。
  101. await reloadElectronSessionSafely(browser);
  102. await selectV4TaskById(first.sessionId!);
  103. await waitForV4TimelineContaining(HOT);
  104. await sendV4Prompt(COLD);
  105. await waitForV4TimelineContaining("ATTACHMENT_RESUME_COLD_OK");
  106. const cold = firstUserContent((await waitForUpstreamNetworkCapture(COLD)).requestJson);
  107. expect(cold).toEqual(hot);
  108. });
  109. });
  110. function firstUserContent(value: unknown): Array<Record<string, unknown>> {
  111. const messages = (value as { messages?: Array<{ role?: string; content?: unknown }> }).messages;
  112. const user = messages?.find(
  113. (message) =>
  114. message.role === "user" &&
  115. Array.isArray(message.content) &&
  116. message.content.some((block) => block?.text === FIRST),
  117. );
  118. if (!user || !Array.isArray(user.content))
  119. throw new Error("AR01 provider capture 缺少首轮 user content");
  120. // SDK 会把此前的 user reminder 合并进同一 message;只截取本次要验证的正文及紧随其后的附件。
  121. const promptIndex = user.content.findIndex((block) => block?.text === FIRST);
  122. return user.content.slice(promptIndex, promptIndex + 2);
  123. }