draftSuggestedPromptItems.test.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import type { ClientSceneConfig, ClientSceneItem } from "@zcode/services";
  2. import { describe, expect, it } from "vitest";
  3. import {
  4. mapClientScenesToDraftSuggestedPromptItems,
  5. resolveDraftSuggestedPromptText,
  6. } from "@/v4/draftSuggestedPromptItems.js";
  7. function createItem(
  8. overrides: Partial<ClientSceneItem> & Pick<ClientSceneItem, "id">,
  9. ): ClientSceneItem {
  10. return {
  11. id: overrides.id,
  12. type: overrides.type ?? "prompt",
  13. contents: overrides.contents ?? { cn: "中文 prompt", en: "English prompt" },
  14. descs: overrides.descs ?? { cn: "", en: "" },
  15. labels: overrides.labels ?? { cn: "中文标签", en: "English label" },
  16. on_finish: overrides.on_finish ?? null,
  17. img: overrides.img ?? null,
  18. share_urls: overrides.share_urls ?? {},
  19. ...overrides,
  20. };
  21. }
  22. function createScene(promptItems: ClientSceneItem[]): ClientSceneConfig {
  23. return {
  24. namespace: "zcode",
  25. scene: "draft-suggestion",
  26. options: {
  27. prompts: {
  28. id: "prompt",
  29. type: "prompt",
  30. contents: { cn: "提示词", en: "Prompt" },
  31. prompts: { cn: "", en: "" },
  32. items: promptItems,
  33. refer: "",
  34. cascades: {},
  35. },
  36. plugin: {
  37. id: "plugin",
  38. type: "plugin",
  39. contents: { cn: "插件", en: "Plugin" },
  40. prompts: { cn: "", en: "" },
  41. items: [
  42. createItem({
  43. id: "document-skills",
  44. type: "plugin",
  45. contents: {
  46. cn: "document-skills@zcode-plugins-official",
  47. en: "document-skills@zcode-plugins-official",
  48. },
  49. labels: { cn: "文档技能", en: "Document skills" },
  50. }),
  51. ],
  52. refer: "",
  53. cascades: {},
  54. },
  55. },
  56. created_at: 1,
  57. updated_at: 2,
  58. };
  59. }
  60. describe("draft suggested prompt Client Scenes mapping", () => {
  61. it("maps draft-suggestion prompt items and reverses defaults into one Plugin reference", () => {
  62. const prompt = createItem({
  63. id: "create-pdf",
  64. img: "file-text",
  65. imgs: {
  66. cn: "https://cdn.example.com/create-pdf-cn.svg",
  67. en: "https://cdn.example.com/create-pdf-en.svg",
  68. },
  69. contents: {
  70. cn: "根据工作区制作 PDF",
  71. en: "Create a PDF from the workspace",
  72. },
  73. labels: { cn: "制作 PDF", en: "Create PDF" },
  74. defaults: { plugin: ["document-skills"] },
  75. });
  76. expect(
  77. mapClientScenesToDraftSuggestedPromptItems([
  78. { ...createScene([]), scene: "ai-slides" },
  79. createScene([prompt]),
  80. ]),
  81. ).toEqual([
  82. {
  83. id: "create-pdf",
  84. iconName: "file-text",
  85. label: { cn: "制作 PDF", en: "Create PDF" },
  86. prompt: {
  87. cn: "根据工作区制作 PDF",
  88. en: "Create a PDF from the workspace",
  89. },
  90. plugin: {
  91. stableId: "document-skills@zcode-plugins-official",
  92. label: { cn: "文档技能", en: "Document skills" },
  93. },
  94. },
  95. ]);
  96. });
  97. it("maps the Lucide icon name only from img and ignores localized imgs", () => {
  98. const withoutImg = createItem({
  99. id: "without-img",
  100. img: null,
  101. imgs: {
  102. cn: "https://cdn.example.com/ignored-cn.svg",
  103. en: "https://cdn.example.com/ignored-en.svg",
  104. },
  105. });
  106. expect(mapClientScenesToDraftSuggestedPromptItems([createScene([withoutImg])])).toEqual([
  107. {
  108. id: "without-img",
  109. label: withoutImg.labels,
  110. prompt: withoutImg.contents,
  111. },
  112. ]);
  113. });
  114. it("splits, trims, deduplicates, and allowlists on_finish actions", () => {
  115. const navigationPrompt = createItem({
  116. id: "open-automations",
  117. on_finish:
  118. " UNKNOWN:ACTION, NAVIGATE:AUTOMATIONS, NAVIGATE:AUTOMATIONS:OFFPEAK, NAVIGATE:AUTOMATIONS , NAVIGATE:AUTOMATIONS:OFFPEAK ",
  119. });
  120. expect(mapClientScenesToDraftSuggestedPromptItems([createScene([navigationPrompt])])).toEqual([
  121. {
  122. id: "open-automations",
  123. label: navigationPrompt.labels,
  124. prompt: navigationPrompt.contents,
  125. actions: ["NAVIGATE:AUTOMATIONS", "NAVIGATE:AUTOMATIONS:OFFPEAK"],
  126. },
  127. ]);
  128. });
  129. it("keeps a prompt without Plugin when defaults cannot be resolved", () => {
  130. const prompt = createItem({
  131. id: "plain",
  132. defaults: { missingOption: ["missing-item"], plugin: ["also-missing"] },
  133. });
  134. expect(mapClientScenesToDraftSuggestedPromptItems([createScene([prompt])])).toEqual([
  135. {
  136. id: "plain",
  137. label: prompt.labels,
  138. prompt: prompt.contents,
  139. },
  140. ]);
  141. });
  142. it("returns no recommendations without draft-suggestion.options.prompts", () => {
  143. expect(mapClientScenesToDraftSuggestedPromptItems([])).toEqual([]);
  144. expect(
  145. mapClientScenesToDraftSuggestedPromptItems([{ ...createScene([]), scene: "ai-writing" }]),
  146. ).toEqual([]);
  147. expect(
  148. mapClientScenesToDraftSuggestedPromptItems([
  149. { ...createScene([]), options: {}, scene: "draft-suggestion" },
  150. ]),
  151. ).toEqual([]);
  152. });
  153. it("resolves cn/en directly and falls back to the other available language", () => {
  154. expect(resolveDraftSuggestedPromptText({ cn: "中文", en: "English" }, "zh-CN")).toBe("中文");
  155. expect(resolveDraftSuggestedPromptText({ cn: "中文", en: "English" }, "en-US")).toBe("English");
  156. expect(resolveDraftSuggestedPromptText({ cn: "中文", en: "" }, "en-US")).toBe("中文");
  157. });
  158. });