mcp-telemetry-source.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { describe, expect, it } from "vitest";
  5. import { createNodePluginAdapter } from "../src/plugins/index.js";
  6. const PLUGIN_NAME = "telemetry-source-fixture";
  7. const MCP_RUNTIME_NAME = `plugin:${PLUGIN_NAME}:browser`;
  8. describe("MCP telemetry source provenance", () => {
  9. it("marks MCP servers discovered from the built-in plugin channel as builtin", async () => {
  10. const fixture = await discoverPluginMcp("builtin");
  11. try {
  12. expect(fixture.outcome.mcpServers[MCP_RUNTIME_NAME]).toMatchObject({
  13. source: { kind: "builtin" },
  14. });
  15. } finally {
  16. await fixture.cleanup();
  17. }
  18. });
  19. it("marks installed MCP servers from the ZCode official marketplace as builtin", async () => {
  20. const fixture = await discoverPluginMcp("official-cdn");
  21. try {
  22. expect(fixture.outcome.plugins[0]).toMatchObject({
  23. marketplace: "zcode-plugins-official",
  24. source: "cache",
  25. });
  26. expect(fixture.outcome.mcpServers[MCP_RUNTIME_NAME]).toMatchObject({
  27. source: { kind: "builtin" },
  28. });
  29. } finally {
  30. await fixture.cleanup();
  31. }
  32. });
  33. it("keeps MCP servers discovered from non-built-in plugin channels as plugin", async () => {
  34. const fixture = await discoverPluginMcp("inline");
  35. try {
  36. expect(fixture.outcome.mcpServers[MCP_RUNTIME_NAME]).toMatchObject({
  37. source: { kind: "plugin" },
  38. });
  39. } finally {
  40. await fixture.cleanup();
  41. }
  42. });
  43. });
  44. async function discoverPluginMcp(channel: "builtin" | "official-cdn" | "inline") {
  45. const root = await mkdtemp(join(tmpdir(), `zcode-${channel}-plugin-mcp-source-`));
  46. const pluginRoot = join(root, PLUGIN_NAME);
  47. const storageRoot = join(root, "storage");
  48. const marketplace = channel === "inline" ? "inline" : "zcode-plugins-official";
  49. const pluginId = `${PLUGIN_NAME}@${marketplace}`;
  50. await mkdir(join(pluginRoot, ".zcode-plugin"), { recursive: true });
  51. await mkdir(storageRoot, { recursive: true });
  52. await writeFile(
  53. join(pluginRoot, ".zcode-plugin", "plugin.json"),
  54. JSON.stringify({ name: PLUGIN_NAME }),
  55. );
  56. await writeFile(
  57. join(pluginRoot, ".mcp.json"),
  58. JSON.stringify({
  59. mcpServers: {
  60. browser: { command: "node", type: "stdio" },
  61. },
  62. }),
  63. );
  64. if (channel === "official-cdn") {
  65. await writeFile(
  66. join(storageRoot, "installed_plugins.json"),
  67. JSON.stringify({
  68. plugins: [
  69. {
  70. id: pluginId,
  71. installPath: pluginRoot,
  72. installedAt: "2026-08-26T00:00:00.000Z",
  73. marketplace,
  74. name: PLUGIN_NAME,
  75. scope: "user",
  76. version: "1.0.0",
  77. },
  78. ],
  79. version: 1,
  80. }),
  81. );
  82. }
  83. const outcome = await createNodePluginAdapter({ storageRoot }).discoverPlugins({
  84. config: {
  85. dirs: channel === "inline" ? [pluginRoot] : [],
  86. enabled: true,
  87. enabledPlugins: { [pluginId]: true },
  88. options: {},
  89. suppressedBuiltins: [],
  90. },
  91. env: {},
  92. ...(channel === "builtin" ? { officialPluginRoots: [pluginRoot] } : {}),
  93. storageRoot,
  94. workingDirectory: root,
  95. });
  96. return {
  97. cleanup: () => rm(root, { force: true, recursive: true }),
  98. outcome,
  99. };
  100. }