255 lines
8.6 KiB
JavaScript
255 lines
8.6 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import { mkdir, readFile, readlink, stat, writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import test from "node:test";
|
|
|
|
import { installMarketplaceLocally } from "./install-local.mjs";
|
|
import { makeTempDir, writeJson, writePluginAt } from "./install-test-fixtures.mjs";
|
|
|
|
test("#given local marketplace #when installing #then copies versioned plugins and enables config", async () => {
|
|
const repoRoot = await makeTempDir();
|
|
const codexHome = await makeTempDir();
|
|
const binDir = await makeTempDir();
|
|
const codexPackageRoot = join(repoRoot, "packages", "omo-codex");
|
|
const pluginRoot = join(codexPackageRoot, "plugin");
|
|
|
|
await writeJson(join(codexPackageRoot, "marketplace.json"), {
|
|
name: "debug-marketplace",
|
|
plugins: [
|
|
{
|
|
name: "alpha",
|
|
source: "./plugins/alpha",
|
|
},
|
|
],
|
|
});
|
|
await writePluginAt(pluginRoot, "alpha", "1.2.3");
|
|
await mkdir(join(codexPackageRoot, "shared-lsp", "dist"), { recursive: true });
|
|
await writeJson(join(codexPackageRoot, "shared-lsp", "package.json"), {
|
|
name: "@example/shared-lsp",
|
|
version: "0.0.0",
|
|
type: "module",
|
|
bin: { "shared-lsp": "./dist/cli.js" },
|
|
});
|
|
await writeFile(join(codexPackageRoot, "shared-lsp", "dist", "cli.js"), "#!/usr/bin/env node\n");
|
|
await writeJson(join(pluginRoot, "package.json"), {
|
|
name: "@example/alpha",
|
|
version: "1.2.3",
|
|
bin: {
|
|
alpha: "./dist/cli.js",
|
|
},
|
|
scripts: {
|
|
build: "node -e \"require('fs').writeFileSync('dist/cli.js', 'console.log(1)')\"",
|
|
},
|
|
dependencies: {
|
|
"@example/shared-lsp": "file:../shared-lsp",
|
|
},
|
|
});
|
|
await writeJson(join(pluginRoot, ".mcp.json"), {
|
|
mcpServers: {
|
|
alpha: {
|
|
command: "node",
|
|
args: ["./dist/cli.js", "mcp"],
|
|
cwd: ".",
|
|
},
|
|
shared: {
|
|
command: "node",
|
|
args: ["../shared-lsp/dist/cli.js", "mcp"],
|
|
cwd: ".",
|
|
},
|
|
},
|
|
});
|
|
await mkdir(join(pluginRoot, "node_modules"), { recursive: true });
|
|
await writeFile(join(pluginRoot, "node_modules", "skip.txt"), "skip");
|
|
await mkdir(join(codexHome, "plugins", "cache", "debug-marketplace", "stale", "0.1.0"), { recursive: true });
|
|
await writeFile(
|
|
join(codexHome, "config.toml"),
|
|
[
|
|
'[plugins."stale@debug-marketplace"]',
|
|
"enabled = true",
|
|
"",
|
|
'[hooks.state."stale@debug-marketplace:hooks/hooks.json:user_prompt_submit:0:0"]',
|
|
'trusted_hash = "sha256:old"',
|
|
"",
|
|
].join("\n"),
|
|
);
|
|
|
|
const commands = [];
|
|
const result = await installMarketplaceLocally({
|
|
repoRoot,
|
|
codexHome,
|
|
binDir,
|
|
platform: "linux",
|
|
runCommand: async (command, args, options) => {
|
|
commands.push([command, args, options.cwd]);
|
|
},
|
|
log: () => {},
|
|
});
|
|
|
|
assert.deepEqual(
|
|
result.installed.map((plugin) => `${plugin.name}@${plugin.version}`),
|
|
["alpha@1.2.3"],
|
|
);
|
|
const alphaCacheRoot = join(codexHome, "plugins", "cache", "debug-marketplace", "alpha", "1.2.3");
|
|
assert.equal((await stat(join(alphaCacheRoot, ".mcp.json"))).isFile(), true);
|
|
assert.equal(await readlink(join(binDir, "alpha")), join(alphaCacheRoot, "dist", "cli.js"));
|
|
const alphaMcp = JSON.parse(await readFile(join(alphaCacheRoot, ".mcp.json"), "utf8"));
|
|
assert.deepEqual(alphaMcp.mcpServers.alpha.args, [join(alphaCacheRoot, "dist", "cli.js"), "mcp"]);
|
|
assert.deepEqual(alphaMcp.mcpServers.shared.args, [join(codexPackageRoot, "shared-lsp", "dist", "cli.js"), "mcp"]);
|
|
assert.equal(
|
|
Object.hasOwn(alphaMcp.mcpServers.alpha, "cwd"),
|
|
false,
|
|
"`cwd: \".\"` must be stripped so the spawned MCP server inherits the caller's workspace cwd",
|
|
);
|
|
assert.equal(Object.hasOwn(alphaMcp.mcpServers.shared, "cwd"), false);
|
|
assert.equal(alphaMcp.mcpServers.alpha.command, "node");
|
|
const alphaPackageJson = JSON.parse(await readFile(join(alphaCacheRoot, "package.json"), "utf8"));
|
|
assert.equal(alphaPackageJson.dependencies["@example/shared-lsp"], `file:${join(codexPackageRoot, "shared-lsp")}`);
|
|
await assert.rejects(
|
|
stat(join(codexHome, "plugins", "cache", "debug-marketplace", "alpha", "1.2.3", "node_modules")),
|
|
/code: 'ENOENT'|ENOENT/,
|
|
);
|
|
await assert.rejects(
|
|
stat(join(codexHome, "plugins", "cache", "debug-marketplace", "stale")),
|
|
/code: 'ENOENT'|ENOENT/,
|
|
);
|
|
assert.deepEqual(
|
|
commands.map(([command, args, cwd]) => [command, args.join(" "), cwd]),
|
|
[
|
|
["npm", "install", pluginRoot],
|
|
["npm", "run build", pluginRoot],
|
|
["npm", "install --omit=dev", join(codexHome, "plugins", "cache", "debug-marketplace", "alpha", "1.2.3")],
|
|
],
|
|
);
|
|
|
|
const config = await readFile(join(codexHome, "config.toml"), "utf8");
|
|
assert.match(config, /\[features\]\n(?:plugin_hooks = true\n)?plugins = true/);
|
|
assert.match(config, /\[marketplaces\.debug-marketplace\]/);
|
|
assert.match(config, /source_type = "local"/);
|
|
assert.match(config, /\[plugins\."alpha@debug-marketplace"\]\nenabled = true/);
|
|
assert.doesNotMatch(config, /stale@debug-marketplace/);
|
|
});
|
|
|
|
test("#given sisyphuslabs marketplace #when installing #then registers lazycodex git source", async () => {
|
|
const repoRoot = await makeTempDir();
|
|
const codexHome = await makeTempDir();
|
|
const codexPackageRoot = join(repoRoot, "packages", "omo-codex");
|
|
|
|
await writeJson(join(codexPackageRoot, "marketplace.json"), {
|
|
name: "sisyphuslabs",
|
|
plugins: [{ name: "omo", source: "./plugins/omo" }],
|
|
});
|
|
await writePluginAt(join(codexPackageRoot, "plugin"), "omo", "0.1.0");
|
|
await mkdir(join(codexHome, "plugins", "cache", "code-yeongyu-codex-plugins", "omo", "0.1.0"), {
|
|
recursive: true,
|
|
});
|
|
await writeJson(join(codexHome, "plugins", "cache", "code-yeongyu-codex-plugins", "omo", "0.1.0", ".mcp.json"), {
|
|
mcpServers: {
|
|
lsp: {
|
|
command: "node",
|
|
args: ["old/components/lsp/packages/lsp-tools-mcp/dist/cli.js", "mcp"],
|
|
},
|
|
},
|
|
});
|
|
await writeFile(
|
|
join(codexHome, "config.toml"),
|
|
[
|
|
"[marketplaces.code-yeongyu-codex-plugins]",
|
|
'last_updated = "2026-05-01T00:00:00Z"',
|
|
'source_type = "git"',
|
|
'source = "https://github.com/code-yeongyu/codex-plugins.git"',
|
|
"",
|
|
'[plugins."omo@code-yeongyu-codex-plugins"]',
|
|
"enabled = true",
|
|
"",
|
|
'[plugins."omo@code-yeongyu-codex-plugins".mcp_servers.lsp]',
|
|
'enabled = true',
|
|
"",
|
|
'[hooks.state."omo@code-yeongyu-codex-plugins:hooks/hooks.json:post_tool_use:0:0"]',
|
|
'trusted_hash = "sha256:old"',
|
|
"",
|
|
].join("\n"),
|
|
);
|
|
|
|
await installMarketplaceLocally({
|
|
repoRoot,
|
|
codexHome,
|
|
runCommand: async () => {},
|
|
log: () => {},
|
|
});
|
|
|
|
const config = await readFile(join(codexHome, "config.toml"), "utf8");
|
|
assert.match(config, /\[marketplaces\.sisyphuslabs\]/);
|
|
assert.match(config, /source_type = "git"/);
|
|
assert.match(config, /source = "https:\/\/github\.com\/code-yeongyu\/lazycodex\.git"/);
|
|
assert.match(config, /ref = "main"/);
|
|
assert.match(config, /\[plugins\."omo@sisyphuslabs"\]\nenabled = true/);
|
|
assert.doesNotMatch(config, /\[marketplaces\.lazycodex\]/);
|
|
assert.doesNotMatch(config, /code-yeongyu-codex-plugins/);
|
|
assert.doesNotMatch(config, /source_type = "local"/);
|
|
await assert.rejects(
|
|
stat(join(codexHome, "plugins", "cache", "code-yeongyu-codex-plugins", "omo")),
|
|
/code: 'ENOENT'|ENOENT/,
|
|
);
|
|
});
|
|
|
|
test("#given plugin hooks #when installing #then records trusted hook hashes", async () => {
|
|
const repoRoot = await makeTempDir();
|
|
const codexHome = await makeTempDir();
|
|
const codexPackageRoot = join(repoRoot, "packages", "omo-codex");
|
|
|
|
await writeJson(join(codexPackageRoot, "marketplace.json"), {
|
|
name: "debug-marketplace",
|
|
plugins: [{ name: "alpha", source: "./plugins/alpha" }],
|
|
});
|
|
const pluginRoot = join(codexPackageRoot, "plugin");
|
|
await writePluginAt(pluginRoot, "alpha", "1.2.3");
|
|
await writeJson(join(pluginRoot, "hooks", "hooks.json"), {
|
|
hooks: {
|
|
UserPromptSubmit: [
|
|
{
|
|
hooks: [
|
|
{
|
|
type: "command",
|
|
command: "node \"${PLUGIN_ROOT}/dist/cli.js\" hook user-prompt-submit",
|
|
timeout: 10,
|
|
statusMessage: "checking alpha",
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
});
|
|
|
|
await installMarketplaceLocally({
|
|
repoRoot,
|
|
codexHome,
|
|
runCommand: async () => {},
|
|
log: () => {},
|
|
});
|
|
|
|
const config = await readFile(join(codexHome, "config.toml"), "utf8");
|
|
assert.match(config, /\[hooks\.state\."alpha@debug-marketplace:hooks\/hooks\.json:user_prompt_submit:0:0"\]/);
|
|
assert.match(config, /trusted_hash = "sha256:[a-f0-9]{64}"/);
|
|
});
|
|
|
|
test("#given bad plugin source path #when installing #then rejects traversal", async () => {
|
|
const repoRoot = await makeTempDir();
|
|
const codexHome = await makeTempDir();
|
|
const codexPackageRoot = join(repoRoot, "packages", "omo-codex");
|
|
|
|
await writeJson(join(codexPackageRoot, "marketplace.json"), {
|
|
name: "debug-marketplace",
|
|
plugins: [
|
|
{
|
|
name: "escape",
|
|
source: "../escape",
|
|
},
|
|
],
|
|
});
|
|
|
|
await assert.rejects(
|
|
installMarketplaceLocally({ repoRoot, codexHome, log: () => {} }),
|
|
/local plugin source path must start with \.\//,
|
|
);
|
|
});
|