Skip to content

Build your personal knowledge base with Trilium Notes

test(commons): cover what this branch left untested

Elian Doran71dc979a78ce
diff --git a/packages/commons/src/lib/attachment_roles.spec.ts b/packages/commons/src/lib/attachment_roles.spec.ts
index be851090c4..2c94e38924 100644
--- a/packages/commons/src/lib/attachment_roles.spec.ts
+++ b/packages/commons/src/lib/attachment_roles.spec.ts
@@ -40,6 +40,12 @@ describe("isEmbeddedAttachmentRole", () => {
for (const role of [ "viewConfig", "canvasLibraryItem", "importSource" ]) {
expect(isEmbeddedAttachmentRole(role), role).toBe(false);
}
+
+ // A role from a script, or from a newer version reached over sync, is not one to hand the
+ // cleanup either — it answers for nothing it has never heard of.
+ expect(isEmbeddedAttachmentRole("somethingAScriptInvented")).toBe(false);
+ expect(isEmbeddedAttachmentRole(undefined)).toBe(false);
+ expect(isEmbeddedAttachmentRole(null)).toBe(false);
});
});
diff --git a/packages/commons/src/lib/favicon_contrast.spec.ts b/packages/commons/src/lib/favicon_contrast.spec.ts
index 3badd18490..1b0ec280e8 100644
--- a/packages/commons/src/lib/favicon_contrast.spec.ts
+++ b/packages/commons/src/lib/favicon_contrast.spec.ts
@@ -1,9 +1,10 @@
-import { describe, expect, it } from "vitest";
+import { afterEach, describe, expect, it, vi } from "vitest";
import {
classifyFaviconContrast,
contrastRatio,
faviconContrastClass,
+ measureFaviconVisibility,
relativeLuminance,
summarizeFaviconVisibility
} from "./favicon_contrast.js";
@@ -114,6 +115,49 @@ describe("classifying a favicon", () => {
});
});
+describe("measureFaviconVisibility", () => {
+ /** A canvas that draws nowhere and answers with the pixels it was given. */
+ function stubCanvas(context: unknown) {
+ vi.stubGlobal("document", { createElement: () => ({ getContext: () => context }) });
+ }
+
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ });
+
+ it("reads the icon back off the canvas it was rasterised into", () => {
+ const drawn: unknown[][] = [];
+ stubCanvas({
+ drawImage: (...args: unknown[]) => drawn.push(args),
+ getImageData: () => ({ data: bitmap(TRANSPARENT, [0, 0, 0, 255, 40]) })
+ });
+
+ const image = {} as CanvasImageSource;
+
+ expect(measureFaviconVisibility(image)).toEqual({ onDark: 0, onLight: 1, hasContent: true });
+ // Into a box of a stated size, because an SVG that declares only a viewBox has no size of
+ // its own for `drawImage` to read — and the whole box is read back, transparent field and all.
+ expect(drawn).toEqual([ [ image, 0, 0, 32, 32 ] ]);
+ });
+
+ it("answers nothing rather than a verdict when the icon cannot be measured", () => {
+ // No 2d context to be had.
+ stubCanvas(null);
+ expect(measureFaviconVisibility({} as CanvasImageSource)).toBeUndefined();
+
+ // `getImageData` throws on a tainted canvas. A preview's pictures are always same-origin, so
+ // this should not arise — but an unreadable icon is a reason to leave it as the site drew it,
+ // never to fail the render around it.
+ stubCanvas({
+ drawImage: () => {},
+ getImageData: () => {
+ throw new Error("tainted canvas");
+ }
+ });
+ expect(measureFaviconVisibility({} as CanvasImageSource)).toBeUndefined();
+ });
+});
+
describe("faviconContrastClass", () => {
it("names the two verdicts that need styling and stays silent on the third", () => {
expect(faviconContrastClass("dark")).toBe("link-embed-favicon-dark");
diff --git a/packages/commons/src/lib/link_embed.spec.ts b/packages/commons/src/lib/link_embed.spec.ts
index aa9ff5c99b..efb54f2b6d 100644
--- a/packages/commons/src/lib/link_embed.spec.ts
+++ b/packages/commons/src/lib/link_embed.spec.ts
@@ -8,6 +8,7 @@ import {
isLocalPreviewImageSrc,
isUrlAloneInBlock,
linkPreviewImageName,
+ safeHostname,
safeLinkPreviewHref,
safeLinkPreviewImageSrc,
YOUTUBE_REGEX
@@ -112,6 +113,18 @@ describe("isLocalPreviewImageSrc / safeLinkPreviewImageSrc", () => {
});
});
+describe("safeHostname", () => {
+ it("names the host, and answers with the address itself where there is no host to name", () => {
+ expect(safeHostname("https://en.wikipedia.org/wiki/Russo-Japanese_War")).toBe("en.wikipedia.org");
+ // An international host is punycoded by the parse, which is what makes it a file name.
+ expect(safeHostname("https://münchen.de/x")).toBe("xn--mnchen-3ya.de");
+
+ // Not an address at all. Both the label a preview falls back to and the title its favicon is
+ // stored under come from here, so answering with nothing would leave a preview unnamed.
+ expect(safeHostname("not a url")).toBe("not a url");
+ });
+});
+
describe("linkPreviewImageName", () => {
const wiki = "https://en.wikipedia.org/wiki/Russo-Japanese_War";