| 1 | // Generated by continue |
| 2 | |
| 3 | import { |
| 4 | findFaviconPath, |
| 5 | getFaviconBase64, |
| 6 | fetchFavicon, |
| 7 | } from "./fetchFavicon"; |
| 8 | |
| 9 | describe("findFaviconPath", () => { |
| 10 | afterEach(() => { |
| 11 | jest.restoreAllMocks(); |
| 12 | }); |
| 13 | |
| 14 | it("should return the favicon URL when a favicon link is present", async () => { |
| 15 | const mockHtml = ` |
| 16 | <html> |
| 17 | <head> |
| 18 | <link rel="icon" href="/favicon.ico"> |
| 19 | </head> |
| 20 | <body></body> |
| 21 | </html> |
| 22 | `; |
| 23 | const mockResponse = { |
| 24 | ok: true, |
| 25 | text: jest.fn().mockResolvedValue(mockHtml), |
| 26 | }; |
| 27 | |
| 28 | global.fetch = jest.fn().mockResolvedValue(mockResponse); |
| 29 | |
| 30 | const url = new URL("https://example.com"); |
| 31 | const faviconPath = await findFaviconPath(url); |
| 32 | |
| 33 | expect(faviconPath).toBe("https://example.com/favicon.ico"); |
| 34 | expect(global.fetch).toHaveBeenCalledWith("https://example.com"); |
| 35 | }); |
| 36 | |
| 37 | it("should return undefined when no favicon link is present", async () => { |
| 38 | const mockHtml = ` |
| 39 | <html> |
| 40 | <head></head> |
| 41 | <body></body> |
| 42 | </html> |
| 43 | `; |
| 44 | const mockResponse = { |
| 45 | ok: true, |
| 46 | text: jest.fn().mockResolvedValue(mockHtml), |
| 47 | }; |
| 48 | |
| 49 | global.fetch = jest.fn().mockResolvedValue(mockResponse); |
| 50 | |
| 51 | const url = new URL("https://example.com"); |
| 52 | const faviconPath = await findFaviconPath(url); |
| 53 | |
| 54 | expect(faviconPath).toBeUndefined(); |
| 55 | expect(global.fetch).toHaveBeenCalledWith("https://example.com"); |
| 56 | }); |
| 57 | |
| 58 | it("should handle fetch errors gracefully and return undefined", async () => { |
| 59 | global.fetch = jest.fn().mockRejectedValue(new Error("Network error")); |
| 60 | |
| 61 | const url = new URL("https://example.com"); |
| 62 | const faviconPath = await findFaviconPath(url); |
| 63 | |
| 64 | expect(faviconPath).toBeUndefined(); |
| 65 | expect(global.fetch).toHaveBeenCalledWith("https://example.com"); |
| 66 | }); |
| 67 | |
| 68 | it("should handle non-OK responses and return undefined", async () => { |
| 69 | const mockResponse = { |
| 70 | ok: false, |
| 71 | status: 404, |
| 72 | }; |
| 73 | |
| 74 | global.fetch = jest.fn().mockResolvedValue(mockResponse); |
| 75 | |
| 76 | const url = new URL("https://example.com"); |
| 77 | const faviconPath = await findFaviconPath(url); |
| 78 | |
| 79 | expect(faviconPath).toBeUndefined(); |
| 80 | expect(global.fetch).toHaveBeenCalledWith("https://example.com"); |
| 81 | }); |
| 82 | |
| 83 | it("should handle relative favicon URLs", async () => { |
| 84 | const mockHtml = ` |
| 85 | <html> |
| 86 | <head> |
| 87 | <link rel="icon" href="favicon.ico"> |
| 88 | </head> |
| 89 | <body></body> |
| 90 | </html> |
| 91 | `; |
| 92 | const mockResponse = { |
| 93 | ok: true, |
| 94 | text: jest.fn().mockResolvedValue(mockHtml), |
| 95 | }; |
| 96 | |
| 97 | global.fetch = jest.fn().mockResolvedValue(mockResponse); |
| 98 | |
| 99 | const url = new URL("https://example.com"); |
| 100 | const faviconPath = await findFaviconPath(url); |
| 101 | |
| 102 | expect(faviconPath).toBe("https://example.com/favicon.ico"); |
| 103 | expect(global.fetch).toHaveBeenCalledWith("https://example.com"); |
| 104 | }); |
| 105 | }); |
| 106 | |
| 107 | describe("getFaviconBase64", () => { |
| 108 | afterEach(() => { |
| 109 | jest.restoreAllMocks(); |
| 110 | }); |
| 111 | |
| 112 | it("should return base64 data URI of the favicon", async () => { |
| 113 | const iconData = "icon data"; |
| 114 | const arrayBuffer = new TextEncoder().encode(iconData).buffer; |
| 115 | const mockResponse = { |
| 116 | ok: true, |
| 117 | arrayBuffer: jest.fn().mockResolvedValue(arrayBuffer), |
| 118 | headers: { |
| 119 | get: jest.fn().mockReturnValue("image/png"), |
| 120 | }, |
| 121 | }; |
| 122 | |
| 123 | global.fetch = jest.fn().mockResolvedValue(mockResponse); |
| 124 | global.btoa = jest |
| 125 | .fn() |
| 126 | .mockImplementation((str) => |
| 127 | Buffer.from(str, "binary").toString("base64"), |
| 128 | ); |
| 129 | |
| 130 | const faviconUrl = "https://example.com/favicon.ico"; |
| 131 | const base64DataUri = await getFaviconBase64(faviconUrl); |
| 132 | |
| 133 | const expectedBase64 = Buffer.from(iconData).toString("base64"); |
| 134 | |
| 135 | expect(base64DataUri).toBe(`data:image/png;base64,${expectedBase64}`); |
| 136 | expect(global.fetch).toHaveBeenCalledWith(faviconUrl); |
| 137 | }); |
| 138 | |
| 139 | it("should handle fetch errors gracefully and return undefined", async () => { |
| 140 | global.fetch = jest.fn().mockRejectedValue(new Error("Network error")); |
| 141 | |
| 142 | const faviconUrl = "https://example.com/favicon.ico"; |
| 143 | const base64DataUri = await getFaviconBase64(faviconUrl); |
| 144 | |
| 145 | expect(base64DataUri).toBeUndefined(); |
| 146 | expect(global.fetch).toHaveBeenCalledWith(faviconUrl); |
| 147 | }); |
| 148 | |
| 149 | it("should handle non-OK responses and return undefined", async () => { |
| 150 | const mockResponse = { |
| 151 | ok: false, |
| 152 | status: 404, |
| 153 | }; |
| 154 | |
| 155 | global.fetch = jest.fn().mockResolvedValue(mockResponse); |
| 156 | |
| 157 | const faviconUrl = "https://example.com/favicon.ico"; |
| 158 | const base64DataUri = await getFaviconBase64(faviconUrl); |
| 159 | |
| 160 | expect(base64DataUri).toBeUndefined(); |
| 161 | expect(global.fetch).toHaveBeenCalledWith(faviconUrl); |
| 162 | }); |
| 163 | |
| 164 | it("should use default mime type if content-type header is missing", async () => { |
| 165 | const iconData = "icon data"; |
| 166 | const arrayBuffer = new TextEncoder().encode(iconData).buffer; |
| 167 | const mockResponse = { |
| 168 | ok: true, |
| 169 | arrayBuffer: jest.fn().mockResolvedValue(arrayBuffer), |
| 170 | headers: { |
| 171 | get: jest.fn().mockReturnValue(null), |
| 172 | }, |
| 173 | }; |
| 174 | |
| 175 | global.fetch = jest.fn().mockResolvedValue(mockResponse); |
| 176 | global.btoa = jest |
| 177 | .fn() |
| 178 | .mockImplementation((str) => |
| 179 | Buffer.from(str, "binary").toString("base64"), |
| 180 | ); |
| 181 | |
| 182 | const faviconUrl = "https://example.com/favicon.ico"; |
| 183 | const base64DataUri = await getFaviconBase64(faviconUrl); |
| 184 | |
| 185 | const expectedBase64 = Buffer.from(iconData).toString("base64"); |
| 186 | |
| 187 | expect(base64DataUri).toBe(`data:image/x-icon;base64,${expectedBase64}`); |
| 188 | expect(global.fetch).toHaveBeenCalledWith(faviconUrl); |
| 189 | }); |
| 190 | }); |
| 191 | |
| 192 | describe("fetchFavicon", () => { |
| 193 | afterEach(() => { |
| 194 | jest.restoreAllMocks(); |
| 195 | }); |
| 196 | |
| 197 | it("should return base64 data URI of the favicon when favicon is found", async () => { |
| 198 | const url = new URL("https://example.com"); |
| 199 | |
| 200 | // Mock fetch for homepage |
| 201 | const homepageHtml = ` |
| 202 | <html> |
| 203 | <head> |
| 204 | <link rel="icon" href="/favicon.ico"> |
| 205 | </head> |
| 206 | <body></body> |
| 207 | </html> |
| 208 | `; |
| 209 | const homepageResponse = { |
| 210 | ok: true, |
| 211 | text: jest.fn().mockResolvedValue(homepageHtml), |
| 212 | }; |
| 213 | |
| 214 | // Mock fetch for favicon |
| 215 | const iconData = "icon data"; |
| 216 | const arrayBuffer = new TextEncoder().encode(iconData).buffer; |
| 217 | const faviconResponse = { |
| 218 | ok: true, |
| 219 | arrayBuffer: jest.fn().mockResolvedValue(arrayBuffer), |
| 220 | headers: { |
| 221 | get: jest.fn().mockReturnValue("image/png"), |
| 222 | }, |
| 223 | }; |
| 224 | |
| 225 | const fetchMock = jest.fn(); |
| 226 | fetchMock.mockResolvedValueOnce(homepageResponse); // First fetch call |
| 227 | fetchMock.mockResolvedValueOnce(faviconResponse); // Second fetch call |
| 228 | |
| 229 | global.fetch = fetchMock; |
| 230 | global.btoa = jest |
| 231 | .fn() |
| 232 | .mockImplementation((str) => |
| 233 | Buffer.from(str, "binary").toString("base64"), |
| 234 | ); |
| 235 | |
| 236 | const result = await fetchFavicon(url); |
| 237 | |
| 238 | const expectedBase64 = Buffer.from(iconData).toString("base64"); |
| 239 | |
| 240 | expect(result).toBe(`data:image/png;base64,${expectedBase64}`); |
| 241 | expect(global.fetch).toHaveBeenCalledTimes(2); |
| 242 | expect(global.fetch).toHaveBeenCalledWith("https://example.com"); |
| 243 | expect(global.fetch).toHaveBeenCalledWith( |
| 244 | "https://example.com/favicon.ico", |
| 245 | ); |
| 246 | }); |
| 247 | |
| 248 | it("should return undefined when favicon is not found", async () => { |
| 249 | const url = new URL("https://example.com"); |
| 250 | |
| 251 | // Mock fetch for homepage |
| 252 | const homepageHtml = ` |
| 253 | <html> |
| 254 | <head></head> |
| 255 | <body></body> |
| 256 | </html> |
| 257 | `; |
| 258 | const homepageResponse = { |
| 259 | ok: true, |
| 260 | text: jest.fn().mockResolvedValue(homepageHtml), |
| 261 | }; |
| 262 | |
| 263 | const fetchMock = jest.fn(); |
| 264 | fetchMock.mockResolvedValueOnce(homepageResponse); // First fetch call |
| 265 | |
| 266 | global.fetch = fetchMock; |
| 267 | |
| 268 | const result = await fetchFavicon(url); |
| 269 | |
| 270 | expect(result).toBeUndefined(); |
| 271 | expect(global.fetch).toHaveBeenCalledTimes(1); |
| 272 | expect(global.fetch).toHaveBeenCalledWith("https://example.com"); |
| 273 | }); |
| 274 | }); |