pluginSyncService.test.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. import { existsSync, mkdtempSync, symlinkSync } from "node:fs";
  2. import { chmod, mkdir, readFile, realpath, stat, writeFile } from "node:fs/promises";
  3. import { tmpdir } from "node:os";
  4. import { join, resolve } from "node:path";
  5. import { afterEach, beforeEach, describe, expect, it } from "vitest";
  6. import { createPluginSyncService } from "../src/plugin-sync/pluginSyncService.js";
  7. function makeHome() {
  8. return mkdtempSync(join(tmpdir(), "zcode-plugin-sync-home-"));
  9. }
  10. async function writeZcodeUserConfig(
  11. home: string,
  12. value: Record<string, unknown>,
  13. ): Promise<string> {
  14. const configPath = join(home, ".zcode", "cli", "config.json");
  15. await mkdir(join(home, ".zcode", "cli"), { recursive: true });
  16. await writeFile(configPath, `${JSON.stringify(value, null, 2)}\n`, "utf-8");
  17. return configPath;
  18. }
  19. async function readZcodeUserConfig(home: string): Promise<Record<string, unknown>> {
  20. const raw = await readFile(join(home, ".zcode", "cli", "config.json"), "utf-8");
  21. return JSON.parse(raw) as Record<string, unknown>;
  22. }
  23. async function writeInlinePlugin(
  24. root: string,
  25. directoryName: string,
  26. manifest: Record<string, unknown> = {},
  27. ): Promise<string> {
  28. const pluginRoot = join(root, directoryName);
  29. await mkdir(join(pluginRoot, ".zcode-plugin"), { recursive: true });
  30. await writeFile(
  31. join(pluginRoot, ".zcode-plugin", "plugin.json"),
  32. `${JSON.stringify({
  33. name: directoryName,
  34. description: `${directoryName} description`,
  35. version: "1.0.0",
  36. ...manifest,
  37. }, null, 2)}\n`,
  38. "utf-8",
  39. );
  40. await mkdir(join(pluginRoot, "skills", "demo"), { recursive: true });
  41. await writeFile(join(pluginRoot, "skills", "demo", "SKILL.md"), "---\nname: demo\n---\n");
  42. return pluginRoot;
  43. }
  44. async function readJsonFile(filePath: string): Promise<Record<string, unknown>> {
  45. return JSON.parse(await readFile(filePath, "utf-8")) as Record<string, unknown>;
  46. }
  47. describe("plugin sync service", () => {
  48. let originalHome: string | undefined;
  49. beforeEach(() => {
  50. originalHome = process.env.HOME;
  51. });
  52. afterEach(() => {
  53. process.env.HOME = originalHome;
  54. });
  55. it("lists user inline plugins from zcode plugins.dirs as local candidates", async () => {
  56. const home = makeHome();
  57. process.env.HOME = home;
  58. const pluginRoot = await writeInlinePlugin(join(home, "external"), "remote-tools", {
  59. name: "remote-tools",
  60. mcpServers: { docs: { command: "node", args: ["server.js"] } },
  61. commands: ["commands"],
  62. hooks: [{ event: "PreToolUse", command: "echo hook" }],
  63. });
  64. await writeZcodeUserConfig(home, {
  65. plugins: {
  66. dirs: [pluginRoot],
  67. enabledPlugins: {
  68. "remote-tools@inline": false,
  69. },
  70. },
  71. });
  72. const service = createPluginSyncService();
  73. const result = await service.listLocalUserPluginCandidates();
  74. const canonicalPluginRoot = await realpath(pluginRoot);
  75. expect(result.candidates).toMatchObject([
  76. {
  77. name: "remote-tools",
  78. pluginId: "remote-tools@inline",
  79. directoryName: "remote-tools",
  80. description: "remote-tools description",
  81. version: "1.0.0",
  82. path: canonicalPluginRoot,
  83. enabled: false,
  84. componentTypes: expect.arrayContaining(["commands", "hooks", "mcp", "skills"]),
  85. },
  86. ]);
  87. expect(result.maxArchiveBytes).toBeGreaterThan(0);
  88. });
  89. it("checks remote plugin and config directory write access before sync", async () => {
  90. const remoteHome = makeHome();
  91. process.env.HOME = remoteHome;
  92. const service = createPluginSyncService();
  93. const result = await service.checkRemoteUserPluginWriteAccess();
  94. expect(result).toMatchObject({
  95. ok: true,
  96. path: [
  97. join(remoteHome, ".zcode", "plugins"),
  98. join(remoteHome, ".zcode", "cli"),
  99. ].join(", "),
  100. });
  101. });
  102. it("reports remote plugin write preflight failure when plugin root is not a directory", async () => {
  103. const remoteHome = makeHome();
  104. process.env.HOME = remoteHome;
  105. await mkdir(join(remoteHome, ".zcode"), { recursive: true });
  106. await writeFile(join(remoteHome, ".zcode", "plugins"), "not a directory");
  107. const service = createPluginSyncService();
  108. const result = await service.checkRemoteUserPluginWriteAccess();
  109. expect(result.ok).toBe(false);
  110. expect(result.path).toBe(join(remoteHome, ".zcode", "plugins"));
  111. expect(result.error).toBeTruthy();
  112. });
  113. it("does not list marketplace installed plugin cache records as V1 candidates", async () => {
  114. const home = makeHome();
  115. process.env.HOME = home;
  116. const cachePluginRoot = await writeInlinePlugin(
  117. join(home, ".zcode", "cli", "plugins", "cache", "market", "cached", "1.0.0"),
  118. "cached",
  119. { name: "cached" },
  120. );
  121. await mkdir(join(home, ".zcode", "cli", "plugins"), { recursive: true });
  122. await writeFile(
  123. join(home, ".zcode", "cli", "plugins", "installed_plugins.json"),
  124. `${JSON.stringify({
  125. plugins: [
  126. {
  127. id: "cached@market",
  128. name: "cached",
  129. marketplace: "market",
  130. version: "1.0.0",
  131. installPath: cachePluginRoot,
  132. },
  133. ],
  134. })}\n`,
  135. "utf-8",
  136. );
  137. const service = createPluginSyncService();
  138. const result = await service.listLocalUserPluginCandidates();
  139. expect(result.candidates).toEqual([]);
  140. });
  141. it("imports selected inline plugins into the remote user plugin root and registers plugins.dirs", async () => {
  142. const localHome = makeHome();
  143. process.env.HOME = localHome;
  144. const pluginRoot = await writeInlinePlugin(join(localHome, "external"), "remote-tools", {
  145. name: "remote-tools",
  146. });
  147. await writeZcodeUserConfig(localHome, {
  148. plugins: {
  149. dirs: [pluginRoot],
  150. enabledPlugins: {
  151. "remote-tools@inline": false,
  152. },
  153. },
  154. });
  155. const localService = createPluginSyncService();
  156. const candidates = await localService.listLocalUserPluginCandidates();
  157. const exported = await localService.exportPluginsArchive({
  158. pluginIds: candidates.candidates.map((candidate) => candidate.id),
  159. });
  160. const remoteHome = makeHome();
  161. process.env.HOME = remoteHome;
  162. const remoteService = createPluginSyncService();
  163. const result = await remoteService.importPluginsArchive({
  164. archive: exported.archive,
  165. overwrite: false,
  166. });
  167. expect(result.results).toMatchObject([
  168. {
  169. name: "remote-tools",
  170. pluginId: "remote-tools@inline",
  171. directoryName: "remote-tools",
  172. status: "synced",
  173. },
  174. ]);
  175. const remotePluginRoot = join(remoteHome, ".zcode", "plugins", "remote-tools");
  176. expect(existsSync(join(remotePluginRoot, ".zcode-plugin", "plugin.json"))).toBe(true);
  177. const remoteConfig = await readZcodeUserConfig(remoteHome);
  178. expect(remoteConfig).toMatchObject({
  179. plugins: {
  180. dirs: [resolve(remotePluginRoot)],
  181. enabledPlugins: {
  182. "remote-tools@inline": false,
  183. },
  184. },
  185. });
  186. });
  187. it("preserves executable file permissions when importing inline plugins", async () => {
  188. if (process.platform === "win32") {
  189. return;
  190. }
  191. const localHome = makeHome();
  192. process.env.HOME = localHome;
  193. const pluginRoot = await writeInlinePlugin(join(localHome, "external"), "remote-tools", {
  194. name: "remote-tools",
  195. commands: ["commands"],
  196. });
  197. await mkdir(join(pluginRoot, "commands"), { recursive: true });
  198. const localCommandPath = join(pluginRoot, "commands", "run.sh");
  199. await writeFile(localCommandPath, "#!/bin/sh\necho ok\n", "utf-8");
  200. await chmod(localCommandPath, 0o755);
  201. await writeZcodeUserConfig(localHome, { plugins: { dirs: [pluginRoot] } });
  202. const localService = createPluginSyncService();
  203. const candidates = await localService.listLocalUserPluginCandidates();
  204. const exported = await localService.exportPluginsArchive({
  205. pluginIds: candidates.candidates.map((candidate) => candidate.id),
  206. });
  207. const remoteHome = makeHome();
  208. process.env.HOME = remoteHome;
  209. const remoteService = createPluginSyncService();
  210. await remoteService.importPluginsArchive({
  211. archive: exported.archive,
  212. overwrite: false,
  213. });
  214. const remoteCommandPath = join(
  215. remoteHome,
  216. ".zcode",
  217. "plugins",
  218. "remote-tools",
  219. "commands",
  220. "run.sh",
  221. );
  222. expect((await stat(remoteCommandPath)).mode & 0o777).toBe(0o755);
  223. });
  224. it("rolls back an inline plugin directory when config registration fails", async () => {
  225. if (process.platform === "win32") {
  226. return;
  227. }
  228. const localHome = makeHome();
  229. process.env.HOME = localHome;
  230. const pluginRoot = await writeInlinePlugin(join(localHome, "external"), "remote-tools", {
  231. name: "remote-tools",
  232. });
  233. await writeZcodeUserConfig(localHome, { plugins: { dirs: [pluginRoot] } });
  234. const localService = createPluginSyncService();
  235. const candidates = await localService.listLocalUserPluginCandidates();
  236. const exported = await localService.exportPluginsArchive({
  237. pluginIds: candidates.candidates.map((candidate) => candidate.id),
  238. });
  239. const remoteHome = makeHome();
  240. process.env.HOME = remoteHome;
  241. await writeZcodeUserConfig(remoteHome, {});
  242. const remoteConfigDir = join(remoteHome, ".zcode", "cli");
  243. const remotePluginRoot = join(remoteHome, ".zcode", "plugins", "remote-tools");
  244. const remoteService = createPluginSyncService();
  245. await chmod(remoteConfigDir, 0o555);
  246. const failed = await remoteService.importPluginsArchive({
  247. archive: exported.archive,
  248. overwrite: false,
  249. });
  250. await chmod(remoteConfigDir, 0o755);
  251. expect(failed.results).toMatchObject([
  252. {
  253. pluginId: "remote-tools@inline",
  254. status: "failed",
  255. },
  256. ]);
  257. expect(existsSync(remotePluginRoot)).toBe(false);
  258. const retried = await remoteService.importPluginsArchive({
  259. archive: exported.archive,
  260. overwrite: false,
  261. });
  262. expect(retried.results).toMatchObject([
  263. {
  264. pluginId: "remote-tools@inline",
  265. status: "synced",
  266. path: remotePluginRoot,
  267. },
  268. ]);
  269. const remoteConfig = await readZcodeUserConfig(remoteHome);
  270. expect(remoteConfig).toMatchObject({
  271. plugins: {
  272. dirs: [resolve(remotePluginRoot)],
  273. },
  274. });
  275. });
  276. it("skips remote import when the same plugin id already exists in remote plugins.dirs", async () => {
  277. const localHome = makeHome();
  278. process.env.HOME = localHome;
  279. const pluginRoot = await writeInlinePlugin(join(localHome, "external"), "remote-tools", {
  280. name: "remote-tools",
  281. });
  282. await writeZcodeUserConfig(localHome, { plugins: { dirs: [pluginRoot] } });
  283. const localService = createPluginSyncService();
  284. const candidates = await localService.listLocalUserPluginCandidates();
  285. const exported = await localService.exportPluginsArchive({
  286. pluginIds: candidates.candidates.map((candidate) => candidate.id),
  287. });
  288. const remoteHome = makeHome();
  289. const existingRemoteRoot = await writeInlinePlugin(
  290. join(remoteHome, "existing"),
  291. "different-dir",
  292. { name: "remote-tools" },
  293. );
  294. await writeZcodeUserConfig(remoteHome, { plugins: { dirs: [existingRemoteRoot] } });
  295. process.env.HOME = remoteHome;
  296. const remoteService = createPluginSyncService();
  297. const result = await remoteService.importPluginsArchive({
  298. archive: exported.archive,
  299. overwrite: false,
  300. });
  301. expect(result.results).toMatchObject([
  302. {
  303. pluginId: "remote-tools@inline",
  304. status: "skipped",
  305. },
  306. ]);
  307. const remoteConfig = await readZcodeUserConfig(remoteHome);
  308. expect(remoteConfig).toMatchObject({ plugins: { dirs: [existingRemoteRoot] } });
  309. });
  310. it("reports remote statuses by target directory and plugin id", async () => {
  311. const remoteHome = makeHome();
  312. process.env.HOME = remoteHome;
  313. const existingRemoteRoot = await writeInlinePlugin(join(remoteHome, "existing"), "known", {
  314. name: "known",
  315. });
  316. await writeInlinePlugin(join(remoteHome, ".zcode", "plugins"), "known", {
  317. name: "other-known",
  318. });
  319. await writeZcodeUserConfig(remoteHome, { plugins: { dirs: [existingRemoteRoot] } });
  320. const service = createPluginSyncService();
  321. const result = await service.listRemoteUserPluginStatuses({
  322. plugins: [
  323. { pluginId: "known@inline", directoryName: "missing-target" },
  324. { pluginId: "new@inline", directoryName: "known" },
  325. { pluginId: "new@inline", directoryName: "new" },
  326. ],
  327. });
  328. expect(result.statuses).toMatchObject([
  329. { pluginId: "known@inline", directoryName: "missing-target", exists: true },
  330. { pluginId: "new@inline", directoryName: "known", exists: true },
  331. { pluginId: "new@inline", directoryName: "new", exists: false },
  332. ]);
  333. });
  334. it("rejects plugin archives containing symlinks", async () => {
  335. if (process.platform === "win32") {
  336. return;
  337. }
  338. const home = makeHome();
  339. process.env.HOME = home;
  340. const pluginRoot = await writeInlinePlugin(join(home, "external"), "remote-tools", {
  341. name: "remote-tools",
  342. });
  343. await writeFile(join(home, "secret.txt"), "secret");
  344. symlinkSync(join(home, "secret.txt"), join(pluginRoot, "secret-link.txt"));
  345. await writeZcodeUserConfig(home, { plugins: { dirs: [pluginRoot] } });
  346. const service = createPluginSyncService();
  347. const candidates = await service.listLocalUserPluginCandidates();
  348. await expect(
  349. service.exportPluginsArchive({
  350. pluginIds: candidates.candidates.map((candidate) => candidate.id),
  351. }),
  352. ).rejects.toThrow("unsupported plugin archive source");
  353. });
  354. it("exports and imports a mirrored directory marketplace source", async () => {
  355. const localHome = makeHome();
  356. process.env.HOME = localHome;
  357. const marketplaceRoot = join(localHome, "team-market");
  358. await writeInlinePlugin(marketplaceRoot, "formatter", { name: "formatter" });
  359. await writeFile(
  360. join(marketplaceRoot, "marketplace.json"),
  361. `${JSON.stringify({
  362. name: "team-market",
  363. plugins: [
  364. {
  365. name: "formatter",
  366. version: "1.0.0",
  367. source: "./formatter",
  368. },
  369. ],
  370. }, null, 2)}\n`,
  371. "utf-8",
  372. );
  373. const localService = createPluginSyncService();
  374. const exported = await localService.exportMarketplaceSourceArchive({
  375. marketplaceId: "team-market",
  376. pluginNames: ["formatter"],
  377. source: { source: "directory", path: marketplaceRoot },
  378. });
  379. const remoteHome = makeHome();
  380. process.env.HOME = remoteHome;
  381. const remoteService = createPluginSyncService();
  382. const imported = await remoteService.importMarketplaceSourceArchive({
  383. archive: exported.archive,
  384. overwrite: false,
  385. });
  386. expect(imported).toMatchObject({
  387. marketplaceId: "team-market",
  388. status: "synced",
  389. });
  390. expect(imported.path.startsWith(join(remoteHome, ".zcode", "plugins", "marketplace-sources"))).toBe(true);
  391. const mirroredManifest = await readJsonFile(join(imported.path, "marketplace.json"));
  392. expect(mirroredManifest).toMatchObject({
  393. name: "team-market",
  394. metadata: {
  395. pluginRoot: "plugins",
  396. },
  397. plugins: [
  398. {
  399. name: "formatter",
  400. source: "./formatter",
  401. },
  402. ],
  403. });
  404. expect(
  405. existsSync(join(imported.path, "plugins", "formatter", ".zcode-plugin", "plugin.json")),
  406. ).toBe(true);
  407. });
  408. it("rejects absolute marketplace plugin source paths outside the marketplace root", async () => {
  409. const localHome = makeHome();
  410. process.env.HOME = localHome;
  411. const marketplaceRoot = join(localHome, "team-market");
  412. const externalPluginRoot = await writeInlinePlugin(join(localHome, "external"), "formatter", {
  413. name: "formatter",
  414. });
  415. await mkdir(marketplaceRoot, { recursive: true });
  416. await writeFile(
  417. join(marketplaceRoot, "marketplace.json"),
  418. `${JSON.stringify({
  419. name: "team-market",
  420. plugins: [
  421. {
  422. name: "formatter",
  423. source: externalPluginRoot,
  424. },
  425. ],
  426. }, null, 2)}\n`,
  427. "utf-8",
  428. );
  429. await expect(
  430. createPluginSyncService().exportMarketplaceSourceArchive({
  431. marketplaceId: "team-market",
  432. pluginNames: ["formatter"],
  433. source: { source: "directory", path: marketplaceRoot },
  434. }),
  435. ).rejects.toThrow("unsafe marketplace plugin source path");
  436. });
  437. it("rejects mirrored marketplace plugin source directories without a plugin manifest", async () => {
  438. const localHome = makeHome();
  439. process.env.HOME = localHome;
  440. const marketplaceRoot = join(localHome, "team-market");
  441. await mkdir(join(marketplaceRoot, "formatter"), { recursive: true });
  442. await writeFile(join(marketplaceRoot, "formatter", "README.md"), "not a plugin\n", "utf-8");
  443. await writeFile(
  444. join(marketplaceRoot, "marketplace.json"),
  445. `${JSON.stringify({
  446. name: "team-market",
  447. plugins: [
  448. {
  449. name: "formatter",
  450. source: "./formatter",
  451. },
  452. ],
  453. }, null, 2)}\n`,
  454. "utf-8",
  455. );
  456. await expect(
  457. createPluginSyncService().exportMarketplaceSourceArchive({
  458. marketplaceId: "team-market",
  459. pluginNames: ["formatter"],
  460. source: { source: "directory", path: marketplaceRoot },
  461. }),
  462. ).rejects.toThrow("local marketplace plugin source is missing plugin manifest");
  463. });
  464. it("changes mirrored marketplace source directory when local plugin contents change", async () => {
  465. const localHome = makeHome();
  466. process.env.HOME = localHome;
  467. const marketplaceRoot = join(localHome, "team-market");
  468. const pluginRoot = await writeInlinePlugin(marketplaceRoot, "formatter", {
  469. name: "formatter",
  470. });
  471. await writeFile(
  472. join(marketplaceRoot, "marketplace.json"),
  473. `${JSON.stringify({
  474. name: "team-market",
  475. plugins: [{ name: "formatter", source: "./formatter" }],
  476. }, null, 2)}\n`,
  477. "utf-8",
  478. );
  479. const service = createPluginSyncService();
  480. const before = await service.exportMarketplaceSourceArchive({
  481. marketplaceId: "team-market",
  482. pluginNames: ["formatter"],
  483. source: { source: "directory", path: marketplaceRoot },
  484. });
  485. await writeFile(join(pluginRoot, "skills", "demo", "SKILL.md"), "---\nname: demo\n---\nnew");
  486. const after = await service.exportMarketplaceSourceArchive({
  487. marketplaceId: "team-market",
  488. pluginNames: ["formatter"],
  489. source: { source: "directory", path: marketplaceRoot },
  490. });
  491. const remoteHome = makeHome();
  492. process.env.HOME = remoteHome;
  493. const remoteService = createPluginSyncService();
  494. const beforeImport = await remoteService.importMarketplaceSourceArchive({
  495. archive: before.archive,
  496. overwrite: false,
  497. });
  498. const afterImport = await remoteService.importMarketplaceSourceArchive({
  499. archive: after.archive,
  500. overwrite: false,
  501. });
  502. expect(afterImport.path).not.toBe(beforeImport.path);
  503. });
  504. it("mirrors same-marketplace dependencies with the selected marketplace plugin", async () => {
  505. const localHome = makeHome();
  506. process.env.HOME = localHome;
  507. const marketplaceRoot = join(localHome, "team-market");
  508. await writeInlinePlugin(marketplaceRoot, "formatter", { name: "formatter" });
  509. await writeInlinePlugin(marketplaceRoot, "helper", { name: "helper" });
  510. await writeFile(
  511. join(marketplaceRoot, "marketplace.json"),
  512. `${JSON.stringify({
  513. name: "team-market",
  514. plugins: [
  515. { name: "formatter", dependencies: ["helper"], source: "./formatter" },
  516. { name: "helper", source: "./helper" },
  517. ],
  518. }, null, 2)}\n`,
  519. "utf-8",
  520. );
  521. const exported = await createPluginSyncService().exportMarketplaceSourceArchive({
  522. marketplaceId: "team-market",
  523. pluginNames: ["formatter"],
  524. source: { source: "directory", path: marketplaceRoot },
  525. });
  526. const remoteHome = makeHome();
  527. process.env.HOME = remoteHome;
  528. const imported = await createPluginSyncService().importMarketplaceSourceArchive({
  529. archive: exported.archive,
  530. overwrite: false,
  531. });
  532. const mirroredManifest = await readJsonFile(join(imported.path, "marketplace.json"));
  533. expect(exported.pluginNames).toEqual(["formatter", "helper"]);
  534. expect(mirroredManifest.plugins).toMatchObject([
  535. { name: "formatter", dependencies: ["helper"], source: "./formatter" },
  536. { name: "helper", source: "./helper" },
  537. ]);
  538. expect(existsSync(join(imported.path, "plugins", "helper"))).toBe(true);
  539. });
  540. it("exports a file marketplace source with plugin paths relative to the marketplace file", async () => {
  541. const localHome = makeHome();
  542. process.env.HOME = localHome;
  543. const marketplaceRoot = join(localHome, "team-market");
  544. await writeInlinePlugin(marketplaceRoot, "formatter", { name: "formatter" });
  545. const marketplaceFile = join(marketplaceRoot, "marketplace.json");
  546. await writeFile(
  547. marketplaceFile,
  548. `${JSON.stringify({
  549. name: "team-market",
  550. plugins: [{ name: "formatter", source: "./formatter" }],
  551. }, null, 2)}\n`,
  552. "utf-8",
  553. );
  554. const exported = await createPluginSyncService().exportMarketplaceSourceArchive({
  555. marketplaceId: "team-market",
  556. pluginNames: ["formatter"],
  557. source: { source: "file", path: marketplaceFile },
  558. });
  559. const remoteHome = makeHome();
  560. process.env.HOME = remoteHome;
  561. const imported = await createPluginSyncService().importMarketplaceSourceArchive({
  562. archive: exported.archive,
  563. overwrite: false,
  564. });
  565. expect(existsSync(join(imported.path, "plugins", "formatter"))).toBe(true);
  566. });
  567. it("exports a settings marketplace source without local cache fallback when entries are remote", async () => {
  568. const home = makeHome();
  569. process.env.HOME = home;
  570. const service = createPluginSyncService();
  571. const exported = await service.exportMarketplaceSourceArchive({
  572. marketplaceId: "team-market",
  573. pluginNames: ["formatter"],
  574. source: {
  575. source: "settings",
  576. marketplace: {
  577. name: "team-market",
  578. plugins: [
  579. {
  580. name: "formatter",
  581. version: "1.0.0",
  582. source: { source: "github", repo: "example/formatter-plugin" },
  583. },
  584. ],
  585. },
  586. },
  587. });
  588. const remoteHome = makeHome();
  589. process.env.HOME = remoteHome;
  590. const imported = await createPluginSyncService().importMarketplaceSourceArchive({
  591. archive: exported.archive,
  592. overwrite: false,
  593. });
  594. const mirroredManifest = await readJsonFile(join(imported.path, "marketplace.json"));
  595. expect(mirroredManifest).toMatchObject({
  596. name: "team-market",
  597. plugins: [
  598. {
  599. name: "formatter",
  600. source: { source: "github", repo: "example/formatter-plugin" },
  601. },
  602. ],
  603. });
  604. expect(existsSync(join(imported.path, "plugins"))).toBe(false);
  605. });
  606. it("rejects settings marketplace entries whose local source root cannot be reconstructed", async () => {
  607. const home = makeHome();
  608. process.env.HOME = home;
  609. const service = createPluginSyncService();
  610. await expect(
  611. service.exportMarketplaceSourceArchive({
  612. marketplaceId: "team-market",
  613. pluginNames: ["formatter"],
  614. source: {
  615. source: "settings",
  616. marketplace: {
  617. name: "team-market",
  618. plugins: [
  619. {
  620. name: "formatter",
  621. source: "./formatter",
  622. },
  623. ],
  624. },
  625. },
  626. }),
  627. ).rejects.toThrow("cannot mirror local marketplace plugin source");
  628. });
  629. });