feat(mongodb): suggest accepted values for field-to-value maps

This commit is contained in:
Loc Nguyen authored and GitHub committed 2026-09-28 21:07:02 +08:00
1 parent ad1e5c9496
commit d1446360cc
4 files changed
+96 -16

No files matched your search

+40 -2
View File
@@ -1,7 +1,7 @@
import { strict as assert } from "node:assert";
import { test } from "vitest";
import { buildMongoCompletionItems, getMongoCompletionContext, getMongoCompletionResultValidFor, inferMongoCompletionFields, shouldAutoOpenMongoCompletion } from "../../apps/desktop/src/lib/mongo/mongoCompletion.ts";
import { ACCUMULATORS, BULK_WRITE_OPERATION_FIELDS, BULK_WRITE_OPERATIONS, EXPRESSION_OPERATORS, EXTENDED_JSON_VALUES, METHOD_OPTION_KEYS, PIPELINE_STAGES, PUSH_MODIFIERS, QUERY_OPERATORS, STAGE_OPTION_KEYS, UPDATE_OPERATORS, VALUE_SNIPPETS } from "../../apps/desktop/src/lib/mongo/mongoCompletionTables.ts";
import { buildMongoCompletionItems, buildMongoCompletionItemsFromContext, getMongoCompletionContext, getMongoCompletionResultValidFor, getMongoDocumentQueryCompletionContext, inferMongoCompletionFields, shouldAutoOpenMongoCompletion } from "../../apps/desktop/src/lib/mongo/mongoCompletion.ts";
import { ACCUMULATORS, BULK_WRITE_OPERATION_FIELDS, BULK_WRITE_OPERATIONS, EXPRESSION_OPERATORS, EXTENDED_JSON_VALUES, KEY_MAP_VALUES, METHOD_OPTION_KEYS, PIPELINE_STAGES, PUSH_MODIFIERS, QUERY_OPERATORS, STAGE_OPTION_KEYS, UPDATE_OPERATORS, VALUE_SNIPPETS } from "../../apps/desktop/src/lib/mongo/mongoCompletionTables.ts";
import { parseMongoCommand } from "../../apps/desktop/src/lib/mongo/mongoShellCommand.ts";
const collections = ["users", "user_events", "order-items", "audit.logs"];
@@ -589,6 +589,44 @@ test("every suggested option key parses on the method that offers it", () => {
}
});
test("suggests the values a field-to-value map accepts", () => {
assert.deepEqual(labels("db.users.find({}).sort({ name: ", { fields }), ["-1", "1"]);
assert.deepEqual(labels("db.users.aggregate([{ $sort: { name: ", { fields }), ["-1", "1"]);
assert.deepEqual(labels("db.users.find({}, { name: ", { fields }), ["0", "1"]);
assert.deepEqual(labels("db.users.findOne({}, { name: ", { fields }), ["0", "1"]);
assert.deepEqual(labels("db.users.createIndex({ name: ", { fields }), ["-1", '"2d"', '"2dsphere"', '"hashed"', '"text"', "1"]);
// The sort and projection options of the find-and-modify helpers are the same maps.
assert.deepEqual(labels("db.users.findOneAndUpdate({}, {$set:{a:1}}, { sort: { name: ", { fields }), ["-1", "1"]);
assert.deepEqual(labels("db.users.findOneAndDelete({}, { projection: { name: ", { fields }), ["0", "1"]);
// Keys are still field names, and a second key in the same map still gets values.
assert.deepEqual(labels("db.users.find({}).sort({ na", { fields }), ["name"]);
assert.deepEqual(labels("db.users.find({}).sort({ name: 1, createdAt: ", { fields }), ["-1", "1"]);
// Inside a quote the engine stays quiet, as it does for every other value position.
assert.deepEqual(labels('db.users.createIndex({ name: "', { fields }), []);
// The document browser's sort bar is the same map without a surrounding command.
const sortBar = getMongoDocumentQueryCompletionContext("{ name: ", "{ name: ".length, "sortKeys");
assert.deepEqual(buildMongoCompletionItemsFromContext(sortBar, { fields }).map((item) => item.label), ["-1", "1"]);
});
test("every suggested map value parses in the position that offers it", () => {
const commands: Record<string, (value: string) => string> = {
sort: (value) => `db.users.find({}).sort({ name: ${value} })`,
projection: (value) => `db.users.find({}, { name: ${value} })`,
index: (value) => `db.users.createIndex({ name: ${value} })`,
};
for (const [keyMap, values] of Object.entries(KEY_MAP_VALUES)) {
const build = commands[keyMap];
assert.ok(build, `${keyMap} needs a sample command in this test`);
for (const value of values) {
assert.ok(parseMongoCommand(build(value.label)), build(value.label));
}
}
});
test("offers nothing rather than top-level snippets inside an unmodelled argument", () => {
// `db.collection.find` is not something that can be typed inside these parentheses, so the
// top-level snippets are noise there; the engine stays quiet until the argument is modelled.