Skip to content

Build your personal knowledge base with Trilium Notes

18.1 KB
Raw Blame
// @vitest-environment-options {"settings":{"disableIframePageLoading":true}}
import { afterEach, describe, expect, it, vi } from "vitest";

import {
    applyLinkEmbeds,
    detectEmbedType,
    fetchMetadata,
    renderEmbedPreview,
    renderMentionPreview,
    safeHostname
} from "./link_embed.js";
import server from "./server.js";

vi.mock("./i18n.js", () => ({ t: (key: string) => key }));

let container: HTMLDivElement | undefined;

function makeContainer() {
    container = document.createElement("div");
    document.body.appendChild(container);
    return container;
}

afterEach(() => {
    if (container) {
        container.remove();
        container = undefined;
    }
    vi.restoreAllMocks();
});

/** Dispatches a native `error` event on an element and flushes Preact's state update. */
async function fireError(el: Element) {
    el.dispatchEvent(new Event("error", { bubbles: false }));
    // Allow Preact's batched setState rerender to flush.
    await new Promise((resolve) => setTimeout(resolve, 0));
}

describe("detectEmbedType", () => {
    it("detects YouTube URLs", () => {
        expect(detectEmbedType("https://youtu.be/fV16ck4Bgc0")).toBe("youtube");
        expect(detectEmbedType("https://www.youtube.com/watch?v=abc12345678")).toBe("youtube");
    });

    it("returns opengraph for non-YouTube URLs", () => {
        expect(detectEmbedType("https://example.com")).toBe("opengraph");
        expect(detectEmbedType("https://github.com/TriliumNext/Notes")).toBe("opengraph");
    });
});

describe("safeHostname", () => {
    it("extracts hostname from valid URL", () => {
        expect(safeHostname("https://www.example.com/page")).toBe("www.example.com");
    });

    it("returns raw string for invalid URL", () => {
        expect(safeHostname("not-a-url")).toBe("not-a-url");
    });

    it("handles URLs with ports", () => {
        expect(safeHostname("http://localhost:8080/api")).toBe("localhost");
    });
});

describe("fetchMetadata", () => {
    it("maps server metadata into the embed shape, POSTing the URL in the body", async () => {
        const url = "https://example.com/path?a=b&c=d";
        const fromServer = {
            url: "https://canonical.example.com",
            embedType: "opengraph",
            title: "Title",
            description: "Desc",
            favicon: "https://example.com/favicon.ico",
            siteName: "Example",
            image: "https://example.com/img.png",
            aspectRatio: "9/16"
        };
        server.post = vi.fn(async () => fromServer) as typeof server.post;

        const result = await fetchMetadata(url, "note1");

        // The URL travels in the body, so it never reaches an access log — a pasted URL can carry a
        // one-time token or a signature in its query string. The note id goes with it: the server
        // stores the preview's pictures as that note's attachments and answers with their URLs.
        expect(server.post).toHaveBeenCalledWith("link-embed/metadata", { url, noteId: "note1" });
        expect(result).toEqual(fromServer);
    });

    it("falls back to local detection when the server request fails", async () => {
        server.post = vi.fn(async () => {
            throw new Error("network down");
        }) as typeof server.post;

        const result = await fetchMetadata("https://youtu.be/abcdefghijk", "note1");
        expect(result).toEqual({
            url: "https://youtu.be/abcdefghijk",
            embedType: "youtube",
            title: "youtu.be",
            // Nothing was actually read about the page, so callers can keep a plain link instead.
            unresolved: true
        });
    });

    it("passes the server's unresolved flag through", async () => {
        server.post = vi.fn(async () => ({
            url: "https://blocked.example.com/x",
            embedType: "opengraph",
            title: "blocked.example.com",
            unresolved: true
        })) as typeof server.post;

        const result = await fetchMetadata("https://blocked.example.com/x", "note1");
        expect(result.unresolved).toBe(true);
    });

    it("takes the server's attachment URLs as they come, having nothing left to upload", async () => {
        // The pictures are stored server-side now: the metadata answers with
        // `api/attachments/...` URLs and the client's only job is to put them in the note.
        const fromServer = {
            url: "https://example.com",
            embedType: "opengraph",
            title: "Title",
            favicon: "api/attachments/att1/image/example.com.ico",
            image: "api/attachments/att2/image/example.com-1a2b3c4d.jpeg"
        };
        server.post = vi.fn(async () => fromServer) as typeof server.post;

        const result = await fetchMetadata("https://example.com", "note1");

        expect(result.favicon).toBe("api/attachments/att1/image/example.com.ico");
        expect(result.image).toBe("api/attachments/att2/image/example.com-1a2b3c4d.jpeg");
    });
});

describe("renderEmbedPreview", () => {
    it("shows a click-to-play facade for a video, contacting YouTube only once clicked", async () => {
        const root = makeContainer();
        renderEmbedPreview(root, {
            url: "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
            embedType: "youtube",
            image: "data:image/jpeg;base64,AAA"
        });

        // Nothing is loaded from YouTube until the user asks for it: no iframe, just the thumbnail
        // stored in the note.
        expect(root.querySelector("iframe")).toBeNull();
        expect(root.querySelector("a.link-embed-card")).toBeNull();
        const facade = root.querySelector<HTMLButtonElement>("button.link-embed-video-facade");
        expect(facade).not.toBeNull();
        expect(root.querySelector("img.link-embed-video-thumbnail")?.getAttribute("src")).toBe("data:image/jpeg;base64,AAA");

        facade?.click();
        await new Promise((resolve) => setTimeout(resolve, 0));

        const iframe = root.querySelector("iframe");
        expect(iframe).not.toBeNull();
        expect(iframe?.getAttribute("src")).toContain("youtube-nocookie.com/embed/dQw4w9WgXcQ");
        // The click was the play command, so the player starts straight away.
        expect(iframe?.getAttribute("src")).toContain("autoplay=1");
        // The facade covers the player until it loads; `link_embed_dom.spec.ts` covers the fade.
        expect(iframe?.nextElementSibling).toBe(facade);
        expect(facade?.classList.contains("link-embed-video-facade-loading")).toBe(true);
    });

    it("omits the player's origin param when not served from a web origin (desktop custom protocol)", async () => {
        // On desktop the renderer is served from trilium-app://app, which YouTube's player rejects
        // as an `origin` value — the param must be left out entirely there.
        const happyDOM = (window as unknown as { happyDOM: { setURL(url: string): void } }).happyDOM;
        const previousUrl = window.location.href;
        happyDOM.setURL("trilium-app://app/index.html");

        try {
            const root = makeContainer();
            renderEmbedPreview(root, {
                url: "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
                embedType: "youtube"
            });
            root.querySelector<HTMLButtonElement>("button.link-embed-video-facade")?.click();
            await new Promise((resolve) => setTimeout(resolve, 0));

            const src = root.querySelector("iframe")?.getAttribute("src") ?? "";
            expect(src).toContain("youtube-nocookie.com/embed/dQw4w9WgXcQ");
            expect(src).not.toContain("origin=");
        } finally {
            happyDOM.setURL(previousUrl);
        }
    });

    it("shows the facade without a thumbnail when the note stores no image", () => {
        const root = makeContainer();
        renderEmbedPreview(root, {
            url: "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
            embedType: "youtube"
        });

        expect(root.querySelector("button.link-embed-video-facade")).not.toBeNull();
        expect(root.querySelector("img.link-embed-video-thumbnail")).toBeNull();
    });

    it("renders a card (not an iframe) for a YouTube URL forced to opengraph", () => {
        const root = makeContainer();
        renderEmbedPreview(root, {
            url: "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
            embedType: "opengraph",
            title: "A title",
            description: "A description",
            siteName: "YouTube",
            image: "api/attachments/abc123/image/x.png"
        });

        expect(root.querySelector("iframe")).toBeNull();
        const card = root.querySelector("a.link-embed-card")!;
        expect(card.getAttribute("target")).toBe("_blank");
        expect(card.querySelector(".link-embed-card-title")!.textContent).toBe("A title");
        expect(card.querySelector(".link-embed-card-description")!.textContent).toBe("A description");
        expect(card.querySelector(".link-embed-card-url")!.textContent).toBe("YouTube");
        // image present -> real <img>, no placeholder
        expect(root.querySelector("img.link-embed-card-image")).not.toBeNull();
        expect(root.querySelector(".link-embed-card-image-placeholder")).toBeNull();
    });

    it("renders a hostile scheme inert rather than as a live link", () => {
        // `data-url` reaches here straight from the stored note HTML — the sanitizers keep `data-*`
        // values verbatim — so a crafted note must not become <a href="javascript:...">.
        const card = makeContainer();
        renderEmbedPreview(card, { url: "javascript:alert(document.cookie)", embedType: "opengraph", title: "Evil" });
        expect(card.querySelector("a.link-embed-card")?.getAttribute("href")).toBe("about:blank");

        const mention = document.createElement("div");
        renderMentionPreview(mention, { url: "javascript:alert(1)", title: "Evil" });
        expect(mention.querySelector("a.link-embed-mention")?.getAttribute("href")).toBe("about:blank");
    });

    it("never loads a remote favicon or image, whatever the note stores", () => {
        // Reaches here straight from the stored note HTML, exactly like the hostile `data-url` above.
        // An <img> needs no click, so a remote value would tell a third party that the reader opened
        // the note — the very thing embedding the metadata server-side exists to prevent.
        const card = makeContainer();
        renderEmbedPreview(card, {
            url: "https://example.com",
            embedType: "opengraph",
            favicon: "https://tracker.test/favicon.ico",
            image: "https://tracker.test/pixel.gif"
        });
        expect(card.querySelector("img.link-embed-card-image")).toBeNull();
        expect(card.querySelector(".link-embed-card-image-placeholder")).not.toBeNull();
        expect(card.querySelector("img.link-embed-mention-favicon")).toBeNull();

        const video = makeContainer();
        renderEmbedPreview(video, {
            url: "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
            embedType: "youtube",
            image: "https://tracker.test/thumb.jpg"
        });
        expect(video.querySelector("img.link-embed-video-thumbnail")).toBeNull();

        const mention = makeContainer();
        renderMentionPreview(mention, { url: "https://example.com", favicon: "http://169.254.169.254/latest/meta-data/" });
        expect(mention.querySelector("img")).toBeNull();
    });

    it("shows the stored favicon beside the site name, and nothing at all without one", () => {
        const root = makeContainer();
        const meta = {
            url: "https://example.com/page",
            embedType: "opengraph",
            siteName: "Example",
            favicon: "data:image/png;base64,AAA"
        };

        renderEmbedPreview(root, meta);

        // The card reuses the favicon the inline mention shows, straight from the stored metadata.
        const urlLine = root.querySelector(".link-embed-card-url")!;
        const favicon = urlLine.querySelector("img.link-embed-mention-favicon")!;
        expect(favicon.getAttribute("src")).toBe(meta.favicon);
        expect(urlLine.textContent).toBe("Example");
        // The favicon leads the line, so it renders to the left of the site name.
        expect(urlLine.firstElementChild).toBe(favicon);

        // Nothing stands in for an icon that could not be had: the URL line is the site name alone.
        const withoutFavicon = document.createElement("div");
        renderEmbedPreview(withoutFavicon, { ...meta, favicon: undefined });
        const urlLineWithout = withoutFavicon.querySelector(".link-embed-card-url");
        expect(urlLineWithout?.querySelector("img")).toBeNull();
        expect(urlLineWithout?.children.length).toBe(1);
    });

    it("omits optional fields and falls back to hostname, and drops target when editable", () => {
        const root = makeContainer();
        renderEmbedPreview(
            root,
            {
                url: "https://no-meta.example.com/page",
                embedType: "opengraph"
            },
            true
        );

        const card = root.querySelector("a.link-embed-card")!;
        expect(card.getAttribute("target")).toBeNull();
        expect(root.querySelector(".link-embed-card-title")).toBeNull();
        expect(root.querySelector(".link-embed-card-description")).toBeNull();
        // siteName missing -> safeHostname(url)
        expect(root.querySelector(".link-embed-card-url")!.textContent).toBe("no-meta.example.com");
        // image missing -> placeholder, no real <img>
        expect(root.querySelector(".link-embed-card-image-placeholder")).not.toBeNull();
        expect(root.querySelector("img.link-embed-card-image")).toBeNull();
    });

    it("replaces a broken card image with the placeholder on error", async () => {
        // How a stored image actually breaks: its attachment was deleted, so the reference 404s.
        const root = makeContainer();
        renderEmbedPreview(root, {
            url: "https://example.com",
            embedType: "opengraph",
            image: "api/attachments/deleted1/image/broken.png"
        });

        const img = root.querySelector("img.link-embed-card-image")!;
        expect(img).not.toBeNull();
        await fireError(img);

        expect(root.querySelector("img.link-embed-card-image")).toBeNull();
        expect(root.querySelector(".link-embed-card-image-placeholder")).not.toBeNull();
    });
});

describe("renderMentionPreview", () => {
    it("renders favicon img + title, with target=_blank by default", () => {
        const root = makeContainer();
        renderMentionPreview(root, {
            url: "https://example.com",
            title: "Example site",
            favicon: "data:image/png;base64,AAA"
        });

        const anchor = root.querySelector("a.link-embed-mention")!;
        expect(anchor.getAttribute("target")).toBe("_blank");
        expect(anchor.querySelector("img.link-embed-mention-favicon")).not.toBeNull();
        expect(anchor.querySelector(".link-embed-mention-title")!.textContent).toBe("Example site");
    });

    it("shows the title alone without a favicon, and drops target when editable", () => {
        const root = makeContainer();
        renderMentionPreview(
            root,
            { url: "https://fallback.example.com/x" },
            true
        );

        const anchor = root.querySelector("a.link-embed-mention")!;
        expect(anchor.getAttribute("target")).toBeNull();
        expect(root.querySelector("img")).toBeNull();
        // The title is the whole mention: nothing is drawn where the icon would have been.
        expect(anchor.children.length).toBe(1);
        expect(anchor.querySelector(".link-embed-mention-title")!.textContent).toBe("fallback.example.com");
    });

    it("drops a broken favicon on error, leaving the title alone", async () => {
        const root = makeContainer();
        renderMentionPreview(root, {
            url: "https://example.com",
            favicon: "api/attachments/deleted1/image/broken.ico"
        });

        const img = root.querySelector("img.link-embed-mention-favicon")!;
        expect(img).not.toBeNull();
        await fireError(img);

        expect(root.querySelector("img.link-embed-mention-favicon")).toBeNull();
        expect(root.querySelector("a.link-embed-mention")?.children.length).toBe(1);
    });
});

describe("applyLinkEmbeds", () => {
    it("renders previews from data attributes and skips elements without a url", () => {
        const root = makeContainer();
        root.innerHTML = `
            <section class="link-embed" data-url="https://full.example.com"
                data-embed-type="opengraph" data-title="T" data-description="D"
                data-site-name="Site" data-image="api/attachments/abc123/image/x.png"></section>
            <section class="link-embed"></section>
            <span class="link-mention" data-url="https://m.example.com"
                data-title="MT" data-favicon="data:image/png;base64,AAA"></span>
            <span class="link-mention"></span>
        `;

        applyLinkEmbeds(root);

        const embeds = root.querySelectorAll("section.link-embed");
        // First embed got rendered into a card; second (no url) stayed empty.
        expect(embeds[0].querySelector("a.link-embed-card")).not.toBeNull();
        expect(embeds[0].querySelector(".link-embed-card-title")!.textContent).toBe("T");
        expect(embeds[1].innerHTML).toBe("");

        const mentions = root.querySelectorAll("span.link-mention");
        expect(mentions[0].querySelector("a.link-embed-mention")).not.toBeNull();
        expect(mentions[0].querySelector(".link-embed-mention-title")!.textContent).toBe("MT");
        expect(mentions[1].innerHTML).toBe("");
    });

    it("defaults embedType to opengraph when the data attribute is absent", () => {
        const root = makeContainer();
        root.innerHTML = `<section class="link-embed" data-url="https://www.youtube.com/watch?v=dQw4w9WgXcQ"></section>`;

        applyLinkEmbeds(root);

        // No embed-type attr -> defaults to "opengraph" -> renders a card, not an iframe.
        const embed = root.querySelector("section.link-embed")!;
        expect(embed.querySelector("iframe")).toBeNull();
        expect(embed.querySelector("a.link-embed-card")).not.toBeNull();
    });
});