automationFormat.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. canVisualizeCronInAutomationEditor,
  4. buildAutomationEditCronExpr,
  5. buildCronExpr,
  6. describeCron,
  7. describeCronBuilder,
  8. formatAutomationCardNextRun,
  9. formatGmtOffset,
  10. hasAutomationFailureState,
  11. hasAutomationRetryQueueState,
  12. parseCronToBuilder,
  13. resolveAutomationStatusKind,
  14. } from "../src/settings/automationFormat.js";
  15. import { describeAutomationCardSchedule } from "../src/settings/automationCardSchedule.js";
  16. describe("automationFormat", () => {
  17. const scheduleIntl = {
  18. formatMessage: ({ id }: { id: string }, values?: Record<string, string>) =>
  19. id === "automations.schedule.customMinutes"
  20. ? `Every ${values?.["interval"]} minutes`
  21. : id,
  22. };
  23. it("分钟步进 cron 展示真实频率而不是默认时间", () => {
  24. expect(describeCron("*/20 * * * *", scheduleIntl)).toBe("Every 20 minutes");
  25. const builder = parseCronToBuilder("*/20 * * * *");
  26. expect(builder).toMatchObject({
  27. frequency: "custom",
  28. customInterval: 20,
  29. customUnit: "minute",
  30. });
  31. expect(buildCronExpr(builder)).toBe("*/20 * * * *");
  32. expect(describeCronBuilder(builder, scheduleIntl)).toBe("Every 20 minutes");
  33. });
  34. it("未知 cron 展示为自定义而不是原表达式", () => {
  35. expect(
  36. describeCron("1,15 2-8 * * *", {
  37. formatMessage: ({ id }) =>
  38. id === "automations.frequency.custom" ? "Custom" : id,
  39. }),
  40. ).toBe("Custom");
  41. });
  42. it("编辑器只接管能够无损回显的 cron", () => {
  43. expect(canVisualizeCronInAutomationEditor("0 9 * * *")).toBe(true);
  44. expect(canVisualizeCronInAutomationEditor("20 18 13 */1 *")).toBe(false);
  45. expect(canVisualizeCronInAutomationEditor("0 8 27 7 *")).toBe(false);
  46. });
  47. it("旧任务的每小时 cron 可以稳定回填编辑状态", () => {
  48. expect(parseCronToBuilder("40 * * * *")).toMatchObject({
  49. frequency: "hourly",
  50. minute: 40,
  51. });
  52. });
  53. it("编辑会话内创建的任务时原样保留 cron", () => {
  54. const builder = parseCronToBuilder("*/10 * * * *");
  55. expect(
  56. buildAutomationEditCronExpr(builder, {
  57. cronExpr: "*/10 * * * *",
  58. targetTaskId: "sess-from-chat",
  59. }),
  60. ).toBe("*/10 * * * *");
  61. });
  62. it("工作日频率与 1-5 cron 可以双向转换", () => {
  63. const cronExpr = buildCronExpr({
  64. frequency: "weekdays",
  65. hour: 9,
  66. minute: 30,
  67. weekdays: [1, 2, 3, 4, 5],
  68. dayOfMonth: 1,
  69. rawExpr: "",
  70. customInterval: 1,
  71. customUnit: "daily",
  72. customWeekdays: [1],
  73. customMonthDays: [1],
  74. customMonth: 1,
  75. customMonthlyMode: "date",
  76. });
  77. expect(cronExpr).toBe("30 9 * * 1-5");
  78. expect(parseCronToBuilder(cronExpr)).toMatchObject({
  79. frequency: "weekdays",
  80. hour: 9,
  81. minute: 30,
  82. });
  83. });
  84. it("自定义小时和天间隔可以双向转换", () => {
  85. const base = {
  86. frequency: "custom" as const,
  87. hour: 9,
  88. minute: 30,
  89. weekdays: [1],
  90. dayOfMonth: 1,
  91. rawExpr: "",
  92. customWeekdays: [1],
  93. customMonthDays: [1],
  94. customMonth: 1,
  95. customMonthlyMode: "date" as const,
  96. };
  97. expect(buildCronExpr({ ...base, customInterval: 3, customUnit: "hourly" })).toBe(
  98. "30 */3 * * *",
  99. );
  100. expect(parseCronToBuilder("30 */3 * * *")).toMatchObject({
  101. frequency: "custom",
  102. customInterval: 3,
  103. customUnit: "hourly",
  104. });
  105. expect(buildCronExpr({ ...base, customInterval: 2, customUnit: "daily" })).toBe("30 9 */2 * *");
  106. });
  107. it("每 31 小时使用合法的每小时候选 cron,真实间隔留给 scheduleRule", () => {
  108. expect(
  109. buildCronExpr({
  110. frequency: "custom",
  111. hour: 9,
  112. minute: 49,
  113. weekdays: [1],
  114. dayOfMonth: 1,
  115. rawExpr: "",
  116. customInterval: 31,
  117. customUnit: "hourly",
  118. customWeekdays: [1],
  119. customMonthDays: [1],
  120. customMonth: 1,
  121. customMonthlyMode: "date",
  122. }),
  123. ).toBe("49 * * * *");
  124. });
  125. it("每 61/200 分钟和 32/200 天使用合法兼容 cron,真实间隔留给 scheduleRule", () => {
  126. const base = {
  127. frequency: "custom" as const,
  128. hour: 9,
  129. minute: 30,
  130. weekdays: [1],
  131. dayOfMonth: 1,
  132. rawExpr: "",
  133. customWeekdays: [1],
  134. customMonthDays: [1],
  135. customMonth: 1,
  136. customMonthlyMode: "date" as const,
  137. };
  138. for (const interval of [61, 200]) {
  139. expect(buildCronExpr({ ...base, customInterval: interval, customUnit: "minute" })).toBe(
  140. "* * * * *",
  141. );
  142. }
  143. for (const interval of [32, 200]) {
  144. expect(buildCronExpr({ ...base, customInterval: interval, customUnit: "daily" })).toBe(
  145. "30 9 * * *",
  146. );
  147. }
  148. });
  149. it("按小时重复只展示分钟偏移,不展示无意义的默认 09:00", () => {
  150. const intl = {
  151. formatMessage: ({ id }: { id: string }, values?: Record<string, string>) =>
  152. id === "automations.schedule.customHourly"
  153. ? `每 ${values?.["interval"]} 小时的第 ${values?.["time"]} 分`
  154. : id,
  155. };
  156. const builder = parseCronToBuilder("0 */2 * * *");
  157. expect(describeCronBuilder(builder, intl)).toBe("每 2 小时的第 00 分");
  158. expect(describeCron("0 */2 * * *", intl)).toBe("每 2 小时的第 00 分");
  159. expect(describeCronBuilder(builder, intl)).not.toContain("09:00");
  160. });
  161. it("GMT 偏移兼容整点、半小时和 UTC", () => {
  162. expect(formatGmtOffset(480)).toBe("GMT+8");
  163. expect(formatGmtOffset(330)).toBe("GMT+5:30");
  164. expect(formatGmtOffset(-210)).toBe("GMT-3:30");
  165. expect(formatGmtOffset(0)).toBe("GMT");
  166. });
  167. it("月度自定义摘要保留用户选择的日期", () => {
  168. const text = describeCronBuilder(
  169. {
  170. frequency: "custom",
  171. hour: 10,
  172. minute: 52,
  173. weekdays: [1],
  174. dayOfMonth: 1,
  175. rawExpr: "",
  176. customInterval: 1,
  177. customUnit: "monthly",
  178. customWeekdays: [1],
  179. customMonthDays: [12, 20],
  180. customMonth: 1,
  181. customMonthlyMode: "date",
  182. },
  183. {
  184. formatMessage: ({ id }, values) =>
  185. id === "automations.schedule.customMonthlyDates"
  186. ? `每 ${values?.["interval"]} 个月的 ${values?.["days"]} 日,${values?.["time"]}`
  187. : id,
  188. },
  189. );
  190. expect(text).toBe("每 1 个月的 12, 20 日,10:52");
  191. });
  192. it("每 29 个月使用合法的每月候选 cron,真实间隔留给 scheduleRule", () => {
  193. const base = {
  194. frequency: "custom" as const,
  195. hour: 9,
  196. minute: 0,
  197. weekdays: [1],
  198. dayOfMonth: 1,
  199. rawExpr: "",
  200. customInterval: 29,
  201. customUnit: "monthly" as const,
  202. customWeekdays: [1],
  203. customMonthDays: [1, 15, 16],
  204. customMonth: 1,
  205. };
  206. expect(buildCronExpr({ ...base, customMonthlyMode: "date" as const })).toBe(
  207. "0 9 1,15,16 * *",
  208. );
  209. expect(buildCronExpr({ ...base, customMonthlyMode: "weekday" as const })).toBe(
  210. "0 9 * * 1#1",
  211. );
  212. });
  213. it("卡片优先按 scheduleRule 展示每 30 个月,不被兼容 cron 降级成每月", () => {
  214. const text = describeAutomationCardSchedule(
  215. {
  216. cronExpr: "0 9 1 * *",
  217. scheduleRule: {
  218. unit: "monthly",
  219. interval: 30,
  220. hour: 9,
  221. minute: 0,
  222. anchorAt: new Date(2026, 6, 27, 9).getTime(),
  223. monthDays: [1],
  224. monthlyMode: "date",
  225. },
  226. },
  227. {
  228. formatMessage: ({ id }, values) =>
  229. id === "automations.schedule.customMonthlyDates"
  230. ? `每 ${values?.["interval"]} 个月的 ${values?.["days"]} 日,${values?.["time"]}`
  231. : id,
  232. },
  233. );
  234. expect(text).toBe("每 30 个月的 1 日,09:00");
  235. });
  236. it("每年自定义组装为 `M H DOM MON *` 并可双向转换", () => {
  237. const cronExpr = buildCronExpr({
  238. frequency: "custom",
  239. hour: 9,
  240. minute: 30,
  241. weekdays: [1],
  242. dayOfMonth: 1,
  243. rawExpr: "",
  244. customInterval: 1,
  245. customUnit: "yearly",
  246. customWeekdays: [1],
  247. customMonthDays: [12],
  248. customMonth: 7,
  249. customMonthlyMode: "date",
  250. });
  251. expect(cronExpr).toBe("30 9 12 7 *");
  252. expect(parseCronToBuilder(cronExpr)).toMatchObject({
  253. frequency: "custom",
  254. hour: 9,
  255. minute: 30,
  256. customUnit: "yearly",
  257. customMonth: 7,
  258. customMonthDays: [12],
  259. });
  260. });
  261. it("每年自定义摘要包含用户选择的月份和日期", () => {
  262. const text = describeCronBuilder(
  263. {
  264. frequency: "custom",
  265. hour: 9,
  266. minute: 0,
  267. weekdays: [1],
  268. dayOfMonth: 1,
  269. rawExpr: "",
  270. customInterval: 1,
  271. customUnit: "yearly",
  272. customWeekdays: [1],
  273. customMonthDays: [12],
  274. customMonth: 7,
  275. customMonthlyMode: "date",
  276. },
  277. {
  278. formatMessage: ({ id }, values) =>
  279. id === "automations.schedule.customYearly"
  280. ? `每 ${values?.["interval"]} 年的 ${values?.["month"]} 月 ${values?.["day"]} 日,${values?.["time"]}`
  281. : id,
  282. },
  283. );
  284. expect(text).toBe("每 1 年的 7 月 12 日,09:00");
  285. });
  286. it("固定日期与无法稳定可视化的 cron 统一展示为自定义", () => {
  287. const intl = {
  288. formatMessage: ({ id }: { id: string }) =>
  289. id === "automations.frequency.custom" ? "自定义" : id,
  290. };
  291. expect(
  292. describeAutomationCardSchedule(
  293. { cronExpr: "0 8 27 7 *" },
  294. intl,
  295. ),
  296. ).toBe("自定义");
  297. expect(
  298. describeAutomationCardSchedule({ cronExpr: "20 18 13 */1 *" }, intl),
  299. ).toBe("自定义");
  300. expect(
  301. describeAutomationCardSchedule({ cronExpr: "1,15 2-8 * * *" }, intl),
  302. ).toBe("自定义");
  303. });
  304. it("相对时间一次性任务不显示为“每 N 分钟”重复,而是自定义", () => {
  305. const intl = {
  306. formatMessage: ({ id }: { id: string }, values?: Record<string, unknown>) =>
  307. id === "automations.frequency.custom"
  308. ? "自定义"
  309. : id === "automations.schedule.customMinutes"
  310. ? `每 ${values?.["interval"]} 分钟`
  311. : id,
  312. };
  313. // “4 分钟后提醒”被存成 minute scheduleRule 作兼容展示,一次性任务应显示自定义。
  314. expect(
  315. describeAutomationCardSchedule(
  316. {
  317. cronExpr: "47 14 28 7 *",
  318. scheduleRule: {
  319. unit: "minute",
  320. interval: 4,
  321. hour: 14,
  322. minute: 47,
  323. anchorAt: new Date(2026, 6, 28, 14, 43).getTime(),
  324. },
  325. recurring: false,
  326. maxRuns: 1,
  327. },
  328. intl,
  329. ),
  330. ).toBe("自定义");
  331. // 有限次(recurring=false + maxRuns>1)确实按频率重复,保留“每 N 分钟”。
  332. expect(
  333. describeAutomationCardSchedule(
  334. {
  335. cronExpr: "*/4 * * * *",
  336. scheduleRule: {
  337. unit: "minute",
  338. interval: 4,
  339. hour: 14,
  340. minute: 47,
  341. anchorAt: new Date(2026, 6, 28, 14, 43).getTime(),
  342. },
  343. recurring: false,
  344. maxRuns: 3,
  345. },
  346. intl,
  347. ),
  348. ).toBe("每 4 分钟");
  349. });
  350. it("定时任务状态展示终态优先于 enabled", () => {
  351. expect(
  352. resolveAutomationStatusKind({
  353. lifecycleStatus: "completed",
  354. enabled: false,
  355. }),
  356. ).toBe("completed");
  357. expect(
  358. resolveAutomationStatusKind({
  359. lifecycleStatus: "failed",
  360. enabled: false,
  361. }),
  362. ).toBe("failed");
  363. expect(
  364. resolveAutomationStatusKind({
  365. lifecycleStatus: "active",
  366. enabled: false,
  367. }),
  368. ).toBe("paused");
  369. expect(
  370. resolveAutomationStatusKind({
  371. lifecycleStatus: "active",
  372. enabled: true,
  373. }),
  374. ).toBe("active");
  375. });
  376. it("真实运行或派发错误会让任务卡片进入失败态", () => {
  377. expect(
  378. hasAutomationFailureState({
  379. lifecycleStatus: "active",
  380. lastError: "Provider request failed",
  381. }),
  382. ).toBe(true);
  383. expect(
  384. hasAutomationFailureState({
  385. lifecycleStatus: "active",
  386. dispatchStatus: "failed_to_dispatch",
  387. }),
  388. ).toBe(true);
  389. expect(hasAutomationFailureState({ lifecycleStatus: "active" })).toBe(false);
  390. });
  391. it("只有真实进入退避重试队列时才展示排队标签", () => {
  392. expect(
  393. hasAutomationRetryQueueState({
  394. lifecycleStatus: "active",
  395. dispatchStatus: "failed_to_dispatch",
  396. dispatchAttempts: 2,
  397. retryAt: Date.now() + 1_000,
  398. }),
  399. ).toBe(true);
  400. expect(
  401. hasAutomationRetryQueueState({
  402. lifecycleStatus: "failed",
  403. dispatchStatus: "failed_to_dispatch",
  404. dispatchAttempts: 3,
  405. }),
  406. ).toBe(false);
  407. });
  408. it("卡片下次运行一个月内展示相对时间,超过一个月展示绝对时间", () => {
  409. const now = new Date(2026, 6, 17, 8, 0, 0, 0).getTime();
  410. const timeIntl = {
  411. formatMessage: ({ id }: { id: string }, values?: Record<string, string>) => {
  412. if (id === "automations.time.days") return `${values?.["value"]}d`;
  413. if (id === "automations.time.in") return `in ${values?.["amount"]}`;
  414. return id;
  415. },
  416. };
  417. expect(formatAutomationCardNextRun(now + 29 * 24 * 60 * 60 * 1000, now, timeIntl)).toBe(
  418. "in 29d",
  419. );
  420. expect(formatAutomationCardNextRun(now + 31 * 24 * 60 * 60 * 1000, now, timeIntl)).toBe(
  421. "2026-08-17 08:00",
  422. );
  423. expect(formatAutomationCardNextRun(now, now, timeIntl)).toBeNull();
  424. expect(formatAutomationCardNextRun(now - 1, now, timeIntl)).toBeNull();
  425. expect(formatAutomationCardNextRun(undefined, now, timeIntl)).toBeNull();
  426. });
  427. });