fix(mongo): ignore comments in shell JSON arguments

This commit is contained in:
James Leong
2026-07-22 15:10:40 +08:00
committed by GitHub
parent 94a40a8025
commit 63f375a7d8
3 changed files with 175 additions and 30 deletions
+88 -12
View File
@@ -41,18 +41,9 @@ test("parseMongoFindCommand parses db collection find with an empty JSON filter"
});
test("normalizeRustMongoCommand preserves the desktop command contract", () => {
assert.deepEqual(
normalizeRustMongoCommand({ kind: "countDocuments", collection: "users", filter: "{}", accurate: false }),
{ kind: "countDocuments", collection: "users", filter: "{}", mode: "legacy" },
);
assert.deepEqual(
normalizeRustMongoCommand({ kind: "dropIndexes", collection: "users", indexes: '"email_1"', single: true }),
{ kind: "dropIndex", collection: "users", index: '"email_1"' },
);
assert.deepEqual(
normalizeRustMongoCommand({ kind: "findOne", collection: "users", filter: "{}", projection: null, options: null }),
{ kind: "findOne", collection: "users", filter: "{}" },
);
assert.deepEqual(normalizeRustMongoCommand({ kind: "countDocuments", collection: "users", filter: "{}", accurate: false }), { kind: "countDocuments", collection: "users", filter: "{}", mode: "legacy" });
assert.deepEqual(normalizeRustMongoCommand({ kind: "dropIndexes", collection: "users", indexes: '"email_1"', single: true }), { kind: "dropIndex", collection: "users", index: '"email_1"' });
assert.deepEqual(normalizeRustMongoCommand({ kind: "findOne", collection: "users", filter: "{}", projection: null, options: null }), { kind: "findOne", collection: "users", filter: "{}" });
});
test("parseMongoFindCommand parses getCollection find with chained sort skip and limit", () => {
@@ -533,6 +524,91 @@ test("parseMongoAggregateCommand accepts an empty pipeline", () => {
});
});
test("parseMongoAggregateCommand ignores comments inside aggregate pipelines", () => {
const command = parseMongoAggregateCommand(`
db.cash.aggregate([
{
$match: {
portfolio_id: "b6f6ec62-8571-11f1-bbfb-000c29caf77f",
date: { $gte: "20260604" }
}
},
{ $unwind: "$cashs" },
{
$project: {
_id: 0,
date: 1,
trans_currency_cd: "$cashs.trans_currency_cd",
cash_local: { $multiply: ["$cashs.cash", "$cashs.fx_rate"] }
}
},
{
$group: {
_id: { date: "$date", currency: "$trans_currency_cd" },
cash_local: { $sum: "$cash_local" }
}
},
// 第二步:按 date 分组,将不同币种转为字段
{
$group: {
_id: "$_id.date",
cny: {
$sum: {
$cond: [{ $eq: ["$_id.currency", "CNY"] }, "$cash_local", 0]
}
},
hkd: {
$sum: {
$cond: [{ $eq: ["$_id.currency", "HKD"] }, "$cash_local", 0]
}
},
total_cash: { $sum: "$cash_local" }
}
},
{ $sort: { _id: 1 } },
/* 可选:将 _id 重命名为 date */
{
$project: {
_id: 0,
date: "$_id",
cny: 1,
hkd: 1,
total_cash: 1
}
}
])
`);
assert.ok(command);
assert.equal(command.collection, "cash");
const pipeline = JSON.parse(command.pipeline);
assert.equal(pipeline.length, 7);
assert.deepEqual(pipeline[4].$group.total_cash, { $sum: "$cash_local" });
});
test("parseMongoAggregateCommand keeps comment markers inside string values", () => {
const command = parseMongoAggregateCommand(`db.logs.aggregate([
{ $match: { url: "https://example.com/a//b", note: "literal /* text */" } },
// comment with closing delimiters )]}
{ $project: { url: 1, note: 1 } }
])`);
assert.ok(command);
assert.deepEqual(JSON.parse(command.pipeline)[0].$match, {
url: "https://example.com/a//b",
note: "literal /* text */",
});
});
test("parseMongoAggregateCommand accepts every JavaScript line terminator after comments", () => {
for (const lineTerminator of ["\n", "\r", "\r\n", "\u2028", "\u2029"]) {
const command = parseMongoAggregateCommand(`db.logs.aggregate([// pipeline${lineTerminator}{ $match: { active: true } }])`);
assert.ok(command, `expected parser result for ${JSON.stringify(lineTerminator)}`);
assert.deepEqual(JSON.parse(command.pipeline), [{ $match: { active: true } }]);
}
});
test("parseMongoAggregateCommand accepts official aggregate options document", () => {
assert.deepEqual(parseMongoAggregateCommand("db.products.aggregate([], {})"), {
collection: "products",