Skip to content

Build your personal knowledge base with Trilium Notes

18.7 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { fakeBrowser } from "wxt/testing/fake-browser";

import TriliumServerFacade, { TriliumError } from "./trilium_server_facade";

const fetchMock = vi.fn<(url: string, init?: RequestInit) => Promise<Response>>();
const SERVER = { triliumServerUrl: "https://trilium.example", authToken: "secret" };
const LOCAL_DATE_TIME = /^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d\.\d{3}[+-]\d\d:\d\d$/;
const DESKTOP_HANDSHAKE = "http://127.0.0.1:37743/api/clipper/handshake";
const FOUND_DESKTOP = { status: "found-desktop", port: 37743, url: "http://127.0.0.1:37743", protocolMinor: 0 };
const FOUND_SERVER = { status: "found-server", url: "https://trilium.example", token: "secret", protocolMinor: 0 };

type PopupMessage = { name: string, triliumSearch?: unknown, searchNote?: unknown };
const sendMessage = vi.fn(async (_message: PopupMessage) => {});

describe("TriliumServerFacade", () => {
    beforeEach(() => {
        fakeBrowser.reset();
        vi.useFakeTimers();
        vi.stubGlobal("fetch", fetchMock);
        fetchMock.mockReset();
        sendMessage.mockReset();
        Object.assign(fakeBrowser.runtime, { sendMessage });
        vi.spyOn(console, "log").mockImplementation(() => {});
        vi.spyOn(console, "debug").mockImplementation(() => {});
    });

    afterEach(() => {
        vi.clearAllTimers();
        vi.useRealTimers();
        vi.unstubAllGlobals();
        vi.unstubAllEnvs();
        vi.restoreAllMocks();
    });

    describe("search for Trilium", () => {
        it("finds the desktop app on the default port without showing a search in progress", async () => {
            fetchMock.mockImplementation(async () => handshake("1.0"));

            await createFacade();

            expect(fetchMock).toHaveBeenCalledWith(DESKTOP_HANDSHAKE, handshakeInit());
            expect(searchStatuses()).toEqual([ FOUND_DESKTOP ]);
        });

        it("looks for a production desktop app on its own port", async () => {
            vi.stubEnv("DEV", false);
            fetchMock.mockImplementation(async () => handshake("1.0"));

            await createFacade();

            expect(fetchMock).toHaveBeenCalledWith(
                "http://127.0.0.1:37840/api/clipper/handshake", handshakeInit());
        });

        it("uses the configured desktop port and checks again every minute in the background", async () => {
            await fakeBrowser.storage.sync.set({ triliumDesktopPort: "12345" });
            fetchMock.mockImplementation(async () => handshake("1.2"));

            await createFacade();
            expect(fetchMock).toHaveBeenLastCalledWith(
                "http://127.0.0.1:12345/api/clipper/handshake", handshakeInit());

            sendMessage.mockClear();
            await vi.advanceTimersByTimeAsync(60 * 1000);
            expect(fetchMock).toHaveBeenCalledTimes(2);
            expect(searchStatuses()).toEqual([
                { status: "found-desktop", port: 12345, url: "http://127.0.0.1:12345", protocolMinor: 2 }
            ]);
        });

        it("shows a search in progress when the popup asks for one", async () => {
            fetchMock.mockImplementation(async () => handshake("1.0"));
            const facade = await createFacade();
            sendMessage.mockClear();

            await facade.triggerSearchForTrilium();

            expect(searchStatuses()).toEqual([ { status: "searching" }, FOUND_DESKTOP ]);
        });

        it("reports a protocol version mismatch", async () => {
            fetchMock.mockImplementation(async () => handshake("2.0"));

            await createFacade();

            expect(lastSearchStatus()).toEqual({
                status: "version-mismatch",
                extensionMajor: 1,
                triliumMajor: 2
            });
        });

        it("asks the server while the desktop app has not answered, and falls back to it", async () => {
            await fakeBrowser.storage.sync.set(SERVER);
            let refuseDesktop: (error: Error) => void = () => {};
            fetchMock.mockImplementation((url) => url === DESKTOP_HANDSHAKE
                ? new Promise((_resolve, reject) => refuseDesktop = reject)
                : Promise.resolve(handshake("1.0")));
            const timeout = vi.spyOn(AbortSignal, "timeout");

            const facade = new TriliumServerFacade();
            await vi.waitFor(() => expect(fetchMock).toHaveBeenCalledTimes(2));
            expect(fetchMock).toHaveBeenCalledWith(
                "https://trilium.example/api/clipper/handshake",
                handshakeInit({ Authorization: "secret" }));
            expect(timeout.mock.calls).toEqual([ [ 10000 ], [ 2000 ] ]);
            expect(sendMessage).not.toHaveBeenCalled();

            refuseDesktop(new DOMException("The operation timed out.", "TimeoutError"));
            await expect(facade.waitForTriliumSearch()).resolves.toEqual(FOUND_SERVER);
            expect(lastSearchStatus()).toEqual(FOUND_SERVER);
        });

        it("prefers the desktop app when the server answers too", async () => {
            await fakeBrowser.storage.sync.set(SERVER);
            fetchMock.mockImplementation(async () => handshake("1.0"));

            await createFacade();

            expect(fetchMock).toHaveBeenCalledTimes(2);
            expect(lastSearchStatus()).toEqual(FOUND_DESKTOP);
        });

        it("reports not-found when neither the desktop app nor the server is Trilium", async () => {
            await fakeBrowser.storage.sync.set(SERVER);
            fetchMock.mockImplementation(async () => Response.json({ appName: "other" }));
            await createFacade();
            expect(lastSearchStatus()).toEqual({ status: "not-found" });
            expect(fetchMock).toHaveBeenCalledTimes(2);

            fetchMock.mockReset();
            fetchMock.mockImplementation(async (url) => {
                if (url === DESKTOP_HANDSHAKE) return new Response("not json");
                throw new Error("server down");
            });
            await createFacade();
            expect(lastSearchStatus()).toEqual({ status: "not-found" });
            expect(console.log).toHaveBeenCalledWith(
                "Handshake with https://trilium.example failed with:", new Error("server down"));
        });

        it("skips the server when no URL or token is configured", async () => {
            await fakeBrowser.storage.sync.set({ triliumServerUrl: "https://trilium.example" });
            fetchMock.mockRejectedValue(new Error("connection refused"));

            await createFacade();

            expect(fetchMock).toHaveBeenCalledTimes(1);
            expect(lastSearchStatus()).toEqual({ status: "not-found" });
        });

        it("starts from the status found before the service worker stopped", async () => {
            await fakeBrowser.storage.session.set({ triliumSearch: FOUND_DESKTOP });
            let refuseDesktop: (error: Error) => void = () => {};
            fetchMock.mockImplementation(() => new Promise((_resolve, reject) => refuseDesktop = reject));

            const facade = new TriliumServerFacade();
            await facade.sendTriliumSearchStatusToPopup();
            expect(searchStatuses()).toEqual([ FOUND_DESKTOP ]);

            await vi.waitFor(() => expect(fetchMock).toHaveBeenCalledOnce());
            refuseDesktop(new Error("connection refused"));
            await expect(facade.waitForTriliumSearch()).rejects.toThrow(
                expect.objectContaining({ reason: "not-found" }));
            expect(lastSearchStatus()).toEqual({ status: "not-found" });
            expect(await fakeBrowser.storage.session.get("triliumSearch"))
                .toEqual({ triliumSearch: { status: "not-found" } });
        });

        it("finds Trilium in a browser without session storage", async () => {
            vi.spyOn(fakeBrowser.storage, "session", "get")
                .mockReturnValue(undefined as unknown as typeof fakeBrowser.storage.session);
            fetchMock.mockImplementation(async () => handshake("1.0"));

            const facade = new TriliumServerFacade();

            await expect(facade.waitForTriliumSearch()).resolves.toEqual(FOUND_DESKTOP);
            expect(lastSearchStatus()).toEqual(FOUND_DESKTOP);
        });

        it("keeps the outcome of the latest search when an older one ends after it", async () => {
            const answers: ((response: Response) => void)[] = [];
            fetchMock.mockImplementation(() => new Promise((resolve) => answers.push(resolve)));
            const facade = new TriliumServerFacade();
            await vi.waitFor(() => expect(answers).toHaveLength(1));

            const call = facade.waitForTriliumSearch();
            const latest = facade.triggerSearchForTrilium();
            await vi.waitFor(() => expect(answers).toHaveLength(2));
            answers[1]?.(handshake("1.0"));
            await latest;
            answers[0]?.(handshake("2.0"));

            await expect(call).resolves.toEqual(FOUND_DESKTOP);
            await vi.advanceTimersByTimeAsync(0);
            expect(lastSearchStatus()).toEqual(FOUND_DESKTOP);
            expect(await fakeBrowser.storage.session.get("triliumSearch"))
                .toEqual({ triliumSearch: FOUND_DESKTOP });
            expect(searchStatuses()).not.toContainEqual(
                expect.objectContaining({ status: "version-mismatch" }));
        });

        it("ignores the popup being closed", async () => {
            sendMessage.mockRejectedValue(new Error("Could not establish connection"));
            fetchMock.mockImplementation(async () => handshake("1.0"));

            const facade = new TriliumServerFacade();
            await expect(facade.waitForTriliumSearch()).resolves.toEqual(FOUND_DESKTOP);
            await expect(facade.sendTriliumSearchNoteToPopup()).resolves.toBeUndefined();
        });
    });

    describe("callService", () => {
        it("sends JSON with the token and the local time to the found server", async () => {
            await fakeBrowser.storage.sync.set(SERVER);
            fetchMock.mockImplementation(async (url) => {
                if (url.startsWith("http://127.0.0.1")) throw new Error("connection refused");
                if (url.endsWith("/handshake")) return handshake("1.0");
                return Response.json({ noteId: "n1" });
            });
            const facade = await createFacade();

            await expect(facade.callService("POST", "clippings", { title: "T" }))
                .resolves.toEqual({ noteId: "n1" });
            const clippingsUrl = "https://trilium.example/api/clipper/clippings";
            expect(fetchMock).toHaveBeenLastCalledWith(clippingsUrl, {
                method: "POST",
                headers: {
                    Authorization: "secret",
                    "Content-Type": "application/json",
                    "trilium-local-now-datetime": expect.stringMatching(LOCAL_DATE_TIME)
                },
                body: JSON.stringify({ title: "T" })
            });

            await facade.callService("POST", "notes", "raw body");
            expect(fetchMock.mock.lastCall?.[1]?.body).toBe("raw body");

            await facade.callService("GET", "notes-by-url/x");
            expect(fetchMock.mock.lastCall?.[1]).not.toHaveProperty("body");
        });

        it("sends no token to the desktop app and reports why a request failed", async () => {
            fetchMock.mockResolvedValue(handshake("1.0"));
            const facade = await createFacade();
            const openNote = () => facade.callService("POST", "open/abc");

            const notFound = Response.json({ message: "Note 'abc' not found" }, { status: 404 });
            fetchMock.mockResolvedValue(notFound);
            await expect(openNote()).rejects.toThrow(requestFailed("Note 'abc' not found"));
            expect(fetchMock).toHaveBeenLastCalledWith(
                "http://127.0.0.1:37743/api/clipper/open/abc",
                expect.objectContaining({
                    headers: expect.objectContaining({ Authorization: "" })
                }));

            fetchMock.mockResolvedValue(new Response("Bad gateway", { status: 502 }));
            await expect(openNote()).rejects.toThrow(requestFailed("Bad gateway"));

            fetchMock.mockResolvedValue(new Response("", { status: 500 }));
            await expect(openNote()).rejects.toThrow(requestFailed("500"));

            fetchMock.mockRejectedValue(new Error("connection reset"));
            await expect(openNote()).rejects.toThrow(requestFailed("connection reset"));

            fetchMock.mockRejectedValue("offline");
            await expect(openNote()).rejects.toThrow(requestFailed("offline"));
        });

        it("waits for a running search and rejects once Trilium is not found", async () => {
            let answerHandshake: (response: Response) => void = () => {};
            fetchMock.mockReturnValueOnce(new Promise((resolve) => answerHandshake = resolve));
            const facade = new TriliumServerFacade();

            const call = facade.callService("GET", "notes-by-url/x");
            await vi.advanceTimersByTimeAsync(1500);
            expect(fetchMock).toHaveBeenCalledTimes(1);

            fetchMock.mockResolvedValue(Response.json({ noteId: "n1" }));
            answerHandshake(handshake("1.0"));
            await vi.advanceTimersByTimeAsync(500);
            await expect(call).resolves.toEqual({ noteId: "n1" });

            fetchMock.mockReset();
            fetchMock.mockRejectedValue(new Error("connection refused"));
            await facade.triggerSearchForTrilium();
            await expect(facade.callService("GET", "notes-by-url/x"))
                .rejects.toThrow(expect.objectContaining({ reason: "not-found" }));
        });

        it("rejects while Trilium has a different protocol version, naming the side to update", async () => {
            fetchMock.mockResolvedValue(handshake("0.9"));
            const facade = await createFacade();

            const call = facade.callService("GET", "notes-by-url/x");
            await expect(call).rejects.toBeInstanceOf(TriliumError);
            await expect(call).rejects.toThrow(expect.objectContaining({
                reason: "version-mismatch",
                message: expect.stringContaining("Update Trilium to the latest version.")
            }));
            expect(fetchMock).toHaveBeenCalledTimes(1);

            fetchMock.mockResolvedValue(handshake("2.0"));
            await facade.triggerSearchForTrilium();
            await expect(facade.callService("GET", "notes-by-url/x")).rejects.toThrow(
                expect.objectContaining({ message: expect.stringContaining("Update the web clipper") }));
        });

        it("rejects a feature that needs a later minor version than Trilium has", async () => {
            fetchMock.mockResolvedValue(handshake("1"));
            const facade = await createFacade();

            await expect(facade.requireMinorVersion(1, "Saving a PDF")).rejects.toThrow(expect.objectContaining({
                reason: "version-mismatch",
                message: "Saving a PDF needs a newer version of Trilium. Update Trilium to the latest version."
            }));
            await expect(facade.requireMinorVersion(0, "Saving a PDF")).resolves.toBeUndefined();

            fetchMock.mockResolvedValue(handshake("1.1"));
            await facade.triggerSearchForTrilium();
            await expect(facade.requireMinorVersion(1, "Saving a PDF")).resolves.toBeUndefined();

            fetchMock.mockResolvedValue(handshake("2.0"));
            await facade.triggerSearchForTrilium();
            await expect(facade.requireMinorVersion(1, "Saving a PDF")).rejects.toThrow(
                expect.objectContaining({ message: expect.stringContaining("Update the web clipper") }));
        });
    });

    it("looks up the note saved for a URL and reports it to the popup", async () => {
        fetchMock.mockResolvedValue(handshake("1.0"));
        const facade = await createFacade();

        fetchMock.mockResolvedValue(Response.json({ noteId: "n1" }));
        await facade.triggerSearchNoteByUrl("https://example.com/a?b=c");
        expect(fetchMock).toHaveBeenLastCalledWith(
            "http://127.0.0.1:37743/api/clipper/notes-by-url/https%3A%2F%2Fexample.com%2Fa%3Fb%3Dc",
            expect.objectContaining({ method: "GET" }));
        expect(sendMessage).toHaveBeenLastCalledWith({
            name: "trilium-previously-visited",
            searchNote: { status: "found", noteId: "n1" }
        });

        const notFound = {
            name: "trilium-previously-visited",
            searchNote: { status: "not-found", noteId: null }
        };
        fetchMock.mockResolvedValue(Response.json({ noteId: null }));
        await facade.triggerSearchNoteByUrl("https://example.com/b");
        expect(sendMessage).toHaveBeenLastCalledWith(notFound);

        sendMessage.mockClear();
        fetchMock.mockRejectedValue(new Error("connection reset"));
        await facade.triggerSearchNoteByUrl("https://example.com/c");
        expect(sendMessage).toHaveBeenLastCalledWith(notFound);
    });

    it("formats the local time with the time zone offset", async () => {
        fetchMock.mockResolvedValue(handshake("1.0"));
        const facade = await createFacade();
        vi.setSystemTime(new Date("2026-03-04T10:20:30.456Z"));

        const offsets: [ number, string ][] = [
            [ -120, "2026-03-04 12:20:30.456+02:00" ],
            [ 300, "2026-03-04 05:20:30.456-05:00" ],
            [ 0, "2026-03-04 10:20:30.456+00:00" ],
            [ -330, "2026-03-04 15:50:30.456+05:30" ],
            [ -345, "2026-03-04 16:05:30.456+05:45" ],
            [ 210, "2026-03-04 06:50:30.456-03:30" ],
            [ -630, "2026-03-04 20:50:30.456+10:30" ]
        ];
        const getTimezoneOffset = vi.spyOn(Date.prototype, "getTimezoneOffset");
        for (const [ offset, expected ] of offsets) {
            getTimezoneOffset.mockReturnValue(offset);
            expect.soft(facade.localNowDateTime(), `offset ${offset}`).toBe(expected);
        }
    });
});

function requestFailed(message: string) {
    return expect.objectContaining({
        reason: "request-failed",
        message: expect.stringContaining(message)
    });
}

function handshake(protocolVersion: string) {
    return Response.json({ appName: "trilium", protocolVersion });
}

function handshakeInit(headers?: Record<string, string>) {
    return { headers, signal: expect.any(AbortSignal) };
}

async function createFacade() {
    sendMessage.mockClear();
    const facade = new TriliumServerFacade();
    await facade.waitForTriliumSearch().catch(() => {});
    return facade;
}

function searchStatuses() {
    return sendMessage.mock.calls
        .map(([ message ]) => message)
        .filter((message) => message.name === "trilium-search-status")
        .map((message) => message.triliumSearch);
}

function lastSearchStatus() {
    return searchStatuses().at(-1) as { status: string } | undefined;
}