conversation-session-empty-completion-retry.test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { clearAppData } from "../helpers/desktop-app.js";
  2. import {
  3. assertUpstreamRequestCapture,
  4. waitForUpstreamNetworkCapture,
  5. } from "../helpers/upstream-capture.js";
  6. import {
  7. countUpstreamRequests,
  8. getUpstreamRequestEvidence,
  9. } from "../helpers/conversation-session-network.js";
  10. import { UPSTREAM_MODEL } from "../helpers/upstream-provider.js";
  11. import {
  12. getV4Messages,
  13. prepareV4ConversationE2E,
  14. sendV4Prompt,
  15. waitForV4AssistantMessageContaining,
  16. waitForV4ConversationState,
  17. } from "../helpers/v4-conversation.js";
  18. const EMPTY_COMPLETION_MARKER = "E2E_EMPTY_COMPLETION_RETRY_STREAM";
  19. const EMPTY_COMPLETION_REPLY = "EMPTY_COMPLETION_RETRY_OK";
  20. describe("generic empty completion retry E2E", () => {
  21. after(async () => {
  22. await browser.electron.restoreAllMocks();
  23. await clearAppData();
  24. });
  25. it("I73: 首个 finish=other 空流不泄漏,并且只重试一次后显示成功回复", async function () {
  26. this.timeout(180000);
  27. await prepareV4ConversationE2E();
  28. const prompt = `${EMPTY_COMPLETION_MARKER}: Reply with exactly "${EMPTY_COMPLETION_REPLY}" and no other text.`;
  29. await sendV4Prompt(prompt);
  30. await waitForV4AssistantMessageContaining(EMPTY_COMPLETION_REPLY);
  31. await waitForV4ConversationState(
  32. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  33. "空 completion 重试成功后没有回到 idle",
  34. 90000,
  35. );
  36. await browser.waitUntil(
  37. async () =>
  38. (await countUpstreamRequests({ includes: [EMPTY_COMPLETION_MARKER] })) === 2,
  39. {
  40. timeout: 30000,
  41. timeoutMsg: "空 completion 没有严格产生 first-empty → second-success 两次请求",
  42. },
  43. );
  44. const request = await waitForUpstreamNetworkCapture(EMPTY_COMPLETION_MARKER);
  45. assertUpstreamRequestCapture(request, {
  46. expectedText: prompt,
  47. model: UPSTREAM_MODEL,
  48. });
  49. const evidence = await getUpstreamRequestEvidence({
  50. includes: [EMPTY_COMPLETION_MARKER],
  51. });
  52. expect(evidence).toHaveLength(2);
  53. expect(evidence.map((item) => item.fixtureId)).toEqual([
  54. "empty-completion-retry-stream-first-empty",
  55. "empty-completion-retry-stream-second-success",
  56. ]);
  57. expect(evidence.every((item) => item.status === "complete")).toBe(true);
  58. expect(evidence.every((item) => item.responseBodyBytes > 0)).toBe(true);
  59. expect(evidence[0]?.responseTextPreview).toContain('"stop_reason":"other"');
  60. expect(evidence[0]?.responseTextPreview).toContain('"output_tokens":0');
  61. expect(evidence[1]?.responseTextPreview).toContain(EMPTY_COMPLETION_REPLY);
  62. const assistantMessages = await getV4Messages("assistant");
  63. expect(assistantMessages).toHaveLength(1);
  64. expect(assistantMessages[0]?.text).toContain(EMPTY_COMPLETION_REPLY);
  65. });
  66. });