marketplace-listing.test.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterEach, describe, expect, it } from "vitest";
  5. import { describeMarketplacePlugin } from "../src/plugins/index.js";
  6. import {
  7. loadMarketplaceManifestSync,
  8. normalizeAuthorValue,
  9. parseEntryStoreListing,
  10. } from "../src/plugins/marketplace.js";
  11. // 商店信息(Store Listing)与 featured 策展名单的解析测试:
  12. // 输入形状对齐官方 CDN 目录 schema(description_i18n / displayName_i18n / author 对象等),
  13. // 见 docs/plugin-marketplace-ui-ux.md「数据模型与 schema 扩展」。
  14. describe("marketplace store listing", () => {
  15. const cleanups: string[] = [];
  16. afterEach(async () => {
  17. await Promise.all(cleanups.splice(0).map((dir) => rm(dir, { force: true, recursive: true })));
  18. });
  19. async function makeStorageRoot(prefix: string): Promise<string> {
  20. const dir = await mkdtemp(join(tmpdir(), prefix));
  21. cleanups.push(dir);
  22. return dir;
  23. }
  24. it("parses listing fields and top-level featured from a CDN-shaped manifest", async () => {
  25. const storageRoot = await makeStorageRoot("zcode-marketplace-listing-");
  26. const marketplaceDir = join(storageRoot, "marketplaces", "zcode-plugins-official");
  27. await mkdir(marketplaceDir, { recursive: true });
  28. await writeFile(
  29. join(marketplaceDir, "marketplace.json"),
  30. JSON.stringify({
  31. name: "zcode-plugins-official",
  32. description: "Official ZCode plugins marketplace",
  33. featured: ["example-plugin", " ", 42, "another"],
  34. plugins: [
  35. {
  36. name: "example-plugin",
  37. description: "A minimal template plugin",
  38. description_i18n: { "zh-CN": "一个最小模板插件" },
  39. displayName: "Example Plugin",
  40. displayName_i18n: { "zh-CN": "示例插件" },
  41. version: "0.1.1",
  42. author: { name: "Z.ai", url: "https://z.ai" },
  43. category: "template",
  44. icon: "https://cdn-zcode.z.ai/icons/example.png",
  45. homepage: "https://z.ai/plugins/example",
  46. privacyPolicy: "https://z.ai/privacy",
  47. termsOfService: "https://z.ai/terms",
  48. heroImage: "https://cdn-zcode.z.ai/hero/example.png",
  49. examplePrompts: ["Try the example"],
  50. examplePrompts_i18n: { "zh-CN": ["试试这个示例"] },
  51. requiresPaidPlan: true,
  52. source: {
  53. source: "url",
  54. type: "zip",
  55. url: "https://cdn.example/x.zip",
  56. sha256: "a".repeat(64),
  57. },
  58. },
  59. {
  60. // 无任何商店字段的条目:listing 必须缺省而不是空对象。
  61. name: "bare-plugin",
  62. source: { source: "github", repo: "acme/bare-plugin" },
  63. },
  64. {
  65. // requiresPaidPlan 只认布尔 true:歧义写法按无需套餐处理。
  66. name: "ambiguous-plan-plugin",
  67. requiresPaidPlan: "true",
  68. source: { source: "github", repo: "acme/ambiguous" },
  69. },
  70. ],
  71. }),
  72. );
  73. const manifest = loadMarketplaceManifestSync(storageRoot, "zcode-plugins-official");
  74. expect(manifest).not.toBeNull();
  75. // featured 只保留非空字符串,保持顺序。
  76. expect(manifest?.featured).toEqual(["example-plugin", "another"]);
  77. const example = manifest?.plugins.find((entry) => entry.name === "example-plugin");
  78. expect(example?.listing).toEqual({
  79. displayName: "Example Plugin",
  80. displayNameI18n: { "zh-CN": "示例插件" },
  81. descriptionI18n: { "zh-CN": "一个最小模板插件" },
  82. icon: "https://cdn-zcode.z.ai/icons/example.png",
  83. category: "template",
  84. author: "Z.ai",
  85. authorUrl: "https://z.ai",
  86. homepage: "https://z.ai/plugins/example",
  87. privacyPolicy: "https://z.ai/privacy",
  88. termsOfService: "https://z.ai/terms",
  89. heroImage: "https://cdn-zcode.z.ai/hero/example.png",
  90. examplePrompts: ["Try the example"],
  91. examplePromptsI18n: { "zh-CN": ["试试这个示例"] },
  92. requiresPaidPlan: true,
  93. });
  94. const bare = manifest?.plugins.find((entry) => entry.name === "bare-plugin");
  95. expect(bare?.listing).toBeUndefined();
  96. const ambiguous = manifest?.plugins.find((entry) => entry.name === "ambiguous-plan-plugin");
  97. expect(ambiguous?.listing).toBeUndefined();
  98. });
  99. it("normalizes author from string and object forms", () => {
  100. expect(normalizeAuthorValue("Acme")).toEqual({ name: "Acme" });
  101. expect(normalizeAuthorValue({ name: "Z.ai", url: "https://z.ai" })).toEqual({
  102. name: "Z.ai",
  103. url: "https://z.ai",
  104. });
  105. expect(normalizeAuthorValue(" ")).toBeUndefined();
  106. expect(normalizeAuthorValue(42)).toBeUndefined();
  107. expect(parseEntryStoreListing({ name: "x", author: "Acme" })).toEqual({ author: "Acme" });
  108. });
  109. it("returns manifest display metadata from describeMarketplacePlugin for installed plugins", async () => {
  110. const storageRoot = await makeStorageRoot("zcode-describe-metadata-");
  111. const pluginRoot = join(storageRoot, "cache", "hello-market", "hello", "1.2.3");
  112. await mkdir(join(pluginRoot, ".zcode-plugin"), { recursive: true });
  113. await writeFile(
  114. join(pluginRoot, ".zcode-plugin", "plugin.json"),
  115. JSON.stringify({
  116. name: "hello",
  117. version: "1.2.3",
  118. description: "hello plugin",
  119. author: { name: "Acme", url: "https://acme.dev" },
  120. homepage: "https://acme.dev/hello",
  121. }),
  122. );
  123. await writeFile(
  124. join(storageRoot, "installed_plugins.json"),
  125. JSON.stringify({
  126. version: 1,
  127. plugins: [
  128. {
  129. id: "hello@hello-market",
  130. name: "hello",
  131. marketplace: "hello-market",
  132. version: "1.2.3",
  133. installPath: pluginRoot,
  134. installedAt: new Date(0).toISOString(),
  135. scope: "user",
  136. },
  137. ],
  138. }),
  139. );
  140. const result = await describeMarketplacePlugin({
  141. marketplace: "hello-market",
  142. name: "hello",
  143. storageRoot,
  144. });
  145. expect(result.metadata).toEqual({
  146. author: "Acme",
  147. authorUrl: "https://acme.dev",
  148. homepage: "https://acme.dev/hello",
  149. version: "1.2.3",
  150. });
  151. });
  152. });