Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProjectFaviconResolver.test.ts

545 lines20.2 KB

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