Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { it, describe, expect } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Path from "effect/Path";
7import { TestClock } from "effect/testing";
8
9import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
10import * as ProjectFaviconResolver from "./ProjectFaviconResolver.ts";
11import * as T3ProjectFileLoader from "./T3ProjectFileLoader.ts";
12
13const layerTest = Layer.empty.pipe(
14 Layer.provideMerge(
15 ProjectFaviconResolver.layer.pipe(
16 Layer.provide(WorkspacePaths.layer),
17 Layer.provide(T3ProjectFileLoader.layer),
18 ),
19 ),
20 Layer.provideMerge(NodeServices.layer),
21);
22
23const makeTempDir = Effect.gen(function* () {
24 const fileSystem = yield* FileSystem.FileSystem;
25 return yield* fileSystem.makeTempDirectoryScoped({
26 prefix: "t3code-project-favicon-",
27 });
28});
29
30const writeTextFile = Effect.fn("writeTextFile")(function* (
31 cwd: string,
32 relativePath: string,
33 contents: string,
34) {
35 const fileSystem = yield* FileSystem.FileSystem;
36 const path = yield* Path.Path;
37 const absolutePath = path.join(cwd, relativePath);
38 yield* fileSystem
39 .makeDirectory(path.dirname(absolutePath), { recursive: true })
40 .pipe(Effect.orDie);
41 yield* fileSystem.writeFileString(absolutePath, contents).pipe(Effect.orDie);
42});
43
44const makeResolverWithFileSystem = (fileSystem: FileSystem.FileSystem) =>
45 ProjectFaviconResolver.make.pipe(
46 Effect.provide([Layer.fresh(WorkspacePaths.layer), T3ProjectFileLoader.layer]),
47 Effect.provideService(FileSystem.FileSystem, fileSystem),
48 );
49
50it.layer(layerTest)("ProjectFaviconResolverLive", (it) => {
51 describe("resolvePath", () => {
52 it.effect("finds a favicon added after a cached miss once the workspace is invalidated", () =>
53 Effect.gen(function* () {
54 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
55 const path = yield* Path.Path;
56 const cwd = yield* makeTempDir;
57
58 // A clone's project exists while its folder is still empty.
59 expect(yield* resolver.resolvePath(cwd)).toBeNull();
60 yield* writeTextFile(cwd, "favicon.ico", "icon");
61 expect(yield* resolver.resolvePath(cwd)).toBeNull();
62
63 yield* resolver.invalidate(cwd);
64 expect(yield* resolver.resolvePath(cwd)).toBe(path.join(cwd, "favicon.ico"));
65 }),
66 );
67
68 it.effect("falls back at once when a cached favicon is deleted", () =>
69 Effect.gen(function* () {
70 const fileSystem = yield* FileSystem.FileSystem;
71 const path = yield* Path.Path;
72 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
73 const cwd = yield* makeTempDir;
74 yield* writeTextFile(cwd, "favicon.svg", "<svg>favicon</svg>");
75
76 expect(yield* resolver.resolvePath(cwd)).not.toBeNull();
77
78 yield* fileSystem.remove(path.join(cwd, "favicon.svg")).pipe(Effect.orDie);
79
80 expect(yield* resolver.resolvePath(cwd)).toBeNull();
81 }).pipe(Effect.provide(TestClock.layer())),
82 );
83
84 it.effect("looks up a missing workspace root once within the negative cache window", () =>
85 Effect.gen(function* () {
86 const fileSystem = yield* FileSystem.FileSystem;
87 const path = yield* Path.Path;
88 const cwd = path.join(yield* makeTempDir, "deleted-project");
89 let rootStats = 0;
90 const resolver = yield* makeResolverWithFileSystem(
91 FileSystem.FileSystem.of({
92 ...fileSystem,
93 stat: (filePath) => {
94 if (filePath === cwd) rootStats += 1;
95 return fileSystem.stat(filePath);
96 },
97 }),
98 );
99
100 yield* Effect.exit(resolver.resolvePath(cwd));
101 yield* Effect.exit(resolver.resolvePath(cwd));
102 expect(rootStats).toBe(1);
103 }),
104 );
105
106 it.effect("prefers a t3.json iconPath over well-known files", () =>
107 Effect.gen(function* () {
108 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
109 const path = yield* Path.Path;
110 const cwd = yield* makeTempDir;
111 yield* writeTextFile(cwd, "t3.json", '{ "iconPath": "brand/mark.svg" }');
112 yield* writeTextFile(cwd, "brand/mark.svg", "<svg>mark</svg>");
113 yield* writeTextFile(cwd, "favicon.svg", "<svg>favicon</svg>");
114
115 const resolved = yield* resolver.resolvePath(cwd);
116
117 expect(resolved).not.toBeNull();
118 expect(resolved).toBe(path.join(cwd, "brand", "mark.svg"));
119 }),
120 );
121
122 it.effect("uses a saved project favicon override", () =>
123 Effect.gen(function* () {
124 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
125 const path = yield* Path.Path;
126 const cwd = yield* makeTempDir;
127 yield* writeTextFile(cwd, "brand/custom.svg", "<svg>custom</svg>");
128 yield* writeTextFile(cwd, "favicon.svg", "<svg>automatic</svg>");
129
130 const resolved = yield* resolver.resolvePath(cwd, "brand/custom.svg");
131
132 expect(resolved).not.toBeNull();
133 expect(resolved).toBe(path.join(cwd, "brand", "custom.svg"));
134 }),
135 );
136
137 it.effect("uses a saved project favicon outside the workspace", () =>
138 Effect.gen(function* () {
139 const path = yield* Path.Path;
140 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
141 const cwd = yield* makeTempDir;
142 const pictures = yield* makeTempDir;
143 yield* writeTextFile(pictures, "custom.png", "image");
144 const externalPath = path.join(pictures, "custom.png");
145
146 const resolved = yield* resolver.resolvePath(cwd, externalPath);
147
148 expect(resolved).toBe(externalPath);
149 }),
150 );
151
152 it.effect("falls back when a saved override is missing from a checkout", () =>
153 Effect.gen(function* () {
154 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
155 const cwd = yield* makeTempDir;
156 yield* writeTextFile(cwd, "favicon.svg", "<svg>automatic</svg>");
157
158 const resolved = yield* resolver.resolvePath(cwd, "brand/missing.svg");
159
160 expect(resolved).not.toBeNull();
161 expect(resolved).toContain("favicon.svg");
162 }),
163 );
164
165 it.effect("ignores invalid t3.json files", () =>
166 Effect.gen(function* () {
167 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
168 const cwd = yield* makeTempDir;
169 yield* writeTextFile(cwd, "t3.json", "{ not json");
170 yield* writeTextFile(cwd, "favicon.svg", "<svg>favicon</svg>");
171
172 const resolved = yield* resolver.resolvePath(cwd);
173
174 expect(resolved).not.toBeNull();
175 expect(resolved).toContain("favicon.svg");
176 }),
177 );
178
179 it.effect("does not resolve a t3.json iconPath outside the workspace root", () =>
180 Effect.gen(function* () {
181 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
182 const parent = yield* makeTempDir;
183 const cwd = `${parent}/app`;
184 yield* writeTextFile(parent, "secret.svg", "<svg>secret</svg>");
185 yield* writeTextFile(cwd, "t3.json", '{ "iconPath": "../secret.svg" }');
186
187 const resolved = yield* resolver.resolvePath(cwd);
188
189 expect(resolved).toBeNull();
190 }),
191 );
192
193 it.effect("resolves icon hrefs from project source files", () =>
194 Effect.gen(function* () {
195 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
196 const cwd = yield* makeTempDir;
197 yield* writeTextFile(
198 cwd,
199 "index.html",
200 `<link rel="icon" href="data:image/png;base64,${"A".repeat(5000)}">
201<link rel="icon" href="https://cdn.example.com/icon.svg">
202<link rel="icon" href="/missing.svg">
203<link rel="icon" href="//cdn.example.com/icon.svg">
204<link rel="icon" href="/index.html/icon.svg">
205<link rel="icon" href="/a%00b.svg">
206<link rel="icon" href="/brand/logo.svg#mark">
207<link rel="icon" href="/brand/late.svg">`,
208 );
209 yield* writeTextFile(cwd, "public/brand/logo.svg", "<svg>brand</svg>");
210 yield* writeTextFile(cwd, "public/brand/late.svg", "<svg>late</svg>");
211 yield* writeTextFile(cwd, "public/cdn.example.com/icon.svg", "<svg>cdn</svg>");
212 yield* writeTextFile(cwd, "web/favicon.png", "flutter");
213
214 const resolved = yield* resolver.resolvePath(cwd);
215
216 expect(resolved).not.toBeNull();
217 expect(resolved).toBe((yield* Path.Path).join(cwd, "public", "brand", "logo.svg"));
218
219 // A cached icon whose `public` folder became a file falls back instead of failing.
220 yield* (yield* FileSystem.FileSystem).remove((yield* Path.Path).join(cwd, "public"), {
221 recursive: true,
222 });
223 yield* writeTextFile(cwd, "public", "not a folder");
224 expect(yield* resolver.resolvePath(cwd)).toBe(
225 (yield* Path.Path).join(cwd, "web", "favicon.png"),
226 );
227
228 const flutterCwd = yield* makeTempDir;
229 yield* writeTextFile(flutterCwd, "web/favicon.png", "flutter");
230 expect(yield* resolver.resolvePath(flutterCwd)).toBe(
231 (yield* Path.Path).join(flutterCwd, "web", "favicon.png"),
232 );
233
234 const paddedCwd = yield* makeTempDir;
235 yield* writeTextFile(
236 paddedCwd,
237 "index.html",
238 '<link rel="icon" href=" brand/custom.svg ">',
239 );
240 yield* writeTextFile(paddedCwd, "brand/custom.svg", "<svg>custom</svg>");
241 expect(yield* resolver.resolvePath(paddedCwd)).toBe(
242 (yield* Path.Path).join(paddedCwd, "brand", "custom.svg"),
243 );
244
245 const tokenListCwd = yield* makeTempDir;
246 yield* writeTextFile(
247 tokenListCwd,
248 "index.html",
249 `<link rel="apple-touch-icon" href="touch.svg"><link rel="icon's" href="touch.svg"><link rel="Icon\tshortcut" href="brand.svg">`,
250 );
251 yield* writeTextFile(tokenListCwd, "touch.svg", "<svg>touch</svg>");
252 yield* writeTextFile(tokenListCwd, "brand.svg", "<svg>brand</svg>");
253 expect(yield* resolver.resolvePath(tokenListCwd)).toBe(
254 (yield* Path.Path).join(tokenListCwd, "brand.svg"),
255 );
256
257 const unquotedCwd = yield* makeTempDir;
258 yield* writeTextFile(
259 unquotedCwd,
260 "index.html",
261 `<link rel=stylesheet data-rel=icon href=app.css><link href=app.css rel=icon/><link rel=icon href={icon} /><link rel=icon data-href=app.css title="href=app.css" href=brand.svg/>`,
262 );
263 yield* writeTextFile(unquotedCwd, "app.css", "body {}");
264 yield* writeTextFile(unquotedCwd, "brand.svg", "<svg>brand</svg>");
265 expect(yield* resolver.resolvePath(unquotedCwd)).toBe(
266 (yield* Path.Path).join(unquotedCwd, "brand.svg"),
267 );
268
269 const quotedValueCwd = yield* makeTempDir;
270 yield* writeTextFile(
271 quotedValueCwd,
272 "index.html",
273 `<link rel="icon" title='preview href=app.css < primary' media="(width > 600px)" href="brand.svg">`,
274 );
275 yield* writeTextFile(quotedValueCwd, "app.css", "body {}");
276 yield* writeTextFile(quotedValueCwd, "brand.svg", "<svg>brand</svg>");
277 expect(yield* resolver.resolvePath(quotedValueCwd)).toBe(
278 (yield* Path.Path).join(quotedValueCwd, "brand.svg"),
279 );
280
281 const unquotedQuoteCwd = yield* makeTempDir;
282 yield* writeTextFile(
283 unquotedQuoteCwd,
284 "index.html",
285 `<link rel=stylesheet title=O'Reilly><link rel="icon" href="brand.svg"><!-- it's -->`,
286 );
287 yield* writeTextFile(unquotedQuoteCwd, "brand.svg", "<svg>brand</svg>");
288 expect(yield* resolver.resolvePath(unquotedQuoteCwd)).toBe(
289 (yield* Path.Path).join(unquotedQuoteCwd, "brand.svg"),
290 );
291
292 const encodedCwd = yield* makeTempDir;
293 yield* writeTextFile(encodedCwd, "index.html", '<link rel="icon" href="/team%20mark.svg">');
294 yield* writeTextFile(encodedCwd, "public/team mark.svg", "<svg>team</svg>");
295 expect(yield* resolver.resolvePath(encodedCwd)).toBe(
296 (yield* Path.Path).join(encodedCwd, "public", "team mark.svg"),
297 );
298
299 for (const [href, filename, rel = "icon"] of [
300 ["/brand/logo.svg#mark", "logo.svg"],
301 ["/brand/design&dev.svg?v=2", "design&dev.svg"],
302 ["/brand/logo%23mark.svg?v=2", "logo#mark.svg"],
303 ["/brand/€.svg", "\u20ac.svg"],
304 ["/brand/design&-dev.svg", "design&-dev.svg"],
305 ["/brand/a©=b.svg", "a©=b.svg"],
306 ["/brand/&dev.svg", "&dev.svg"],
307 ["/brand/&apos.svg", "&apos.svg"],
308 ["/brand/primary.svg", "primary.svg", "icon"],
309 ["/brand/secondary.svg", "secondary.svg", "shortcut	icon"],
310 ] as const) {
311 const entityCwd = yield* makeTempDir;
312 yield* writeTextFile(entityCwd, "index.html", `<link rel="${rel}" href="${href}">`);
313 yield* writeTextFile(entityCwd, `public/brand/${filename}`, "<svg>brand</svg>");
314 expect(yield* resolver.resolvePath(entityCwd), href).toBe(
315 (yield* Path.Path).join(entityCwd, "public", "brand", filename),
316 );
317 }
318
319 const jsxCwd = yield* makeTempDir;
320 yield* writeTextFile(
321 jsxCwd,
322 "app/root.tsx",
323 '<link rel="shortcut	icon" href="/brand/other.svg" /><link rel="icon" href="/brand/a&-b.svg" />',
324 );
325 yield* writeTextFile(jsxCwd, "public/brand/other.svg", "<svg>other</svg>");
326 yield* writeTextFile(jsxCwd, "public/brand/a&-b.svg", "<svg>brand</svg>");
327 expect(yield* resolver.resolvePath(jsxCwd)).toBe(
328 (yield* Path.Path).join(jsxCwd, "public", "brand", "a&-b.svg"),
329 );
330
331 const malformedCwd = yield* makeTempDir;
332 yield* writeTextFile(malformedCwd, "index.html", '<link rel="icon" href="/100%.svg">');
333 yield* writeTextFile(malformedCwd, "public/100%.svg", "<svg>100</svg>");
334 expect(yield* resolver.resolvePath(malformedCwd)).toBe(
335 (yield* Path.Path).join(malformedCwd, "public", "100%.svg"),
336 );
337 }),
338 );
339
340 it.effect("resolves relative hrefs in src/index.html against src after public and root", () =>
341 Effect.gen(function* () {
342 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
343 const path = yield* Path.Path;
344 const cwd = yield* makeTempDir;
345 yield* writeTextFile(
346 cwd,
347 "src/index.html",
348 '<link rel="icon" href="/brand/root.svg"><link rel="icon" type="image/png" href="assets/brand/icon.png">',
349 );
350 yield* writeTextFile(cwd, "src/brand/root.svg", "<svg>src</svg>");
351 yield* writeTextFile(cwd, "src/assets/brand/icon.png", "png");
352 expect(yield* resolver.resolvePath(cwd)).toBe(
353 path.join(cwd, "src", "assets", "brand", "icon.png"),
354 );
355 yield* writeTextFile(cwd, "public/assets/brand/icon.png", "public");
356 yield* resolver.invalidate(cwd);
357 expect(yield* resolver.resolvePath(cwd)).toBe(
358 path.join(cwd, "public", "assets", "brand", "icon.png"),
359 );
360 }),
361 );
362
363 it.effect("skips icon links inside HTML comments", () =>
364 Effect.gen(function* () {
365 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
366 const path = yield* Path.Path;
367 const cwd = yield* makeTempDir;
368 // A template's icon link commented out in favour of the project's own.
369 yield* writeTextFile(
370 cwd,
371 "index.html",
372 '<!-- <link rel="icon" type="image/svg+xml" href="/vite.svg" /> -->\n<link rel="icon" type="image/png" href="/brand.png" />',
373 );
374 yield* writeTextFile(cwd, "public/vite.svg", "<svg>vite</svg>");
375 yield* writeTextFile(cwd, "public/brand.png", "png");
376 expect(yield* resolver.resolvePath(cwd)).toBe(path.join(cwd, "public", "brand.png"));
377 }),
378 );
379
380 it.effect("skips source metadata beyond the favicon scan limit", () =>
381 Effect.gen(function* () {
382 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
383 const cwd = yield* makeTempDir;
384 yield* writeTextFile(
385 cwd,
386 "index.html",
387 `<!-- ${" ".repeat(128 * 1024)} --><link rel="icon" href="/brand/logo.svg">`,
388 );
389 yield* writeTextFile(cwd, "public/brand/logo.svg", "<svg>brand</svg>");
390
391 expect(yield* resolver.resolvePath(cwd)).toBeNull();
392 }),
393 );
394
395 it.effect("resolves icon hrefs from object-literal route metadata", () =>
396 Effect.gen(function* () {
397 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
398 const cwd = yield* makeTempDir;
399 yield* writeTextFile(
400 cwd,
401 "src/routes/__root.tsx",
402 `export const Route = createRootRoute({
403 head: () => ({
404 links: [
405 { rel: "stylesheet", href: "/app.css" },
406 { rel: "icon", href: "https://cdn.example.com/icon.svg" },
407 { rel: "icon shortcut", href: "/brand/logo.svg#mark" },
408 ],
409 }),
410});`,
411 );
412 yield* writeTextFile(cwd, "public/brand/logo.svg", "<svg>brand</svg>");
413
414 const resolved = yield* resolver.resolvePath(cwd);
415
416 expect(resolved).not.toBeNull();
417 expect(resolved).toBe((yield* Path.Path).join(cwd, "public", "brand", "logo.svg"));
418 }),
419 );
420
421 it.effect("keeps apostrophes inside double-quoted route metadata hrefs", () =>
422 Effect.gen(function* () {
423 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
424 const cwd = yield* makeTempDir;
425 yield* writeTextFile(
426 cwd,
427 "src/routes/__root.tsx",
428 `const links = [{ rel: "icon", href: "/brand/chef's-hat.svg#mark" }];`,
429 );
430 yield* writeTextFile(cwd, "public/brand/chef's-hat.svg", "<svg>brand</svg>");
431
432 expect(yield* resolver.resolvePath(cwd)).toBe(
433 (yield* Path.Path).join(cwd, "public", "brand", "chef's-hat.svg"),
434 );
435 }),
436 );
437
438 it.effect("resolves escaped apostrophes in single-quoted route metadata hrefs", () =>
439 Effect.gen(function* () {
440 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
441 const cwd = yield* makeTempDir;
442 yield* writeTextFile(
443 cwd,
444 "src/routes/__root.tsx",
445 String.raw`const links = [{ 'rel': 'icon', 'href': '/brand/it\'s.svg' }];`,
446 );
447 yield* writeTextFile(cwd, "public/brand/it's.svg", "<svg>brand</svg>");
448
449 expect(yield* resolver.resolvePath(cwd)).toBe(
450 (yield* Path.Path).join(cwd, "public", "brand", "it's.svg"),
451 );
452 }),
453 );
454
455 it.effect("resolves quoted property names in route metadata", () =>
456 Effect.gen(function* () {
457 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
458 const path = yield* Path.Path;
459 for (const [links, file] of [
460 [`[{ "rel": "icon", "href": "/brand/double.svg" }]`, "double.svg"],
461 [`[{ 'href': '/brand/single.svg', 'rel': 'icon' }]`, "single.svg"],
462 [`[{ "href": "/brand/mixed.svg", rel: "icon" }]`, "mixed.svg"],
463 ] as const) {
464 const cwd = yield* makeTempDir;
465 yield* writeTextFile(cwd, "src/routes/__root.tsx", `const links = ${links};`);
466 yield* writeTextFile(cwd, `public/brand/${file}`, "<svg>brand</svg>");
467 expect(yield* resolver.resolvePath(cwd), file).toBe(
468 path.join(cwd, "public", "brand", file),
469 );
470 }
471 }),
472 );
473
474 // regression: #5538 favicon resolution pinned the event loop
475 it.effect("skips icon metadata without an href and keeps scanning", () =>
476 Effect.gen(function* () {
477 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
478 const cwd = yield* makeTempDir;
479 yield* writeTextFile(
480 cwd,
481 "src/root.tsx",
482 `const links = [{ rel: "icon" }, { rel: "icon", href: "/brand/logo.svg" }];`,
483 );
484 yield* writeTextFile(cwd, "public/brand/logo.svg", "<svg>brand</svg>");
485
486 const resolved = yield* resolver.resolvePath(cwd);
487
488 expect(resolved).not.toBeNull();
489 expect(resolved).toBe((yield* Path.Path).join(cwd, "public", "brand", "logo.svg"));
490 }),
491 );
492
493 it.effect("returns a found favicon without waiting for lower-ranked probes", () =>
494 Effect.gen(function* () {
495 const fileSystem = yield* FileSystem.FileSystem;
496 const path = yield* Path.Path;
497 const cwd = yield* makeTempDir;
498 yield* writeTextFile(cwd, "favicon.svg", "<svg>root</svg>");
499 const stuckPath = path.join(cwd, "favicon.ico");
500 const resolver = yield* makeResolverWithFileSystem(
501 FileSystem.FileSystem.of({
502 ...fileSystem,
503 stat: (filePath) => (filePath === stuckPath ? Effect.never : fileSystem.stat(filePath)),
504 }),
505 );
506
507 expect(yield* resolver.resolvePath(cwd)).toBe(path.join(cwd, "favicon.svg"));
508 }),
509 );
510
511 it.effect("skips icon metadata paths outside the workspace", () =>
512 Effect.gen(function* () {
513 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
514 const path = yield* Path.Path;
515 const root = yield* makeTempDir;
516 const cwd = path.join(root, "parent", "workspace");
517 yield* writeTextFile(root, "outside.svg", "<svg>two-levels-up</svg>");
518 yield* writeTextFile(root, "parent/outside.svg", "<svg>one-level-up</svg>");
519 yield* writeTextFile(
520 cwd,
521 "index.html",
522 '<link rel="icon" href="../../outside.svg"><link rel="icon" href="/%2e%2e%2f%2e%2e%2foutside.svg">',
523 );
524 yield* writeTextFile(cwd, "src/index.html", '<link rel="icon" href="../../outside.svg">');
525
526 const resolved = yield* resolver.resolvePath(cwd);
527
528 expect(resolved).toBeNull();
529 }),
530 );
531 });
532});