Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2
3import {
4 explicitFaviconUrl,
5 faviconUrlForOrigin,
6 faviconUrlForPage,
7 toolActivityFaviconUrl,
8} from "./favicon.ts";
9
10describe("faviconUrlForOrigin", () => {
11 it.each([
12 "http://192.168.1.10:8080",
13 "http://localhost:3000",
14 "http://home.arpa",
15 "https://printer.local.",
16 "https://api.internal",
17 "https://box.tailnet.ts.net",
18 "http://127.1",
19 "http://0x7f000001",
20 "http://[::]",
21 "http://[::1]",
22 "http://[::ffff:192.168.1.10]",
23 "http://[fd00::1]",
24 "http://[fe80::1]",
25 "http://100.64.0.1",
26 "http://198.51.100.1",
27 "http://[2001:db8::1]",
28 "http://service.test",
29 "http://private.onion",
30 "http://127.1..",
31 ])("does not disclose %s to the favicon provider", (origin) => {
32 expect(faviconUrlForOrigin(origin)).toBeNull();
33 });
34
35 it("keeps the public origin, port and requested size", () => {
36 expect(faviconUrlForOrigin("https://github.com:8443/pingdotgg/t3code?private=query", 64)).toBe(
37 "https://www.google.com/s2/favicons?domain=github.com%3A8443&sz=64",
38 );
39 });
40
41 it.each([null, undefined, "", "invalid URL", "file:///tmp/private", "data:text/plain,private"])(
42 "rejects an invalid or unsupported origin %s",
43 (origin) => {
44 expect(faviconUrlForOrigin(origin)).toBeNull();
45 },
46 );
47});
48
49describe("faviconUrlForPage", () => {
50 it("uses the page origin instead of a third-party favicon service", () => {
51 expect(faviconUrlForPage("https://example.com/docs/page?q=1")).toBe(
52 "https://example.com/favicon.ico",
53 );
54 expect(faviconUrlForPage("http://localhost:5173/app")).toBe(
55 "http://localhost:5173/favicon.ico",
56 );
57 });
58
59 it("selects site-owned light and dark variants without filtering full-color icons", () => {
60 expect(toolActivityFaviconUrl({ pageUrl: "https://github.com/openai/codex" }, "light")).toBe(
61 "https://github.githubassets.com/favicons/favicon.svg",
62 );
63 expect(toolActivityFaviconUrl({ pageUrl: "https://github.com/openai/codex" }, "dark")).toBe(
64 "https://github.githubassets.com/favicons/favicon-dark.svg",
65 );
66
67 const fullColorIcon = {
68 pageUrl: "https://example.com/docs",
69 faviconUrl: "https://cdn.example.com/full-color.png",
70 };
71 expect(toolActivityFaviconUrl(fullColorIcon, "light")).toBe(fullColorIcon.faviconUrl);
72 expect(toolActivityFaviconUrl(fullColorIcon, "dark")).toBe(fullColorIcon.faviconUrl);
73 });
74
75 it("prefers a provider-supplied dark favicon", () => {
76 expect(
77 toolActivityFaviconUrl(
78 {
79 pageUrl: "https://example.com/docs",
80 faviconUrl: "https://example.com/light.svg",
81 faviconUrlDark: "https://example.com/dark.svg",
82 },
83 "dark",
84 ),
85 ).toBe("https://example.com/dark.svg");
86 });
87
88 it("accepts provider-supplied image URLs but rejects extension URLs", () => {
89 expect(explicitFaviconUrl("https://example.com/icon.png")).toBe("https://example.com/icon.png");
90 expect(explicitFaviconUrl("chrome-extension://example/_favicon/")).toBeNull();
91 });
92});