Skip to content

Build your personal knowledge base with Trilium Notes

9.9 KB
Raw Blame
import type { HiddenSubtreeItem } from "@triliumnext/commons";
import { describe, expect, it } from "vitest";

import buildHiddenSubtreeTemplates from "./hidden_subtree_templates.js";

function childById(root: HiddenSubtreeItem, id: string): HiddenSubtreeItem {
    const child = root.children?.find((c) => c.id === id);
    expect(child, `expected child ${id} to exist`).toBeDefined();
    return child!;
}

function viewTypeOf(item: HiddenSubtreeItem): string | undefined {
    return item.attributes?.find((a) => a.name === "viewType")?.value;
}

describe("buildHiddenSubtreeTemplates", () => {
    it("builds the _templates book root with enforced attributes", () => {
        const templates = buildHiddenSubtreeTemplates();

        expect(templates.id).toBe("_templates");
        expect(templates.type).toBe("book");
        expect(templates.enforceAttributes).toBe(true);
        expect(templates.children?.length).toBeGreaterThan(0);
    });

    it("marks every direct child template with enforceAttributes", () => {
        const templates = buildHiddenSubtreeTemplates();

        for (const child of templates.children ?? []) {
            expect(child.enforceAttributes, `child ${child.id}`).toBe(true);
        }
    });

    it("exposes the expected set of template ids exactly once", () => {
        const templates = buildHiddenSubtreeTemplates();
        const ids = (templates.children ?? []).map((c) => c.id);

        expect(ids).toEqual([
            "_template_text_snippet",
            "_template_markdown_snippet",
            "_template_code_snippet",
            "_template_ai_quick_action",
            "_template_list_view",
            "_template_grid_view",
            "_template_calendar",
            "_template_table",
            "_template_geo_map",
            "_template_board",
            "_template_presentation_slide",
            "_template_presentation",
            "_template_dashboard"
        ]);
        // No duplicate ids.
        expect(new Set(ids).size).toBe(ids.length);
    });

    it("tags each collection template with its matching viewType label", () => {
        const templates = buildHiddenSubtreeTemplates();

        expect(viewTypeOf(childById(templates, "_template_list_view"))).toBe("list");
        expect(viewTypeOf(childById(templates, "_template_grid_view"))).toBe("grid");
        expect(viewTypeOf(childById(templates, "_template_calendar"))).toBe("calendar");
        expect(viewTypeOf(childById(templates, "_template_table"))).toBe("table");
        expect(viewTypeOf(childById(templates, "_template_geo_map"))).toBe("geoMap");
        expect(viewTypeOf(childById(templates, "_template_board"))).toBe("board");
        expect(viewTypeOf(childById(templates, "_template_presentation"))).toBe("presentation");
        expect(viewTypeOf(childById(templates, "_template_dashboard"))).toBe("dashboard");
    });

    it("flags collection templates as templates and as collections", () => {
        const templates = buildHiddenSubtreeTemplates();
        const collectionIds = [
            "_template_list_view",
            "_template_grid_view",
            "_template_calendar",
            "_template_table",
            "_template_geo_map",
            "_template_board",
            "_template_presentation",
            "_template_dashboard"
        ];

        for (const id of collectionIds) {
            const item = childById(templates, id);
            const names = (item.attributes ?? []).map((a) => a.name);
            expect(names, id).toContain("template");
            expect(names, id).toContain("collection");
        }
    });

    it("marks the dashboard collection template as beta", () => {
        const templates = buildHiddenSubtreeTemplates();
        const dashboard = childById(templates, "_template_dashboard");

        const beta = dashboard.attributes?.find((a) => a.name === "beta");
        expect(beta).toBeDefined();
        expect(beta?.type).toBe("label");
    });

    it("configures the text snippet template as a text note with promoted description", () => {
        const templates = buildHiddenSubtreeTemplates();
        const snippet = childById(templates, "_template_text_snippet");

        expect(snippet.type).toBe("text");
        const names = (snippet.attributes ?? []).map((a) => a.name);
        expect(names).toContain("template");
        expect(names).toContain("textSnippet");

        const descriptor = snippet.attributes?.find((a) => a.name === "label:textSnippetDescription");
        expect(descriptor?.type).toBe("label");
        expect(descriptor?.value).toContain("promoted");
        expect(descriptor?.value).toContain("single");
        expect(descriptor?.value).toContain("text");
    });

    it("configures the markdown and code snippet templates as code notes with the unified snippet label", () => {
        const templates = buildHiddenSubtreeTemplates();
        const expectedMimes: Record<string, string> = {
            _template_markdown_snippet: "text/x-markdown",
            _template_code_snippet: "text/plain"
        };

        for (const [id, mime] of Object.entries(expectedMimes)) {
            const snippet = childById(templates, id);

            expect(snippet.type, id).toBe("code");
            expect(snippet.mime, id).toBe(mime);

            const names = (snippet.attributes ?? []).map((a) => a.name);
            expect(names, id).toContain("template");
            expect(names, id).toContain("snippet");

            const descriptor = snippet.attributes?.find((a) => a.name === "label:snippetDescription");
            expect(descriptor?.type, id).toBe("label");
            expect(descriptor?.value, id).toContain("promoted");
        }
    });

    it("applies the hidden-subtree label on the templates that declare it", () => {
        const templates = buildHiddenSubtreeTemplates();
        const offByDefault = ["_template_calendar", "_template_table", "_template_geo_map"];

        for (const id of offByDefault) {
            const item = childById(templates, id);
            const attr = item.attributes?.find((a) => a.name === "subtreeHidden");
            expect(attr, id).toBeDefined();
            expect(attr?.type, id).toBe("label");
            expect(attr?.value, id).toBe("false");
        }

        // The board and the presentation hide their subtree by default: the cards, or the slide
        // sidebar, already list the children.
        for (const id of ["_template_board", "_template_presentation"]) {
            const item = childById(templates, id);
            const attr = item.attributes?.find((a) => a.name === "subtreeHidden");
            expect(attr?.type, id).toBe("label");
            expect(attr?.value ?? "", id).toBe("");
        }

        // The list/grid views intentionally do NOT hide their subtree.
        for (const id of ["_template_list_view", "_template_grid_view"]) {
            const item = childById(templates, id);
            expect(item.attributes?.some((a) => a.name === "subtreeHidden"), id).toBe(false);
        }
    });

    it("declares the calendar's inheritable promoted date/time attributes", () => {
        const templates = buildHiddenSubtreeTemplates();
        const calendar = childById(templates, "_template_calendar");

        for (const name of ["label:startDate", "label:endDate", "label:startTime", "label:endTime"]) {
            const attr = calendar.attributes?.find((a) => a.name === name);
            expect(attr, name).toBeDefined();
            expect(attr?.isInheritable, name).toBe(true);
            expect(attr?.value, name).toContain("promoted");
        }
    });

    it("builds the board template with three seeded child notes carrying status labels", () => {
        const templates = buildHiddenSubtreeTemplates();
        const board = childById(templates, "_template_board");

        // The status definition belongs to each board, not to the template every board shares: one
        // option list here would be everyone's option list. Boards write their own.
        expect(board.attributes?.some((a) => a.name === "label:status")).toBe(false);

        const childIds = (board.children ?? []).map((c) => c.id);
        expect(childIds).toEqual([
            "_template_board_first",
            "_template_board_second",
            "_template_board_third"
        ]);

        for (const child of board.children ?? []) {
            expect(child.type).toBe("text");
            const statusLabel = child.attributes?.find((a) => a.name === "status");
            expect(statusLabel?.type, child.id).toBe("label");
            expect(statusLabel?.value, child.id).toBeTruthy();
        }
    });

    it("gives the slides of a presentation no template of their own", () => {
        const templates = buildHiddenSubtreeTemplates();
        const presentation = childById(templates, "_template_presentation");

        expect(presentation.attributes?.some((a) => a.name === "child:template")).toBe(false);
        expect((presentation.children ?? []).map((c) => c.id)).toEqual([
            "_template_presentation_first",
            "_template_presentation_second"
        ]);
        for (const slide of presentation.children ?? []) {
            expect(slide.type, slide.id).toBe("text");
            expect(slide.attributes?.some((a) => a.name === "template") ?? false, slide.id).toBe(false);
        }
    });

    it("keeps the former slide template as a bare note, for the slides that still link to it", () => {
        const templates = buildHiddenSubtreeTemplates();
        const slide = childById(templates, "_template_presentation_slide");

        expect(slide.type).toBe("text");
        expect(slide.attributes ?? []).toEqual([]);
        expect(slide.icon).toBeUndefined();
    });

    it("returns a fresh independent structure on each invocation", () => {
        const first = buildHiddenSubtreeTemplates();
        const second = buildHiddenSubtreeTemplates();

        expect(first).not.toBe(second);
        expect(first.children).not.toBe(second.children);
        expect(first.children?.length).toBe(second.children?.length);
    });
});