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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. seedSettings,
  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. // 该 case 的证据包含本地化后的调度摘要;显式固定中文 locale,避免 WDIO
  42. // 继承运行机系统语言后把同一语义渲染成 Custom,导致证明在不同开发机上漂移。
  43. await seedSettings({ locale: "zh-CN", localePreference: "zh-CN" });
  44. await prepareV4ConversationE2E({ resetDraftBeforeProvider: true });
  45. // 1) 触发回放的相对时间 CronCreate 并批准副作用权限,等待工具真实执行落库。
  46. await sendV4Prompt(`${MARKER} remind me to drink water in 5 minutes`);
  47. await waitForV4TimelineContaining(MARKER);
  48. await approveV4Permission();
  49. await waitForV4Pane(
  50. (snapshot) =>
  51. snapshot.sessionId !== null && snapshot.sessionId !== "draft" && !snapshot.canStop,
  52. "会话内相对时间 CronCreate 没有完成",
  53. 90000,
  54. );
  55. // 2) 聊天里出现 CronCreate 卡片并显示任务标题。
  56. await expandAssistantHistoriesWithContent();
  57. await waitForTestIdByDom(TID_CRON_CREATE_CARD, {
  58. timeout: 15000,
  59. timeoutMsg: "会话内没有渲染相对时间 CronCreate 定时任务卡片",
  60. });
  61. await browser.waitUntil(
  62. async () => (await readTestIdText(TID_CRON_CREATE_CARD)).includes(AUTOMATION_TITLE),
  63. {
  64. timeout: 10000,
  65. timeoutMsg: `CronCreate 卡片没有显示定时任务标题:${AUTOMATION_TITLE}`,
  66. },
  67. );
  68. // 3) 直达详情:相对时间任务由 host 生成兼容 cron + minute scheduleRule,
  69. // 只读摘要必须显示“自定义”,不得回显成可视化重复频率。
  70. await clickTestIdByDom(TID_CRON_CREATE_OPEN, {
  71. timeoutMsg: "CronCreate 卡片没有可点击的「去到定时任务」按钮",
  72. });
  73. await browser.waitUntil(
  74. async () => (await readTestIdValue(TID_AUTOMATION_FORM_TITLE)) === AUTOMATION_TITLE,
  75. {
  76. timeout: 15000,
  77. timeoutMsg: `任务详情没有回显会话创建的标题:${AUTOMATION_TITLE}`,
  78. },
  79. );
  80. await waitForTestIdByDom(TID_AUTOMATION_SCHEDULE_PREVIEW, {
  81. timeout: 10000,
  82. timeoutMsg: "相对时间任务详情没有显示只读调度摘要",
  83. });
  84. expect(await readTestIdText(TID_AUTOMATION_SCHEDULE_PREVIEW)).toBe("自定义");
  85. // 4) 回归:一次性任务详情未做任何修改,返回必须直接回列表,不弹“未保存”确认框。
  86. // 详情页回退已迁到 Settings header breadcrumb;它挂载在编辑页之外,侧栏 test id
  87. // 在当前承载面并不存在,必须点击真实 breadcrumb 返回列表。
  88. await clickAutomationsBreadcrumbBack();
  89. expect(await hasVisibleTestId(TID_CONFIRM_DIALOG_CONFIRM)).toBe(false);
  90. await waitForTestIdByDom(TID_AUTOMATIONS_LIST, {
  91. timeout: 15000,
  92. timeoutMsg: "无修改返回没有直接回到任务列表(疑似误弹未保存确认框)",
  93. });
  94. // 5) 回归:一次性任务列表卡片显示“自定义 · 下次运行 …”,不得误显“每 5 分钟”重复。
  95. const badgeText = await readScheduleBadgeText();
  96. expect(badgeText).toContain("自定义");
  97. expect(badgeText).toContain("下次运行");
  98. expect(badgeText).not.toContain("每 ");
  99. const cardText = await readTestIdText(TID_AUTOMATION_CARD);
  100. // Card 是定时与立即运行的累计次数;一次性任务的 maxRuns 不能作为分母,避免误导用户。
  101. expect(cardText).toContain("已运行 0 次");
  102. expect(cardText).not.toContain("0/1");
  103. });
  104. });
  105. /** Settings header 的 Automations breadcrumb 是任务详情返回列表的真实入口。 */
  106. async function clickAutomationsBreadcrumbBack(): Promise<void> {
  107. const buttons = await browser.$$("nav[aria-label] button");
  108. for (const button of buttons) {
  109. if (/Automations|自动化/u.test(await button.getText())) {
  110. await button.click();
  111. return;
  112. }
  113. }
  114. throw new Error("任务详情没有返回 Automations 列表的 breadcrumb 入口");
  115. }
  116. /** 读取某个 test id 元素的可见文本。 */
  117. async function readTestIdText(currentTestId: string): Promise<string> {
  118. return browser.execute((tid) => {
  119. const el = Array.from(document.querySelectorAll<HTMLElement>("[data-testid]")).find(
  120. (item) => item.dataset.testid === tid,
  121. );
  122. return el?.textContent?.trim() ?? "";
  123. }, currentTestId);
  124. }
  125. /** 读取 input/textarea 的当前值,避免用 textContent 误判受控表单。 */
  126. async function readTestIdValue(currentTestId: string): Promise<string> {
  127. return browser.execute((tid) => {
  128. const element = document.querySelector<HTMLInputElement | HTMLTextAreaElement>(
  129. `[data-testid="${tid}"]`,
  130. );
  131. return element?.value ?? "";
  132. }, currentTestId);
  133. }
  134. /** 某个 test id 元素当前是否挂载(用于断言确认弹窗没有出现)。 */
  135. async function hasVisibleTestId(currentTestId: string): Promise<boolean> {
  136. return browser.execute(
  137. (tid) => Boolean(document.querySelector(`[data-testid="${tid}"]`)),
  138. currentTestId,
  139. );
  140. }
  141. /** 读取列表卡片的调度 badge 文本(AutomationScheduleBadge 内的 schedule-text)。 */
  142. async function readScheduleBadgeText(): Promise<string> {
  143. return browser.execute(
  144. () =>
  145. document
  146. .querySelector<HTMLElement>('[data-testid="automation-schedule-text"]')
  147. ?.textContent?.trim() ?? "",
  148. );
  149. }