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

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