2025-02-26 18:58:08 +02:00
|
|
|
import { beforeAll, describe, expect, it, vi } from "vitest";
|
2025-02-20 20:38:58 +02:00
|
|
|
import fs from "fs";
|
|
|
|
import path from "path";
|
|
|
|
import { fileURLToPath } from "url";
|
|
|
|
import { dirname } from "path";
|
|
|
|
import becca from "../../becca/becca.js";
|
|
|
|
import BNote from "../../becca/entities/bnote.js";
|
|
|
|
import TaskContext from "../task_context.js";
|
|
|
|
import cls from "../cls.js";
|
|
|
|
import sql_init from "../sql_init.js";
|
|
|
|
import { initializeTranslations } from "../i18n.js";
|
|
|
|
import single from "./single.js";
|
2025-02-22 01:01:15 +02:00
|
|
|
import stripBom from "strip-bom";
|
2025-02-20 20:38:58 +02:00
|
|
|
const scriptDir = dirname(fileURLToPath(import.meta.url));
|
|
|
|
|
2025-02-22 01:01:15 +02:00
|
|
|
async function testImport(fileName: string, mimetype: string) {
|
|
|
|
const buffer = fs.readFileSync(path.join(scriptDir, "samples", fileName));
|
2025-02-22 00:50:19 +02:00
|
|
|
const taskContext = TaskContext.getInstance("import-mdx", "import", {
|
2025-02-22 01:01:15 +02:00
|
|
|
textImportedAsText: true,
|
|
|
|
codeImportedAsCode: true
|
2025-02-22 00:50:19 +02:00
|
|
|
});
|
|
|
|
|
2025-03-02 20:47:57 +01:00
|
|
|
return new Promise<{ buffer: Buffer; importedNote: BNote }>((resolve, reject) => {
|
2025-02-22 00:50:19 +02:00
|
|
|
cls.init(async () => {
|
|
|
|
const rootNote = becca.getNote("root");
|
|
|
|
if (!rootNote) {
|
|
|
|
reject("Missing root note.");
|
|
|
|
}
|
|
|
|
|
2025-03-02 20:47:57 +01:00
|
|
|
const importedNote = single.importSingleFile(
|
|
|
|
taskContext,
|
|
|
|
{
|
|
|
|
originalname: fileName,
|
|
|
|
mimetype,
|
|
|
|
buffer: buffer
|
|
|
|
},
|
|
|
|
rootNote as BNote
|
|
|
|
);
|
2025-02-22 01:01:15 +02:00
|
|
|
resolve({
|
|
|
|
buffer,
|
|
|
|
importedNote
|
|
|
|
});
|
2025-02-20 20:38:58 +02:00
|
|
|
});
|
2025-02-22 00:50:19 +02:00
|
|
|
});
|
|
|
|
}
|
2025-02-20 20:38:58 +02:00
|
|
|
|
2025-02-22 00:50:19 +02:00
|
|
|
describe("processNoteContent", () => {
|
|
|
|
beforeAll(async () => {
|
2025-02-26 18:58:08 +02:00
|
|
|
// Prevent download of images.
|
|
|
|
vi.mock("../image.js", () => {
|
|
|
|
return {
|
|
|
|
default: { saveImageToAttachment: () => {} }
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2025-02-22 00:50:19 +02:00
|
|
|
initializeTranslations();
|
|
|
|
sql_init.initializeDb();
|
|
|
|
await sql_init.dbReady;
|
|
|
|
});
|
2025-02-20 20:38:58 +02:00
|
|
|
|
2025-02-22 00:50:19 +02:00
|
|
|
it("treats single MDX as Markdown", async () => {
|
2025-02-22 01:01:15 +02:00
|
|
|
const { importedNote } = await testImport("Text Note.mdx", "text/mdx");
|
2025-02-22 00:50:19 +02:00
|
|
|
expect(importedNote.mime).toBe("text/html");
|
|
|
|
expect(importedNote.type).toBe("text");
|
|
|
|
expect(importedNote.title).toBe("Text Note");
|
|
|
|
});
|
2025-02-20 20:38:58 +02:00
|
|
|
|
2025-02-22 00:50:19 +02:00
|
|
|
it("supports HTML note with UTF-16 (w/ BOM) from Microsoft Outlook", async () => {
|
2025-02-22 01:01:15 +02:00
|
|
|
const { importedNote } = await testImport("IREN Reports Q2 FY25 Results.htm", "text/html");
|
2025-02-22 00:50:19 +02:00
|
|
|
expect(importedNote.mime).toBe("text/html");
|
|
|
|
expect(importedNote.title).toBe("IREN Reports Q2 FY25 Results");
|
|
|
|
expect(importedNote.getContent().toString().substring(0, 5)).toEqual("<html");
|
2025-02-20 20:38:58 +02:00
|
|
|
});
|
2025-02-22 01:01:15 +02:00
|
|
|
|
|
|
|
it("supports code note with UTF-16", async () => {
|
|
|
|
const { importedNote, buffer } = await testImport("UTF-16LE Code Note.json", "application/json");
|
|
|
|
expect(importedNote.mime).toBe("application/json");
|
|
|
|
expect(importedNote.getContent().toString()).toStrictEqual(stripBom(buffer.toString("utf-16le")));
|
|
|
|
});
|
2025-02-22 01:06:25 +02:00
|
|
|
|
|
|
|
it("supports plain text note with UTF-16", async () => {
|
|
|
|
const { importedNote } = await testImport("UTF-16LE Text Note.txt", "text/plain");
|
|
|
|
expect(importedNote.mime).toBe("text/html");
|
|
|
|
expect(importedNote.getContent().toString()).toBe("<p>Plain text goes here.<br></p>");
|
|
|
|
});
|
2025-02-22 01:09:24 +02:00
|
|
|
|
|
|
|
it("supports markdown note with UTF-16", async () => {
|
|
|
|
const { importedNote } = await testImport("UTF-16LE Text Note.md", "text/markdown");
|
|
|
|
expect(importedNote.mime).toBe("text/html");
|
|
|
|
expect(importedNote.getContent().toString()).toBe("<h2>Hello world</h2>\n<p>Plain text goes here.</p>\n");
|
|
|
|
});
|
2025-03-02 20:47:57 +01:00
|
|
|
});
|