test(server/note_map): clipper processing notes & images

This commit is contained in:
Elian Doran
2025-09-28 18:54:57 +03:00
parent 151a2c284d
commit 517bfd2c9a
3 changed files with 53 additions and 3 deletions

View File

@@ -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, "<p>Hello world.</p>");
expect(processed).toStrictEqual("<p>Hello world.</p>")
});
it("processes plain text", () => {
const processed = processContent([], note, "Hello world.");
expect(processed).toStrictEqual("<p>Hello world.</p>")
});
it("replaces images", () => {
const processed = processContent(
[{"imageId":"OKZxZA3MonZJkwFcEhId","src":"inline.png","dataUrl":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAkAAAAQCAYAAADESFVDAAAAF0lEQVQoU2P8DwQMBADjqKLRIGAgKggAzHs/0SoYCGwAAAAASUVORK5CYII="}],
note, `<img src="OKZxZA3MonZJkwFcEhId">`
);
expect(processed).toStrictEqual(`<img src="api/attachments/foo/image/encodedTitle">`);
});
it("skips over non-data images", () => {
for (const url of [ "foo", "" ]) {
const processed = processContent(
[{"imageId":"OKZxZA3MonZJkwFcEhId","src":"inline.png","dataUrl": url}],
note, `<img src="OKZxZA3MonZJkwFcEhId">`
);
expect(processed).toStrictEqual(`<img src="OKZxZA3MonZJkwFcEhId">`);
}
});
});

View File

@@ -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) {

View File

@@ -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,