marketplace-listing.test.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. parseEntryExtensionSummary,
  10. parseEntryStoreListing,
  11. } from "../src/plugins/marketplace.js";
  12. // 商店信息(Store Listing)与 featured 策展名单的解析测试:
  13. // 输入形状对齐官方 CDN 目录 schema(description_i18n / displayName_i18n / author 对象等),
  14. // 见 docs/plugin-marketplace-ui-ux.md「数据模型与 schema 扩展」。
  15. describe("marketplace store listing", () => {
  16. const cleanups: string[] = [];
  17. afterEach(async () => {
  18. await Promise.all(cleanups.splice(0).map((dir) => rm(dir, { force: true, recursive: true })));
  19. });
  20. async function makeStorageRoot(prefix: string): Promise<string> {
  21. const dir = await mkdtemp(join(tmpdir(), prefix));
  22. cleanups.push(dir);
  23. return dir;
  24. }
  25. it("parses listing fields and top-level featured from a CDN-shaped manifest", async () => {
  26. const storageRoot = await makeStorageRoot("zcode-marketplace-listing-");
  27. const marketplaceDir = join(storageRoot, "marketplaces", "zcode-plugins-official");
  28. await mkdir(marketplaceDir, { recursive: true });
  29. await writeFile(
  30. join(marketplaceDir, "marketplace.json"),
  31. JSON.stringify({
  32. name: "zcode-plugins-official",
  33. description: "Official ZCode plugins marketplace",
  34. featured: ["example-plugin", " ", 42, "another"],
  35. plugins: [
  36. {
  37. name: "example-plugin",
  38. description: "A minimal template plugin",
  39. description_i18n: { "zh-CN": "一个最小模板插件" },
  40. displayName: "Example Plugin",
  41. displayName_i18n: { "zh-CN": "示例插件" },
  42. version: "0.1.1",
  43. author: { name: "Z.ai", url: "https://z.ai" },
  44. category: "template",
  45. icon: "https://cdn-zcode.z.ai/icons/example.png",
  46. homepage: "https://z.ai/plugins/example",
  47. privacyPolicy: "https://z.ai/privacy",
  48. termsOfService: "https://z.ai/terms",
  49. heroImage: "https://cdn-zcode.z.ai/hero/example.png",
  50. examplePrompts: ["Try the example"],
  51. examplePrompts_i18n: { "zh-CN": ["试试这个示例"] },
  52. requiresPaidPlan: true,
  53. source: {
  54. source: "url",
  55. type: "zip",
  56. url: "https://cdn.example/x.zip",
  57. sha256: "a".repeat(64),
  58. },
  59. },
  60. {
  61. // 无任何商店字段的条目:listing 必须缺省而不是空对象。
  62. name: "bare-plugin",
  63. source: { source: "github", repo: "acme/bare-plugin" },
  64. },
  65. {
  66. // requiresPaidPlan 只认布尔 true:歧义写法按无需套餐处理。
  67. name: "ambiguous-plan-plugin",
  68. requiresPaidPlan: "true",
  69. source: { source: "github", repo: "acme/ambiguous" },
  70. },
  71. ],
  72. }),
  73. );
  74. const manifest = loadMarketplaceManifestSync(storageRoot, "zcode-plugins-official");
  75. expect(manifest).not.toBeNull();
  76. // featured 只保留非空字符串,保持顺序。
  77. expect(manifest?.featured).toEqual(["example-plugin", "another"]);
  78. const example = manifest?.plugins.find((entry) => entry.name === "example-plugin");
  79. expect(example?.listing).toEqual({
  80. displayName: "Example Plugin",
  81. displayNameI18n: { "zh-CN": "示例插件" },
  82. descriptionI18n: { "zh-CN": "一个最小模板插件" },
  83. icon: "https://cdn-zcode.z.ai/icons/example.png",
  84. category: "template",
  85. author: "Z.ai",
  86. authorUrl: "https://z.ai",
  87. homepage: "https://z.ai/plugins/example",
  88. privacyPolicy: "https://z.ai/privacy",
  89. termsOfService: "https://z.ai/terms",
  90. heroImage: "https://cdn-zcode.z.ai/hero/example.png",
  91. examplePrompts: ["Try the example"],
  92. examplePromptsI18n: { "zh-CN": ["试试这个示例"] },
  93. requiresPaidPlan: true,
  94. });
  95. const bare = manifest?.plugins.find((entry) => entry.name === "bare-plugin");
  96. expect(bare?.listing).toBeUndefined();
  97. const ambiguous = manifest?.plugins.find((entry) => entry.name === "ambiguous-plan-plugin");
  98. expect(ambiguous?.listing).toBeUndefined();
  99. });
  100. it("normalizes author from string and object forms", () => {
  101. expect(normalizeAuthorValue("Acme")).toEqual({ name: "Acme" });
  102. expect(normalizeAuthorValue({ name: "Z.ai", url: "https://z.ai" })).toEqual({
  103. name: "Z.ai",
  104. url: "https://z.ai",
  105. });
  106. expect(normalizeAuthorValue(" ")).toBeUndefined();
  107. expect(normalizeAuthorValue(42)).toBeUndefined();
  108. expect(parseEntryStoreListing({ name: "x", author: "Acme" })).toEqual({ author: "Acme" });
  109. });
  110. it("preserves publisher classification independently of usage category", () => {
  111. expect(parseEntryStoreListing({ publisherType: "community", category: "productivity" }))
  112. .toEqual({ publisherType: "community", category: "productivity" });
  113. expect(parseEntryStoreListing({ publisherType: "official" }))
  114. .toEqual({ publisherType: "official" });
  115. expect(parseEntryStoreListing({ publisherType: "personal" })).toBeUndefined();
  116. expect(parseEntryStoreListing({ publisherType: true })).toBeUndefined();
  117. });
  118. // 按登录区域定向展示(docs/plugin-marketplace-ui-ux.md):只保留 cn/global 并去重;
  119. // 未知值忽略不报错,缺省、非数组、过滤后为空都不写,表示全部区域可见。
  120. it("keeps only known store regions and omits the field when nothing is left", () => {
  121. expect(parseEntryStoreListing({ regions: ["cn"] })).toEqual({ regions: ["cn"] });
  122. expect(parseEntryStoreListing({ regions: ["global"] })).toEqual({ regions: ["global"] });
  123. expect(parseEntryStoreListing({ regions: ["global", "cn", "cn"] })).toEqual({
  124. regions: ["cn", "global"],
  125. });
  126. expect(parseEntryStoreListing({ regions: ["cn", "mars", 1, null] })).toEqual({
  127. regions: ["cn"],
  128. });
  129. expect(parseEntryStoreListing({ name: "x" })).toBeUndefined();
  130. expect(parseEntryStoreListing({ regions: [] })).toBeUndefined();
  131. expect(parseEntryStoreListing({ regions: ["mars"] })).toBeUndefined();
  132. expect(parseEntryStoreListing({ regions: "cn" })).toBeUndefined();
  133. expect(parseEntryStoreListing({ regions: { cn: true } })).toBeUndefined();
  134. });
  135. // 扩展摘要(docs/plugins/plugin-publishing.md §4.2–4.3):CLI 只透传,只留 engines.zcode 与字符串 grants,
  136. // 不检查范围格式;它不进 Store Listing,由 overview 按请求参数决定带不带。
  137. it("parses the catalog extension summary and keeps only the two known keys", () => {
  138. expect(
  139. parseEntryExtensionSummary({
  140. extensionSummary: {
  141. engines: { zcode: "^1.4.0", node: ">=20" },
  142. grants: ["sessions.fullAccess", 42, "notifications"],
  143. main: "./dist/extension.js",
  144. },
  145. }),
  146. ).toEqual({ engines: { zcode: "^1.4.0" }, grants: ["sessions.fullAccess", "notifications"] });
  147. // 范围格式不检查:App 不认识的写法由 App 判不兼容。
  148. expect(
  149. parseEntryExtensionSummary({ extensionSummary: { engines: { zcode: ">=1.4.0 <3" } } }),
  150. ).toEqual({ engines: { zcode: ">=1.4.0 <3" }, grants: [] });
  151. expect(parseEntryExtensionSummary({})).toBeUndefined();
  152. expect(parseEntryExtensionSummary({ extensionSummary: "^1.4.0" })).toBeUndefined();
  153. expect(parseEntryExtensionSummary({ extensionSummary: { grants: [] } })).toBeUndefined();
  154. expect(
  155. parseEntryExtensionSummary({ extensionSummary: { engines: { zcode: " " } } }),
  156. ).toBeUndefined();
  157. expect(
  158. parseEntryExtensionSummary({ extensionSummary: { engines: { zcode: 1 } } }),
  159. ).toBeUndefined();
  160. expect(
  161. parseEntryStoreListing({
  162. extensionSummary: { engines: { zcode: "^1.4.0" }, grants: [] },
  163. }),
  164. ).toBeUndefined();
  165. });
  166. it("returns manifest display metadata from describeMarketplacePlugin for installed plugins", async () => {
  167. const storageRoot = await makeStorageRoot("zcode-describe-metadata-");
  168. const pluginRoot = join(storageRoot, "cache", "hello-market", "hello", "1.2.3");
  169. await mkdir(join(pluginRoot, ".zcode-plugin"), { recursive: true });
  170. await writeFile(
  171. join(pluginRoot, ".zcode-plugin", "plugin.json"),
  172. JSON.stringify({
  173. name: "hello",
  174. version: "1.2.3",
  175. description: "hello plugin",
  176. author: { name: "Acme", url: "https://acme.dev" },
  177. homepage: "https://acme.dev/hello",
  178. }),
  179. );
  180. await writeFile(
  181. join(storageRoot, "installed_plugins.json"),
  182. JSON.stringify({
  183. version: 1,
  184. plugins: [
  185. {
  186. id: "hello@hello-market",
  187. name: "hello",
  188. marketplace: "hello-market",
  189. version: "1.2.3",
  190. installPath: pluginRoot,
  191. installedAt: new Date(0).toISOString(),
  192. scope: "user",
  193. },
  194. ],
  195. }),
  196. );
  197. const result = await describeMarketplacePlugin({
  198. marketplace: "hello-market",
  199. name: "hello",
  200. storageRoot,
  201. });
  202. expect(result.metadata).toEqual({
  203. author: "Acme",
  204. authorUrl: "https://acme.dev",
  205. homepage: "https://acme.dev/hello",
  206. version: "1.2.3",
  207. });
  208. });
  209. });