conversation-session-ttft-local-logs.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { readFile, readdir } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import {
  4. prepareV4ConversationE2E,
  5. sendV4Prompt,
  6. setV4ComposerText,
  7. waitForV4AssistantMessageContaining,
  8. waitForV4ConversationState,
  9. } from "#e2e/helpers/v4-conversation";
  10. async function readLogs(directory: string): Promise<string> {
  11. const files = await readdir(directory).catch(() => [] as string[]);
  12. return (
  13. await Promise.all(
  14. files
  15. .filter((file) => /\.(log|jsonl)$/.test(file))
  16. .map((file) => readFile(join(directory, file), "utf8")),
  17. )
  18. ).join("\n");
  19. }
  20. describe("TTFT03 生产本地日志", () => {
  21. it("云端关闭时 Enter 发送仍记录真实接收、投影、DOM 和绘制估计", async function () {
  22. this.timeout(180000);
  23. await prepareV4ConversationE2E();
  24. // 冷启动首轮可能重建订阅;先等待真实轮次结束,正向样本只验证连续在线内容。
  25. await sendV4Prompt("E2E_TTFT_LOCAL_WARMUP: Reply exactly ready.");
  26. await waitForV4AssistantMessageContaining("ttft-local-warmup-ready");
  27. await waitForV4ConversationState((state) => state.state === "idle", "准备轮未结束");
  28. const appLogDir = await browser.electron.execute(() => process.env.ZCODE_E2E_RUNTIME_LOG_DIR!);
  29. const cliLogDir = process.env.ZCODE_LOG_DIR!;
  30. expect(appLogDir).toBeTruthy();
  31. expect(cliLogDir).toBeTruthy();
  32. await browser.electron.execute((electron) => {
  33. const win = electron.BrowserWindow.getAllWindows().find((window) => !window.isDestroyed());
  34. electron.app.focus({ steal: true });
  35. win?.show();
  36. win?.focus();
  37. win?.webContents.focus();
  38. });
  39. const before = (await readLogs(appLogDir)).length;
  40. await setV4ComposerText("E2E_TTFT_LOCAL_LOGS: Reply exactly ttft-local-reply-private.");
  41. await browser.keys("Enter");
  42. await waitForV4AssistantMessageContaining("ttft-local-reply-private");
  43. await waitForV4ConversationState((state) => state.state === "idle", "本地 TTFT 轮未结束");
  44. let entries: Array<Record<string, unknown>> = [];
  45. await browser.waitUntil(
  46. async () => {
  47. entries = (await readLogs(appLogDir))
  48. .slice(before)
  49. .split("\n")
  50. .flatMap((line) => {
  51. const offset = line.indexOf("[ttft] ");
  52. if (offset < 0) return [];
  53. try {
  54. return [JSON.parse(line.slice(offset + "[ttft] ".length))];
  55. } catch {
  56. return [];
  57. }
  58. });
  59. const start = entries.find((entry) => entry.event === "ttft.start");
  60. const current = start
  61. ? entries.filter((entry) => entry.observationId === start.observationId)
  62. : [];
  63. const excluded = current.find((entry) => entry.event === "ttft.excluded");
  64. if (excluded) throw new Error(`TTFT 样本已排除:${String(excluded.outcome)}`);
  65. return current.some((entry) => entry.event === "ttft.text.paint_estimate");
  66. },
  67. { timeout: 15000, timeoutMsg: "本地文件缺少首正文绘制观测" },
  68. );
  69. const start = entries.find((entry) => entry.event === "ttft.start")!;
  70. const observation = entries.filter((entry) => entry.observationId === start.observationId);
  71. for (const event of [
  72. "ttft.first_output",
  73. "ttft.first_text",
  74. "ttft.text.projected",
  75. "ttft.text.committed",
  76. "ttft.text.paint_estimate",
  77. ]) {
  78. expect(observation.filter((entry) => entry.event === event)).toHaveLength(1);
  79. }
  80. expect(start.localOnly).toBe(true);
  81. const received = observation.find((entry) => entry.event === "ttft.first_text")!;
  82. const painted = observation.find((entry) => entry.event === "ttft.text.paint_estimate")!;
  83. expect(Number(painted.durationMs)).toBeGreaterThanOrEqual(Number(received.durationMs));
  84. expect(painted.visible).toBe(true);
  85. expect(
  86. observation.some((entry) => entry.phase === "host.runtime" && entry.status === "completed"),
  87. ).toBe(true);
  88. expect(JSON.stringify(observation)).not.toContain("ttft-local-reply-private");
  89. const requestId = received.requestId as string;
  90. expect(requestId).toBeTruthy();
  91. await browser.waitUntil(
  92. async () => {
  93. const lines = (await readLogs(cliLogDir))
  94. .split("\n")
  95. .filter((line) => line.includes(requestId));
  96. return ["fetch_started", "response_headers", "first_content"].every((event) =>
  97. lines.some((line) => line.includes(`ttft.model.${event}`)),
  98. );
  99. },
  100. {
  101. timeout: 15000,
  102. timeoutMsg: "CLI 文件缺少同一输入的首有效内容日志",
  103. },
  104. );
  105. });
  106. });