official-marketplace.test.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import { mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises";
  2. import { createServer } from "node:http";
  3. import { tmpdir } from "node:os";
  4. import { join } from "node:path";
  5. import { setTimeout as delay } from "node:timers/promises";
  6. import { afterEach, describe, expect, it } from "vitest";
  7. import {
  8. writeBundledOfficialMarketplacePartitionSync,
  9. writeCdnOfficialMarketplacePartitionSync,
  10. updateMarketplace,
  11. } from "../src/plugins/index.js";
  12. describe("ZCode official marketplace partitions", () => {
  13. const cleanups: string[] = [];
  14. afterEach(async () => {
  15. await Promise.all(cleanups.splice(0).map((dir) => rm(dir, { force: true, recursive: true })));
  16. });
  17. async function makeStorageRoot(): Promise<string> {
  18. const storageRoot = await mkdtemp(join(tmpdir(), "zcode-official-marketplace-"));
  19. cleanups.push(storageRoot);
  20. return storageRoot;
  21. }
  22. it("merges bundled and CDN entries under one marketplace with CDN entries taking precedence", async () => {
  23. const storageRoot = await makeStorageRoot();
  24. writeBundledOfficialMarketplacePartitionSync({
  25. manifest: {
  26. name: "zcode-plugins-official",
  27. plugins: [
  28. { name: "shared", source: "filesystem", version: "1.0.0" },
  29. { name: "bundled-only", source: "filesystem", version: "1.0.0" },
  30. ],
  31. version: 1,
  32. },
  33. storageRoot,
  34. });
  35. writeCdnOfficialMarketplacePartitionSync({
  36. manifest: {
  37. name: "zcode-plugins-official",
  38. featured: ["remote"],
  39. plugins: [
  40. { name: "shared", source: { source: "url", type: "zip" }, version: "9.0.0" },
  41. { name: "remote", source: { source: "url", type: "zip" }, version: "2.0.0" },
  42. ],
  43. },
  44. storageRoot,
  45. });
  46. const merged = JSON.parse(
  47. await readFile(
  48. join(storageRoot, "marketplaces", "zcode-plugins-official", "marketplace.json"),
  49. "utf8",
  50. ),
  51. ) as { featured?: string[]; name: string; plugins: Array<Record<string, unknown>> };
  52. expect(merged.name).toBe("zcode-plugins-official");
  53. expect(merged.featured).toEqual(["remote"]);
  54. expect(merged.plugins.map((plugin) => plugin.name)).toEqual([
  55. "shared",
  56. "remote",
  57. "bundled-only",
  58. ]);
  59. expect(merged.plugins[0]).toMatchObject({
  60. source: { source: "url", type: "zip" },
  61. version: "9.0.0",
  62. });
  63. });
  64. it("preserves the CDN partition when the application reseeds bundled plugins", async () => {
  65. const storageRoot = await makeStorageRoot();
  66. writeCdnOfficialMarketplacePartitionSync({
  67. manifest: {
  68. name: "zcode-plugins-official",
  69. plugins: [{ name: "remote", source: { source: "url", type: "zip" } }],
  70. },
  71. storageRoot,
  72. });
  73. writeBundledOfficialMarketplacePartitionSync({
  74. manifest: {
  75. name: "zcode-plugins-official",
  76. plugins: [{ name: "bundled-v2", source: "sea", version: "2.0.0" }],
  77. version: 1,
  78. },
  79. storageRoot,
  80. });
  81. const merged = JSON.parse(
  82. await readFile(
  83. join(storageRoot, "marketplaces", "zcode-plugins-official", "marketplace.json"),
  84. "utf8",
  85. ),
  86. ) as { plugins: Array<{ name: string }> };
  87. expect(merged.plugins.map((plugin) => plugin.name)).toEqual(["remote", "bundled-v2"]);
  88. });
  89. it("does not rewrite unchanged official marketplace files", async () => {
  90. const storageRoot = await makeStorageRoot();
  91. const bundledManifest = {
  92. name: "zcode-plugins-official",
  93. plugins: [{ name: "bundled", source: "filesystem", version: "1.0.0" }],
  94. };
  95. const cdnManifest = {
  96. name: "zcode-plugins-official",
  97. plugins: [{ name: "remote", source: { source: "url", type: "zip" } }],
  98. };
  99. const marketplaceRoot = join(storageRoot, "marketplaces", "zcode-plugins-official");
  100. writeBundledOfficialMarketplacePartitionSync({
  101. manifest: bundledManifest,
  102. storageRoot,
  103. });
  104. writeCdnOfficialMarketplacePartitionSync({
  105. manifest: cdnManifest,
  106. storageRoot,
  107. });
  108. const paths = [
  109. join(marketplaceRoot, "bundled-marketplace.json"),
  110. join(marketplaceRoot, "cdn-marketplace.json"),
  111. join(marketplaceRoot, "marketplace.json"),
  112. ];
  113. const firstMtimes = await Promise.all(paths.map(async (path) => (await stat(path)).mtimeMs));
  114. await delay(30);
  115. writeBundledOfficialMarketplacePartitionSync({
  116. manifest: bundledManifest,
  117. storageRoot,
  118. });
  119. writeCdnOfficialMarketplacePartitionSync({
  120. manifest: cdnManifest,
  121. storageRoot,
  122. });
  123. expect(await Promise.all(paths.map(async (path) => (await stat(path)).mtimeMs))).toEqual(
  124. firstMtimes,
  125. );
  126. });
  127. it("rejects the retired CDN marketplace id instead of creating a compatibility alias", async () => {
  128. const storageRoot = await makeStorageRoot();
  129. expect(() =>
  130. writeCdnOfficialMarketplacePartitionSync({
  131. manifest: { name: "zcode-plugins", plugins: [] },
  132. storageRoot,
  133. }),
  134. ).toThrow("Official marketplace manifest must be named zcode-plugins-official");
  135. });
  136. it("refreshes the registered official source and returns the merged plugin count", async () => {
  137. const storageRoot = await makeStorageRoot();
  138. writeBundledOfficialMarketplacePartitionSync({
  139. manifest: {
  140. name: "zcode-plugins-official",
  141. plugins: [{ name: "bundled", source: "filesystem" }],
  142. },
  143. storageRoot,
  144. });
  145. const server = createServer((_request, response) => {
  146. response.writeHead(200, { "content-type": "application/json" });
  147. response.end(
  148. JSON.stringify({
  149. name: "zcode-plugins-official",
  150. plugins: [{ name: "remote", source: { source: "url", type: "zip" } }],
  151. }),
  152. );
  153. });
  154. await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
  155. try {
  156. const address = server.address();
  157. if (!address || typeof address === "string") throw new Error("test server address missing");
  158. await writeFile(
  159. join(storageRoot, "known_marketplaces.json"),
  160. JSON.stringify({
  161. version: 1,
  162. marketplaces: [
  163. {
  164. id: "zcode-plugins-official",
  165. source: { source: "url", url: `http://127.0.0.1:${address.port}/marketplace.json` },
  166. name: "zcode-plugins-official",
  167. addedAt: "2026-01-01T00:00:00.000Z",
  168. pluginCount: 1,
  169. },
  170. ],
  171. }),
  172. );
  173. const [updated] = await updateMarketplace({
  174. marketplace: "zcode-plugins-official",
  175. storageRoot,
  176. });
  177. expect(updated).toMatchObject({
  178. id: "zcode-plugins-official",
  179. pluginCount: 2,
  180. });
  181. const merged = JSON.parse(
  182. await readFile(
  183. join(storageRoot, "marketplaces", "zcode-plugins-official", "marketplace.json"),
  184. "utf8",
  185. ),
  186. ) as { plugins: Array<{ name: string }> };
  187. expect(merged.plugins.map((plugin) => plugin.name)).toEqual(["remote", "bundled"]);
  188. } finally {
  189. await new Promise<void>((resolve, reject) => {
  190. server.close((error) => (error ? reject(error) : resolve()));
  191. });
  192. }
  193. });
  194. });