Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { it, describe, expect } from "@effect/vitest";
3import * as Duration from "effect/Duration";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Layer from "effect/Layer";
7import * as Path from "effect/Path";
8import * as PlatformError from "effect/PlatformError";
9import { TestClock } from "effect/testing";
10
11import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
12import * as ProjectFaviconResolver from "./ProjectFaviconResolver.ts";
13import * as T3ProjectFileLoader from "./T3ProjectFileLoader.ts";
14
15const layerTest = Layer.empty.pipe(
16 Layer.provideMerge(
17 ProjectFaviconResolver.layer.pipe(
18 Layer.provide(WorkspacePaths.layer),
19 Layer.provide(T3ProjectFileLoader.layer),
20 ),
21 ),
22 Layer.provideMerge(NodeServices.layer),
23);
24
25const makeTempDir = Effect.gen(function* () {
26 const fileSystem = yield* FileSystem.FileSystem;
27 return yield* fileSystem.makeTempDirectoryScoped({
28 prefix: "t3code-project-favicon-",
29 });
30});
31
32const writeTextFile = Effect.fn("writeTextFile")(function* (
33 cwd: string,
34 relativePath: string,
35 contents: string,
36) {
37 const fileSystem = yield* FileSystem.FileSystem;
38 const path = yield* Path.Path;
39 const absolutePath = path.join(cwd, relativePath);
40 yield* fileSystem
41 .makeDirectory(path.dirname(absolutePath), { recursive: true })
42 .pipe(Effect.orDie);
43 yield* fileSystem.writeFileString(absolutePath, contents).pipe(Effect.orDie);
44});
45
46const makeResolverWithFileSystem = (fileSystem: FileSystem.FileSystem) =>
47 ProjectFaviconResolver.make.pipe(
48 Effect.provide([WorkspacePaths.layer, T3ProjectFileLoader.layer]),
49 Effect.provideService(FileSystem.FileSystem, fileSystem),
50 );
51
52it.layer(layerTest)("ProjectFaviconResolverLive", (it) => {
53 describe("resolvePath", () => {
54 it.effect("serves repeated resolves from cache instead of re-walking candidates", () =>
55 Effect.gen(function* () {
56 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
57 const path = yield* Path.Path;
58 const cwd = yield* makeTempDir;
59 yield* writeTextFile(cwd, "public/favicon.svg", "<svg>public</svg>");
60
61 const resolved = yield* resolver.resolvePath(cwd);
62 expect(resolved?.endsWith(path.join("public", "favicon.svg"))).toBe(true);
63
64 yield* writeTextFile(cwd, "favicon.svg", "<svg>root</svg>");
65
66 for (const _attempt of [1, 2, 3]) {
67 expect(yield* resolver.resolvePath(cwd)).toBe(resolved);
68 }
69
70 yield* TestClock.adjust(Duration.minutes(11));
71
72 expect(yield* resolver.resolvePath(cwd)).toBe(path.join(cwd, "favicon.svg"));
73 expect(yield* resolver.resolvePath(cwd)).not.toBe(resolved);
74 }).pipe(Effect.provide(TestClock.layer())),
75 );
76
77 it.effect("falls back at once when a cached favicon is deleted", () =>
78 Effect.gen(function* () {
79 const fileSystem = yield* FileSystem.FileSystem;
80 const path = yield* Path.Path;
81 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
82 const cwd = yield* makeTempDir;
83 yield* writeTextFile(cwd, "favicon.svg", "<svg>favicon</svg>");
84
85 expect(yield* resolver.resolvePath(cwd)).not.toBeNull();
86
87 yield* fileSystem.remove(path.join(cwd, "favicon.svg")).pipe(Effect.orDie);
88
89 expect(yield* resolver.resolvePath(cwd)).toBeNull();
90 }).pipe(Effect.provide(TestClock.layer())),
91 );
92
93 it.effect("re-probes for a favicon added after a miss once the negative TTL expires", () =>
94 Effect.gen(function* () {
95 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
96 const cwd = yield* makeTempDir;
97
98 expect(yield* resolver.resolvePath(cwd)).toBeNull();
99
100 yield* writeTextFile(cwd, "favicon.svg", "<svg>favicon</svg>");
101 expect(yield* resolver.resolvePath(cwd)).toBeNull();
102
103 yield* TestClock.adjust(Duration.minutes(2));
104
105 expect(yield* resolver.resolvePath(cwd)).not.toBeNull();
106 }).pipe(Effect.provide(TestClock.layer())),
107 );
108
109 it.effect("prefers well-known favicon files", () =>
110 Effect.gen(function* () {
111 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
112 const cwd = yield* makeTempDir;
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).toContain("favicon.svg");
119 }),
120 );
121
122 it.effect("prefers a t3.json iconPath over well-known files", () =>
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, "t3.json", '{ "iconPath": "brand/mark.svg" }');
128 yield* writeTextFile(cwd, "brand/mark.svg", "<svg>mark</svg>");
129 yield* writeTextFile(cwd, "favicon.svg", "<svg>favicon</svg>");
130
131 const resolved = yield* resolver.resolvePath(cwd);
132
133 expect(resolved).not.toBeNull();
134 expect(resolved).toBe(path.join(cwd, "brand", "mark.svg"));
135 }),
136 );
137
138 it.effect("uses a saved project favicon override", () =>
139 Effect.gen(function* () {
140 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
141 const path = yield* Path.Path;
142 const cwd = yield* makeTempDir;
143 yield* writeTextFile(cwd, "brand/custom.svg", "<svg>custom</svg>");
144 yield* writeTextFile(cwd, "favicon.svg", "<svg>automatic</svg>");
145
146 const resolved = yield* resolver.resolvePath(cwd, "brand/custom.svg");
147
148 expect(resolved).not.toBeNull();
149 expect(resolved).toBe(path.join(cwd, "brand", "custom.svg"));
150 }),
151 );
152
153 it.effect("uses a saved project favicon outside the workspace", () =>
154 Effect.gen(function* () {
155 const path = yield* Path.Path;
156 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
157 const cwd = yield* makeTempDir;
158 const pictures = yield* makeTempDir;
159 yield* writeTextFile(pictures, "custom.png", "image");
160 const externalPath = path.join(pictures, "custom.png");
161
162 const resolved = yield* resolver.resolvePath(cwd, externalPath);
163
164 expect(resolved).toBe(externalPath);
165 }),
166 );
167
168 it.effect("falls back when a saved override is missing from a checkout", () =>
169 Effect.gen(function* () {
170 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
171 const cwd = yield* makeTempDir;
172 yield* writeTextFile(cwd, "favicon.svg", "<svg>automatic</svg>");
173
174 const resolved = yield* resolver.resolvePath(cwd, "brand/missing.svg");
175
176 expect(resolved).not.toBeNull();
177 expect(resolved).toContain("favicon.svg");
178 }),
179 );
180
181 it.effect("falls back to well-known files when the t3.json iconPath does not exist", () =>
182 Effect.gen(function* () {
183 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
184 const cwd = yield* makeTempDir;
185 yield* writeTextFile(cwd, "t3.json", '{ "iconPath": "brand/missing.svg" }');
186 yield* writeTextFile(cwd, "favicon.svg", "<svg>favicon</svg>");
187
188 const resolved = yield* resolver.resolvePath(cwd);
189
190 expect(resolved).not.toBeNull();
191 expect(resolved).toContain("favicon.svg");
192 }),
193 );
194
195 it.effect("ignores invalid t3.json files", () =>
196 Effect.gen(function* () {
197 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
198 const cwd = yield* makeTempDir;
199 yield* writeTextFile(cwd, "t3.json", "{ not json");
200 yield* writeTextFile(cwd, "favicon.svg", "<svg>favicon</svg>");
201
202 const resolved = yield* resolver.resolvePath(cwd);
203
204 expect(resolved).not.toBeNull();
205 expect(resolved).toContain("favicon.svg");
206 }),
207 );
208
209 it.effect("does not resolve a t3.json iconPath outside the workspace root", () =>
210 Effect.gen(function* () {
211 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
212 const parent = yield* makeTempDir;
213 const cwd = `${parent}/app`;
214 yield* writeTextFile(parent, "secret.svg", "<svg>secret</svg>");
215 yield* writeTextFile(cwd, "t3.json", '{ "iconPath": "../secret.svg" }');
216
217 const resolved = yield* resolver.resolvePath(cwd);
218
219 expect(resolved).toBeNull();
220 }),
221 );
222
223 it.effect("resolves icon hrefs from project source files", () =>
224 Effect.gen(function* () {
225 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
226 const cwd = yield* makeTempDir;
227 yield* writeTextFile(cwd, "index.html", '<link rel="icon" href="/brand/logo.svg">');
228 yield* writeTextFile(cwd, "public/brand/logo.svg", "<svg>brand</svg>");
229
230 const resolved = yield* resolver.resolvePath(cwd);
231
232 expect(resolved).not.toBeNull();
233 expect(resolved).toBe((yield* Path.Path).join(cwd, "public", "brand", "logo.svg"));
234 }),
235 );
236
237 it.effect("resolves icon hrefs from object-literal route metadata", () =>
238 Effect.gen(function* () {
239 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
240 const cwd = yield* makeTempDir;
241 yield* writeTextFile(
242 cwd,
243 "src/routes/__root.tsx",
244 `export const Route = createRootRoute({
245 head: () => ({
246 links: [
247 { rel: "stylesheet", href: "/app.css" },
248 { rel: "icon", href: "/brand/logo.svg" },
249 ],
250 }),
251});`,
252 );
253 yield* writeTextFile(cwd, "public/brand/logo.svg", "<svg>brand</svg>");
254
255 const resolved = yield* resolver.resolvePath(cwd);
256
257 expect(resolved).not.toBeNull();
258 expect(resolved).toBe((yield* Path.Path).join(cwd, "public", "brand", "logo.svg"));
259 }),
260 );
261
262 it.effect("resolves object-literal icon metadata when href precedes rel", () =>
263 Effect.gen(function* () {
264 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
265 const cwd = yield* makeTempDir;
266 yield* writeTextFile(
267 cwd,
268 "src/root.tsx",
269 `const links = [{ href: "/brand/logo.svg", rel: "shortcut icon" }];`,
270 );
271 yield* writeTextFile(cwd, "public/brand/logo.svg", "<svg>brand</svg>");
272
273 const resolved = yield* resolver.resolvePath(cwd);
274
275 expect(resolved).not.toBeNull();
276 expect(resolved).toBe((yield* Path.Path).join(cwd, "public", "brand", "logo.svg"));
277 }),
278 );
279
280 it.effect("resolves object-literal icon metadata alongside nested objects", () =>
281 Effect.gen(function* () {
282 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
283 const cwd = yield* makeTempDir;
284 yield* writeTextFile(
285 cwd,
286 "src/root.tsx",
287 `const links = [{ attributes: {}, rel: "icon", href: "/brand/logo.svg" }];`,
288 );
289 yield* writeTextFile(cwd, "public/brand/logo.svg", "<svg>brand</svg>");
290
291 const resolved = yield* resolver.resolvePath(cwd);
292
293 expect(resolved).not.toBeNull();
294 expect(resolved).toBe((yield* Path.Path).join(cwd, "public", "brand", "logo.svg"));
295 }),
296 );
297
298 it.effect("skips icon metadata without an href and keeps scanning", () =>
299 Effect.gen(function* () {
300 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
301 const cwd = yield* makeTempDir;
302 yield* writeTextFile(
303 cwd,
304 "src/root.tsx",
305 `const links = [{ rel: "icon" }, { rel: "icon", href: "/brand/logo.svg" }];`,
306 );
307 yield* writeTextFile(cwd, "public/brand/logo.svg", "<svg>brand</svg>");
308
309 const resolved = yield* resolver.resolvePath(cwd);
310
311 expect(resolved).not.toBeNull();
312 expect(resolved).toBe((yield* Path.Path).join(cwd, "public", "brand", "logo.svg"));
313 }),
314 );
315
316 it.effect("scans large icon sources without an icon in reasonable time", () =>
317 Effect.gen(function* () {
318 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
319 const cwd = yield* makeTempDir;
320 const filler = `<p>${"pokopia companion guide ".repeat(24)}</p>\n`;
321 yield* writeTextFile(
322 cwd,
323 "index.html",
324 `<!doctype html><html><head><title>guide</title></head><body>\n${filler.repeat(1200)}</body></html>`,
325 );
326
327 const startedAt = performance.now();
328 const resolved = yield* resolver.resolvePath(cwd);
329 const elapsedMs = performance.now() - startedAt;
330
331 expect(resolved).toBeNull();
332 expect(elapsedMs).toBeLessThan(5_000);
333 }),
334 );
335
336 it.effect("returns null when no icon is present", () =>
337 Effect.gen(function* () {
338 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
339 const cwd = yield* makeTempDir;
340
341 const resolved = yield* resolver.resolvePath(cwd);
342
343 expect(resolved).toBeNull();
344 }),
345 );
346
347 it.effect("preserves workspace normalization context", () =>
348 Effect.gen(function* () {
349 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
350 const cwd = yield* makeTempDir;
351 const missingCwd = `${cwd}/missing`;
352
353 const error = yield* resolver.resolvePath(missingCwd).pipe(Effect.flip);
354
355 expect(error).toMatchObject({
356 _tag: "ProjectFaviconResolutionError",
357 operation: "normalize-workspace",
358 workspaceRoot: missingCwd,
359 });
360 expect(error.cause).toBeInstanceOf(WorkspacePaths.WorkspaceRootNotExistsError);
361 }),
362 );
363
364 it.effect("preserves non-missing candidate stat failures", () =>
365 Effect.gen(function* () {
366 const fileSystem = yield* FileSystem.FileSystem;
367 const path = yield* Path.Path;
368 const cwd = yield* makeTempDir;
369 const faviconPath = path.join(cwd, "favicon.svg");
370 const cause = PlatformError.systemError({
371 _tag: "PermissionDenied",
372 module: "FileSystem",
373 method: "stat",
374 pathOrDescriptor: faviconPath,
375 });
376 const resolver = yield* makeResolverWithFileSystem(
377 FileSystem.FileSystem.of({
378 ...fileSystem,
379 stat: (filePath) =>
380 filePath === faviconPath ? Effect.fail(cause) : fileSystem.stat(filePath),
381 }),
382 );
383
384 const error = yield* resolver.resolvePath(cwd).pipe(Effect.flip);
385
386 expect(error).toMatchObject({
387 _tag: "ProjectFaviconResolutionError",
388 operation: "stat-candidate",
389 workspaceRoot: cwd,
390 relativePath: "favicon.svg",
391 absolutePath: faviconPath,
392 });
393 expect(error.cause).toBe(cause);
394 }),
395 );
396
397 it.effect("probes well-known candidates concurrently", () =>
398 Effect.gen(function* () {
399 const fileSystem = yield* FileSystem.FileSystem;
400 const cwd = yield* makeTempDir;
401 let inFlight = 0;
402 let peakInFlight = 0;
403 const resolver = yield* makeResolverWithFileSystem(
404 FileSystem.FileSystem.of({
405 ...fileSystem,
406 stat: (filePath) =>
407 Effect.suspend(() => {
408 inFlight += 1;
409 peakInFlight = Math.max(peakInFlight, inFlight);
410 return Effect.yieldNow.pipe(Effect.andThen(fileSystem.stat(filePath)));
411 }).pipe(Effect.ensuring(Effect.sync(() => (inFlight -= 1)))),
412 }),
413 );
414
415 expect(yield* resolver.resolvePath(cwd)).toBeNull();
416 expect(peakInFlight).toBeGreaterThan(1);
417 }),
418 );
419
420 it.effect("returns a found favicon without waiting for lower-ranked probes", () =>
421 Effect.gen(function* () {
422 const fileSystem = yield* FileSystem.FileSystem;
423 const path = yield* Path.Path;
424 const cwd = yield* makeTempDir;
425 yield* writeTextFile(cwd, "favicon.svg", "<svg>root</svg>");
426 const stuckPath = path.join(cwd, "favicon.ico");
427 const resolver = yield* makeResolverWithFileSystem(
428 FileSystem.FileSystem.of({
429 ...fileSystem,
430 stat: (filePath) => (filePath === stuckPath ? Effect.never : fileSystem.stat(filePath)),
431 }),
432 );
433
434 expect(yield* resolver.resolvePath(cwd)).toBe(path.join(cwd, "favicon.svg"));
435 }),
436 );
437
438 it.effect("ignores a failing candidate that ranks below a found favicon", () =>
439 Effect.gen(function* () {
440 const fileSystem = yield* FileSystem.FileSystem;
441 const path = yield* Path.Path;
442 const cwd = yield* makeTempDir;
443 yield* writeTextFile(cwd, "favicon.svg", "<svg>root</svg>");
444 const failingPath = path.join(cwd, "favicon.ico");
445 const resolver = yield* makeResolverWithFileSystem(
446 FileSystem.FileSystem.of({
447 ...fileSystem,
448 stat: (filePath) =>
449 filePath === failingPath
450 ? Effect.fail(
451 PlatformError.systemError({
452 _tag: "PermissionDenied",
453 module: "FileSystem",
454 method: "stat",
455 pathOrDescriptor: failingPath,
456 }),
457 )
458 : fileSystem.stat(filePath),
459 }),
460 );
461
462 expect(yield* resolver.resolvePath(cwd)).toBe(path.join(cwd, "favicon.svg"));
463 }),
464 );
465
466 it.effect("preserves icon source read failures", () =>
467 Effect.gen(function* () {
468 const fileSystem = yield* FileSystem.FileSystem;
469 const path = yield* Path.Path;
470 const cwd = yield* makeTempDir;
471 const sourcePath = path.join(cwd, "index.html");
472 yield* writeTextFile(cwd, "index.html", '<link rel="icon" href="/favicon.svg">');
473 const cause = PlatformError.systemError({
474 _tag: "PermissionDenied",
475 module: "FileSystem",
476 method: "readFileString",
477 pathOrDescriptor: sourcePath,
478 });
479 const resolver = yield* makeResolverWithFileSystem(
480 FileSystem.FileSystem.of({
481 ...fileSystem,
482 readFileString: (filePath, options) =>
483 filePath === sourcePath
484 ? Effect.fail(cause)
485 : fileSystem.readFileString(filePath, options),
486 }),
487 );
488
489 const error = yield* resolver.resolvePath(cwd).pipe(Effect.flip);
490
491 expect(error).toMatchObject({
492 _tag: "ProjectFaviconResolutionError",
493 operation: "read-source",
494 workspaceRoot: cwd,
495 relativePath: "index.html",
496 absolutePath: sourcePath,
497 });
498 expect(error.cause).toBe(cause);
499 }),
500 );
501
502 it.effect("skips icon metadata paths outside the workspace", () =>
503 Effect.gen(function* () {
504 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
505 const cwd = yield* makeTempDir;
506 yield* writeTextFile(cwd, "index.html", '<link rel="icon" href="../../secret.svg">');
507
508 const resolved = yield* resolver.resolvePath(cwd);
509
510 expect(resolved).toBeNull();
511 }),
512 );
513
514 it.effect("continues to later sources after an outside-root icon href", () =>
515 Effect.gen(function* () {
516 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
517 const cwd = yield* makeTempDir;
518 yield* writeTextFile(cwd, "index.html", '<link rel="icon" href="../../secret.svg">');
519 yield* writeTextFile(cwd, "public/index.html", '<link rel="icon" href="/brand/logo.svg">');
520 yield* writeTextFile(cwd, "public/brand/logo.svg", "<svg>brand</svg>");
521
522 const resolved = yield* resolver.resolvePath(cwd);
523
524 expect(resolved).not.toBeNull();
525 expect(resolved).toBe((yield* Path.Path).join(cwd, "public", "brand", "logo.svg"));
526 }),
527 );
528 });
529});