automationCron.test.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. buildIntervalScheduleRule,
  4. buildRelativeDelaySchedule,
  5. computeInitialAutomationNextRunAt,
  6. computeNextRunAt,
  7. computeScheduleRuleNextRunAt,
  8. inferMinuteIntervalScheduleRule,
  9. isOneShotAutomation,
  10. isValidCronExpr,
  11. StaleOneShotAutomationScheduleError,
  12. } from "../src/session/automationCron.js";
  13. describe("automationCron", () => {
  14. it("isOneShotAutomation:只有非循环且至多一次的任务是纯一次性", () => {
  15. // 一次性语义是确定的目标时刻,scheduler misfire 后据此终态,不得再排程。
  16. expect(isOneShotAutomation({ recurring: false })).toBe(true);
  17. expect(isOneShotAutomation({ recurring: false, maxRuns: 1 })).toBe(true);
  18. expect(isOneShotAutomation({ recurring: false, maxRuns: 3 })).toBe(false);
  19. expect(isOneShotAutomation({ recurring: true })).toBe(false);
  20. });
  21. it("isValidCronExpr:合法 5 段表达式返回 true", () => {
  22. expect(isValidCronExpr("0 9 * * *")).toBe(true);
  23. expect(isValidCronExpr("*/5 * * * *")).toBe(true);
  24. expect(isValidCronExpr("30 18 * * 1,3,5")).toBe(true);
  25. expect(isValidCronExpr("0 9 * */2 1#1")).toBe(true);
  26. });
  27. it("isValidCronExpr:非法表达式返回 false", () => {
  28. expect(isValidCronExpr("not a cron")).toBe(false);
  29. expect(isValidCronExpr("99 99 * * *")).toBe(false);
  30. expect(isValidCronExpr("")).toBe(false);
  31. });
  32. it("computeNextRunAt:每天 09:00 的下一次严格晚于基准且命中 09:00", () => {
  33. // 基准:本地 2023-11-14T20:00 左右(用固定戳,避免依赖当前时间)。
  34. const from = new Date(2023, 10, 14, 20, 0, 0).getTime();
  35. const next = computeNextRunAt("0 9 * * *", from);
  36. expect(next).not.toBeNull();
  37. expect(next!).toBeGreaterThan(from);
  38. const d = new Date(next!);
  39. expect(d.getHours()).toBe(9);
  40. expect(d.getMinutes()).toBe(0);
  41. });
  42. it("computeNextRunAt:从 08:00 出发,当天 09:00 即为下一次", () => {
  43. const from = new Date(2023, 10, 14, 8, 0, 0).getTime();
  44. const next = computeNextRunAt("0 9 * * *", from);
  45. const expected = new Date(2023, 10, 14, 9, 0, 0).getTime();
  46. expect(next).toBe(expected);
  47. });
  48. it("computeNextRunAt:每分钟表达式返回下一分钟", () => {
  49. const from = new Date(2023, 10, 14, 8, 0, 30).getTime();
  50. const next = computeNextRunAt("* * * * *", from);
  51. const expected = new Date(2023, 10, 14, 8, 1, 0).getTime();
  52. expect(next).toBe(expected);
  53. });
  54. it("相对分钟计划以服务端真实时钟为锚点,保留秒级延迟", () => {
  55. const from = new Date(2026, 6, 28, 14, 44, 28).getTime();
  56. const schedule = buildRelativeDelaySchedule(3, from);
  57. expect(schedule.cronExpr).toBe("47 14 28 7 *");
  58. expect(schedule.scheduleRule).toMatchObject({
  59. unit: "minute",
  60. interval: 3,
  61. anchorAt: from,
  62. });
  63. expect(computeScheduleRuleNextRunAt(schedule.scheduleRule!, from)).toBe(from + 3 * 60_000);
  64. });
  65. it("一次性固定月日 cron 刚错过目标分钟时立即到期,避免滚到下一年", () => {
  66. const from = new Date(2026, 6, 16, 17, 31, 30).getTime();
  67. expect(
  68. computeInitialAutomationNextRunAt(
  69. {
  70. cronExpr: "31 17 16 7 *",
  71. recurring: false,
  72. },
  73. from,
  74. ),
  75. ).toBe(from);
  76. });
  77. it("一次性固定月日 cron 目标已过去 4 分 53 秒时拒绝创建", () => {
  78. const from = new Date(2026, 6, 28, 14, 38, 53).getTime();
  79. expect(() =>
  80. computeInitialAutomationNextRunAt(
  81. {
  82. cronExpr: "34 14 28 7 *",
  83. recurring: false,
  84. },
  85. from,
  86. ),
  87. ).toThrow(StaleOneShotAutomationScheduleError);
  88. });
  89. it("一次性固定月日 cron 跨过目标整一分钟时不再补执行", () => {
  90. const from = new Date(2026, 6, 28, 14, 35, 0).getTime();
  91. expect(() =>
  92. computeInitialAutomationNextRunAt(
  93. {
  94. cronExpr: "34 14 28 7 *",
  95. recurring: false,
  96. },
  97. from,
  98. ),
  99. ).toThrow(StaleOneShotAutomationScheduleError);
  100. });
  101. it("循环任务与尚未到期的一次性日历任务保持原计划", () => {
  102. const from = new Date(2026, 6, 16, 17, 31, 30).getTime();
  103. const nextYear = new Date(2027, 6, 16, 17, 31, 0).getTime();
  104. expect(
  105. computeInitialAutomationNextRunAt(
  106. { cronExpr: "31 17 16 7 *", recurring: true },
  107. from,
  108. ),
  109. ).toBe(nextYear);
  110. expect(
  111. computeInitialAutomationNextRunAt(
  112. { cronExpr: "32 17 16 7 *", recurring: false },
  113. from,
  114. ),
  115. ).toBe(new Date(2026, 6, 16, 17, 32, 0).getTime());
  116. });
  117. it("一次性固定月日 cron 目标已过去 6 分钟(模型时钟漂移)时拒绝创建,不静默滚到下一年", () => {
  118. // 回归:'8分钟后' 被模型自算成固定日历 cron,创建时目标分钟已过去几分钟,
  119. // 旧的 5 分钟窗口放过它 → 静默滚到下一年(下次运行显示 2027)。30 分钟窗口覆盖漂移,改为拒绝。
  120. const from = new Date(2026, 6, 16, 17, 37, 0).getTime();
  121. expect(() =>
  122. computeInitialAutomationNextRunAt(
  123. { cronExpr: "31 17 16 7 *", recurring: false },
  124. from,
  125. ),
  126. ).toThrow(StaleOneShotAutomationScheduleError);
  127. });
  128. it("一次性日历任务在目标月份之后创建时保留下一年语义", () => {
  129. // 创建于 8 月、目标是 7 月 16 日:目标已过去约一个月,远超陈旧识别窗口,
  130. // 属于用户明确要约到下一年,而非模型算错的近似当下时刻。
  131. const from = new Date(2026, 7, 16, 12, 0, 0).getTime();
  132. expect(
  133. computeInitialAutomationNextRunAt(
  134. { cronExpr: "31 17 16 7 *", recurring: false },
  135. from,
  136. ),
  137. ).toBe(new Date(2027, 6, 16, 17, 31, 0).getTime());
  138. });
  139. it("computeScheduleRuleNextRunAt:每 31 小时按锚点推进并保持第 49 分", () => {
  140. const anchorAt = new Date(2026, 7, 4, 18, 38).getTime();
  141. const rule = {
  142. unit: "hourly" as const,
  143. interval: 31,
  144. hour: 0,
  145. minute: 49,
  146. anchorAt,
  147. };
  148. const first = computeScheduleRuleNextRunAt(rule, anchorAt);
  149. expect(first).toBe(new Date(2026, 7, 4, 18, 49).getTime());
  150. expect(computeScheduleRuleNextRunAt(rule, first!)).toBe(
  151. new Date(2026, 7, 6, 1, 49).getTime(),
  152. );
  153. });
  154. it("computeScheduleRuleNextRunAt:每 2 周保持锚点周序", () => {
  155. const anchorAt = new Date(2024, 0, 1, 8, 0).getTime(); // 周一
  156. const next = computeScheduleRuleNextRunAt(
  157. {
  158. unit: "weekly",
  159. interval: 2,
  160. hour: 9,
  161. minute: 0,
  162. anchorAt,
  163. weekdays: [2],
  164. },
  165. new Date(2024, 0, 2, 10, 0).getTime(),
  166. );
  167. expect(next).toBe(new Date(2024, 0, 16, 9, 0).getTime());
  168. });
  169. it("每 10 分钟从创建时刻计时,不提前命中墙钟刻度且不随晚派发漂移", () => {
  170. const anchorAt = new Date(2026, 6, 20, 11, 46, 15).getTime();
  171. const rule = inferMinuteIntervalScheduleRule("*/10 * * * *", anchorAt);
  172. expect(rule).toMatchObject({ unit: "minute", interval: 10, anchorAt });
  173. expect(computeScheduleRuleNextRunAt(rule!, anchorAt)).toBe(
  174. new Date(2026, 6, 20, 11, 56, 15).getTime(),
  175. );
  176. expect(computeScheduleRuleNextRunAt(rule!, new Date(2026, 6, 20, 11, 57).getTime())).toBe(
  177. new Date(2026, 6, 20, 12, 6, 15).getTime(),
  178. );
  179. });
  180. it("computeScheduleRuleNextRunAt:每 2 年保持锚点月日", () => {
  181. const anchorAt = new Date(2026, 6, 12, 8, 0).getTime();
  182. const next = computeScheduleRuleNextRunAt(
  183. {
  184. unit: "yearly",
  185. interval: 2,
  186. hour: 9,
  187. minute: 30,
  188. anchorAt,
  189. monthDays: [12],
  190. },
  191. new Date(2026, 6, 13, 0, 0).getTime(),
  192. );
  193. expect(next).toBe(new Date(2028, 6, 12, 9, 30).getTime());
  194. });
  195. it("computeScheduleRuleNextRunAt:月度间隔 1200 的边界仍能计算下一轮", () => {
  196. const anchorAt = new Date(2026, 0, 1, 10, 0).getTime();
  197. const next = computeScheduleRuleNextRunAt(
  198. {
  199. unit: "monthly",
  200. interval: 1_200,
  201. hour: 9,
  202. minute: 0,
  203. anchorAt,
  204. monthDays: [1],
  205. monthlyMode: "date",
  206. },
  207. anchorAt,
  208. );
  209. expect(next).toBe(new Date(2126, 0, 1, 9, 0).getTime());
  210. });
  211. it("computeScheduleRuleNextRunAt:months 覆盖锚点月份", () => {
  212. // 锚点在 7 月,但规则指定 3 月 5 日 → 下次应落到(次年)3 月。
  213. const anchorAt = new Date(2026, 6, 12, 8, 0).getTime();
  214. const next = computeScheduleRuleNextRunAt(
  215. {
  216. unit: "yearly",
  217. interval: 1,
  218. hour: 9,
  219. minute: 30,
  220. anchorAt,
  221. months: [3],
  222. monthDays: [5],
  223. },
  224. new Date(2026, 6, 13, 0, 0).getTime(),
  225. );
  226. expect(next).toBe(new Date(2027, 2, 5, 9, 30).getTime());
  227. });
  228. it("computeScheduleRuleNextRunAt:2/29 只在闰年触发(溢出守卫)", () => {
  229. const anchorAt = new Date(2025, 0, 1, 8, 0).getTime();
  230. const next = computeScheduleRuleNextRunAt(
  231. {
  232. unit: "yearly",
  233. interval: 1,
  234. hour: 9,
  235. minute: 30,
  236. anchorAt,
  237. months: [2],
  238. monthDays: [29],
  239. },
  240. new Date(2025, 5, 1, 0, 0).getTime(),
  241. );
  242. // 2026/2027 非闰年被守卫跳过,落到 2028-02-29。
  243. expect(next).toBe(new Date(2028, 1, 29, 9, 30).getTime());
  244. });
  245. describe("buildIntervalScheduleRule:会话侧长间隔 carrier 归一化", () => {
  246. // Bug 背景:cron 各字段有步长上限(minute 59、hour 24、day-of-month 31、month 12),
  247. // 「每50小时/每40天/每13个月」无法用五段 cron 直接表达。会话侧用 intervalUnit+interval
  248. // 受控 carrier,host 端用本函数把兼容 cron 各字段解析成权威 scheduleRule,真实间隔由
  249. // scheduleRule 承载,cronExpr 仅作展示。anchorAt 由调用方(service)传入真实创建/修改时刻。
  250. const anchorAt = new Date(2026, 7, 4, 10, 30, 0).getTime();
  251. it("hourly:从兼容 cron 解析 minute,间隔 50 落入 scheduleRule", () => {
  252. // 兼容 cron '15 * * * *' 表示「每小时的第15分」,真实间隔 50 小时由 carrier 承载。
  253. const rule = buildIntervalScheduleRule("hourly", 50, "15 * * * *", anchorAt);
  254. expect(rule).toMatchObject({
  255. unit: "hourly",
  256. interval: 50,
  257. hour: 0,
  258. minute: 15,
  259. anchorAt,
  260. });
  261. expect(rule!.weekdays).toBeUndefined();
  262. // 下一次:锚点 10:30 之后第一个 15 分,再 +50 小时。
  263. expect(computeScheduleRuleNextRunAt(rule!, anchorAt)).toBe(
  264. new Date(2026, 7, 4, 10, 15).getTime() + 50 * 3_600_000,
  265. );
  266. });
  267. it("daily:解析 hour/minute,间隔 40 落入 scheduleRule", () => {
  268. const rule = buildIntervalScheduleRule("daily", 40, "9 9 * * *", anchorAt);
  269. expect(rule).toMatchObject({
  270. unit: "daily",
  271. interval: 40,
  272. hour: 9,
  273. minute: 9,
  274. anchorAt,
  275. });
  276. // 下一次:锚点 8/4 10:30 已过当天 09:09,按 40 天间隔推进,下一轮是 8/4 + 40 = 9/13 09:09。
  277. expect(computeScheduleRuleNextRunAt(rule!, anchorAt)).toBe(
  278. new Date(2026, 8, 13, 9, 9).getTime(),
  279. );
  280. });
  281. it("weekly:从 dow 解析 weekdays,间隔 8 落入 scheduleRule", () => {
  282. // 兼容 cron '0 9 * * 1,3,5' 表示周一/三/五 09:00,真实间隔 8 周。
  283. const rule = buildIntervalScheduleRule("weekly", 8, "0 9 * * 1,3,5", anchorAt);
  284. expect(rule).toMatchObject({
  285. unit: "weekly",
  286. interval: 8,
  287. hour: 9,
  288. minute: 0,
  289. weekdays: [1, 3, 5],
  290. anchorAt,
  291. });
  292. });
  293. it("monthly:从 dom 解析 monthDays,间隔 13 落入 scheduleRule", () => {
  294. // 兼容 cron '0 9 15 * *' 表示每月 15 日 09:00,真实间隔 13 个月。
  295. const rule = buildIntervalScheduleRule("monthly", 13, "0 9 15 * *", anchorAt);
  296. expect(rule).toMatchObject({
  297. unit: "monthly",
  298. interval: 13,
  299. hour: 9,
  300. minute: 0,
  301. monthDays: [15],
  302. monthlyMode: "date",
  303. anchorAt,
  304. });
  305. });
  306. it("yearly:从 dom+month 解析 months+monthDays,间隔 2 落入 scheduleRule", () => {
  307. // 兼容 cron '0 9 15 6 *' 表示每年 6 月 15 日 09:00,真实间隔 2 年。
  308. const rule = buildIntervalScheduleRule("yearly", 2, "0 9 15 6 *", anchorAt);
  309. expect(rule).toMatchObject({
  310. unit: "yearly",
  311. interval: 2,
  312. hour: 9,
  313. minute: 0,
  314. months: [6],
  315. monthDays: [15],
  316. anchorAt,
  317. });
  318. });
  319. it("minute:直接以 anchorAt 的时分作展示锚点,间隔 90 落入 scheduleRule", () => {
  320. // 兼容 cron '* * * * *' 仅作占位展示,真实间隔 90 分钟。
  321. const rule = buildIntervalScheduleRule("minute", 90, "* * * * *", anchorAt);
  322. expect(rule).toMatchObject({
  323. unit: "minute",
  324. interval: 90,
  325. hour: 10,
  326. minute: 30,
  327. anchorAt,
  328. });
  329. });
  330. it("兼容 cron 字段缺失时回退到安全默认值,不抛错", () => {
  331. // 模型可能传 '* * * * *' 占位 cron 给 weekly/monthly/yearly;解析失败时给合理默认值
  332. // (weekday=[1]、monthDays=[1]、months=[锚点月]),保证调度引擎仍可计算。
  333. // hour/minute 取锚点时分('*' 解析为 undefined 后回退到 anchorAt 的时分)。
  334. const rule = buildIntervalScheduleRule("weekly", 5, "* * * * *", anchorAt);
  335. expect(rule).toMatchObject({
  336. unit: "weekly",
  337. interval: 5,
  338. weekdays: [1],
  339. hour: 10,
  340. minute: 30,
  341. });
  342. const monthlyRule = buildIntervalScheduleRule("monthly", 7, "* * * * *", anchorAt);
  343. expect(monthlyRule).toMatchObject({ unit: "monthly", monthDays: [4] });
  344. const yearlyRule = buildIntervalScheduleRule("yearly", 3, "* * * * *", anchorAt);
  345. expect(yearlyRule).toMatchObject({
  346. unit: "yearly",
  347. months: [new Date(anchorAt).getMonth() + 1],
  348. monthDays: [new Date(anchorAt).getDate()],
  349. });
  350. });
  351. it("无效 unit 抛错,避免静默生成错误规则", () => {
  352. expect(() =>
  353. buildIntervalScheduleRule("decade" as never, 10, "0 9 * * *", anchorAt),
  354. ).toThrow();
  355. });
  356. });
  357. });