Skip to content

Build your personal knowledge base with Trilium Notes

10.6 KB
Raw Blame
import { deferred, LOCALES } from "@triliumnext/commons";
import { becca, becca_loader, binary_utils, cls, getSql, hidden_subtree as hiddenSubtreeService, i18n, note_service as notes, TaskContext } from "@triliumnext/core";
import protectedSessionService from "@triliumnext/core/src/services/protected_session.js";
import { beforeAll, describe, expect, it } from "vitest";

import branches from "./branches.js";
import sql_init from "./sql_init.js";

describe("Hidden Subtree", () => {
    beforeAll(async () => {
        sql_init.initializeDb();
        await sql_init.dbReady;
        cls.init(() => hiddenSubtreeService.checkHiddenSubtree());
    });

    describe("Launcher movement persistence", () => {
        it("should persist launcher movement between visible and available after integrity check", () => {
            // Move backend log to visible launchers.
            const backendLogBranch = becca.getBranchFromChildAndParent("_lbBackendLog", "_lbAvailableLaunchers");
            expect(backendLogBranch).toBeDefined();

            // Move launcher to visible launchers.
            cls.init(() => {
                branches.moveBranchToNote(backendLogBranch!, "_lbVisibleLaunchers");
                hiddenSubtreeService.checkHiddenSubtree();
            });

            // Ensure the launcher is still in visible launchers.
            const childBranches = backendLogBranch?.childNote.getParentBranches()
                .filter((b) => !b.isDeleted);
            expect(childBranches).toBeDefined();
            expect(childBranches![0].parentNoteId).toStrictEqual("_lbVisibleLaunchers");
        });

        it("provides the help subtree as virtual notes, with no persisted rows left behind", () => {
            // The help subtree is injected into becca from the bundled meta as virtual notes;
            // the fixture DB carried persisted _help rows which migration 242 must have
            // removed, entity_changes included.
            const helpRoot = becca.getNoteOrThrow("_help");
            expect(helpRoot.isVirtual).toBe(true);
            expect(helpRoot.getParentNotes().map((n) => n.noteId)).toEqual(["_hidden"]);
            expect(helpRoot.getChildNotes().length).toBeGreaterThan(0);

            const page = becca.getNoteOrThrow("_help_Vc8PjrjAGuOp");
            expect(page.isVirtual).toBe(true);

            const sql = getSql();
            expect(sql.getValue(/*sql*/`SELECT COUNT(*) FROM notes WHERE noteId LIKE '\\_help%' ESCAPE '\\'`)).toBe(0);
            expect(sql.getValue(/*sql*/`SELECT COUNT(*) FROM branches WHERE noteId LIKE '\\_help%' ESCAPE '\\' OR parentNoteId LIKE '\\_help%' ESCAPE '\\'`)).toBe(0);
            expect(sql.getValue(/*sql*/`SELECT COUNT(*) FROM attributes WHERE noteId LIKE '\\_help%' ESCAPE '\\'`)).toBe(0);
            expect(sql.getValue(/*sql*/`SELECT COUNT(*) FROM entity_changes WHERE entityId LIKE '\\_help%' ESCAPE '\\'`)).toBe(0);
        });

        it("rejects moving a help note", () => {
            const originalBranch = becca.getBranchFromChildAndParent("_help_Vc8PjrjAGuOp", "_help_gh7bpGYxajRS");
            expect(originalBranch?.isVirtual).toBe(true);

            // validateParentChild rejects the move before anything is touched
            const result = originalBranch && cls.init(() => branches.moveBranchToNote(originalBranch, "_help"));
            expect(Array.isArray(result) ? result[1] : result).toMatchObject({ success: false });

            // structure is unchanged
            const parents = becca.getNoteOrThrow("_help_Vc8PjrjAGuOp").getParentNotes().map((n) => n.noteId);
            expect(parents).toEqual(["_help_gh7bpGYxajRS"]);
        });

        it("enforces renames of launcher notes", () => {
            const jumpToNote = becca.getNote("_lbJumpTo");
            expect(jumpToNote).toBeDefined();
            jumpToNote!.title = "Renamed";

            cls.init(() => {
                jumpToNote!.save();
                hiddenSubtreeService.checkHiddenSubtree(true);
            });

            const updatedJumpToNote = becca.getNote("_lbJumpTo");
            expect(updatedJumpToNote).toBeDefined();
            expect(updatedJumpToNote?.title).not.toBe("Renamed");
        });

        it("enforces renames of templates", () => {
            const boardTemplate = becca.getNote("_template_board");
            expect(boardTemplate).toBeDefined();
            boardTemplate!.title = "My renamed board";

            cls.init(() => {
                boardTemplate!.save();
                hiddenSubtreeService.checkHiddenSubtree(true);
            });

            const updatedBoardTemplate = becca.getNote("_template_board");
            expect(updatedBoardTemplate).toBeDefined();
            expect(updatedBoardTemplate?.title).not.toBe("My renamed board");
        });

        it("rejects attribute changes on virtual help notes", () => {
            const apiRefNote = becca.getNoteOrThrow("_help_9qPsTWBorUhQ");
            expect(apiRefNote.isVirtual).toBe(true);

            expect(() => cls.init(() => apiRefNote.setAttribute("label", "webViewSrc", "foo"))).toThrow(/virtual/);

            // nothing was persisted; reload to shed the in-memory leftover of the failed save
            cls.init(() => becca_loader.reload("test"));
            expect(becca.getNoteOrThrow("_help_9qPsTWBorUhQ").getLabelValue("webViewSrc")).not.toBe("foo");
        });

        it("maintains launchers hidden, if they were shown by default but moved by the user", () => {
            const launcher = becca.getNote("_lbCalendar");
            const branch = launcher?.getParentBranches()[0];
            expect(branch).toBeDefined();
            expect(branch!.parentNoteId).toBe("_lbVisibleLaunchers");
            expect(launcher).toBeDefined();

            cls.init(() => {
                branches.moveBranchToNote(branch!, "_lbAvailableLaunchers");
                hiddenSubtreeService.checkHiddenSubtree();
            });

            const newBranches = launcher?.getParentBranches().filter(b => !b.isDeleted);
            expect(newBranches).toHaveLength(1);
            expect(newBranches![0].parentNoteId).toBe("_lbAvailableLaunchers");
        });

        it("can restore names in all languages", async () => {
            const done = deferred<void>();
            cls.wrap(async () => {
                for (const locale of LOCALES) {
                    if (locale.contentOnly) {
                        continue;
                    }

                    try {
                        await i18n.changeLanguage(locale.id);
                    } catch (error) {
                        done.reject(error);
                    }
                }
                done.resolve();
            })();
            await done;
        });
    });

    describe("Hidden subtree", () => {
        it("cleans up exclude from note map at the root", async () => {
            const hiddenSubtree = becca.getNoteOrThrow("_hidden");
            cls.init(() => hiddenSubtree.addLabel("excludeFromNoteMap"));
            expect(hiddenSubtree.hasLabel("excludeFromNoteMap")).toBeTruthy();
            cls.init(() => hiddenSubtreeService.checkHiddenSubtree());
            expect(hiddenSubtree.hasLabel("excludeFromNoteMap")).toBeFalsy();
        });

        it("cleans up attribute change in templates", () => {
            const template = becca.getNoteOrThrow("_template_table");
            cls.init(() => {
                template.setLabel("subtreeHidden", "foo");
                hiddenSubtreeService.checkHiddenSubtree();
            });
            expect(template.getLabelValue("subtreeHidden")).toBe("false");
        });

        it("cleans up item to be deleted", async () => {
            const noteId = "_lbLlmChat";
            let llmNote = becca.getNote(noteId);

            cls.init(() => {
                if (!llmNote) {
                    llmNote = notes.createNewNote({
                        parentNoteId: "_lbVisibleLaunchers",
                        noteId,
                        title: "LLM chat",
                        type: "launcher",
                        content: ""
                    }).note;
                }

                hiddenSubtreeService.checkHiddenSubtree();
                becca_loader.reload("test");
            });

            llmNote = becca.getNote(noteId);
            expect(llmNote).toBeFalsy();
        });

        it("leaves the content of a protected template alone when no protected session is available (#10549)", () => {
            const dataKey = binary_utils.encodeUtf8("0123456789abcdef"); // exactly 16 bytes
            const note = becca.getNoteOrThrow("_template_presentation_first");
            const originalContent = note.getContent();
            expect(originalContent).toBeTruthy();

            // Simulate a user who protected the hidden template while a protected session was active...
            protectedSessionService.setDataKey(dataKey);
            try {
                cls.init(() => notes.protectNoteRecursively(note, true, false, new TaskContext("test-protect", "protectNotes", { protect: true })));
                expect(note.isProtected).toBe(true);

                // ...and then the server restarts without a protected session — the boot state of #10549.
                protectedSessionService.resetDataKey();
                expect(note.isContentAvailable()).toBe(false);

                // The check must neither throw (it used to crash the server on startup) nor touch the content.
                expect(() => cls.init(() => hiddenSubtreeService.checkHiddenSubtree(true))).not.toThrow();

                // With the session restored, the content still decrypts to the original.
                protectedSessionService.setDataKey(dataKey);
                expect(note.getContent()).toBe(originalContent);

                // Clean up: unprotect the note again so the remaining tests see the default state.
                cls.init(() => notes.protectNoteRecursively(note, false, false, new TaskContext("test-unprotect", "protectNotes", { protect: false })));
                expect(note.isProtected).toBe(false);
                expect(note.getContent()).toBe(originalContent);
            } finally {
                protectedSessionService.resetDataKey();
            }
        });

        it("fixes attribute of wrong type", () => {
            const template = becca.getNoteOrThrow("_template_table");
            cls.init(() => {
                template.setAttribute("relation", "template", "root");
                hiddenSubtreeService.checkHiddenSubtree();
            });
            const attr = template.getAttributes().find(a => a.name === "template");
            expect(attr).toBeDefined();
            expect(attr?.type).toBe("label");
        });
    });
});