🛠️ Update various documentation, scripts, and configuration templates to enhance clarity, functionality, and maintainability across the project
This commit is contained in:
282
install/merge-managed-mcp-config.test.mjs
Normal file
282
install/merge-managed-mcp-config.test.mjs
Normal file
@@ -0,0 +1,282 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import test from "node:test";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const scriptDir = path.dirname(fileURLToPath(import.meta.url));
|
||||
const repoRoot = path.resolve(scriptDir, "..");
|
||||
const mergeScript = path.join(scriptDir, "merge-managed-mcp-config.mjs");
|
||||
const vscodeTemplateFile = path.join(
|
||||
repoRoot,
|
||||
"config",
|
||||
"mcp",
|
||||
"vscode.mcp.template.jsonc",
|
||||
);
|
||||
const copilotCliTemplateFile = path.join(
|
||||
repoRoot,
|
||||
"config",
|
||||
"mcp",
|
||||
"copilot-cli.mcp.template.jsonc",
|
||||
);
|
||||
|
||||
function stripJsonComments(input) {
|
||||
let output = "";
|
||||
let inString = false;
|
||||
let escaping = false;
|
||||
let inLineComment = false;
|
||||
let inBlockComment = false;
|
||||
|
||||
for (let index = 0; index < input.length; index += 1) {
|
||||
const char = input[index];
|
||||
const next = input[index + 1];
|
||||
|
||||
if (inLineComment) {
|
||||
if (char === "\n") {
|
||||
inLineComment = false;
|
||||
output += char;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (inBlockComment) {
|
||||
if (char === "*" && next === "/") {
|
||||
inBlockComment = false;
|
||||
index += 1;
|
||||
} else if (char === "\n" || char === "\r") {
|
||||
output += char;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (inString) {
|
||||
output += char;
|
||||
if (escaping) {
|
||||
escaping = false;
|
||||
} else if (char === "\\") {
|
||||
escaping = true;
|
||||
} else if (char === '"') {
|
||||
inString = false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === '"') {
|
||||
inString = true;
|
||||
output += char;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === "/" && next === "/") {
|
||||
inLineComment = true;
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === "/" && next === "*") {
|
||||
inBlockComment = true;
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
output += char;
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
function stripTrailingCommas(input) {
|
||||
let output = "";
|
||||
let inString = false;
|
||||
let escaping = false;
|
||||
|
||||
for (let index = 0; index < input.length; index += 1) {
|
||||
const char = input[index];
|
||||
|
||||
if (inString) {
|
||||
output += char;
|
||||
if (escaping) {
|
||||
escaping = false;
|
||||
} else if (char === "\\") {
|
||||
escaping = true;
|
||||
} else if (char === '"') {
|
||||
inString = false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === '"') {
|
||||
inString = true;
|
||||
output += char;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === ",") {
|
||||
let lookahead = index + 1;
|
||||
while (lookahead < input.length && /\s/.test(input[lookahead])) {
|
||||
lookahead += 1;
|
||||
}
|
||||
if (input[lookahead] === "}" || input[lookahead] === "]") {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
output += char;
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
function parseJsonc(input) {
|
||||
return JSON.parse(stripTrailingCommas(stripJsonComments(input)));
|
||||
}
|
||||
|
||||
function runMerge({ targetFile, templateFile, serverKey, overridesFile }) {
|
||||
const args = [
|
||||
mergeScript,
|
||||
"--target",
|
||||
targetFile,
|
||||
"--template",
|
||||
templateFile,
|
||||
"--server-key",
|
||||
serverKey,
|
||||
"--set",
|
||||
"COPILOT_RESOURCES_HOME=/repo/home",
|
||||
];
|
||||
|
||||
if (overridesFile) {
|
||||
args.push("--overrides", overridesFile);
|
||||
}
|
||||
|
||||
return spawnSync(process.execPath, args, {
|
||||
cwd: repoRoot,
|
||||
encoding: "utf8",
|
||||
});
|
||||
}
|
||||
|
||||
test("preserves comments and custom servers while pruning stale managed MCP entries", () => {
|
||||
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "copilot-mcp-"));
|
||||
const targetFile = path.join(tempDir, "mcp.json");
|
||||
|
||||
fs.writeFileSync(
|
||||
targetFile,
|
||||
`{
|
||||
// keep this comment
|
||||
"servers": {
|
||||
// custom server stays
|
||||
"customServer": {
|
||||
"type": "http",
|
||||
"url": "https://example.com/mcp"
|
||||
},
|
||||
"gitea": {
|
||||
"type": "stdio",
|
||||
"command": "docker",
|
||||
"args": ["stale"]
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
"utf8",
|
||||
);
|
||||
|
||||
const result = runMerge({
|
||||
targetFile,
|
||||
templateFile: vscodeTemplateFile,
|
||||
serverKey: "servers",
|
||||
});
|
||||
assert.equal(result.status, 0, result.stderr);
|
||||
|
||||
const output = fs.readFileSync(targetFile, "utf8");
|
||||
assert.match(output, /\/\/ keep this comment/);
|
||||
assert.match(output, /\/\/ custom server stays/);
|
||||
assert.match(output, /"customServer"/);
|
||||
assert.match(output, /"playwright"/);
|
||||
assert.match(output, /"filesystem"/);
|
||||
assert.doesNotMatch(output, /"gitea"/);
|
||||
|
||||
const parsed = parseJsonc(output);
|
||||
assert.equal(parsed.servers.customServer.type, "http");
|
||||
assert.equal(parsed.servers.playwright.command, "npx");
|
||||
assert.equal(parsed.servers.filesystem.command, "docker");
|
||||
assert.equal(parsed.servers.gitea, undefined);
|
||||
});
|
||||
|
||||
test("renders optional Gitea config when local overrides are complete", () => {
|
||||
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "copilot-mcp-"));
|
||||
const targetFile = path.join(tempDir, "mcp-config.json");
|
||||
const overridesFile = path.join(tempDir, "mcp.local.jsonc");
|
||||
|
||||
fs.writeFileSync(
|
||||
overridesFile,
|
||||
`{
|
||||
"servers": {
|
||||
"gitea": {
|
||||
"enabled": true,
|
||||
"serverUrl": "https://git.example.com",
|
||||
"token": "secret-token"
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
"utf8",
|
||||
);
|
||||
|
||||
const result = runMerge({
|
||||
targetFile,
|
||||
templateFile: copilotCliTemplateFile,
|
||||
serverKey: "mcpServers",
|
||||
overridesFile,
|
||||
});
|
||||
assert.equal(result.status, 0, result.stderr);
|
||||
|
||||
const parsed = parseJsonc(fs.readFileSync(targetFile, "utf8"));
|
||||
assert.equal(
|
||||
parsed.mcpServers.filesystem.args[0],
|
||||
"/repo/home/install/mcp/copilot-cli-filesystem-wrapper.mjs",
|
||||
);
|
||||
assert.equal(parsed.mcpServers.gitea.env.FORGEJOMCP_SERVER, "https://git.example.com");
|
||||
assert.equal(parsed.mcpServers.gitea.env.FORGEJOMCP_TOKEN, "secret-token");
|
||||
assert.equal(parsed.mcpServers.playwright, undefined);
|
||||
});
|
||||
|
||||
test("second run is idempotent and keeps the file text unchanged", () => {
|
||||
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "copilot-mcp-"));
|
||||
const targetFile = path.join(tempDir, "mcp.json");
|
||||
|
||||
fs.writeFileSync(
|
||||
targetFile,
|
||||
`{
|
||||
// user comment
|
||||
"servers": {
|
||||
"customServer": {
|
||||
"type": "http",
|
||||
"url": "https://example.com/mcp"
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
"utf8",
|
||||
);
|
||||
|
||||
const firstRun = runMerge({
|
||||
targetFile,
|
||||
templateFile: vscodeTemplateFile,
|
||||
serverKey: "servers",
|
||||
});
|
||||
assert.equal(firstRun.status, 0, firstRun.stderr);
|
||||
const firstOutput = fs.readFileSync(targetFile, "utf8");
|
||||
|
||||
const secondRun = runMerge({
|
||||
targetFile,
|
||||
templateFile: vscodeTemplateFile,
|
||||
serverKey: "servers",
|
||||
});
|
||||
assert.equal(secondRun.status, 0, secondRun.stderr);
|
||||
assert.match(secondRun.stdout, /already up to date/);
|
||||
|
||||
const secondOutput = fs.readFileSync(targetFile, "utf8");
|
||||
assert.equal(secondOutput, firstOutput);
|
||||
assert.match(secondOutput, /\/\/ user comment/);
|
||||
});
|
||||
Reference in New Issue
Block a user