feat(import/zip): support UTF-16 LE with BOM (closes #1241)

This commit is contained in:
Elian Doran
2025-02-22 01:37:02 +02:00
parent c925ae5f15
commit bedc61c3d0
5 changed files with 73 additions and 47 deletions

View File

@@ -1,4 +1,4 @@
import { describe, expect, it } from "vitest";
import { beforeAll, describe, expect, it } from "vitest";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
@@ -12,35 +12,46 @@ import sql_init from "../sql_init.js";
import { initializeTranslations } from "../i18n.js";
const scriptDir = dirname(fileURLToPath(import.meta.url));
describe("processNoteContent", () => {
it("treats single MDX as Markdown in ZIP as text note", async () => {
const mdxSample = fs.readFileSync(path.join(scriptDir, "samples", "mdx.zip"));
const taskContext = TaskContext.getInstance("import-mdx", "import", {
textImportedAsText: true
});
async function testImport(fileName: string) {
const mdxSample = fs.readFileSync(path.join(scriptDir, "samples", fileName));
const taskContext = TaskContext.getInstance("import-mdx", "import", {
textImportedAsText: true
});
await new Promise<void>((resolve, reject) => {
cls.init(async () => {
initializeTranslations();
sql_init.initializeDb();
await sql_init.dbReady;
return new Promise<{ importedNote: BNote; rootNote: BNote }>((resolve, reject) => {
cls.init(async () => {
const rootNote = becca.getNote("root");
if (!rootNote) {
expect(rootNote).toBeTruthy();
return;
}
const rootNote = becca.getNote("root");
if (!rootNote) {
expect(rootNote).toBeTruthy();
return;
}
const importedNote = await zip.importZip(taskContext, mdxSample, rootNote as BNote);
try {
expect(importedNote.mime).toBe("text/mdx");
expect(importedNote.type).toBe("text");
expect(importedNote.title).toBe("Text Note");
} catch (e) {
reject(e);
}
resolve();
const importedNote = await zip.importZip(taskContext, mdxSample, rootNote as BNote);
resolve({
importedNote,
rootNote
});
});
});
}
describe("processNoteContent", () => {
beforeAll(async () => {
initializeTranslations();
sql_init.initializeDb();
await sql_init.dbReady;
});
it("treats single MDX as Markdown in ZIP as text note", async () => {
const { importedNote } = await testImport("mdx.zip");
expect(importedNote.mime).toBe("text/mdx");
expect(importedNote.type).toBe("text");
expect(importedNote.title).toBe("Text Note");
});
it("can import email from Microsoft Outlook with UTF-16 with BOM", async () => {
const { rootNote, importedNote } = await testImport("IREN.Reports.Q2.FY25.Results_files.zip");
const htmlNote = rootNote.children.find((ch) => ch.title === "IREN Reports Q2 FY25 Results");
expect(htmlNote?.getContent().toString().substring(0, 4)).toEqual("<div");
});
})