automationTemplateCatalog.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import { describe, expect, it } from "vitest";
  2. import type { ClientSceneConfig } from "@zcode/services";
  3. import {
  4. appendLocalCustomizeTemplate,
  5. materializeOffPeakTemplateDraft,
  6. materializeScheduledTemplateDraft,
  7. mapClientScenesToAutomationTemplates,
  8. resolveOffPeakTemplateText,
  9. resolveAutomationTemplateText,
  10. } from "@/settings/automationTemplateCatalog.js";
  11. function createScenes(cronExpr = "0 9 * * 1-5"): ClientSceneConfig[] {
  12. return [
  13. {
  14. namespace: "zcode",
  15. scene: "scheduled-task",
  16. options: {
  17. prompts: {
  18. id: "scheduled-prompts",
  19. type: "prompts",
  20. contents: { cn: "定时任务模板", en: "Scheduled task template" },
  21. items: [
  22. {
  23. id: "item-morningDevBrief",
  24. type: "prompts",
  25. contents: { cn: "中文定时指令", en: "English scheduled prompt" },
  26. labels: { cn: "晨会动态", en: "Morning dev brief" },
  27. on_finish: null,
  28. img: null,
  29. defaults: { cronExpr: ["item-morningDevBrief-cronExpr"] },
  30. },
  31. ],
  32. refer: "",
  33. templates: { cn: "{prompt}", en: "{prompt}" },
  34. },
  35. cronExpr: {
  36. id: "scheduled-cron",
  37. type: "cronExpr",
  38. contents: { cn: "调度规则", en: "Schedule" },
  39. items: [
  40. {
  41. id: "item-morningDevBrief-cronExpr",
  42. type: "cronExpr",
  43. contents: { cn: cronExpr, en: cronExpr },
  44. // 服务端 label 只作配置说明,外显必须由 contents 中的 cron 本地解析。
  45. labels: { cn: "错误的服务端外显", en: "Wrong server display" },
  46. on_finish: null,
  47. img: null,
  48. },
  49. ],
  50. refer: "",
  51. templates: { cn: "{prompt}", en: "{prompt}" },
  52. },
  53. },
  54. created_at: 0,
  55. updated_at: 0,
  56. },
  57. {
  58. namespace: "zcode",
  59. scene: "off-peak-task",
  60. options: {
  61. prompts: {
  62. id: "off-peak-prompts",
  63. type: "prompts",
  64. contents: { cn: "闲时任务模板", en: "Idle-time task template" },
  65. items: [
  66. {
  67. id: "item-ciFlakyReport",
  68. type: "prompts",
  69. contents: { cn: "中文闲时指令", en: "English idle prompt" },
  70. labels: { cn: "CI 报告", en: "CI report" },
  71. on_finish: null,
  72. img: null,
  73. },
  74. ],
  75. refer: "",
  76. templates: { cn: "{prompt}", en: "{prompt}" },
  77. },
  78. },
  79. created_at: 0,
  80. updated_at: 0,
  81. },
  82. ] as ClientSceneConfig[];
  83. }
  84. describe("Client Scenes automation template catalog", () => {
  85. it("maps scheduled-task defaults to cron contents and never keeps cron labels", () => {
  86. const result = mapClientScenesToAutomationTemplates(
  87. createScenes(),
  88. (cronExpr) => cronExpr === "0 9 * * 1-5",
  89. );
  90. expect(result.scheduled).toEqual([
  91. expect.objectContaining({
  92. id: "item-morningDevBrief",
  93. title: { cn: "晨会动态", en: "Morning dev brief" },
  94. prompt: { cn: "中文定时指令", en: "English scheduled prompt" },
  95. cronExpr: "0 9 * * 1-5",
  96. icon: "target",
  97. }),
  98. ]);
  99. expect(JSON.stringify(result.scheduled)).not.toContain("错误的服务端外显");
  100. expect(JSON.stringify(result.scheduled)).not.toContain("Wrong server display");
  101. });
  102. it("keeps prompt item img as the Lucide icon name for both task catalogs", () => {
  103. const scenes = createScenes();
  104. scenes[0]!.options.prompts!.items![0]!.img = " calendar-clock ";
  105. scenes[0]!.options.prompts!.items![0]!.imgs = {
  106. cn: "https://cdn.example.com/scheduled-template-cn.svg",
  107. };
  108. scenes[1]!.options.prompts!.items![0]!.img = "activity";
  109. const result = mapClientScenesToAutomationTemplates(scenes, () => true);
  110. expect(result.scheduled[0]).toEqual(
  111. expect.objectContaining({
  112. iconName: "calendar-clock",
  113. icon: "target",
  114. }),
  115. );
  116. expect(result.offPeak[0]).toEqual(
  117. expect.objectContaining({
  118. iconName: "activity",
  119. icon: "ciFlakyReport",
  120. }),
  121. );
  122. });
  123. it("rejects a missing or invalid cron before the template can enter creation", () => {
  124. const invalid = mapClientScenesToAutomationTemplates(createScenes("invalid cron"), () => false);
  125. const missingReferenceScenes = createScenes();
  126. const scheduledPrompt = missingReferenceScenes[0]!.options.prompts!.items![0]!;
  127. scheduledPrompt.defaults = { cronExpr: ["missing-cron-item"] };
  128. const missing = mapClientScenesToAutomationTemplates(missingReferenceScenes, () => true);
  129. expect(invalid.scheduled).toEqual([]);
  130. expect(invalid.rejectedScheduledTemplateIds).toEqual(["item-morningDevBrief"]);
  131. expect(missing.scheduled).toEqual([]);
  132. expect(missing.rejectedScheduledTemplateIds).toEqual(["item-morningDevBrief"]);
  133. });
  134. it("rejects a service-valid cron that the Automation editor cannot round-trip", () => {
  135. const result = mapClientScenesToAutomationTemplates(createScenes("0 0 9 * * 1-5"), () => true);
  136. expect(result.scheduled).toEqual([]);
  137. expect(result.rejectedScheduledTemplateIds).toEqual(["item-morningDevBrief"]);
  138. });
  139. it("rejects scheduled and idle templates whose localized titles are both blank", () => {
  140. const scenes = createScenes();
  141. scenes[0]!.options.prompts!.items![0]!.labels = { cn: " ", en: "" };
  142. scenes[1]!.options.prompts!.items![0]!.labels = { cn: "", en: " " };
  143. const result = mapClientScenesToAutomationTemplates(scenes, () => true);
  144. expect(result.scheduled).toEqual([]);
  145. expect(result.rejectedScheduledTemplateIds).toEqual(["item-morningDevBrief"]);
  146. expect(result.offPeak).toEqual([]);
  147. });
  148. it("keeps the Automations idle catalog remote-only", () => {
  149. const result = mapClientScenesToAutomationTemplates(createScenes(), () => true);
  150. expect(result.offPeak).toEqual([
  151. expect.objectContaining({
  152. id: "item-ciFlakyReport",
  153. title: { cn: "CI 报告", en: "CI report" },
  154. prompt: { cn: "中文闲时指令", en: "English idle prompt" },
  155. customize: false,
  156. icon: "ciFlakyReport",
  157. }),
  158. ]);
  159. });
  160. it("prefers idle template descs for homepage display while preserving other contents fields", () => {
  161. const scenes = createScenes();
  162. scenes[1]!.options.prompts!.items![0]!.descs = {
  163. cn: "中文闲时描述",
  164. en: "English idle description",
  165. };
  166. const result = mapClientScenesToAutomationTemplates(scenes, () => true);
  167. expect(result.offPeak[0]).toEqual(
  168. expect.objectContaining({
  169. homepageDescription: { cn: "中文闲时描述", en: "English idle description" },
  170. description: { cn: "中文闲时指令", en: "English idle prompt" },
  171. prompt: { cn: "中文闲时指令", en: "English idle prompt" },
  172. }),
  173. );
  174. });
  175. it("falls back from a blank idle desc to contents in the same locale", () => {
  176. const scenes = createScenes();
  177. scenes[1]!.options.prompts!.items![0]!.descs = {
  178. cn: " ",
  179. en: "English idle description",
  180. };
  181. const result = mapClientScenesToAutomationTemplates(scenes, () => true);
  182. expect(result.offPeak[0]?.homepageDescription).toEqual({
  183. cn: "中文闲时指令",
  184. en: "English idle description",
  185. });
  186. expect(result.offPeak[0]?.description).toEqual({
  187. cn: "中文闲时指令",
  188. en: "English idle prompt",
  189. });
  190. });
  191. it("keeps only the first two ordinary remote templates and appends local Customize last", () => {
  192. const remoteOnly = mapClientScenesToAutomationTemplates(createScenes(), () => true).offPeak;
  193. const first = remoteOnly[0]!;
  194. const remoteCustomize = {
  195. ...first,
  196. id: "item-customize",
  197. title: {},
  198. description: {},
  199. prompt: { cn: "", en: "" },
  200. customize: true,
  201. icon: "customize" as const,
  202. };
  203. const second = { ...first, id: "item-second" };
  204. const third = { ...first, id: "item-third" };
  205. expect(
  206. appendLocalCustomizeTemplate([first, remoteCustomize, second, third]).map(({ id }) => id),
  207. ).toEqual(["item-ciFlakyReport", "item-second", "customize"]);
  208. expect(appendLocalCustomizeTemplate([]).map(({ id }) => id)).toEqual(["customize"]);
  209. });
  210. it("resolves the local Customize copy through the active i18n formatter", () => {
  211. const customize = appendLocalCustomizeTemplate([])[0]!;
  212. const messages = {
  213. "offPeak.newTask.template.customize.title": "自定义",
  214. "offPeak.newTask.template.customize.description": "跳过模板,直接告诉它你想做什么。",
  215. };
  216. const formatMessage = ({ id }: { id: string }) => messages[id as keyof typeof messages] ?? id;
  217. expect(resolveOffPeakTemplateText(customize, "title", "zh-CN", formatMessage)).toBe("自定义");
  218. expect(resolveOffPeakTemplateText(customize, "description", "zh-CN", formatMessage)).toBe(
  219. "跳过模板,直接告诉它你想做什么。",
  220. );
  221. expect(customize.title).toEqual({});
  222. expect(customize.description).toEqual({});
  223. });
  224. it("materializes current-locale primitives that are independent from later catalog refreshes", () => {
  225. const first = mapClientScenesToAutomationTemplates(createScenes(), () => true);
  226. const scheduledDraft = materializeScheduledTemplateDraft(first.scheduled[0]!, "zh-CN");
  227. const offPeakDraft = materializeOffPeakTemplateDraft(first.offPeak[0]!, "zh-CN");
  228. const refreshedScenes = createScenes();
  229. refreshedScenes[0]!.options.prompts!.items![0]!.labels.cn = "后台刷新标题";
  230. refreshedScenes[1]!.options.prompts!.items![0]!.contents.cn = "后台刷新指令";
  231. mapClientScenesToAutomationTemplates(refreshedScenes, () => true);
  232. expect(scheduledDraft).toEqual({
  233. templateId: "item-morningDevBrief",
  234. title: "晨会动态",
  235. cronExpr: "0 9 * * 1-5",
  236. prompt: "中文定时指令",
  237. });
  238. expect(offPeakDraft).toEqual({
  239. templateId: "item-ciFlakyReport",
  240. title: "CI 报告",
  241. prompt: "中文闲时指令",
  242. });
  243. });
  244. it("resolves the active locale with a deterministic opposite-language fallback", () => {
  245. expect(resolveAutomationTemplateText({ cn: "中文", en: "English" }, "zh-CN")).toBe("中文");
  246. expect(resolveAutomationTemplateText({ en: "English" }, "zh-CN")).toBe("English");
  247. expect(resolveAutomationTemplateText({ cn: "中文" }, "en-US")).toBe("中文");
  248. });
  249. });