conversation-session-automation-cron-relative.test.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import {
  2. TID_AUTOMATION_CARD,
  3. TID_AUTOMATION_FORM_TITLE,
  4. TID_AUTOMATION_SCHEDULE_PREVIEW,
  5. TID_AUTOMATIONS_LIST,
  6. TID_CONFIRM_DIALOG_CONFIRM,
  7. TID_CRON_CREATE_CARD,
  8. TID_CRON_CREATE_OPEN,
  9. } from "@zcode/shared/test-ids";
  10. import {
  11. clearAppData,
  12. clickTestIdByDom,
  13. pinE2ELocale,
  14. waitForTestIdByDom,
  15. } from "#e2e/helpers/desktop-app";
  16. import { expandAssistantHistoriesWithContent } from "#e2e/helpers/conversation-session-tool-diagnostics";
  17. import {
  18. approveV4Permission,
  19. prepareV4ConversationE2E,
  20. sendV4Prompt,
  21. waitForV4Pane,
  22. waitForV4TimelineContaining,
  23. } from "#e2e/helpers/v4-conversation";
  24. // 与 fixtures/upstream/conversation-session/conversation-session-automation-cron-relative.json 对齐:
  25. // prompt 里的 marker 触发回放的相对时间 CronCreate(delayMinutes=5、无 cron、一次性)。
  26. const MARKER = "E2E_AUTOMATION_CRON_RELATIVE";
  27. const AUTOMATION_TITLE = "E2E_CRON_RELATIVE_ONESHOT";
  28. /**
  29. * 会话内相对时间 CronCreate(delayMinutes)→ host 换算调度落库 → 详情/列表一次性语义 端到端。
  30. *
  31. * 模型响应走 upstream 回放;CronCreate 真实执行,host 用真实时钟把 delayMinutes 换算成
  32. * 未来一次性调度。不等待任务真实触发(delayMinutes=5 远大于 case 时长),因此是确定性的。
  33. */
  34. describe("会话内相对时间一次性定时任务 E2E", () => {
  35. after(async () => {
  36. await browser.electron.restoreAllMocks();
  37. await clearAppData();
  38. });
  39. it("delayMinutes 创建一次性任务 → 详情与列表显示自定义而非重复频率", async function () {
  40. this.timeout(150000);
  41. await prepareV4ConversationE2E({ resetDraftBeforeProvider: true });
  42. // 首帧跟随宿主语言,mac runner 上是英文;seedSettings 对已启动的 renderer 无效,须用 pinE2ELocale。
  43. await pinE2ELocale("zh-CN");
  44. // 1) 触发回放的相对时间 CronCreate 并批准副作用权限,等待工具真实执行落库。
  45. await sendV4Prompt(`${MARKER} remind me to drink water in 5 minutes`);
  46. await waitForV4TimelineContaining(MARKER);
  47. await approveV4Permission();
  48. await waitForV4Pane(
  49. (snapshot) =>
  50. snapshot.sessionId !== null && snapshot.sessionId !== "draft" && !snapshot.canStop,
  51. "会话内相对时间 CronCreate 没有完成",
  52. 90000,
  53. );
  54. // 2) 聊天里出现 CronCreate 卡片并显示任务标题。
  55. await expandAssistantHistoriesWithContent();
  56. await waitForTestIdByDom(TID_CRON_CREATE_CARD, {
  57. timeout: 15000,
  58. timeoutMsg: "会话内没有渲染相对时间 CronCreate 定时任务卡片",
  59. });
  60. await browser.waitUntil(
  61. async () => (await readTestIdText(TID_CRON_CREATE_CARD)).includes(AUTOMATION_TITLE),
  62. {
  63. timeout: 10000,
  64. timeoutMsg: `CronCreate 卡片没有显示定时任务标题:${AUTOMATION_TITLE}`,
  65. },
  66. );
  67. // 3) 直达详情:相对时间任务由 host 生成兼容 cron + minute scheduleRule,
  68. // 只读摘要必须显示“自定义”,不得回显成可视化重复频率。
  69. await clickTestIdByDom(TID_CRON_CREATE_OPEN, {
  70. timeoutMsg: "CronCreate 卡片没有可点击的「去到定时任务」按钮",
  71. });
  72. await browser.waitUntil(
  73. async () => (await readTestIdValue(TID_AUTOMATION_FORM_TITLE)) === AUTOMATION_TITLE,
  74. {
  75. timeout: 15000,
  76. timeoutMsg: `任务详情没有回显会话创建的标题:${AUTOMATION_TITLE}`,
  77. },
  78. );
  79. await waitForTestIdByDom(TID_AUTOMATION_SCHEDULE_PREVIEW, {
  80. timeout: 10000,
  81. timeoutMsg: "相对时间任务详情没有显示只读调度摘要",
  82. });
  83. expect(await readTestIdText(TID_AUTOMATION_SCHEDULE_PREVIEW)).toBe("自定义");
  84. // 4) 回归:一次性任务详情未做任何修改,返回必须直接回列表,不弹“未保存”确认框。
  85. // 详情页回退已迁到 Settings header breadcrumb;它挂载在编辑页之外,侧栏 test id
  86. // 在当前承载面并不存在,必须点击真实 breadcrumb 返回列表。
  87. await clickAutomationsBreadcrumbBack();
  88. expect(await hasVisibleTestId(TID_CONFIRM_DIALOG_CONFIRM)).toBe(false);
  89. await waitForTestIdByDom(TID_AUTOMATIONS_LIST, {
  90. timeout: 15000,
  91. timeoutMsg: "无修改返回没有直接回到任务列表(疑似误弹未保存确认框)",
  92. });
  93. // 5) 回归:一次性任务列表卡片显示“自定义 · 下次运行 …”,不得误显“每 5 分钟”重复。
  94. const badgeText = await readScheduleBadgeText();
  95. expect(badgeText).toContain("自定义");
  96. expect(badgeText).toContain("下次运行");
  97. expect(badgeText).not.toContain("每 ");
  98. const cardText = await readTestIdText(TID_AUTOMATION_CARD);
  99. // Card 是定时与立即运行的累计次数;一次性任务的 maxRuns 不能作为分母,避免误导用户。
  100. expect(cardText).toContain("已运行 0 次");
  101. expect(cardText).not.toContain("0/1");
  102. });
  103. });
  104. /** Settings header 的 Automations breadcrumb 是任务详情返回列表的真实入口。 */
  105. async function clickAutomationsBreadcrumbBack(): Promise<void> {
  106. const buttons = await browser.$$("nav[aria-label] button");
  107. for (const button of buttons) {
  108. if (/Automations|自动化/u.test(await button.getText())) {
  109. await button.click();
  110. return;
  111. }
  112. }
  113. throw new Error("任务详情没有返回 Automations 列表的 breadcrumb 入口");
  114. }
  115. /** 读取某个 test id 元素的可见文本。 */
  116. async function readTestIdText(currentTestId: string): Promise<string> {
  117. return browser.execute((tid) => {
  118. const el = Array.from(document.querySelectorAll<HTMLElement>("[data-testid]")).find(
  119. (item) => item.dataset.testid === tid,
  120. );
  121. return el?.textContent?.trim() ?? "";
  122. }, currentTestId);
  123. }
  124. /** 读取 input/textarea 的当前值,避免用 textContent 误判受控表单。 */
  125. async function readTestIdValue(currentTestId: string): Promise<string> {
  126. return browser.execute((tid) => {
  127. const element = document.querySelector<HTMLInputElement | HTMLTextAreaElement>(
  128. `[data-testid="${tid}"]`,
  129. );
  130. return element?.value ?? "";
  131. }, currentTestId);
  132. }
  133. /** 某个 test id 元素当前是否挂载(用于断言确认弹窗没有出现)。 */
  134. async function hasVisibleTestId(currentTestId: string): Promise<boolean> {
  135. return browser.execute(
  136. (tid) => Boolean(document.querySelector(`[data-testid="${tid}"]`)),
  137. currentTestId,
  138. );
  139. }
  140. /** 读取列表卡片的调度 badge 文本(AutomationScheduleBadge 内的 schedule-text)。 */
  141. async function readScheduleBadgeText(): Promise<string> {
  142. return browser.execute(
  143. () =>
  144. document
  145. .querySelector<HTMLElement>('[data-testid="automation-schedule-text"]')
  146. ?.textContent?.trim() ?? "",
  147. );
  148. }