From 517bfd2c9a4fb61cd3689e72e202ae87f51ac552 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sun, 28 Sep 2025 18:54:57 +0300 Subject: [PATCH] test(server/note_map): clipper processing notes & images --- apps/server/src/routes/api/clipper.spec.ts | 50 ++++++++++++++++++++++ apps/server/src/routes/api/clipper.ts | 2 +- apps/server/src/test/becca_easy_mocking.ts | 4 +- 3 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 apps/server/src/routes/api/clipper.spec.ts diff --git a/apps/server/src/routes/api/clipper.spec.ts b/apps/server/src/routes/api/clipper.spec.ts new file mode 100644 index 000000000..6df445dd0 --- /dev/null +++ b/apps/server/src/routes/api/clipper.spec.ts @@ -0,0 +1,50 @@ +import { BNote } from "../../services/backend_script_entrypoint"; +import { buildNote } from "../../test/becca_easy_mocking"; +import { processContent } from "./clipper"; + +let note!: BNote; + +describe("processContent", () => { + beforeAll(() => { + note = buildNote({}); + note.saveAttachment = () => {}; + vi.mock("../../services/image.js", () => ({ + default: { + saveImageToAttachment() { + return { + attachmentId: "foo", + title: "encodedTitle", + } + } + } + })); + }); + + it("processes basic note", () => { + const processed = processContent([], note, "

Hello world.

"); + expect(processed).toStrictEqual("

Hello world.

") + }); + + it("processes plain text", () => { + const processed = processContent([], note, "Hello world."); + expect(processed).toStrictEqual("

Hello world.

") + }); + + it("replaces images", () => { + const processed = processContent( + [{"imageId":"OKZxZA3MonZJkwFcEhId","src":"inline.png","dataUrl":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAkAAAAQCAYAAADESFVDAAAAF0lEQVQoU2P8DwQMBADjqKLRIGAgKggAzHs/0SoYCGwAAAAASUVORK5CYII="}], + note, `` + ); + expect(processed).toStrictEqual(``); + }); + + it("skips over non-data images", () => { + for (const url of [ "foo", "" ]) { + const processed = processContent( + [{"imageId":"OKZxZA3MonZJkwFcEhId","src":"inline.png","dataUrl": url}], + note, `` + ); + expect(processed).toStrictEqual(``); + } + }); +}); diff --git a/apps/server/src/routes/api/clipper.ts b/apps/server/src/routes/api/clipper.ts index 2535a26e2..0b87aabd6 100644 --- a/apps/server/src/routes/api/clipper.ts +++ b/apps/server/src/routes/api/clipper.ts @@ -147,7 +147,7 @@ async function createNote(req: Request) { }; } -function processContent(images: Image[], note: BNote, content: string) { +export function processContent(images: Image[], note: BNote, content: string) { let rewrittenContent = htmlSanitizer.sanitize(content); if (images) { diff --git a/apps/server/src/test/becca_easy_mocking.ts b/apps/server/src/test/becca_easy_mocking.ts index 579ded515..6df198a5a 100644 --- a/apps/server/src/test/becca_easy_mocking.ts +++ b/apps/server/src/test/becca_easy_mocking.ts @@ -7,7 +7,7 @@ type RelationDefinitions = { [key in `~${string}`]: string; }; interface NoteDefinition extends AttributeDefinitions, RelationDefinitions { id?: string | undefined; - title: string; + title?: string; content?: string; } @@ -39,7 +39,7 @@ export function buildNotes(notes: NoteDefinition[]) { export function buildNote(noteDef: NoteDefinition) { const note = new BNote({ noteId: noteDef.id ?? utils.randomString(12), - title: noteDef.title, + title: noteDef.title ?? "New note", type: "text", mime: "text/html", isProtected: false,