| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- import { mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises";
- import { createServer } from "node:http";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import { setTimeout as delay } from "node:timers/promises";
- import { afterEach, describe, expect, it } from "vitest";
- import {
- writeBundledOfficialMarketplacePartitionSync,
- writeCdnOfficialMarketplacePartitionSync,
- updateMarketplace,
- } from "../src/plugins/index.js";
- describe("ZCode official marketplace partitions", () => {
- const cleanups: string[] = [];
- afterEach(async () => {
- await Promise.all(cleanups.splice(0).map((dir) => rm(dir, { force: true, recursive: true })));
- });
- async function makeStorageRoot(): Promise<string> {
- const storageRoot = await mkdtemp(join(tmpdir(), "zcode-official-marketplace-"));
- cleanups.push(storageRoot);
- return storageRoot;
- }
- it("merges bundled and CDN entries under one marketplace with CDN entries taking precedence", async () => {
- const storageRoot = await makeStorageRoot();
- writeBundledOfficialMarketplacePartitionSync({
- manifest: {
- name: "zcode-plugins-official",
- plugins: [
- { name: "shared", source: "filesystem", version: "1.0.0" },
- { name: "bundled-only", source: "filesystem", version: "1.0.0" },
- ],
- version: 1,
- },
- storageRoot,
- });
- writeCdnOfficialMarketplacePartitionSync({
- manifest: {
- name: "zcode-plugins-official",
- featured: ["remote"],
- plugins: [
- { name: "shared", source: { source: "url", type: "zip" }, version: "9.0.0" },
- { name: "remote", source: { source: "url", type: "zip" }, version: "2.0.0" },
- ],
- },
- storageRoot,
- });
- const merged = JSON.parse(
- await readFile(
- join(storageRoot, "marketplaces", "zcode-plugins-official", "marketplace.json"),
- "utf8",
- ),
- ) as { featured?: string[]; name: string; plugins: Array<Record<string, unknown>> };
- expect(merged.name).toBe("zcode-plugins-official");
- expect(merged.featured).toEqual(["remote"]);
- expect(merged.plugins.map((plugin) => plugin.name)).toEqual([
- "shared",
- "remote",
- "bundled-only",
- ]);
- expect(merged.plugins[0]).toMatchObject({
- source: { source: "url", type: "zip" },
- version: "9.0.0",
- });
- });
- it("preserves the CDN partition when the application reseeds bundled plugins", async () => {
- const storageRoot = await makeStorageRoot();
- writeCdnOfficialMarketplacePartitionSync({
- manifest: {
- name: "zcode-plugins-official",
- plugins: [{ name: "remote", source: { source: "url", type: "zip" } }],
- },
- storageRoot,
- });
- writeBundledOfficialMarketplacePartitionSync({
- manifest: {
- name: "zcode-plugins-official",
- plugins: [{ name: "bundled-v2", source: "sea", version: "2.0.0" }],
- version: 1,
- },
- storageRoot,
- });
- const merged = JSON.parse(
- await readFile(
- join(storageRoot, "marketplaces", "zcode-plugins-official", "marketplace.json"),
- "utf8",
- ),
- ) as { plugins: Array<{ name: string }> };
- expect(merged.plugins.map((plugin) => plugin.name)).toEqual(["remote", "bundled-v2"]);
- });
- it("does not rewrite unchanged official marketplace files", async () => {
- const storageRoot = await makeStorageRoot();
- const bundledManifest = {
- name: "zcode-plugins-official",
- plugins: [{ name: "bundled", source: "filesystem", version: "1.0.0" }],
- };
- const cdnManifest = {
- name: "zcode-plugins-official",
- plugins: [{ name: "remote", source: { source: "url", type: "zip" } }],
- };
- const marketplaceRoot = join(storageRoot, "marketplaces", "zcode-plugins-official");
- writeBundledOfficialMarketplacePartitionSync({
- manifest: bundledManifest,
- storageRoot,
- });
- writeCdnOfficialMarketplacePartitionSync({
- manifest: cdnManifest,
- storageRoot,
- });
- const paths = [
- join(marketplaceRoot, "bundled-marketplace.json"),
- join(marketplaceRoot, "cdn-marketplace.json"),
- join(marketplaceRoot, "marketplace.json"),
- ];
- const firstMtimes = await Promise.all(paths.map(async (path) => (await stat(path)).mtimeMs));
- await delay(30);
- writeBundledOfficialMarketplacePartitionSync({
- manifest: bundledManifest,
- storageRoot,
- });
- writeCdnOfficialMarketplacePartitionSync({
- manifest: cdnManifest,
- storageRoot,
- });
- expect(await Promise.all(paths.map(async (path) => (await stat(path)).mtimeMs))).toEqual(
- firstMtimes,
- );
- });
- it("rejects the retired CDN marketplace id instead of creating a compatibility alias", async () => {
- const storageRoot = await makeStorageRoot();
- expect(() =>
- writeCdnOfficialMarketplacePartitionSync({
- manifest: { name: "zcode-plugins", plugins: [] },
- storageRoot,
- }),
- ).toThrow("Official marketplace manifest must be named zcode-plugins-official");
- });
- it("refreshes the registered official source and returns the merged plugin count", async () => {
- const storageRoot = await makeStorageRoot();
- writeBundledOfficialMarketplacePartitionSync({
- manifest: {
- name: "zcode-plugins-official",
- plugins: [{ name: "bundled", source: "filesystem" }],
- },
- storageRoot,
- });
- const server = createServer((_request, response) => {
- response.writeHead(200, { "content-type": "application/json" });
- response.end(
- JSON.stringify({
- name: "zcode-plugins-official",
- plugins: [{ name: "remote", source: { source: "url", type: "zip" } }],
- }),
- );
- });
- await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
- try {
- const address = server.address();
- if (!address || typeof address === "string") throw new Error("test server address missing");
- await writeFile(
- join(storageRoot, "known_marketplaces.json"),
- JSON.stringify({
- version: 1,
- marketplaces: [
- {
- id: "zcode-plugins-official",
- source: { source: "url", url: `http://127.0.0.1:${address.port}/marketplace.json` },
- name: "zcode-plugins-official",
- addedAt: "2026-01-01T00:00:00.000Z",
- pluginCount: 1,
- },
- ],
- }),
- );
- const [updated] = await updateMarketplace({
- marketplace: "zcode-plugins-official",
- storageRoot,
- });
- expect(updated).toMatchObject({
- id: "zcode-plugins-official",
- pluginCount: 2,
- });
- const merged = JSON.parse(
- await readFile(
- join(storageRoot, "marketplaces", "zcode-plugins-official", "marketplace.json"),
- "utf8",
- ),
- ) as { plugins: Array<{ name: string }> };
- expect(merged.plugins.map((plugin) => plugin.name)).toEqual(["remote", "bundled"]);
- } finally {
- await new Promise<void>((resolve, reject) => {
- server.close((error) => (error ? reject(error) : resolve()));
- });
- }
- });
- });
|