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

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