dev-marketplace.test.mjs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { test } from "node:test";
  2. import assert from "node:assert/strict";
  3. import { mkdtemp, mkdir, readFile, writeFile, rm, symlink } from "node:fs/promises";
  4. import { join } from "node:path";
  5. import { tmpdir } from "node:os";
  6. import { createPlugin } from "../skills/plugin-creator/scripts/create-basic-plugin.mjs";
  7. import { upsertDevMarketplace } from "../skills/plugin-creator/scripts/upsert-dev-marketplace.mjs";
  8. import { atomicJson } from "../skills/plugin-creator/scripts/marketplace-files.mjs";
  9. test("does not delete an existing temporary file when exclusive creation fails", async () => {
  10. const root = await mkdtemp(join(tmpdir(), "creator-atomic-"));
  11. try {
  12. const path = join(root, "marketplace.json");
  13. const temporary = `${path}.${process.pid}.tmp`;
  14. await writeFile(temporary, "existing writer");
  15. await assert.rejects(atomicJson(path, { name: "dev", plugins: [] }), /exist/i);
  16. assert.equal(await readFile(temporary, "utf8"), "existing writer");
  17. } finally {
  18. await rm(root, { recursive: true, force: true });
  19. }
  20. });
  21. test("dev listing is stable, localized, idempotent and preserves source and other entries", async () => {
  22. const root = await mkdtemp(join(tmpdir(), "creator-dev-"));
  23. try {
  24. const plugin = await createPlugin({
  25. name: "demo",
  26. parentPath: join(root, "plugins"),
  27. components: ["skills"],
  28. });
  29. const source = await readFile(join(plugin, "skills/demo/SKILL.md"), "utf8");
  30. const first = await upsertDevMarketplace({
  31. pluginPath: plugin,
  32. displayName: "Demo",
  33. nameZh: "演示",
  34. descriptionZh: "演示工作流",
  35. });
  36. assert.match(first.marketplaceId, /^dev-creator-dev-.*-[a-f0-9]{8}$/);
  37. assert.equal(first.pluginId, `demo@${first.marketplaceId}`);
  38. assert.equal(first.marketplacePath, join(first.marketplaceRoot, "marketplace.json"));
  39. const market = JSON.parse(await readFile(first.marketplacePath, "utf8"));
  40. assert.equal(market.plugins[0].source, "./demo");
  41. assert.equal(market.plugins[0].displayName_i18n["zh-CN"], "演示");
  42. market.owner = { name: "Test" };
  43. market.plugins.unshift({ name: "other", source: "./other", custom: true });
  44. market.plugins[1].custom = 42;
  45. await writeFile(first.marketplacePath, JSON.stringify(market));
  46. const manifestPath = join(plugin, ".zcode-plugin/plugin.json");
  47. const manifest = JSON.parse(await readFile(manifestPath, "utf8"));
  48. manifest.version = "0.1.1";
  49. await writeFile(manifestPath, JSON.stringify(manifest));
  50. const second = await upsertDevMarketplace({ pluginPath: plugin });
  51. assert.equal(second.marketplaceId, first.marketplaceId);
  52. const updated = JSON.parse(await readFile(first.marketplacePath, "utf8"));
  53. assert.deepEqual(
  54. updated.plugins.map((p) => p.name),
  55. ["other", "demo"],
  56. );
  57. assert.equal(updated.owner.name, "Test");
  58. assert.equal(updated.plugins[1].custom, 42);
  59. assert.equal(updated.plugins[1].version, "0.1.1");
  60. assert.equal(updated.plugins[1].displayName_i18n["zh-CN"], "演示");
  61. assert.equal(await readFile(join(plugin, "skills/demo/SKILL.md"), "utf8"), source);
  62. assert.equal((await upsertDevMarketplace({ pluginPath: plugin })).changed, false);
  63. } finally {
  64. await rm(root, { recursive: true, force: true });
  65. }
  66. });
  67. test("same-named workspaces get distinct dev markets", async () => {
  68. const root = await mkdtemp(join(tmpdir(), "creator-dev-identity-"));
  69. try {
  70. const markets = [];
  71. for (const name of ["one", "two"]) {
  72. const plugin = await createPlugin({
  73. name: "demo",
  74. parentPath: join(root, name, "project", "plugins"),
  75. });
  76. markets.push(await upsertDevMarketplace({ pluginPath: plugin }));
  77. }
  78. assert.notEqual(markets[0].marketplaceId, markets[1].marketplaceId);
  79. assert.match(markets[0].marketplaceId, /^dev-project-/);
  80. } finally {
  81. await rm(root, { recursive: true, force: true });
  82. }
  83. });
  84. test("refuses implicit takeover, duplicate entries, source conflicts, escaping and locked markets", async () => {
  85. const root = await mkdtemp(join(tmpdir(), "creator-dev-conflict-"));
  86. try {
  87. const plugin = await createPlugin({ name: "demo", parentPath: join(root, "plugins") });
  88. const marketplacePath = join(root, "plugins", "marketplace.json");
  89. await writeFile(marketplacePath, JSON.stringify({ name: "production", plugins: [] }));
  90. await assert.rejects(upsertDevMarketplace({ pluginPath: plugin }), /explicit|development/i);
  91. const result = await upsertDevMarketplace({ pluginPath: plugin, marketplacePath });
  92. assert.equal(result.marketplaceId, "production");
  93. for (const plugins of [
  94. [{ name: "demo", source: "./other" }],
  95. [
  96. { name: "demo", source: "./demo" },
  97. { name: "demo", source: "./demo" },
  98. ],
  99. ]) {
  100. const content = JSON.stringify({ name: "production", plugins });
  101. await writeFile(marketplacePath, content);
  102. await assert.rejects(
  103. upsertDevMarketplace({ pluginPath: plugin, marketplacePath }),
  104. /source|duplicate/i,
  105. );
  106. assert.equal(await readFile(marketplacePath, "utf8"), content);
  107. }
  108. await assert.rejects(
  109. upsertDevMarketplace({
  110. pluginPath: plugin,
  111. marketplacePath: join(root, "elsewhere", "marketplace.json"),
  112. }),
  113. /outside/i,
  114. );
  115. await writeFile(marketplacePath, JSON.stringify({ name: "production", plugins: [] }));
  116. await writeFile(`${marketplacePath}.lock`, "busy");
  117. await assert.rejects(
  118. upsertDevMarketplace({ pluginPath: plugin, marketplacePath }),
  119. /busy|lock/i,
  120. );
  121. assert.equal(await readFile(`${marketplacePath}.lock`, "utf8"), "busy");
  122. if (process.platform !== "win32") {
  123. await mkdir(join(root, "linked-market"));
  124. await symlink(marketplacePath, join(root, "linked-market", "marketplace.json"));
  125. await assert.rejects(
  126. upsertDevMarketplace({
  127. pluginPath: plugin,
  128. marketplacePath: join(root, "linked-market", "marketplace.json"),
  129. }),
  130. );
  131. }
  132. } finally {
  133. await rm(root, { recursive: true, force: true });
  134. }
  135. });