feat(mongo): open and save js files in mongo tabs

This commit is contained in:
lxk955 authored and GitHub committed 2026-10-01 22:53:16 +08:00
1 parent 906f5c271c
commit 51b8342f5d
7 files changed
+152 -36

No files matched your search

@@ -56,6 +56,18 @@ describe("external SQL file targets", () => {
});
});
it("reuses the active MongoDB tab context for a script file only when explicitly allowed", () => {
const tabs = [{ id: "mongo-tab", connectionId: "mongo-connection", database: "app_db", mode: "query" as const }];
const lookup = () => ({ db_type: "mongodb" as const });
const target = { connectionId: "mongo-connection", database: "app_db", catalog: undefined, schema: undefined };
expect(activeTabExternalSqlFileTarget(tabs, "mongo-tab", lookup)).toEqual(unassociatedExternalSqlFileTarget());
expect(resolveExternalSqlFileTargetForActiveTab("/work/query.js", tabs, "mongo-tab", lookup)).toEqual(unassociatedExternalSqlFileTarget());
expect(activeTabExternalSqlFileTarget(tabs, "mongo-tab", lookup, { allowMongoScripts: true })).toEqual(target);
expect(resolveExternalSqlFileTargetForActiveTab("/work/query.js", tabs, "mongo-tab", lookup, { allowMongoScripts: true })).toEqual(target);
expect(activeTabExternalSqlFileTarget(tabs, "mongo-tab", () => ({ db_type: "redis" }), { allowMongoScripts: true })).toEqual(unassociatedExternalSqlFileTarget());
});
it("keeps the fallback unassociated without an active tab", () => {
const tabs = [{ id: "inactive-tab", connectionId: "sql-connection", database: "analytics", mode: "query" as const }];
@@ -63,7 +75,7 @@ describe("external SQL file targets", () => {
expect(activeTabExternalSqlFileTarget(tabs, "missing-tab", () => ({ db_type: "postgres" }))).toEqual(unassociatedExternalSqlFileTarget());
});
it.each(["mq", "zookeeper", "plugin", "redis"] as const)("does not reuse a non-SQL %s tab target", (dbType) => {
it.each(["mq", "zookeeper", "plugin", "redis", "mongodb"] as const)("does not reuse a non-SQL %s tab target", (dbType) => {
const tabs = [{ id: "active-tab", connectionId: "non-sql-connection", database: "invalid", catalog: "invalid", schema: "invalid", mode: "query" as const }];
expect(activeTabExternalSqlFileTarget(tabs, "active-tab", () => ({ db_type: dbType }))).toEqual(unassociatedExternalSqlFileTarget());
+37
View File
@@ -2,18 +2,22 @@
import { describe, expect, it } from "vitest";
import {
defaultSavedQueryFileName,
ExternalSqlFileTooLargeError,
clampExternalSqlEditorMaxMbInput,
externalSqlEditorMaxBytes,
externalSqlFileDisplayTitles,
externalSqlFileOpenErrorMessage,
formatSqlFileSize,
isScriptFilePath,
isSqlFilePath,
MAX_EXTERNAL_SQL_EDITOR_FILE_BYTES,
MAX_EXTERNAL_SQL_EDITOR_FILE_MB,
MIN_EXTERNAL_SQL_EDITOR_FILE_MB,
normalizeExternalSqlEditorMaxMb,
normalizeExternalSqlPath,
queryEditorOpenFileAccept,
queryEditorOpenFileFilters,
readBrowserSqlFile,
} from "@/lib/sql/sqlFileOpen";
@@ -27,6 +31,12 @@ describe("external SQL file paths", () => {
expect(isSqlFilePath("/work/script.py")).toBe(false);
});
it("distinguishes script files (.sql and .js) from unsupported files", () => {
expect(isScriptFilePath("C:\\work\\demo.SQL")).toBe(true);
expect(isScriptFilePath("/work/mongo_query.js")).toBe(true);
expect(isScriptFilePath("/work/script.py")).toBe(false);
});
it("uses the shortest unique parent path for duplicate filenames", () => {
expect(externalSqlFileDisplayTitles(["/work/demo/create.sql", "/work/learn/create.sql", "/work/query.sql"])).toEqual(["demo/create.sql", "learn/create.sql", "query.sql"]);
});
@@ -36,6 +46,33 @@ describe("external SQL file paths", () => {
});
});
describe("query editor script file filters and names", () => {
it("provides MongoDB script filters for MongoDB database type", () => {
const filters = queryEditorOpenFileFilters("mongodb");
expect(filters).toEqual([
{ name: "MongoDB Script", extensions: ["js"] },
{ name: "SQL", extensions: ["sql"] },
{ name: "All Files", extensions: ["*"] },
]);
expect(queryEditorOpenFileAccept("mongodb")).toBe(".js,.sql");
});
it("keeps non-MongoDB pickers SQL-only", () => {
expect(queryEditorOpenFileFilters("postgres")).toEqual([{ name: "SQL", extensions: ["sql"] }]);
expect(queryEditorOpenFileFilters(undefined)).toEqual([{ name: "SQL", extensions: ["sql"] }]);
expect(queryEditorOpenFileAccept("postgres")).toBe(".sql");
expect(queryEditorOpenFileAccept(undefined)).toBe(".sql");
});
it("generates correct default save file names for MongoDB vs SQL", () => {
expect(defaultSavedQueryFileName("query_1", "mongodb")).toBe("query_1.js");
expect(defaultSavedQueryFileName("users list", "mongodb")).toBe("users_list.js");
expect(defaultSavedQueryFileName("existing.js", "mongodb")).toBe("existing.js");
expect(defaultSavedQueryFileName("query_1", "mysql")).toBe("query_1.sql");
expect(defaultSavedQueryFileName("query_1")).toBe("query_1.sql");
});
});
describe("external SQL file editor limit", () => {
it("accepts the exact browser editor limit", async () => {
const file = new Blob(["select 1;"]);