conversation-session-first-submit-visibility.test.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { DatabaseSync } from "node:sqlite";
  2. import { join } from "node:path";
  3. import { writeFile } from "node:fs/promises";
  4. import { TID_V4_SESSION_PANE, testId } from "@zcode/shared/test-ids";
  5. import {
  6. prepareV4ConversationE2E,
  7. sendV4Prompt,
  8. startNewV4Draft,
  9. V4_MAIN_PANE_ID,
  10. waitForV4UserMessageContaining,
  11. waitForV4AssistantMessageContaining,
  12. waitForV4ConversationState,
  13. } from "#e2e/helpers/v4-conversation";
  14. describe("草稿首发列表与详情同步", () => {
  15. for (const scope of ["global", "project"] as const) {
  16. it(`FSV05:${scope} 首次详情帧已有同一个选中的侧栏任务`, async function () {
  17. this.timeout(150_000);
  18. if (scope === "global") {
  19. await prepareV4ConversationE2E();
  20. // 通用准备固定在 fixture 项目;FSV05 的全局分支必须实际点击全局新建。
  21. await startNewV4Draft();
  22. }
  23. // 展开所属项目;折叠偏好不是本用例要改变的创建语义。
  24. const project = $('[data-project-name="ZCodeProject"]');
  25. let expectedProjectId: string | null = null;
  26. if (scope === "project") {
  27. await project.waitForDisplayed();
  28. expectedProjectId = await project.getAttribute("data-project-id");
  29. const trigger = project.$('[data-testid="virtual-project-toggle"]');
  30. if ((await trigger.getAttribute("aria-expanded")) === "false") await trigger.click();
  31. await trigger.moveTo();
  32. await project.$('[data-testid="virtual-project-create-task"]').click();
  33. await browser.waitUntil(
  34. async () =>
  35. (await $(
  36. `[data-testid="${testId(TID_V4_SESSION_PANE, V4_MAIN_PANE_ID)}"]`,
  37. ).getAttribute("data-session-id")) === "draft",
  38. );
  39. }
  40. const paneId = testId(TID_V4_SESSION_PANE, V4_MAIN_PANE_ID);
  41. await browser.execute((id) => {
  42. const before = new Set(
  43. [...document.querySelectorAll('[data-testid^="task-item-"]')].map((row) =>
  44. row.getAttribute("data-testid"),
  45. ),
  46. );
  47. const state = {
  48. done: false,
  49. frames: [] as Array<{
  50. sessionId: string;
  51. row: boolean;
  52. selected: boolean;
  53. newRows: number;
  54. }>,
  55. };
  56. (window as typeof window & { __firstSubmitEvidence?: typeof state }).__firstSubmitEvidence =
  57. state;
  58. const sample = () => {
  59. const sessionId =
  60. document.querySelector(`[data-testid="${id}"]`)?.getAttribute("data-session-id") ??
  61. "draft";
  62. const row = document.querySelector<HTMLElement>(`[data-testid="task-item-${sessionId}"]`);
  63. state.frames.push({
  64. sessionId,
  65. row: Boolean(row && row.getBoundingClientRect().height > 0),
  66. newRows: [...document.querySelectorAll('[data-testid^="task-item-"]')].filter(
  67. (item) => !before.has(item.getAttribute("data-testid")),
  68. ).length,
  69. selected: Boolean(row?.classList.contains("bg-selected")),
  70. });
  71. if (sessionId !== "draft" && sessionId) state.done = true;
  72. else requestAnimationFrame(sample);
  73. };
  74. requestAnimationFrame(sample);
  75. }, paneId);
  76. const marker = `E2E_FIRST_SUBMIT_VISIBILITY_${scope}`;
  77. await sendV4Prompt(`${marker}: Reply with exactly "deepseek-e2e-ok".`);
  78. await browser.waitUntil(
  79. () =>
  80. browser.execute(() =>
  81. Boolean(
  82. (window as typeof window & { __firstSubmitEvidence?: { done: boolean } })
  83. .__firstSubmitEvidence?.done,
  84. ),
  85. ),
  86. { timeout: 30_000 },
  87. );
  88. const evidence = await browser.execute(
  89. () =>
  90. (
  91. window as typeof window & {
  92. __firstSubmitEvidence?: {
  93. frames: Array<{
  94. sessionId: string;
  95. row: boolean;
  96. selected: boolean;
  97. newRows: number;
  98. }>;
  99. };
  100. }
  101. ).__firstSubmitEvidence!,
  102. );
  103. await writeFile(
  104. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, `FSV05-${scope}-frames.json`),
  105. JSON.stringify(evidence, null, 2),
  106. );
  107. const firstDetail = evidence.frames.find(
  108. (frame) => frame.sessionId !== "draft" && frame.sessionId,
  109. );
  110. expect(firstDetail).toMatchObject({ row: true, selected: true });
  111. expect(
  112. evidence.frames
  113. .filter((frame) => frame.sessionId === "draft")
  114. .every((frame) => frame.newRows === 0),
  115. ).toBe(true);
  116. const db = new DatabaseSync(
  117. join(process.env.ZCODE_E2E_HOME_DIR!, ".zcode/v2/tasks-index.sqlite"),
  118. { readOnly: true },
  119. );
  120. try {
  121. const fact = db
  122. .prepare("SELECT project_id,cli_created_at,cli_title FROM tasks WHERE task_id=?")
  123. .get(firstDetail!.sessionId);
  124. expect(fact?.project_id).toBe(expectedProjectId);
  125. expect(Number(fact?.cli_created_at)).toBeGreaterThan(0);
  126. expect(
  127. String(fact?.cli_title).includes(marker) || fact?.cli_title === "First submit visibility",
  128. ).toBe(true);
  129. } finally {
  130. db.close();
  131. }
  132. await browser.saveScreenshot(
  133. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, `FSV05-${scope}-first-detail.png`),
  134. );
  135. await waitForV4UserMessageContaining(marker);
  136. await waitForV4AssistantMessageContaining("deepseek-e2e-ok");
  137. await waitForV4ConversationState((state) => state.state === "idle", "首发未完成", 60_000);
  138. });
  139. }
  140. });