automation.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. CronCreateInputJsonSchema,
  4. CronCreateInputSchema,
  5. CronUpdateInputJsonSchema,
  6. CronUpdateInputSchema,
  7. } from "../src/tools/automation.js";
  8. describe("automation tool contracts", () => {
  9. it("CronCreate 要求标题保留用户的时间规则", () => {
  10. expect(
  11. CronCreateInputSchema.safeParse({
  12. cron: "*/20 * * * *",
  13. delayMinutes: null,
  14. prompt: "提醒我喝水",
  15. }).success,
  16. ).toBe(false);
  17. expect(CronCreateInputJsonSchema.required).toContain("title");
  18. // 向后兼容:delayMinutes 是新增的相对时间能力,绝不能变成必填让旧调用方
  19. // (只传 cron/prompt/title 的模型或客户端)被 schema 拒绝。
  20. expect(CronCreateInputJsonSchema.required).not.toContain("delayMinutes");
  21. expect(CronCreateInputJsonSchema).toHaveProperty(
  22. "properties.title.description",
  23. expect.stringContaining("preserves the user's natural-language schedule phrase"),
  24. );
  25. });
  26. it("CronCreate schema 说明本地五段 cron 与有限次数语义", () => {
  27. expect(CronCreateInputJsonSchema).toHaveProperty(
  28. "properties.cron.description",
  29. expect.stringContaining("minute hour day-of-month month day-of-week"),
  30. );
  31. expect(CronCreateInputJsonSchema).toHaveProperty(
  32. "properties.cron.description",
  33. expect.stringContaining("*/20 * * * *"),
  34. );
  35. expect(CronCreateInputJsonSchema).toHaveProperty(
  36. "properties.delayMinutes.description",
  37. expect.stringContaining("host calculates the future local schedule"),
  38. );
  39. expect(CronCreateInputJsonSchema).toHaveProperty(
  40. "properties.recurring.description",
  41. expect.stringContaining("true (default)"),
  42. );
  43. expect(CronCreateInputJsonSchema).toHaveProperty(
  44. "properties.maxRuns.description",
  45. expect.stringContaining("recurring=false"),
  46. );
  47. expect(CronCreateInputJsonSchema).toHaveProperty(
  48. "properties.prompt.description",
  49. expect.stringContaining("do not ask it to create or schedule another automation"),
  50. );
  51. });
  52. it("CronCreate 相对时间使用结构化 delayMinutes", () => {
  53. expect(
  54. CronCreateInputSchema.parse({
  55. delayMinutes: 3,
  56. prompt: "提醒我接水",
  57. title: "3分钟后接水提醒",
  58. recurring: false,
  59. }),
  60. ).toMatchObject({ delayMinutes: 3, recurring: false });
  61. // 向后兼容:旧调用方省略 delayMinutes(以及 provider 丢弃 null 字段)时,
  62. // 普通 cron 创建必须原样可用,且不得被误判成相对任务。
  63. expect(
  64. CronCreateInputSchema.parse({
  65. cron: "0 9 * * *",
  66. prompt: "每天九点提醒我喝水",
  67. title: "每天9点喝水提醒",
  68. }),
  69. ).toMatchObject({ cron: "0 9 * * *" });
  70. expect(
  71. CronCreateInputSchema.parse({
  72. cron: "*/20 * * * *",
  73. delayMinutes: null,
  74. prompt: "提醒我喝水",
  75. title: "每20分钟喝水提醒",
  76. }),
  77. ).toMatchObject({ cron: "*/20 * * * *", delayMinutes: null });
  78. // 省略 delayMinutes 时 cron 仍必填,缺 cron 必须拒绝而不是当成相对任务。
  79. expect(
  80. CronCreateInputSchema.safeParse({
  81. prompt: "提醒我接水",
  82. title: "接水提醒",
  83. }).success,
  84. ).toBe(false);
  85. expect(
  86. CronCreateInputSchema.safeParse({
  87. delayMinutes: null,
  88. prompt: "提醒我接水",
  89. title: "接水提醒",
  90. }).success,
  91. ).toBe(false);
  92. // 相对延迟由 host 用真实时钟换算,模型不得再同时提交 cron。
  93. expect(
  94. CronCreateInputSchema.safeParse({
  95. cron: "*/5 * * * *",
  96. delayMinutes: 3,
  97. prompt: "提醒我接水",
  98. title: "3分钟后接水提醒",
  99. recurring: false,
  100. }).success,
  101. ).toBe(false);
  102. });
  103. it("CronUpdate 接受严格的非空 patch,并维护 recurring/maxRuns 组合不变量", () => {
  104. expect(
  105. CronUpdateInputSchema.parse({
  106. id: "automation-1",
  107. title: "新的标题",
  108. recurring: true,
  109. maxRuns: null,
  110. }),
  111. ).toEqual({
  112. id: "automation-1",
  113. title: "新的标题",
  114. recurring: true,
  115. maxRuns: null,
  116. });
  117. expect(
  118. CronUpdateInputSchema.parse({
  119. id: "automation-1",
  120. title: "无限循环的新标题",
  121. recurring: true,
  122. }),
  123. ).toEqual({
  124. id: "automation-1",
  125. title: "无限循环的新标题",
  126. recurring: true,
  127. });
  128. expect(CronUpdateInputSchema.safeParse({ id: "automation-1" }).success).toBe(false);
  129. expect(
  130. CronUpdateInputSchema.safeParse({
  131. id: "automation-1",
  132. cron: "*/6 * * * *",
  133. }).success,
  134. ).toBe(false);
  135. expect(
  136. CronUpdateInputSchema.safeParse({
  137. id: "automation-1",
  138. title: "新的标题",
  139. maxRuns: null,
  140. }).success,
  141. ).toBe(false);
  142. expect(
  143. CronUpdateInputSchema.safeParse({
  144. id: "automation-1",
  145. title: "新的标题",
  146. recurring: false,
  147. maxRuns: null,
  148. }).success,
  149. ).toBe(false);
  150. expect(
  151. CronUpdateInputSchema.safeParse({
  152. id: "automation-1",
  153. title: "新的标题",
  154. recurring: true,
  155. maxRuns: 3,
  156. }).success,
  157. ).toBe(false);
  158. expect(
  159. CronUpdateInputSchema.safeParse({
  160. id: "automation-1",
  161. title: "新的标题",
  162. prompt: "新的提示词",
  163. enabled: false,
  164. }).success,
  165. ).toBe(false);
  166. });
  167. it("CronUpdate provider schema 强制同步 title,且不使用 provider-internal 组合关键字", () => {
  168. expect(CronUpdateInputJsonSchema.required).toContain("id");
  169. expect(CronUpdateInputJsonSchema.required).toContain("title");
  170. expect(CronUpdateInputJsonSchema).toHaveProperty(
  171. "properties.title.description",
  172. expect.stringContaining("every 5 minutes to every 6 minutes"),
  173. );
  174. // 与 core 跨 provider 守卫同一规范:provider-visible schema 禁止 anyOf 等
  175. // provider-internal key。recurring/maxRuns 组合不变量由 runtime refine 强制,
  176. // 模型侧靠字段 description 提示。
  177. expect(CronUpdateInputJsonSchema).not.toHaveProperty("anyOf");
  178. expect(CronUpdateInputJsonSchema).toHaveProperty(
  179. "properties.maxRuns.description",
  180. expect.stringContaining("recurring=true"),
  181. );
  182. expect(CronUpdateInputJsonSchema).toHaveProperty(
  183. "properties.recurring.description",
  184. expect.stringContaining("Do not combine true with a numeric maxRuns"),
  185. );
  186. });
  187. it("CronCreate 长间隔周期用 intervalUnit + interval 统一 carrier", () => {
  188. // cron 字段步长超限的场景(含 hourly N>24、daily N>31 等)统一用 intervalUnit+interval。
  189. expect(
  190. CronCreateInputSchema.parse({
  191. cron: "0 * * * *",
  192. intervalUnit: "hourly",
  193. interval: 50,
  194. prompt: "每50小时发送你好",
  195. title: "每50小时发送你好",
  196. }),
  197. ).toMatchObject({ intervalUnit: "hourly", interval: 50 });
  198. expect(
  199. CronCreateInputSchema.parse({
  200. cron: "0 9 * * *",
  201. intervalUnit: "daily",
  202. interval: 40,
  203. prompt: "每40天发送1+1",
  204. title: "每40天发送1+1",
  205. }),
  206. ).toMatchObject({ intervalUnit: "daily", interval: 40 });
  207. // interval 必须统一限制为 UI 自定义重复的 1-200 整数范围。
  208. expect(
  209. CronCreateInputSchema.parse({
  210. cron: "* * * * *",
  211. intervalUnit: "minute",
  212. interval: 200,
  213. prompt: "x",
  214. title: "每200分钟提醒",
  215. }),
  216. ).toMatchObject({ intervalUnit: "minute", interval: 200 });
  217. for (const interval of [0, 201, 1.5]) {
  218. expect(
  219. CronCreateInputSchema.safeParse({
  220. cron: "0 9 * * *",
  221. intervalUnit: "daily",
  222. interval,
  223. prompt: "x",
  224. title: "t",
  225. }).success,
  226. ).toBe(false);
  227. }
  228. // intervalUnit 与 interval 必须配对:只传一个要拒绝。
  229. expect(
  230. CronCreateInputSchema.safeParse({
  231. cron: "0 9 * * *",
  232. intervalUnit: "daily",
  233. prompt: "x",
  234. title: "t",
  235. }).success,
  236. ).toBe(false);
  237. expect(
  238. CronCreateInputSchema.safeParse({
  239. cron: "0 9 * * *",
  240. interval: 40,
  241. prompt: "x",
  242. title: "t",
  243. }).success,
  244. ).toBe(false);
  245. // 与 delayMinutes(一次性)互斥。
  246. expect(
  247. CronCreateInputSchema.safeParse({
  248. cron: "0 9 * * *",
  249. intervalUnit: "daily",
  250. interval: 40,
  251. delayMinutes: 5,
  252. prompt: "x",
  253. title: "t",
  254. recurring: false,
  255. }).success,
  256. ).toBe(false);
  257. // interval carrier 的领域语义是无限循环,不能伪装成一次性或有限次数任务。
  258. for (const invalidMode of [{ recurring: false }, { maxRuns: 2 }]) {
  259. expect(
  260. CronCreateInputSchema.safeParse({
  261. cron: "0 9 * * *",
  262. intervalUnit: "daily",
  263. interval: 40,
  264. prompt: "x",
  265. title: "每40天提醒",
  266. ...invalidMode,
  267. }).success,
  268. ).toBe(false);
  269. }
  270. // intervalUnit 必须是已知枚举。
  271. expect(
  272. CronCreateInputSchema.safeParse({
  273. cron: "0 9 * * *",
  274. intervalUnit: "decade",
  275. interval: 10,
  276. prompt: "x",
  277. title: "t",
  278. }).success,
  279. ).toBe(false);
  280. });
  281. it("CronUpdate 支持把任务改为长间隔周期", () => {
  282. expect(
  283. CronUpdateInputSchema.parse({
  284. id: "automation-1",
  285. title: "每40天发送1+1",
  286. cron: "0 9 * * *",
  287. intervalUnit: "daily",
  288. interval: 40,
  289. }),
  290. ).toEqual({
  291. id: "automation-1",
  292. title: "每40天发送1+1",
  293. cron: "0 9 * * *",
  294. intervalUnit: "daily",
  295. interval: 40,
  296. });
  297. // 边界与 UI 一致:200 合法,0 / 201 / 小数必须拒绝。
  298. expect(
  299. CronUpdateInputSchema.parse({
  300. id: "automation-1",
  301. title: "每200个月发送1+1",
  302. cron: "0 9 15 * *",
  303. intervalUnit: "monthly",
  304. interval: 200,
  305. }),
  306. ).toMatchObject({ intervalUnit: "monthly", interval: 200 });
  307. for (const interval of [0, 201, 1.5]) {
  308. expect(
  309. CronUpdateInputSchema.safeParse({
  310. id: "automation-1",
  311. title: "t",
  312. intervalUnit: "monthly",
  313. interval,
  314. }).success,
  315. ).toBe(false);
  316. }
  317. // interval carrier 会切换为无限循环,不能和一次性 / 有限次数语义组合。
  318. for (const invalidMode of [{ recurring: false }, { maxRuns: 2 }]) {
  319. expect(
  320. CronUpdateInputSchema.safeParse({
  321. id: "automation-1",
  322. title: "每40天发送1+1",
  323. intervalUnit: "daily",
  324. interval: 40,
  325. ...invalidMode,
  326. }).success,
  327. ).toBe(false);
  328. }
  329. // 仅传 intervalUnit 不传 interval 被拒。
  330. expect(
  331. CronUpdateInputSchema.safeParse({
  332. id: "automation-1",
  333. title: "t",
  334. intervalUnit: "monthly",
  335. }).success,
  336. ).toBe(false);
  337. });
  338. });