Skip to content

Build your personal knowledge base with Trilium Notes

19.7 KB
Raw Blame
import { describe, expect, it } from "vitest";

import {
    CALENDAR_EVENT_ICON, CANVAS_ATTACHMENT_MIME, GEO_LOCATION_ATTRIBUTE, GEO_SHAPE_ATTRIBUTE,
    getAttachedFileMime, getContentRendererGroup, getDrawingPictureTitle,
    getImageAttachmentTitle, getMimeIcon, getNoteContentType, getNoteIcon, isFullWidthNoteType,
    MAP_POSITION_ATTRIBUTE, MAP_SHAPE_ATTRIBUTE, MEETING_RECORDING_ICON, MEETING_RECORDING_LABEL,
    MERMAID_ATTACHMENT_MIME, NOTE_TYPE_ICONS, NOTE_TYPE_IMAGE_ATTACHMENTS, parseMindMapNoteLink
} from "./notes.js";
import { NoteType } from "./rows.js";

function buildArgs(overrides: {
    noteId?: string;
    type?: NoteType;
    mime?: string;
    iconClass?: string | undefined;
    workspaceIconClass?: string | undefined;
    isFolder?: () => boolean;
    getLabelValue?: (name: string) => string | null;
}) {
    return {
        noteId: "abc123",
        type: "text" as NoteType,
        mime: "",
        iconClass: undefined,
        workspaceIconClass: undefined,
        isFolder: () => false,
        getLabelValue: () => null,
        ...overrides
    };
}

describe("NOTE_TYPE_ICONS", () => {
    it("maps note types to their boxicon classes", () => {
        expect(NOTE_TYPE_ICONS.file).toBe("bx bx-file");
        expect(NOTE_TYPE_ICONS.image).toBe("bx bx-image");
        expect(NOTE_TYPE_ICONS.code).toBe("bx bx-code");
        expect(NOTE_TYPE_ICONS.book).toBe("bx bx-book");
        expect(NOTE_TYPE_ICONS.mermaid).toBe("bx bx-selection");
        expect(NOTE_TYPE_ICONS.mindMap).toBe("bx bx-sitemap");
        expect(NOTE_TYPE_ICONS.llmChat).toBe("bx bx-message-square-dots");
    });
});

describe("getImageAttachmentTitle", () => {
    // These titles are a wire contract: the type widgets write attachments under them, the
    // `api/images` routes look them up by title, and the ZIP export/import pair resolves embeds
    // through them. Spelled out literally rather than read back off the table, so a typo in
    // NOTE_TYPE_IMAGE_ATTACHMENTS fails here instead of silently breaking every one of them.
    it("resolves the note types whose image lives in a generated attachment", () => {
        expect(getImageAttachmentTitle("canvas")).toBe("canvas-export.svg");
        expect(getImageAttachmentTitle("mermaid")).toBe("mermaid-export.svg");
        expect(getImageAttachmentTitle("mindMap")).toBe("mindmap-export.svg");
        expect(getImageAttachmentTitle("spreadsheet")).toBe("spreadsheet-export.png");

        // Indexing the table directly narrows to the literal, for statically known types.
        expect(NOTE_TYPE_IMAGE_ATTACHMENTS.mermaid).toBe("mermaid-export.svg");
    });

    it("returns undefined for note types that carry their image as content, and for no type", () => {
        // An image note is served straight from its own content, so it has no such attachment.
        expect(getImageAttachmentTitle("image")).toBeUndefined();
        expect(getImageAttachmentTitle("text")).toBeUndefined();
        expect(getImageAttachmentTitle(null)).toBeUndefined();
        expect(getImageAttachmentTitle(undefined)).toBeUndefined();
    });
});

describe("getDrawingPictureTitle", () => {
    it("titles the picture of a canvas drawing after the attachment of the drawing", () => {
        expect(getDrawingPictureTitle("drawing1")).toBe("drawing1.svg");
    });
});

describe("getNoteIcon", () => {
    it("returns the explicit iconClass when provided", () => {
        const icon = getNoteIcon(buildArgs({ iconClass: "bx bx-custom", workspaceIconClass: "bx bx-ws" }));
        expect(icon).toBe("bx bx-custom");
    });

    it("returns the workspaceIconClass when no iconClass is provided", () => {
        const icon = getNoteIcon(buildArgs({ workspaceIconClass: "bx bx-workspace" }));
        expect(icon).toBe("bx bx-workspace");
    });

    it("returns the home icon for the root note", () => {
        const icon = getNoteIcon(buildArgs({ noteId: "root" }));
        expect(icon).toBe("bx bx-home-alt-2");
    });

    it("returns the share icon for the _share note", () => {
        const icon = getNoteIcon(buildArgs({ noteId: "_share", type: "doc" }));
        expect(icon).toBe("bx bx-share-alt");
    });

    it("returns the folder icon for a text note that is a folder", () => {
        const icon = getNoteIcon(buildArgs({ type: "text", isFolder: () => true }));
        expect(icon).toBe("bx bx-folder");
    });

    it("draws a located text note as a pin, ahead of the folder icon but behind its own", () => {
        // The pin is what a marker wears without one being written onto it, so an icon the geo map
        // hands down through `#child:iconClass` or a template still applies (see the map's api).
        const located = (value = "48.85,2.36") =>
            (name: string) => name === GEO_LOCATION_ATTRIBUTE ? value : null;

        expect(getNoteIcon(buildArgs({ getLabelValue: located() }))).toBe("bx bx-pin");
        expect(getNoteIcon(buildArgs({ getLabelValue: located(), isFolder: () => true })))
            .toBe("bx bx-pin");
        expect(getNoteIcon(buildArgs({ getLabelValue: located(), iconClass: "bx bx-store" })))
            .toBe("bx bx-store");
        // Taking a marker off the map empties the label rather than removing it (see moveMarker in
        // the geo map's api), and a note that stands nowhere is a plain note again.
        expect(getNoteIcon(buildArgs({ getLabelValue: located("") }))).toBe("bx bx-note");
        // Only the generic note icon is displaced: a file put on the map still says what it holds.
        expect(getNoteIcon(buildArgs({
            type: "file", mime: "application/gpx+xml", getLabelValue: located()
        }))).toBe("bx bx-trip");
    });

    it("draws a web clipping by its clip type, behind its own icon", () => {
        const clipped = (clipType: string) => (name: string) => name === "clipType" ? clipType : null;

        expect([ "clippings", "page", "note", "tabs", "share" ].map((clipType) =>
            getNoteIcon(buildArgs({ getLabelValue: clipped(clipType) }))))
            .toEqual([ "bx bx-paperclip", "bx bx-globe", "bx bx-bookmark", "bx bx-window-alt", "bx bx-share" ]);
        expect(getNoteIcon(buildArgs({ getLabelValue: clipped("page"), isFolder: () => true })))
            .toBe("bx bx-globe");
        expect(getNoteIcon(buildArgs({ getLabelValue: clipped("page"), iconClass: "bx bx-star" })))
            .toBe("bx bx-star");
        for (const clipType of [ "unknown", "constructor" ]) {
            expect(getNoteIcon(buildArgs({ getLabelValue: clipped(clipType) }))).toBe("bx bx-note");
        }
    });

    it("draws a recorded meeting by its label, ahead of a start date and the folder icon", () => {
        const labelled = (labels: Record<string, string>) => (name: string) => labels[name] ?? null;
        const meeting = labelled({ [MEETING_RECORDING_LABEL]: "" });

        expect(getNoteIcon(buildArgs({ getLabelValue: meeting }))).toBe(MEETING_RECORDING_ICON);
        expect(getNoteIcon(buildArgs({ getLabelValue: meeting, isFolder: () => true })))
            .toBe(MEETING_RECORDING_ICON);
        expect(getNoteIcon(buildArgs({
            getLabelValue: labelled({ [MEETING_RECORDING_LABEL]: "", startDate: "2026-10-13" })
        }))).toBe(MEETING_RECORDING_ICON);
        expect(getNoteIcon(buildArgs({ getLabelValue: meeting, iconClass: "bx bx-star" })))
            .toBe("bx bx-star");
    });

    it("draws a calendar event by its start date, under its own name where it renames it", () => {
        const labelled = (labels: Record<string, string>) => (name: string) => labels[name] ?? null;

        expect(getNoteIcon(buildArgs({ getLabelValue: labelled({ startDate: "2026-10-12" }) })))
            .toBe(CALENDAR_EVENT_ICON);
        expect(getNoteIcon(buildArgs({
            getLabelValue: labelled({ startDate: "2026-10-12" }), isFolder: () => true
        }))).toBe(CALENDAR_EVENT_ICON);
        expect(getNoteIcon(buildArgs({
            getLabelValue: labelled({ "calendar:startDate": "due", due: "2026-10-12" })
        }))).toBe(CALENDAR_EVENT_ICON);
        expect(getNoteIcon(buildArgs({
            getLabelValue: labelled({ startDate: "2026-10-12" }), iconClass: "bx bx-cake"
        }))).toBe("bx bx-cake");
        expect(getNoteIcon(buildArgs({ getLabelValue: labelled({ startDate: "" }) })))
            .toBe("bx bx-note");
        expect(getNoteIcon(buildArgs({
            type: "file", mime: "application/pdf", getLabelValue: labelled({ startDate: "2026-10-12" })
        }))).toBe("bx bxs-file-pdf");
    });

    it("draws a shape note as the shape its label names, by the same rules as a pin", () => {
        const drawn = (value: string) =>
            (name: string) => name === GEO_SHAPE_ATTRIBUTE ? value : null;

        expect(getNoteIcon(buildArgs({ getLabelValue: drawn("line:48.85,2.29 48.86,2.35") })))
            .toBe("bx bx-vector");
        expect(getNoteIcon(buildArgs({ getLabelValue: drawn("polygon:48.85,2.29 48.9,2.3") })))
            .toBe("bx bx-shape-polygon");
        expect(getNoteIcon(buildArgs({ getLabelValue: drawn("circle:48.85,2.29 500") })))
            .toBe("bx bx-shape-circle");
        // Nothing is written onto a shape note either, so the map's own icon still wins, and the
        // icon follows a shape redrawn as another kind.
        expect(getNoteIcon(buildArgs({
            getLabelValue: drawn("circle:48.85,2.29 500"), iconClass: "bx bx-store"
        }))).toBe("bx bx-store");
        // A label nobody can read draws no shape on the map, but the note still carries one, so
        // the line icon is the fallback.
        expect(getNoteIcon(buildArgs({ getLabelValue: drawn("nonsense") }))).toBe("bx bx-vector");
        expect(getNoteIcon(buildArgs({ getLabelValue: drawn("") }))).toBe("bx bx-note");
    });

    it("draws the notes of an image map by their map labels, as it does those of a geo map", () => {
        const labelled = (labels: Record<string, string>) => (name: string) => labels[name] ?? null;

        expect(getNoteIcon(buildArgs({ getLabelValue: labelled({ [MAP_POSITION_ATTRIBUTE]: "10,20" }) })))
            .toBe("bx bx-pin");
        expect(getNoteIcon(buildArgs({ getLabelValue: labelled({ [MAP_SHAPE_ATTRIBUTE]: "circle:10,20 5" }) })))
            .toBe("bx bx-shape-circle");
        // A note taken off a geo map keeps an empty `#geoShape`, which leaves the image's to answer.
        expect(getNoteIcon(buildArgs({
            getLabelValue: labelled({ [GEO_SHAPE_ATTRIBUTE]: "", [MAP_SHAPE_ATTRIBUTE]: "polygon:0,0 1,0 1,1" })
        }))).toBe("bx bx-shape-polygon");
    });

    it("returns the note icon for a text note that is not a folder", () => {
        const icon = getNoteIcon(buildArgs({ type: "text", isFolder: () => false }));
        expect(icon).toBe("bx bx-note");
    });

    it("returns the mime-specific icon for a code note whose mime has an icon", () => {
        const icon = getNoteIcon(buildArgs({ type: "code", mime: "text/css" }));
        expect(icon).toBe("bx bxs-file-css");
    });

    it("falls back to the code icon for a code note whose mime is not in the dictionary", () => {
        const icon = getNoteIcon(buildArgs({ type: "code", mime: "text/x-unknownxyz" }));
        expect(icon).toBe("bx bx-code");
    });

    it("falls back to the code icon for a code note whose mime has no icon", () => {
        const icon = getNoteIcon(buildArgs({ type: "code", mime: "text/apl" }));
        expect(icon).toBe("bx bx-code");
    });

    it("returns the video icon for a file note with a video mime", () => {
        const icon = getNoteIcon(buildArgs({ type: "file", mime: "video/mp4" }));
        expect(icon).toBe("bx bx-video");
    });

    it("returns the music icon for a file note with an audio mime", () => {
        const icon = getNoteIcon(buildArgs({ type: "file", mime: "audio/mpeg" }));
        expect(icon).toBe("bx bx-music");
    });

    it("returns the mapped file icon for a file note with a known mime", () => {
        const icon = getNoteIcon(buildArgs({ type: "file", mime: "application/pdf" }));
        expect(icon).toBe("bx bxs-file-pdf");
    });

    it("marks a font file as a font, whichever media type it arrived under", () => {
        expect(getNoteIcon(buildArgs({ type: "file", mime: "font/woff2" }))).toBe("bx bx-font");
        expect(getNoteIcon(buildArgs({ type: "file", mime: "application/x-font-ttf" }))).toBe("bx bx-font");
        // EOT is no font Trilium can draw, so it stays a plain file.
        expect(getNoteIcon(buildArgs({ type: "file", mime: "application/vnd.ms-fontobject" }))).toBe("bx bx-file");
    });

    it("marks a GPX track as the journey it holds rather than as a file", () => {
        const icon = getNoteIcon(buildArgs({ type: "file", mime: "application/gpx+xml" }));
        expect(icon).toBe("bx bx-trip");
    });

    it("marks every spreadsheet format as a spreadsheet", () => {
        for (const mime of [
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
            "application/vnd.oasis.opendocument.spreadsheet",
            "application/vnd.ms-excel",
            "text/csv"
        ]) {
            expect(getNoteIcon(buildArgs({ type: "file", mime }))).toBe("bx bx-spreadsheet");
        }
    });

    it("falls back to the file icon for a file note with an unmapped mime", () => {
        const icon = getNoteIcon(buildArgs({ type: "file", mime: "text/plain" }));
        expect(icon).toBe("bx bx-file");
    });

    it("returns the mapped image icon for an image note with a known mime", () => {
        const icon = getNoteIcon(buildArgs({ type: "image", mime: "image/gif" }));
        expect(icon).toBe("bx bxs-file-gif");
    });

    it("falls back to the image icon for an image note with an unmapped mime", () => {
        const icon = getNoteIcon(buildArgs({ type: "image", mime: "image/png" }));
        expect(icon).toBe("bx bx-image");
    });

    it("returns the note-type icon for any other type", () => {
        const icon = getNoteIcon(buildArgs({ type: "book" }));
        expect(icon).toBe("bx bx-book");
    });
});

describe("parseMindMapNoteLink", () => {
    it("tells a link to a note from one pointing outside Trilium", () => {
        expect(parseMindMapNoteLink("#root/abc123")).toEqual({ notePath: "root/abc123", noteId: "abc123" });
        // The whole path is kept — it is what places the note — and the note is the end of it.
        expect(parseMindMapNoteLink("#root/parent/abc123")).toEqual({ notePath: "root/parent/abc123", noteId: "abc123" });
        expect(parseMindMapNoteLink("#root")).toEqual({ notePath: "root", noteId: "root" });

        for (const link of [
            null, undefined, "", 42,
            "https://example.com",
            // An address of its own that happens to carry a note path is still a page elsewhere.
            "https://example.com/#root/abc123",
            "#rootless/abc",
            // The address as Mind Elixir would hold it, or not at all.
            "root/abc123",
            "#root/abc123?bookmark=x"
        ]) {
            expect(parseMindMapNoteLink(link)).toBeNull();
        }
    });
});

describe("getMimeIcon", () => {
    it("reads a media type the way a note of that content is read", () => {
        // Same answers as the file/image branches above, which now go through here: a PDF is a PDF
        // whether it arrived as a note or as an attachment.
        expect(getMimeIcon("application/pdf")).toBe("bx bxs-file-pdf");
        expect(getMimeIcon("video/mp4")).toBe("bx bx-video");
        expect(getMimeIcon("audio/mpeg")).toBe("bx bx-music");
        expect(getMimeIcon("image/gif")).toBe("bx bxs-file-gif");
        expect(getMimeIcon("image/png")).toBe("bx bx-image");
        expect(getMimeIcon("application/gpx+xml")).toBe("bx bx-trip");
        expect(getMimeIcon("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")).toBe("bx bx-spreadsheet");
        expect(getMimeIcon("text/csv")).toBe("bx bx-spreadsheet");
        expect(getMimeIcon(CANVAS_ATTACHMENT_MIME)).toBe(NOTE_TYPE_ICONS.canvas);
        expect(getMimeIcon(MERMAID_ATTACHMENT_MIME)).toBe(NOTE_TYPE_ICONS.mermaid);
        expect(getMimeIcon("text/plain")).toBe("bx bx-file");
    });

    it("falls back to the file icon when there is no media type to read", () => {
        expect(getMimeIcon(undefined)).toBe("bx bx-file");
        expect(getMimeIcon(null)).toBe("bx bx-file");
        expect(getMimeIcon("")).toBe("bx bx-file");
    });

    it("does not answer with something off the mapping tables' prototype", () => {
        // The media type is whatever was stored. Read off a table rather than checked against it,
        // these return a function, which survives the `??` and is handed on as an icon class.
        expect(getMimeIcon("constructor")).toBe("bx bx-file");
        expect(getMimeIcon("toString")).toBe("bx bx-file");
    });
});

describe("isFullWidthNoteType", () => {
    it("takes the full width for canvas-like types and for PDF, video and audio files", () => {
        for (const type of [ "code", "image", "mermaid", "book", "canvas", "webView", "mindMap", "spreadsheet" ]) {
            expect(isFullWidthNoteType(type, "")).toBe(true);
        }
        expect(isFullWidthNoteType("file", "application/pdf")).toBe(true);
        expect(isFullWidthNoteType("file", "video/mp4")).toBe(true);
        expect(isFullWidthNoteType("file", "audio/ogg")).toBe(true);

        expect(isFullWidthNoteType("text", "text/html")).toBe(false);
        expect(isFullWidthNoteType("file", "application/zip")).toBe(false);
        expect(isFullWidthNoteType("search", "")).toBe(false);
    });
});

describe("getNoteContentType", () => {
    it("names the content a note renders as, and the group of its renderer", () => {
        const kinds = [
            [ "code", "text/x-gfm", false ],
            [ "code", "application/json", true ],
            [ "file", "application/json", true ],
            [ "code", "application/json", false ],
            [ "file", "application/json", false ],
            [ "file", "application/pdf", false ],
            [ "file", CANVAS_ATTACHMENT_MIME, false ],
            [ "file", MERMAID_ATTACHMENT_MIME, false ],
            [ "file", "audio/mpeg", false ],
            [ "file", "video/mp4", false ],
            [ "file", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", false ],
            [ "file", "application/zip", false ],
            [ "mermaid", "text/vnd.mermaid", false ]
        ] as const;

        expect(kinds.map(([ type, mime, hasIconPackLabel ]) => {
            const kind = getNoteContentType(type, mime, hasIconPackLabel);
            return [ kind, getContentRendererGroup(kind) ];
        })).toEqual([
            [ "markdown", "markdown" ],
            [ "iconPack", "iconPack" ],
            [ "iconPack", "iconPack" ],
            [ "code", undefined ],
            [ "code", undefined ],
            [ "pdf", "file" ],
            [ "canvasDrawing", "file" ],
            [ "mermaidDiagram", "file" ],
            [ "audio", "file" ],
            [ "video", "file" ],
            [ "office", undefined ],
            [ "file", undefined ],
            [ "mermaid", undefined ]
        ]);
        expect(getContentRendererGroup("constructor")).toBeUndefined();
    });
});

describe("getAttachedFileMime", () => {
    it("saves an Excalidraw file as a canvas drawing and keeps the type of any other", () => {
        expect([
            getAttachedFileMime("Drawing.excalidraw", "application/octet-stream"),
            getAttachedFileMime("Sketch.EXCALIDRAW", ""),
            getAttachedFileMime("Drawing.excalidraw", "application/json"),
            getAttachedFileMime("data.json", "application/json"),
            getAttachedFileMime("excalidraw", "application/octet-stream"),
            getAttachedFileMime("Drawing.excalidraw.md", "text/markdown")
        ]).toEqual([
            CANVAS_ATTACHMENT_MIME,
            CANVAS_ATTACHMENT_MIME,
            CANVAS_ATTACHMENT_MIME,
            "application/json",
            "application/octet-stream",
            "text/markdown"
        ]);
    });
});