draftSuggestedPromptPrefill.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import type { ZCodePluginReferenceCatalogEntry } from "@zcode/shared";
  2. import { describe, expect, it, vi } from "vitest";
  3. import { buildDraftSuggestedPromptPrefill } from "@/v4/draftSuggestedPromptPrefill.js";
  4. const DOCUMENT_SKILLS_PLUGIN = {
  5. stableId: "document-skills@zcode-plugins-official",
  6. label: "文档技能",
  7. };
  8. function catalogEntry(
  9. overrides: Partial<ZCodePluginReferenceCatalogEntry> = {},
  10. ): ZCodePluginReferenceCatalogEntry {
  11. return {
  12. pluginId: DOCUMENT_SKILLS_PLUGIN.stableId,
  13. name: "document-skills",
  14. marketplace: "zcode-plugins-official",
  15. enabled: true,
  16. conflictingPluginIds: [],
  17. skillQualifiedNames: ["document-skills:pdf"],
  18. mcpServerNames: [],
  19. subagentNames: [],
  20. icon: "https://cdn.example.com/document-skills.png",
  21. ...overrides,
  22. };
  23. }
  24. describe("buildDraftSuggestedPromptPrefill", () => {
  25. it("returns a plain replacement prompt without querying the catalog when no Plugin is configured", async () => {
  26. const loadPluginCatalog = vi.fn();
  27. await expect(
  28. buildDraftSuggestedPromptPrefill({
  29. prompt: "检查当前工作区近 7 天的 Git commit",
  30. loadPluginCatalog,
  31. }),
  32. ).resolves.toEqual({ text: "检查当前工作区近 7 天的 Git commit" });
  33. expect(loadPluginCatalog).not.toHaveBeenCalled();
  34. });
  35. it("builds canonical text and a structured mention for an enabled conflict-free Plugin", async () => {
  36. await expect(
  37. buildDraftSuggestedPromptPrefill({
  38. prompt: "根据当前工作区内容制作一份 PDF 文档",
  39. plugin: DOCUMENT_SKILLS_PLUGIN,
  40. loadPluginCatalog: async () => [catalogEntry()],
  41. }),
  42. ).resolves.toEqual({
  43. text: "[@文档技能](plugin://document-skills@zcode-plugins-official) 根据当前工作区内容制作一份 PDF 文档",
  44. mention: {
  45. id: "plugin:document-skills@zcode-plugins-official",
  46. category: "plugins",
  47. label: "文档技能",
  48. value: "document-skills@zcode-plugins-official",
  49. markdown:
  50. "[@文档技能](plugin://document-skills@zcode-plugins-official)",
  51. data: {
  52. pluginId: "document-skills@zcode-plugins-official",
  53. icon: "https://cdn.example.com/document-skills.png",
  54. },
  55. },
  56. });
  57. });
  58. it.each([
  59. ["missing", []],
  60. ["disabled", [catalogEntry({ enabled: false })]],
  61. ])(
  62. "marks the Plugin unavailable while degrading to the plain prompt when it is %s",
  63. async (_state, entries) => {
  64. await expect(
  65. buildDraftSuggestedPromptPrefill({
  66. prompt: "制作一份 PDF",
  67. plugin: DOCUMENT_SKILLS_PLUGIN,
  68. loadPluginCatalog: async () => entries,
  69. }),
  70. ).resolves.toEqual({
  71. text: "制作一份 PDF",
  72. pluginUnavailable: true,
  73. });
  74. },
  75. );
  76. it("degrades a conflicted Plugin without marking it missing or disabled", async () => {
  77. await expect(
  78. buildDraftSuggestedPromptPrefill({
  79. prompt: "制作一份 PDF",
  80. plugin: DOCUMENT_SKILLS_PLUGIN,
  81. loadPluginCatalog: async () => [
  82. catalogEntry({
  83. conflictingPluginIds: ["document-skills@another-market"],
  84. }),
  85. ],
  86. }),
  87. ).resolves.toEqual({ text: "制作一份 PDF" });
  88. });
  89. it("degrades to the plain prompt when catalog lookup fails", async () => {
  90. await expect(
  91. buildDraftSuggestedPromptPrefill({
  92. prompt: "制作一份 PDF",
  93. plugin: DOCUMENT_SKILLS_PLUGIN,
  94. loadPluginCatalog: async () =>
  95. Promise.reject(new Error("remote disconnected")),
  96. }),
  97. ).resolves.toEqual({ text: "制作一份 PDF" });
  98. });
  99. });