automationTemplateCatalog.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. }),
  97. ]);
  98. expect(JSON.stringify(result.scheduled)).not.toContain("错误的服务端外显");
  99. expect(JSON.stringify(result.scheduled)).not.toContain("Wrong server display");
  100. });
  101. it("keeps prompt item img as the Lucide icon name for both task catalogs", () => {
  102. const scenes = createScenes();
  103. scenes[0]!.options.prompts!.items![0]!.img = " calendar-clock ";
  104. scenes[0]!.options.prompts!.items![0]!.imgs = {
  105. cn: "https://cdn.example.com/scheduled-template-cn.svg",
  106. };
  107. scenes[1]!.options.prompts!.items![0]!.img = "activity";
  108. const result = mapClientScenesToAutomationTemplates(scenes, () => true);
  109. expect(result.scheduled[0]).toEqual(
  110. expect.objectContaining({
  111. iconName: "calendar-clock",
  112. }),
  113. );
  114. expect(result.offPeak[0]).toEqual(
  115. expect.objectContaining({
  116. iconName: "activity",
  117. }),
  118. );
  119. });
  120. it("rejects a missing or invalid cron before the template can enter creation", () => {
  121. const invalid = mapClientScenesToAutomationTemplates(createScenes("invalid cron"), () => false);
  122. const missingReferenceScenes = createScenes();
  123. const scheduledPrompt = missingReferenceScenes[0]!.options.prompts!.items![0]!;
  124. scheduledPrompt.defaults = { cronExpr: ["missing-cron-item"] };
  125. const missing = mapClientScenesToAutomationTemplates(missingReferenceScenes, () => true);
  126. expect(invalid.scheduled).toEqual([]);
  127. expect(invalid.rejectedScheduledTemplateIds).toEqual(["item-morningDevBrief"]);
  128. expect(missing.scheduled).toEqual([]);
  129. expect(missing.rejectedScheduledTemplateIds).toEqual(["item-morningDevBrief"]);
  130. });
  131. it("rejects a service-valid cron that the Automation editor cannot round-trip", () => {
  132. const result = mapClientScenesToAutomationTemplates(createScenes("0 0 9 * * 1-5"), () => true);
  133. expect(result.scheduled).toEqual([]);
  134. expect(result.rejectedScheduledTemplateIds).toEqual(["item-morningDevBrief"]);
  135. });
  136. it("rejects scheduled and idle templates whose localized titles are both blank", () => {
  137. const scenes = createScenes();
  138. scenes[0]!.options.prompts!.items![0]!.labels = { cn: " ", en: "" };
  139. scenes[1]!.options.prompts!.items![0]!.labels = { cn: "", en: " " };
  140. const result = mapClientScenesToAutomationTemplates(scenes, () => true);
  141. expect(result.scheduled).toEqual([]);
  142. expect(result.rejectedScheduledTemplateIds).toEqual(["item-morningDevBrief"]);
  143. expect(result.offPeak).toEqual([]);
  144. });
  145. it("keeps the Automations idle catalog remote-only", () => {
  146. const result = mapClientScenesToAutomationTemplates(createScenes(), () => true);
  147. expect(result.offPeak).toEqual([
  148. expect.objectContaining({
  149. id: "item-ciFlakyReport",
  150. title: { cn: "CI 报告", en: "CI report" },
  151. prompt: { cn: "中文闲时指令", en: "English idle prompt" },
  152. customize: false,
  153. }),
  154. ]);
  155. });
  156. it("prefers idle template descs for homepage display while preserving other contents fields", () => {
  157. const scenes = createScenes();
  158. scenes[1]!.options.prompts!.items![0]!.descs = {
  159. cn: "中文闲时描述",
  160. en: "English idle description",
  161. };
  162. const result = mapClientScenesToAutomationTemplates(scenes, () => true);
  163. expect(result.offPeak[0]).toEqual(
  164. expect.objectContaining({
  165. homepageDescription: { cn: "中文闲时描述", en: "English idle description" },
  166. description: { cn: "中文闲时指令", en: "English idle prompt" },
  167. prompt: { cn: "中文闲时指令", en: "English idle prompt" },
  168. }),
  169. );
  170. });
  171. it("falls back from a blank idle desc to contents in the same locale", () => {
  172. const scenes = createScenes();
  173. scenes[1]!.options.prompts!.items![0]!.descs = {
  174. cn: " ",
  175. en: "English idle description",
  176. };
  177. const result = mapClientScenesToAutomationTemplates(scenes, () => true);
  178. expect(result.offPeak[0]?.homepageDescription).toEqual({
  179. cn: "中文闲时指令",
  180. en: "English idle description",
  181. });
  182. expect(result.offPeak[0]?.description).toEqual({
  183. cn: "中文闲时指令",
  184. en: "English idle prompt",
  185. });
  186. });
  187. it("keeps only the first two ordinary remote templates and appends local Customize last", () => {
  188. const remoteOnly = mapClientScenesToAutomationTemplates(createScenes(), () => true).offPeak;
  189. const first = remoteOnly[0]!;
  190. const remoteCustomize = {
  191. ...first,
  192. id: "item-customize",
  193. title: {},
  194. description: {},
  195. prompt: { cn: "", en: "" },
  196. customize: true,
  197. icon: "customize" as const,
  198. };
  199. const second = { ...first, id: "item-second" };
  200. const third = { ...first, id: "item-third" };
  201. expect(
  202. appendLocalCustomizeTemplate([first, remoteCustomize, second, third]).map(({ id }) => id),
  203. ).toEqual(["item-ciFlakyReport", "item-second", "customize"]);
  204. expect(appendLocalCustomizeTemplate([]).map(({ id }) => id)).toEqual(["customize"]);
  205. });
  206. it("resolves the local Customize copy through the active i18n formatter", () => {
  207. const customize = appendLocalCustomizeTemplate([])[0]!;
  208. const messages = {
  209. "offPeak.newTask.template.customize.title": "自定义",
  210. "offPeak.newTask.template.customize.description": "跳过模板,直接告诉它你想做什么。",
  211. };
  212. const formatMessage = ({ id }: { id: string }) => messages[id as keyof typeof messages] ?? id;
  213. expect(resolveOffPeakTemplateText(customize, "title", "zh-CN", formatMessage)).toBe("自定义");
  214. expect(resolveOffPeakTemplateText(customize, "description", "zh-CN", formatMessage)).toBe(
  215. "跳过模板,直接告诉它你想做什么。",
  216. );
  217. expect(customize.title).toEqual({});
  218. expect(customize.description).toEqual({});
  219. });
  220. it("materializes current-locale primitives that are independent from later catalog refreshes", () => {
  221. const first = mapClientScenesToAutomationTemplates(createScenes(), () => true);
  222. const scheduledDraft = materializeScheduledTemplateDraft(first.scheduled[0]!, "zh-CN");
  223. const offPeakDraft = materializeOffPeakTemplateDraft(first.offPeak[0]!, "zh-CN");
  224. const refreshedScenes = createScenes();
  225. refreshedScenes[0]!.options.prompts!.items![0]!.labels.cn = "后台刷新标题";
  226. refreshedScenes[1]!.options.prompts!.items![0]!.contents.cn = "后台刷新指令";
  227. mapClientScenesToAutomationTemplates(refreshedScenes, () => true);
  228. expect(scheduledDraft).toEqual({
  229. templateId: "item-morningDevBrief",
  230. title: "晨会动态",
  231. cronExpr: "0 9 * * 1-5",
  232. prompt: "中文定时指令",
  233. });
  234. expect(offPeakDraft).toEqual({
  235. templateId: "item-ciFlakyReport",
  236. title: "CI 报告",
  237. prompt: "中文闲时指令",
  238. });
  239. });
  240. it("resolves the active locale with a deterministic opposite-language fallback", () => {
  241. expect(resolveAutomationTemplateText({ cn: "中文", en: "English" }, "zh-CN")).toBe("中文");
  242. expect(resolveAutomationTemplateText({ en: "English" }, "zh-CN")).toBe("English");
  243. expect(resolveAutomationTemplateText({ cn: "中文" }, "en-US")).toBe("中文");
  244. });
  245. });