automation.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. // 非法组合必须由 runtime schema 拒绝,这是 anyOf 删除后的唯一强制层。
  187. expect(
  188. CronUpdateInputSchema.safeParse({
  189. id: "automation-1",
  190. title: "新的标题",
  191. recurring: true,
  192. maxRuns: 3,
  193. }).success,
  194. ).toBe(false);
  195. expect(
  196. CronCreateInputSchema.safeParse({
  197. id: "automation-1",
  198. title: "新的标题",
  199. maxRuns: null,
  200. }).success,
  201. ).toBe(false);
  202. });
  203. it("CronCreate 长间隔周期用 intervalUnit + interval 统一 carrier", () => {
  204. // cron 字段步长超限的场景(含 hourly N>24、daily N>31 等)统一用 intervalUnit+interval。
  205. expect(
  206. CronCreateInputSchema.parse({
  207. cron: "0 * * * *",
  208. intervalUnit: "hourly",
  209. interval: 50,
  210. prompt: "每50小时发送你好",
  211. title: "每50小时发送你好",
  212. }),
  213. ).toMatchObject({ intervalUnit: "hourly", interval: 50 });
  214. expect(
  215. CronCreateInputSchema.parse({
  216. cron: "0 9 * * *",
  217. intervalUnit: "daily",
  218. interval: 40,
  219. prompt: "每40天发送1+1",
  220. title: "每40天发送1+1",
  221. }),
  222. ).toMatchObject({ intervalUnit: "daily", interval: 40 });
  223. // interval 必须统一限制为 UI 自定义重复的 1-200 整数范围。
  224. expect(
  225. CronCreateInputSchema.parse({
  226. cron: "* * * * *",
  227. intervalUnit: "minute",
  228. interval: 200,
  229. prompt: "x",
  230. title: "每200分钟提醒",
  231. }),
  232. ).toMatchObject({ intervalUnit: "minute", interval: 200 });
  233. for (const interval of [0, 201, 1.5]) {
  234. expect(
  235. CronCreateInputSchema.safeParse({
  236. cron: "0 9 * * *",
  237. intervalUnit: "daily",
  238. interval,
  239. prompt: "x",
  240. title: "t",
  241. }).success,
  242. ).toBe(false);
  243. }
  244. // intervalUnit 与 interval 必须配对:只传一个要拒绝。
  245. expect(
  246. CronCreateInputSchema.safeParse({
  247. cron: "0 9 * * *",
  248. intervalUnit: "daily",
  249. prompt: "x",
  250. title: "t",
  251. }).success,
  252. ).toBe(false);
  253. expect(
  254. CronCreateInputSchema.safeParse({
  255. cron: "0 9 * * *",
  256. interval: 40,
  257. prompt: "x",
  258. title: "t",
  259. }).success,
  260. ).toBe(false);
  261. // 与 delayMinutes(一次性)互斥。
  262. expect(
  263. CronCreateInputSchema.safeParse({
  264. cron: "0 9 * * *",
  265. intervalUnit: "daily",
  266. interval: 40,
  267. delayMinutes: 5,
  268. prompt: "x",
  269. title: "t",
  270. recurring: false,
  271. }).success,
  272. ).toBe(false);
  273. // interval carrier 的领域语义是无限循环,不能伪装成一次性或有限次数任务。
  274. for (const invalidMode of [{ recurring: false }, { maxRuns: 2 }]) {
  275. expect(
  276. CronCreateInputSchema.safeParse({
  277. cron: "0 9 * * *",
  278. intervalUnit: "daily",
  279. interval: 40,
  280. prompt: "x",
  281. title: "每40天提醒",
  282. ...invalidMode,
  283. }).success,
  284. ).toBe(false);
  285. }
  286. // intervalUnit 必须是已知枚举。
  287. expect(
  288. CronCreateInputSchema.safeParse({
  289. cron: "0 9 * * *",
  290. intervalUnit: "decade",
  291. interval: 10,
  292. prompt: "x",
  293. title: "t",
  294. }).success,
  295. ).toBe(false);
  296. });
  297. it("CronUpdate 支持把任务改为长间隔周期", () => {
  298. expect(
  299. CronUpdateInputSchema.parse({
  300. id: "automation-1",
  301. title: "每40天发送1+1",
  302. cron: "0 9 * * *",
  303. intervalUnit: "daily",
  304. interval: 40,
  305. }),
  306. ).toEqual({
  307. id: "automation-1",
  308. title: "每40天发送1+1",
  309. cron: "0 9 * * *",
  310. intervalUnit: "daily",
  311. interval: 40,
  312. });
  313. // 边界与 UI 一致:200 合法,0 / 201 / 小数必须拒绝。
  314. expect(
  315. CronUpdateInputSchema.parse({
  316. id: "automation-1",
  317. title: "每200个月发送1+1",
  318. cron: "0 9 15 * *",
  319. intervalUnit: "monthly",
  320. interval: 200,
  321. }),
  322. ).toMatchObject({ intervalUnit: "monthly", interval: 200 });
  323. for (const interval of [0, 201, 1.5]) {
  324. expect(
  325. CronUpdateInputSchema.safeParse({
  326. id: "automation-1",
  327. title: "t",
  328. intervalUnit: "monthly",
  329. interval,
  330. }).success,
  331. ).toBe(false);
  332. }
  333. // interval carrier 会切换为无限循环,不能和一次性 / 有限次数语义组合。
  334. for (const invalidMode of [{ recurring: false }, { maxRuns: 2 }]) {
  335. expect(
  336. CronUpdateInputSchema.safeParse({
  337. id: "automation-1",
  338. title: "每40天发送1+1",
  339. intervalUnit: "daily",
  340. interval: 40,
  341. ...invalidMode,
  342. }).success,
  343. ).toBe(false);
  344. }
  345. // 仅传 intervalUnit 不传 interval 被拒。
  346. expect(
  347. CronUpdateInputSchema.safeParse({
  348. id: "automation-1",
  349. title: "t",
  350. intervalUnit: "monthly",
  351. }).success,
  352. ).toBe(false);
  353. });
  354. });