conversation-session-output-limit-continue-ui.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { TID_V4_TIMELINE } from "@zcode/shared/test-ids";
  2. import { clearAppData } from "#e2e/helpers/desktop-app";
  3. import {
  4. countUpstreamRequests,
  5. getUpstreamRequestEvidence,
  6. } from "#e2e/helpers/conversation-session-network";
  7. import {
  8. prepareV4ConversationE2E,
  9. sendV4Prompt,
  10. waitForV4AssistantMessageContaining,
  11. getV4CompactMarkers,
  12. waitForV4ConversationState,
  13. } from "#e2e/helpers/v4-conversation";
  14. const CASE_MARKER = "E2E_OUTPUT_LIMIT_CONTINUE_UI";
  15. const PARTIAL_ONE_ANCHOR = "即使底层已经";
  16. const PARTIAL_TWO_ANCHOR = "例如这里再让句子停在";
  17. const FINAL_ANCHOR = "一个尚未完成的位置";
  18. const MAIN_REQUEST_QUERY = {
  19. excludes: ["Generate a concise title", "CRITICAL: Respond with TEXT ONLY"],
  20. includes: [CASE_MARKER],
  21. };
  22. const COMPACT_REQUEST_QUERY = {
  23. includes: ["CRITICAL: Respond with TEXT ONLY"],
  24. };
  25. describe("Output-limit Continue 句中截断 UI manual review", () => {
  26. after(async () => {
  27. await browser.electron.restoreAllMocks();
  28. await clearAppData();
  29. });
  30. it("OTC08: 同一自然段两次句中截断后只渲染一个累计 assistant", async function () {
  31. const holdMs = readManualReviewHoldMs();
  32. this.timeout(120000 + holdMs);
  33. await prepareV4ConversationE2E();
  34. await sendV4Prompt(
  35. `${CASE_MARKER}: 请按 provider fixture 返回一段连续正文,并在句中连续两次命中 output-limit。`,
  36. );
  37. await waitForV4AssistantMessageContaining(FINAL_ANCHOR);
  38. await waitForV4ConversationState(
  39. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  40. "OTC08 Continue 恢复链完成后没有回到 idle",
  41. 90000,
  42. );
  43. await browser.waitUntil(async () => (await countUpstreamRequests(MAIN_REQUEST_QUERY)) === 3, {
  44. timeout: 30000,
  45. timeoutMsg: "OTC08 没有严格完成 p1(length) → p2(length) → p3(stop) 三次主请求",
  46. });
  47. const evidence = await getUpstreamRequestEvidence(MAIN_REQUEST_QUERY);
  48. expect(evidence.map((item) => item.fixtureId)).toEqual([
  49. "output-limit-continue-ui-partial-p1",
  50. "output-limit-continue-ui-partial-p2",
  51. "output-limit-continue-ui-final-p3",
  52. ]);
  53. expect(evidence.every((item) => item.status === "complete")).toBe(true);
  54. expect(evidence[0]?.responseTextPreview).toContain('"stop_reason":"max_tokens"');
  55. expect(evidence[1]?.responseTextPreview).toContain('"stop_reason":"max_tokens"');
  56. expect(evidence[2]?.responseTextPreview).toContain('"stop_reason":"end_turn"');
  57. expect(await countUpstreamRequests(COMPACT_REQUEST_QUERY)).toBe(0);
  58. expect(await getV4CompactMarkers()).toEqual([]);
  59. const rendered = await readContinueTurnSnapshot();
  60. expect(rendered.turnId).toBeTruthy();
  61. expect(rendered.assistantRows).toHaveLength(1);
  62. expect(rendered.assistantRows[0]?.inHistory).toBe(false);
  63. expect(rendered.assistantRows[0]?.rowId).toBeTruthy();
  64. const renderedText = rendered.assistantRows[0]?.text ?? "";
  65. const partialOneIndex = renderedText.indexOf(PARTIAL_ONE_ANCHOR);
  66. const partialTwoIndex = renderedText.indexOf(PARTIAL_TWO_ANCHOR);
  67. const finalIndex = renderedText.indexOf(FINAL_ANCHOR);
  68. expect(partialOneIndex).toBeGreaterThanOrEqual(0);
  69. expect(partialTwoIndex).toBeGreaterThan(partialOneIndex);
  70. expect(finalIndex).toBeGreaterThan(partialTwoIndex);
  71. expect(renderedText).toContain(`${PARTIAL_ONE_ANCHOR}发起了新的 provider 请求`);
  72. expect(renderedText).toContain(`${PARTIAL_TWO_ANCHOR}${FINAL_ANCHOR}`);
  73. if (holdMs > 0) {
  74. await browser.pause(holdMs);
  75. }
  76. });
  77. });
  78. interface ContinueAssistantRowSnapshot {
  79. inHistory: boolean;
  80. rowId: string | null;
  81. text: string;
  82. }
  83. interface ContinueTurnSnapshot {
  84. assistantRows: ContinueAssistantRowSnapshot[];
  85. turnId: string | null;
  86. }
  87. function readContinueTurnSnapshot(): Promise<ContinueTurnSnapshot> {
  88. return browser.execute(
  89. (timelineTestId, userMarker) => {
  90. const timeline = document.querySelector<HTMLElement>(`[data-testid="${timelineTestId}"]`);
  91. const userRow = Array.from(
  92. timeline?.querySelectorAll<HTMLElement>("[data-row-id]") ?? [],
  93. ).find(
  94. (row) =>
  95. row.classList.contains("group/user-row") &&
  96. (row.innerText || row.textContent || "").includes(userMarker),
  97. );
  98. const turn = userRow?.closest<HTMLElement>("[data-turn-id]") ?? null;
  99. const assistantRows = Array.from(turn?.querySelectorAll<HTMLElement>("[data-row-id]") ?? [])
  100. .filter((row) => row.classList.contains("group/assistant-row"))
  101. .map((row) => ({
  102. inHistory: Boolean(row.closest("[data-slot='collapsible-content']")),
  103. rowId: row.getAttribute("data-row-id"),
  104. text: (row.innerText || row.textContent || "").replace(/\u00a0/g, " "),
  105. }));
  106. return {
  107. assistantRows,
  108. turnId: turn?.getAttribute("data-turn-id") ?? null,
  109. };
  110. },
  111. TID_V4_TIMELINE,
  112. CASE_MARKER,
  113. );
  114. }
  115. function readManualReviewHoldMs(): number {
  116. const parsed = Number(process.env.ZCODE_E2E_MANUAL_REVIEW_HOLD_MS ?? "0");
  117. return Number.isFinite(parsed) && parsed > 0 ? Math.floor(parsed) : 0;
  118. }