feat(settings): configure SQL file size limits

This commit is contained in:
t8y2 authored and GitHub committed 2026-09-08 19:42:32 +08:00
1 parent 9a0e3e744c
commit 39e34d44f7
34 files changed
+554 -79

No files matched your search

+45 -1
View File
@@ -1,7 +1,21 @@
// @vitest-environment happy-dom
import { describe, expect, it } from "vitest";
import { ExternalSqlFileTooLargeError, externalSqlFileDisplayTitles, externalSqlFileOpenErrorMessage, formatSqlFileSize, isSqlFilePath, MAX_EXTERNAL_SQL_EDITOR_FILE_BYTES, normalizeExternalSqlPath, readBrowserSqlFile } from "@/lib/sql/sqlFileOpen";
import {
ExternalSqlFileTooLargeError,
clampExternalSqlEditorMaxMbInput,
externalSqlEditorMaxBytes,
externalSqlFileDisplayTitles,
externalSqlFileOpenErrorMessage,
formatSqlFileSize,
isSqlFilePath,
MAX_EXTERNAL_SQL_EDITOR_FILE_BYTES,
MAX_EXTERNAL_SQL_EDITOR_FILE_MB,
MIN_EXTERNAL_SQL_EDITOR_FILE_MB,
normalizeExternalSqlEditorMaxMb,
normalizeExternalSqlPath,
readBrowserSqlFile,
} from "@/lib/sql/sqlFileOpen";
describe("external SQL file paths", () => {
it("normalizes Windows separators for identity checks", () => {
@@ -41,6 +55,36 @@ describe("external SQL file editor limit", () => {
});
});
it("honors a custom browser editor limit", async () => {
const file = new Blob(["select 1;"]);
Object.defineProperty(file, "size", { value: 8 });
await expect(readBrowserSqlFile(file, 4)).rejects.toMatchObject({
name: "ExternalSqlFileTooLargeError",
sizeBytes: 8,
maxSizeBytes: 4,
});
});
it("normalizes the configurable editor size limit in MB", () => {
expect(normalizeExternalSqlEditorMaxMb(undefined)).toBe(64);
expect(normalizeExternalSqlEditorMaxMb(0)).toBe(MIN_EXTERNAL_SQL_EDITOR_FILE_MB);
expect(normalizeExternalSqlEditorMaxMb(128)).toBe(128);
expect(normalizeExternalSqlEditorMaxMb(99999)).toBe(MAX_EXTERNAL_SQL_EDITOR_FILE_MB);
expect(normalizeExternalSqlEditorMaxMb(12.6)).toBe(13);
});
it("clamps the editor size limit input", () => {
expect(clampExternalSqlEditorMaxMbInput("abc")).toBe(MIN_EXTERNAL_SQL_EDITOR_FILE_MB);
expect(clampExternalSqlEditorMaxMbInput(0)).toBe(MIN_EXTERNAL_SQL_EDITOR_FILE_MB);
expect(clampExternalSqlEditorMaxMbInput(256)).toBe(256);
expect(clampExternalSqlEditorMaxMbInput(99999)).toBe(MAX_EXTERNAL_SQL_EDITOR_FILE_MB);
});
it("converts the editor size limit to bytes", () => {
expect(externalSqlEditorMaxBytes(64)).toBe(64 * 1024 * 1024);
});
it("formats large file sizes", () => {
expect(formatSqlFileSize(64 * 1024 * 1024)).toBe("64.0 MB");
expect(formatSqlFileSize(50 * 1024 * 1024 * 1024)).toBe("50.0 GB");