Files
bw-copilot-resources/install/merge-vscode-settings.test.mjs

258 lines
6.0 KiB
JavaScript

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-vscode-settings.mjs");
const templateFile = path.join(
repoRoot,
"config",
"vscode",
"settings.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) {
return spawnSync(
process.execPath,
[
mergeScript,
"--target",
targetFile,
"--template",
templateFile,
"--set",
"COPILOT_RESOURCES_HOME=/repo/home",
],
{
cwd: repoRoot,
encoding: "utf8",
},
);
}
test("preserves comments and custom nested entries while inserting managed settings", () => {
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "copilot-settings-"));
const targetFile = path.join(tempDir, "settings.json");
fs.writeFileSync(
targetFile,
`{
// keep this comment
"workbench.colorTheme": "GitHub Dark Mode",
// preserve nested comments
"chat.agentFilesLocations": {
// custom agents stay
"/custom/agents": true
},
"chat.instructionsFilesLocations": {
"/old/instructions": true,
"~/.claude/rules": false,
}
}
`,
"utf8",
);
const result = runMerge(targetFile);
assert.equal(result.status, 0, result.stderr);
const output = fs.readFileSync(targetFile, "utf8");
assert.match(output, /\/\/ keep this comment/);
assert.match(output, /\/\/ preserve nested comments/);
assert.match(output, /\/\/ custom agents stay/);
assert.match(output, /"\/custom\/agents": true/);
assert.match(output, /"\/repo\/home\/resources\/agents": true/);
const parsed = parseJsonc(output);
assert.equal(parsed["workbench.colorTheme"], "GitHub Dark Mode");
assert.equal(parsed["chat.agentFilesLocations"]["/custom/agents"], true);
assert.equal(
parsed["chat.agentFilesLocations"]["/repo/home/resources/agents"],
true,
);
assert.equal(parsed["chat.agentFilesLocations"]["~/.copilot/agents"], true);
assert.equal(
parsed["chat.instructionsFilesLocations"]["/old/instructions"],
true,
);
assert.equal(
parsed["chat.instructionsFilesLocations"][
"/repo/home/resources/instructions"
],
true,
);
assert.equal(
parsed["chat.instructionsFilesLocations"]["~/.claude/rules"],
true,
);
});
test("second run is idempotent and keeps the file text unchanged", () => {
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "copilot-settings-"));
const targetFile = path.join(tempDir, "settings.json");
fs.writeFileSync(
targetFile,
`{
// user comment
"chat.promptFilesLocations": {
"/custom/prompts": true
}
}
`,
"utf8",
);
const firstRun = runMerge(targetFile);
assert.equal(firstRun.status, 0, firstRun.stderr);
const firstOutput = fs.readFileSync(targetFile, "utf8");
const secondRun = runMerge(targetFile);
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/);
});
test("creates a valid settings file from an empty target", () => {
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "copilot-settings-"));
const targetFile = path.join(tempDir, "settings.json");
fs.writeFileSync(targetFile, "{}\n", "utf8");
const result = runMerge(targetFile);
assert.equal(result.status, 0, result.stderr);
const parsed = parseJsonc(fs.readFileSync(targetFile, "utf8"));
assert.equal(
parsed["chat.agentFilesLocations"]["/repo/home/resources/agents"],
true,
);
assert.equal(
parsed["chat.instructionsFilesLocations"][
"/repo/home/resources/instructions"
],
true,
);
});