Skip to content

Commit ce72042

Browse files
test(cli): validate TOML editor across 60 configs
1 parent 1561d41 commit ce72042

3 files changed

Lines changed: 322 additions & 145 deletions

File tree

packages/cli/src/__tests__/setup.test.ts

Lines changed: 0 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import {
2626
readJsonConfig,
2727
writeJsonConfig,
2828
readTomlServerExists,
29-
patchTomlStdioApiKey,
3029
buildTomlServerBlock,
3130
appendTomlServer,
3231
removeTomlServer,
@@ -1244,144 +1243,4 @@ describe("agent config integration", () => {
12441243
expect(patched.cwd).toBe("/custom");
12451244
});
12461245
});
1247-
1248-
describe("patchTomlStdioApiKey", () => {
1249-
let tempDir: string;
1250-
1251-
beforeEach(async () => {
1252-
tempDir = join(tmpdir(), `ctx7-test-${Date.now()}`);
1253-
await mkdir(tempDir, { recursive: true });
1254-
});
1255-
1256-
afterEach(async () => {
1257-
await rm(tempDir, { recursive: true, force: true });
1258-
});
1259-
1260-
test("returns false for missing file", async () => {
1261-
expect(await patchTomlStdioApiKey(join(tempDir, "nope.toml"), "context7", "NEW")).toBe(false);
1262-
});
1263-
1264-
test("returns false for missing section", async () => {
1265-
const path = join(tempDir, "config.toml");
1266-
await writeFile(path, '[mcp_servers.other]\nurl = "https://other.com"\n', "utf-8");
1267-
expect(await patchTomlStdioApiKey(path, "context7", "NEW")).toBe(false);
1268-
});
1269-
1270-
test("patches a single-line stdio array", async () => {
1271-
const path = join(tempDir, "config.toml");
1272-
await writeFile(
1273-
path,
1274-
'[mcp_servers.context7]\ncommand = "npx"\nargs = ["-y", "@upstash/context7-mcp@latest", "--api-key", "OLD"]\n',
1275-
"utf-8"
1276-
);
1277-
expect(await patchTomlStdioApiKey(path, "context7", "NEW")).toBe(true);
1278-
const content = await readFile(path, "utf-8");
1279-
expect(content).toContain("@upstash/context7-mcp@latest");
1280-
expect(content).toContain('"--api-key", "NEW"');
1281-
expect(content).not.toContain('"OLD"');
1282-
});
1283-
1284-
test("handles CRLF, trailing commas, escapes, and brackets inside strings", async () => {
1285-
const path = join(tempDir, "config.toml");
1286-
await writeFile(
1287-
path,
1288-
'[mcp_servers.context7]\r\ncommand = "npx"\r\nargs = [\r\n "-y",\r\n "@upstash/context7-mcp@0.6.0",\r\n "label=[global],",\r\n "--api-key",\r\n "OLD\\u0021",\r\n]\r\n',
1289-
"utf-8"
1290-
);
1291-
1292-
expect(await patchTomlStdioApiKey(path, "context7", "NEW")).toBe(true);
1293-
const content = await readFile(path, "utf-8");
1294-
expect(content).toContain('"label=[global],"');
1295-
expect(content).toContain('\r\n "NEW",\r\n');
1296-
expect(content).not.toContain("OLD\\u0021");
1297-
});
1298-
1299-
test("adds or removes the API key without rewriting surrounding fields", async () => {
1300-
const path = join(tempDir, "config.toml");
1301-
const original =
1302-
'[mcp_servers.context7]\ncommand = "npx"\nargs = ["-y", "@upstash/context7-mcp@0.6.0"]\ncwd = "/custom"\n';
1303-
await writeFile(path, original, "utf-8");
1304-
1305-
expect(await patchTomlStdioApiKey(path, "context7", "NEW")).toBe(true);
1306-
const withKey = await readFile(path, "utf-8");
1307-
expect(withKey).toContain('args = ["-y","@upstash/context7-mcp@0.6.0","--api-key","NEW"]');
1308-
expect(withKey).toContain('cwd = "/custom"');
1309-
1310-
expect(await patchTomlStdioApiKey(path, "context7", undefined)).toBe(true);
1311-
const withoutKey = await readFile(path, "utf-8");
1312-
expect(withoutKey).toContain('args = ["-y","@upstash/context7-mcp@0.6.0"]');
1313-
expect(withoutKey).toContain('cwd = "/custom"');
1314-
expect(withoutKey).not.toContain("--api-key");
1315-
});
1316-
1317-
test("returns false for an HTTP entry", async () => {
1318-
const path = join(tempDir, "config.toml");
1319-
await writeFile(
1320-
path,
1321-
'[mcp_servers.context7]\ntype = "http"\nurl = "https://mcp.context7.com/mcp"\n\n[mcp_servers.context7.http_headers]\nCONTEXT7_API_KEY = "k"\n',
1322-
"utf-8"
1323-
);
1324-
expect(await patchTomlStdioApiKey(path, "context7", "NEW")).toBe(false);
1325-
});
1326-
1327-
test("patches multiline arrays while preserving surrounding TOML byte-for-byte", async () => {
1328-
const path = join(tempDir, "config.toml");
1329-
await writeFile(
1330-
path,
1331-
`model = "gpt-5.2-codex"
1332-
1333-
[mcp_servers.context7]
1334-
command = "npx"
1335-
args = [
1336-
'-y', # runner
1337-
"@upstash/context7-mcp@0.6.0", # pinned
1338-
"--api-key",
1339-
"OLD",
1340-
]
1341-
cwd = "/custom"
1342-
env = { FOO = "bar" }
1343-
1344-
[mcp_servers.context7.env]
1345-
BAZ = "qux"
1346-
1347-
[mcp_servers.filesystem]
1348-
command = "uvx"
1349-
`,
1350-
"utf-8"
1351-
);
1352-
expect(await patchTomlStdioApiKey(path, "context7", "NEW")).toBe(true);
1353-
const content = await readFile(path, "utf-8");
1354-
expect(content).toContain("@upstash/context7-mcp@0.6.0");
1355-
expect(content).toContain("# runner");
1356-
expect(content).toContain("# pinned");
1357-
expect(content).toContain('env = { FOO = "bar" }');
1358-
expect(content).toContain('[mcp_servers.context7.env]\nBAZ = "qux"');
1359-
expect(content).toContain('[mcp_servers.filesystem]\ncommand = "uvx"');
1360-
expect(content).toContain(' "NEW",');
1361-
expect(content).not.toContain(' "OLD",');
1362-
});
1363-
1364-
test("fails closed when an existing args array cannot be parsed safely", async () => {
1365-
const path = join(tempDir, "config.toml");
1366-
const original =
1367-
'[mcp_servers.context7]\ncommand = "npx"\nargs = ["@upstash/context7-mcp", 42]\n';
1368-
await writeFile(path, original, "utf-8");
1369-
1370-
await expect(patchTomlStdioApiKey(path, "context7", "NEW")).rejects.toThrow(
1371-
"MCP args must be a TOML array containing only strings"
1372-
);
1373-
expect(await readFile(path, "utf-8")).toBe(original);
1374-
});
1375-
1376-
test("fails closed instead of replacing an existing stdio entry without args", async () => {
1377-
const path = join(tempDir, "config.toml");
1378-
const original = '[mcp_servers.context7]\ncommand = "custom-context7-wrapper"\n';
1379-
await writeFile(path, original, "utf-8");
1380-
1381-
await expect(patchTomlStdioApiKey(path, "context7", "NEW")).rejects.toThrow(
1382-
"Existing MCP stdio entry has no safely editable args array"
1383-
);
1384-
expect(await readFile(path, "utf-8")).toBe(original);
1385-
});
1386-
});
13871246
});

0 commit comments

Comments
 (0)