Skip to content

Build your personal knowledge base with Trilium Notes

12.3 KB
Raw Blame
import { afterEach, describe, expect, it, vi } from "vitest";

import { getContext } from "../services/context.js";
import eventService from "../services/events.js";
import noteService from "../services/notes.js";
import { getSql } from "../services/sql/index.js";
import ws from "../services/ws.js";
import becca from "./becca.js";
import beccaLoader, { load, reload } from "./becca_loader.js";
import BAttribute from "./entities/battribute.js";
import BNote from "./entities/bnote.js";

let counter = 0;

function createNote(parentNoteId: string): BNote {
    counter++;
    return getContext().init(() =>
        noteService.createNewNote({
            parentNoteId,
            title: `becca-loader-spec-${counter}`,
            content: "<p>hi</p>",
            type: "text"
        }).note
    );
}

describe("becca_loader", () => {
    afterEach(() => {
        // The shared fixture DB is loaded (becca.loaded === true) after setup;
        // some tests flip this, so always restore it.
        becca.loaded = true;
        vi.restoreAllMocks();
    });

    describe("ENTITY_CHANGE_SYNCED listener", () => {
        it("returns early without touching becca when becca is not loaded", () => {
            becca.loaded = false;

            const before = Object.keys(becca.notes).length;
            eventService.emit(eventService.ENTITY_CHANGE_SYNCED, {
                entityName: "notes",
                entityRow: {
                    noteId: "loaderNotLoaded",
                    title: "x",
                    type: "text",
                    mime: "text/html",
                    isProtected: false,
                    blobId: ""
                }
            });

            // Nothing was created since the listener bailed out.
            expect(becca.getNote("loaderNotLoaded")).toBeNull();
            expect(Object.keys(becca.notes).length).toBe(before);
        });

        it("updates an existing becca entity from the synced row", () => {
            const note = createNote("root");

            eventService.emit(eventService.ENTITY_CHANGE_SYNCED, {
                entityName: "notes",
                entityRow: {
                    noteId: note.noteId,
                    title: "synced-updated-title",
                    type: "code",
                    mime: "text/plain",
                    isProtected: false,
                    blobId: note.blobId
                }
            });

            const updated = becca.getNoteOrThrow(note.noteId);
            expect(updated.title).toBe("synced-updated-title");
            expect(updated.type).toBe("code");
        });

        it("creates a brand-new becca entity from a synced row when not yet present", () => {
            const newNoteId = "loaderBrandNew1";
            expect(becca.getNote(newNoteId)).toBeNull();

            eventService.emit(eventService.ENTITY_CHANGE_SYNCED, {
                entityName: "notes",
                entityRow: {
                    noteId: newNoteId,
                    title: "brand-new",
                    type: "text",
                    mime: "text/html",
                    isProtected: false,
                    blobId: ""
                }
            });

            const created = becca.getNoteOrThrow(newNoteId);
            expect(created).toBeInstanceOf(BNote);
            expect(created.title).toBe("brand-new");
            // init() ran, so the derived collections are initialised.
            expect(Array.isArray(created.children)).toBe(true);
        });

        it("ignores synced rows for entity names outside the handled set", () => {
            // "blobs" is a valid entity but not in the becca_loader handled list,
            // so neither the create/update branch nor post-processing should run.
            expect(() =>
                eventService.emit(eventService.ENTITY_CHANGE_SYNCED, {
                    entityName: "blobs",
                    entityRow: { blobId: "loaderIgnoredBlob" }
                })
            ).not.toThrow();
        });
    });

    describe("ENTITY_DELETED listener", () => {
        it("branchDeleted returns early for an unknown branch id", () => {
            const branchesBefore = Object.keys(becca.branches).length;

            expect(() =>
                eventService.emit(eventService.ENTITY_DELETED, {
                    entityName: "branches",
                    entityId: "nope-no-such-branch"
                })
            ).not.toThrow();

            // No branches were removed.
            expect(Object.keys(becca.branches).length).toBe(branchesBefore);
        });

        it("attributeDeleted returns early for an unknown attribute id", () => {
            const attributesBefore = Object.keys(becca.attributes).length;

            expect(() =>
                eventService.emit(eventService.ENTITY_DELETED, {
                    entityName: "attributes",
                    entityId: "nope-no-such-attribute"
                })
            ).not.toThrow();

            expect(Object.keys(becca.attributes).length).toBe(attributesBefore);
        });

        it("attributeDeleted removes an attribute from its index bucket without rebuilding it", () => {
            const makeLabel = (attributeId: string, noteId: string) => new BAttribute({
                attributeId, noteId, type: "label", name: "loaderSharedLabel",
                value: "", isInheritable: false, position: 0
            });
            const deleteAttribute = (attribute: BAttribute) => eventService.emit(
                eventService.ENTITY_DELETED,
                { entityName: "attributes", entityId: attribute.attributeId, entity: attribute }
            );

            const first = makeLabel("loaderShared1", "loaderSharedNote1");
            const second = makeLabel("loaderShared2", "loaderSharedNote2");
            const third = makeLabel("loaderShared3", "loaderSharedNote1");
            const bucket = becca.attributeIndex["label-loadersharedlabel"];

            deleteAttribute(second);

            // Rebuilding the bucket per deletion makes deleting n same-named attributes O(n²).
            expect(becca.attributeIndex["label-loadersharedlabel"]).toBe(bucket);
            expect(becca.findAttributes("label", "loaderSharedLabel")).toEqual([ first, third ]);
            expect(becca.getNote("loaderSharedNote2")).toBeNull();

            deleteAttribute(first);
            deleteAttribute(third);
            expect(becca.findAttributes("label", "loaderSharedLabel")).toEqual([]);
        });
    });

    describe("ENTER_PROTECTED_SESSION listener", () => {
        it("swallows errors thrown while decrypting protected notes", () => {
            const spy = vi.spyOn(becca, "decryptProtectedNotes").mockImplementation(() => {
                throw new Error("boom");
            });

            // The error must be caught inside the listener (and additionally by
            // the event dispatcher), so emitting never throws.
            expect(() => eventService.emit(eventService.ENTER_PROTECTED_SESSION)).not.toThrow();
            expect(spy).toHaveBeenCalled();
        });
    });

    describe("LEAVE_PROTECTED_SESSION listener", () => {
        it("reloads becca from disk, purging decrypted titles from memory and the flat-text index", () => {
            // Simulate the post-unlock state: a protected note whose plaintext title lives
            // only in becca memory (on disk the title is ciphertext, here it has no row at all).
            const note = new BNote();
            note.updateFromRow({
                noteId: "loaderLeaveProtected",
                title: "swordfish plaintext",
                isProtected: true,
                type: "text",
                mime: "text/html",
                blobId: ""
            });
            note.init();
            note.isDecrypted = true;

            // The flat-text search index serves the plaintext title (this is the #10115
            // leak surface: title-word search matching protected notes without a session).
            expect(becca.getFlatTextIndex().flatTexts.some((text) => text.includes("swordfish"))).toBe(true);

            getContext().init(() => eventService.emit(eventService.LEAVE_PROTECTED_SESSION));

            // Full reload from disk: the memory-only decrypted state is discarded...
            expect(becca.getNote("loaderLeaveProtected")).toBeNull();
            // ...and the rebuilt search index no longer matches the plaintext.
            expect(becca.getFlatTextIndex().flatTexts.some((text) => text.includes("swordfish"))).toBe(false);
        });
    });

    describe("load()", () => {
        it("loads etapi tokens into becca", () => {
            const etapiTokenId = "loaderEtapiToken1";
            const now = "2025-01-01 00:00:00.000Z";

            getContext().init(() => {
                getSql().execute(
                    /*sql*/`INSERT INTO etapi_tokens (etapiTokenId, name, tokenHash, utcDateCreated, utcDateModified, isDeleted)
                            VALUES (?, ?, ?, ?, ?, 0)`,
                    [etapiTokenId, "loader-test-token", "hash-value", now, now]
                );

                load();
            });

            const token = becca.getEtapiToken(etapiTokenId);
            expect(token).not.toBeNull();
            expect(token?.name).toBe("loader-test-token");
            expect(becca.loaded).toBe(true);
        });

        it("orders a note's children by notePosition rather than by insertion order", () => {
            const parent = createNote("root");
            const children = [
                createNote(parent.noteId), createNote(parent.noteId), createNote(parent.noteId)
            ];

            getContext().init(() => {
                // Reverse the positions, so the rowid order the table is scanned in disagrees
                // with the order the children belong in.
                const update = /*sql*/`UPDATE branches SET notePosition = ?
                                       WHERE noteId = ? AND parentNoteId = ?`;
                for (const [index, child] of children.entries()) {
                    const position = (children.length - index) * 10;
                    getSql().execute(update, [position, child.noteId, parent.noteId]);
                }

                load();
            });

            const loadedParent = becca.getNoteOrThrow(parent.noteId);
            expect(loadedParent.children.map((child) => child.noteId)).toEqual(
                [...children].reverse().map((child) => child.noteId)
            );
        });
    });

    describe("reload()", () => {
        it("reloads becca and has the clients refresh froca with the supplied reason", () => {
            const refresh = vi.spyOn(ws, "reloadFroca").mockImplementation(() => {});
            const fullReload = vi.spyOn(ws, "reloadFrontend").mockImplementation(() => {});

            getContext().init(() => reload("custom reason"));

            expect(becca.loaded).toBe(true);
            expect(refresh).toHaveBeenCalledWith("custom reason");
            expect(fullReload).not.toHaveBeenCalled();
        });

        it("falls back to a default reason when none is given", () => {
            const spy = vi.spyOn(ws, "reloadFroca").mockImplementation(() => {});

            getContext().init(() => reload(""));

            expect(spy).toHaveBeenCalledWith("becca reloaded");
        });
    });

    describe("note update", () => {
        it("drops the title-derived search caches so a rename is searchable under its new title", () => {
            const note = createNote("root");
            // Populate both caches, as a search would.
            expect(note.getFlatText()).toContain("becca-loader-spec");

            expect(note.getSearchableTitle().normalized).toBe(note.title.toLowerCase());

            // A local rename assigns the title and saves; `updateFromRow` is not involved.
            note.title = "Renamed Vienna";
            getContext().init(() => note.save());

            expect(note.getSearchableTitle().normalized).toBe("renamed vienna");
            expect(note.getSearchableTitle().words).toEqual([ "renamed", "vienna" ]);
            expect(note.getFlatText()).toContain("renamed vienna");
        });
    });

    it("exposes load and reload through the default export", () => {
        expect(typeof beccaLoader.load).toBe("function");
        expect(typeof beccaLoader.reload).toBe("function");
        expect(beccaLoader.beccaLoaded).toBeInstanceOf(Promise);
    });
});