Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
import type { ElectronApi } from "@triliumnext/commons";
import { beforeEach, describe, expect, it, vi } from "vitest";
import dialog from "../services/dialog.js";
import froca from "../services/froca.js";
import server from "../services/server.js";
import toastService from "../services/toast.js";
import { buildNote } from "../test/easy-froca.js";
import appContext from "./app_context.js";
import Entrypoints from "./entrypoints.js";
import type NoteContext from "./note_context.js";
import type TabManager from "./tab_manager.js";
describe("openInWindowCommand", () => {
const entrypoints = new Entrypoints();
beforeEach(() => {
vi.restoreAllMocks();
delete window.electronApi;
});
it("opens a browser window on the extra-window URL", async () => {
const open = vi.spyOn(window, "open").mockReturnValue(null);
await entrypoints.openInWindowCommand({ notePath: "root/abc123", hoistedNoteId: "root" });
const [url] = open.mock.calls[0];
expect(String(url)).toMatch(/[?&]extraWindow=1/);
expect(String(url)).toMatch(/#root\/abc123$/);
});
it("uses window.open on desktop too; the main process adopts the child", async () => {
window.electronApi = {} as unknown as ElectronApi;
const open = vi.spyOn(window, "open").mockReturnValue(null);
await entrypoints.openInWindowCommand({ notePath: "root/abc123", hoistedNoteId: "root" });
const [url] = open.mock.calls[0];
expect(String(url)).toMatch(/[?&]extraWindow=1/);
expect(String(url)).toMatch(/#root\/abc123$/);
});
});
describe("logoutCommand", () => {
const entrypoints = new Entrypoints();
beforeEach(() => {
vi.restoreAllMocks();
document.body.replaceChildren();
window.history.replaceState({}, "", "/");
window.glob = {
...window.glob,
baseApiUrl: "api/",
csrfToken: "csrf-test-token"
};
});
it("submits a CSRF-protected form navigation to the logout route", () => {
const submit = vi.spyOn(HTMLFormElement.prototype, "submit").mockImplementation(() => {});
entrypoints.logoutCommand();
const form = document.body.querySelector("form");
expect(form).not.toBeNull();
expect(form?.method).toBe("POST");
expect(form?.action).toBe("http://localhost:3000/logout");
expect(form?.querySelector("input[name='x-csrf-token']")?.getAttribute("value"))
.toBe("csrf-test-token");
expect(submit).toHaveBeenCalledOnce();
// The route resolves against the page like every other request, so a sub-path
// deployment keeps its prefix.
document.body.replaceChildren();
window.history.replaceState({}, "", "/trilium/");
entrypoints.logoutCommand();
expect(document.body.querySelector("form")?.action)
.toBe("http://localhost:3000/trilium/logout");
window.history.replaceState({}, "", "/");
});
});
describe("runActiveNoteCommand", () => {
const entrypoints = new Entrypoints();
beforeEach(() => {
vi.restoreAllMocks();
});
it("runs the note of the context the command names, rather than the active tab's", async () => {
buildNote({ id: "tabNote", title: "Tab", type: "code", mime: "text/x-sqlite;schema=trilium" });
buildNote({ id: "popupQuery", title: "Popup", type: "code", mime: "text/x-sqlite;schema=trilium" });
const tabContext = { ntxId: "tab", note: froca.getNoteFromCache("tabNote") } as NoteContext;
const popupContext = { ntxId: "_popup-editor", note: froca.getNoteFromCache("popupQuery") } as NoteContext;
appContext.tabManager = {
getCommandContext: (ntxId?: string | null) => (ntxId === popupContext.ntxId ? popupContext : tabContext)
} as TabManager;
const post = vi.spyOn(server, "post").mockResolvedValue({ success: true, results: [] });
const triggerEvent = vi.spyOn(appContext, "triggerEvent").mockResolvedValue(undefined);
vi.spyOn(toastService, "showMessage").mockImplementation(() => {});
await entrypoints.runActiveNoteCommand({ ntxId: "_popup-editor" });
expect(post).toHaveBeenCalledWith("sql/execute/popupQuery");
expect(triggerEvent).toHaveBeenCalledWith("sqlQueryResults", expect.objectContaining({ ntxId: "_popup-editor" }));
// Without a context named, the active tab's note runs.
post.mockClear();
await entrypoints.runActiveNoteCommand();
expect(post).toHaveBeenCalledWith("sql/execute/tabNote");
});
});
describe("revision commands", () => {
const entrypoints = new Entrypoints();
beforeEach(() => {
vi.restoreAllMocks();
});
it("save a revision of the note in the context with the command's ntxId, rather than the active tab's", async () => {
const tabContext = { ntxId: "tab", noteId: "tabNote" } as NoteContext;
const popupContext = { ntxId: "_popup-editor", noteId: "popupNote" } as NoteContext;
appContext.tabManager = {
getCommandContext: (ntxId?: string | null) => (ntxId === popupContext.ntxId ? popupContext : tabContext)
} as TabManager;
const post = vi.spyOn(server, "post").mockResolvedValue({});
vi.spyOn(toastService, "showMessage").mockImplementation(() => {});
vi.spyOn(dialog, "prompt").mockResolvedValue("Milestone");
await entrypoints.forceSaveRevisionCommand({ ntxId: "_popup-editor" });
expect(post).toHaveBeenLastCalledWith("notes/popupNote/revision");
await entrypoints.saveNamedRevisionCommand({ ntxId: "_popup-editor" });
expect(post).toHaveBeenLastCalledWith("notes/popupNote/revision", { description: "Milestone" });
await entrypoints.forceSaveRevisionCommand();
expect(post).toHaveBeenLastCalledWith("notes/tabNote/revision");
});
});
describe("toggleFullscreenCommand", () => {
const entrypoints = new Entrypoints();
beforeEach(() => {
vi.restoreAllMocks();
delete window.electronApi;
});
it("enters and leaves the page's full screen in a browser", () => {
const requestFullscreen = vi.fn(async () => {});
const exitFullscreen = vi.fn(async () => {});
document.documentElement.requestFullscreen = requestFullscreen;
document.exitFullscreen = exitFullscreen;
Object.defineProperty(document, "fullscreenElement", { value: null, configurable: true });
entrypoints.toggleFullscreenCommand();
expect(requestFullscreen).toHaveBeenCalledOnce();
expect(exitFullscreen).not.toHaveBeenCalled();
Object.defineProperty(document, "fullscreenElement", {
value: document.documentElement,
configurable: true
});
entrypoints.toggleFullscreenCommand();
expect(exitFullscreen).toHaveBeenCalledOnce();
expect(requestFullscreen).toHaveBeenCalledOnce();
});
});