conversation-session-create-telemetry.test.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import {
  2. clearAppData,
  3. DEFAULT_WORKSPACE,
  4. waitForDefaultWorkspaceReady,
  5. } from "#e2e/helpers/desktop-app";
  6. import { startDraftFromProject } from "#e2e/helpers/project-sidebar";
  7. import {
  8. prepareV4ConversationE2E,
  9. selectV4TaskById,
  10. sendV4Prompt,
  11. startNewV4Draft,
  12. waitForV4ComposerSelectionReady,
  13. waitForV4Pane,
  14. waitForV4TimelineContaining,
  15. } from "#e2e/helpers/v4-conversation";
  16. describe("TEL11 session_create", () => {
  17. after(async () => {
  18. await browser.electron.restoreAllMocks();
  19. await clearAppData();
  20. });
  21. it("预热零上报、首发一次、续发和重开不重复", async function () {
  22. this.timeout(360_000);
  23. await waitForDefaultWorkspaceReady(90_000);
  24. await prepareV4ConversationE2E();
  25. // 原因:准备 helper 可复用启动空态;只有真实点击项目新建才产生 project 来源。
  26. await startDraftFromProject(DEFAULT_WORKSPACE);
  27. const telemetryFetch = await browser.electron.mock("net", "fetch");
  28. await telemetryFetch.mockResolvedValue({ ok: true, status: 204 });
  29. const reports = async (elementName = "session_create") => {
  30. await telemetryFetch.update();
  31. return telemetryFetch.mock.calls.flatMap((call) => {
  32. if (!String(call[0]).includes("/api/v1/event/report")) return [];
  33. const init = call[1] as { body?: unknown } | undefined;
  34. if (typeof init?.body !== "string") return [];
  35. const body = JSON.parse(init.body) as {
  36. element_name?: string;
  37. event_id?: string;
  38. talk_id?: string;
  39. message_id?: string;
  40. event_extra_detail?: Record<string, string>;
  41. };
  42. return body.element_name === elementName ? [body] : [];
  43. });
  44. };
  45. const waitForMatchingCompletion = async (created: {
  46. talk_id?: string;
  47. message_id?: string;
  48. }) => {
  49. expect(created.message_id).toBeTruthy();
  50. try {
  51. await browser.waitUntil(
  52. async () =>
  53. (await reports("message_completion")).some(
  54. (completed) =>
  55. completed.talk_id === created.talk_id &&
  56. completed.message_id === created.message_id,
  57. ),
  58. { timeout: 15_000, timeoutMsg: "创建/完成事件关联失败" },
  59. );
  60. } catch (error) {
  61. throw new Error(
  62. `创建/完成事件关联失败: ${JSON.stringify({ created, completions: (await reports("message_completion")).map(({ talk_id, message_id }) => ({ talk_id, message_id })) })}`,
  63. { cause: error },
  64. );
  65. }
  66. };
  67. await waitForV4ComposerSelectionReady();
  68. expect(await reports()).toEqual([]);
  69. await sendV4Prompt('E2E_SESSION_CREATE_TELEMETRY_FIRST: Reply exactly "SESSION_CREATE_OK".');
  70. await waitForV4TimelineContaining("SESSION_CREATE_OK", 60_000);
  71. const pane = await waitForV4Pane(
  72. (state) => !!state.sessionId && state.sessionId !== "draft" && !state.canStop,
  73. "首发未创建正式 session",
  74. 60_000,
  75. );
  76. await browser.waitUntil(async () => (await reports()).length === 1, {
  77. timeout: 15_000,
  78. timeoutMsg: "首发没有 session_create",
  79. });
  80. const sessionId = pane.sessionId!;
  81. await browser.waitUntil(
  82. async () =>
  83. (await reports("message_completion")).some((event) => event.talk_id === sessionId),
  84. { timeout: 15_000 },
  85. );
  86. const firstMessageId = (await reports("message_completion")).find(
  87. (event) => event.talk_id === sessionId,
  88. )!.message_id;
  89. expect(firstMessageId).toBeTruthy();
  90. expect((await reports())[0]?.message_id).toBe(firstMessageId);
  91. expect((await reports())[0]?.event_id).toMatch(
  92. /^[0-9a-f]{8}-[0-9a-f]{4}-8[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/,
  93. );
  94. expect(await reports()).toEqual([
  95. expect.objectContaining({
  96. talk_id: sessionId,
  97. event_extra_detail: {
  98. create_source: "project",
  99. client_kind: "desktop",
  100. workspace_kind: "local",
  101. remote_kind: "",
  102. },
  103. }),
  104. ]);
  105. await sendV4Prompt('E2E_SESSION_CREATE_TELEMETRY_NEXT: Reply exactly "SESSION_NEXT_OK".');
  106. await waitForV4TimelineContaining("SESSION_NEXT_OK", 60_000);
  107. await waitForV4Pane((state) => !state.canStop, "后续轮次没有结束", 60_000);
  108. await startNewV4Draft();
  109. await waitForV4ComposerSelectionReady();
  110. await selectV4TaskById(sessionId);
  111. await waitForV4Pane((state) => state.sessionId === sessionId, "历史重开失败", 30_000);
  112. expect(await reports()).toHaveLength(1);
  113. // 使用真实 Group 页新建入口,不能直接修改 store 伪造 create_source。
  114. const tabs = await $$('[role="tab"]');
  115. let groupTab;
  116. for (const tab of tabs) {
  117. if (["Group", "分组"].includes((await tab.getText()).trim())) groupTab = tab;
  118. }
  119. if (!groupTab) throw new Error("没有 Group 视图入口");
  120. await groupTab.click();
  121. await startNewV4Draft();
  122. await waitForV4ComposerSelectionReady();
  123. expect(await reports()).toHaveLength(1);
  124. await sendV4Prompt('E2E_SESSION_CREATE_TELEMETRY_GROUP: Reply exactly "SESSION_GROUP_OK".');
  125. await waitForV4TimelineContaining("SESSION_GROUP_OK", 60_000);
  126. await waitForV4Pane((state) => !state.canStop, "Group 首发未结束", 60_000);
  127. await browser.waitUntil(async () => (await reports()).length === 2, { timeout: 15_000 });
  128. expect((await reports())[1]?.event_extra_detail?.create_source).toBe("group");
  129. // 先验证该轮完成事件再刷新,避免把刷新时的异步上报中断误判为 ID 不一致。
  130. await waitForMatchingCompletion((await reports())[1]!);
  131. // 重载后的默认空态直接发首条消息;没有新建点击来设置来源。
  132. await startNewV4Draft();
  133. await browser.refresh();
  134. // Group 视图没有 Project 侧栏项;刷新后以实际草稿 pane 就绪为准。
  135. await waitForV4Pane(
  136. (state) => !state.sessionId || state.sessionId === "draft",
  137. "刷新后草稿未就绪",
  138. 90_000,
  139. );
  140. await waitForV4ComposerSelectionReady();
  141. await sendV4Prompt('E2E_SESSION_CREATE_TELEMETRY_DIRECT: Reply exactly "SESSION_DIRECT_OK".');
  142. await waitForV4TimelineContaining("SESSION_DIRECT_OK", 60_000);
  143. await browser.waitUntil(async () => (await reports()).length === 3, { timeout: 15_000 });
  144. expect((await reports())[2]?.event_extra_detail?.create_source).toBe("session");
  145. expect(new Set((await reports()).map((event) => event.event_id)).size).toBe(3);
  146. for (const created of await reports()) await waitForMatchingCompletion(created);
  147. });
  148. });