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 here" 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 encodedCwd = yield* makeTempDir;
282 yield* writeTextFile(encodedCwd, "index.html", '<link rel="icon" href="/team%20mark.svg">');
283 yield* writeTextFile(encodedCwd, "public/team mark.svg", "<svg>team</svg>");
284 expect(yield* resolver.resolvePath(encodedCwd)).toBe(
285 (yield* Path.Path).join(encodedCwd, "public", "team mark.svg"),
286 );
287
288 const malformedCwd = yield* makeTempDir;
289 yield* writeTextFile(malformedCwd, "index.html", '<link rel="icon" href="/100%.svg">');
290 yield* writeTextFile(malformedCwd, "public/100%.svg", "<svg>100</svg>");
291 expect(yield* resolver.resolvePath(malformedCwd)).toBe(
292 (yield* Path.Path).join(malformedCwd, "public", "100%.svg"),
293 );
294 }),
295 );
296
297 it.effect("skips source metadata beyond the favicon scan limit", () =>
298 Effect.gen(function* () {
299 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
300 const cwd = yield* makeTempDir;
301 yield* writeTextFile(
302 cwd,
303 "index.html",
304 `<!-- ${" ".repeat(128 * 1024)} --><link rel="icon" href="/brand/logo.svg">`,
305 );
306 yield* writeTextFile(cwd, "public/brand/logo.svg", "<svg>brand</svg>");
307
308 expect(yield* resolver.resolvePath(cwd)).toBeNull();
309 }),
310 );
311
312 it.effect("resolves icon hrefs from object-literal route metadata", () =>
313 Effect.gen(function* () {
314 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
315 const cwd = yield* makeTempDir;
316 yield* writeTextFile(
317 cwd,
318 "src/routes/__root.tsx",
319 `export const Route = createRootRoute({
320 head: () => ({
321 links: [
322 { rel: "stylesheet", href: "/app.css" },
323 { rel: "icon", href: "https://cdn.example.com/icon.svg" },
324 { rel: "icon shortcut", href: "/brand/logo.svg#mark" },
325 ],
326 }),
327});`,
328 );
329 yield* writeTextFile(cwd, "public/brand/logo.svg", "<svg>brand</svg>");
330
331 const resolved = yield* resolver.resolvePath(cwd);
332
333 expect(resolved).not.toBeNull();
334 expect(resolved).toBe((yield* Path.Path).join(cwd, "public", "brand", "logo.svg"));
335 }),
336 );
337
338 // regression: #5538 favicon resolution pinned the event loop
339 it.effect("skips icon metadata without an href and keeps scanning", () =>
340 Effect.gen(function* () {
341 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
342 const cwd = yield* makeTempDir;
343 yield* writeTextFile(
344 cwd,
345 "src/root.tsx",
346 `const links = [{ rel: "icon" }, { rel: "icon", href: "/brand/logo.svg" }];`,
347 );
348 yield* writeTextFile(cwd, "public/brand/logo.svg", "<svg>brand</svg>");
349
350 const resolved = yield* resolver.resolvePath(cwd);
351
352 expect(resolved).not.toBeNull();
353 expect(resolved).toBe((yield* Path.Path).join(cwd, "public", "brand", "logo.svg"));
354 }),
355 );
356
357 it.effect("returns a found favicon without waiting for lower-ranked probes", () =>
358 Effect.gen(function* () {
359 const fileSystem = yield* FileSystem.FileSystem;
360 const path = yield* Path.Path;
361 const cwd = yield* makeTempDir;
362 yield* writeTextFile(cwd, "favicon.svg", "<svg>root</svg>");
363 const stuckPath = path.join(cwd, "favicon.ico");
364 const resolver = yield* makeResolverWithFileSystem(
365 FileSystem.FileSystem.of({
366 ...fileSystem,
367 stat: (filePath) => (filePath === stuckPath ? Effect.never : fileSystem.stat(filePath)),
368 }),
369 );
370
371 expect(yield* resolver.resolvePath(cwd)).toBe(path.join(cwd, "favicon.svg"));
372 }),
373 );
374
375 it.effect("skips icon metadata paths outside the workspace", () =>
376 Effect.gen(function* () {
377 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
378 const path = yield* Path.Path;
379 const root = yield* makeTempDir;
380 const cwd = path.join(root, "parent", "workspace");
381 yield* writeTextFile(root, "outside.svg", "<svg>two-levels-up</svg>");
382 yield* writeTextFile(root, "parent/outside.svg", "<svg>one-level-up</svg>");
383 yield* writeTextFile(
384 cwd,
385 "index.html",
386 '<link rel="icon" href="../../outside.svg"><link rel="icon" href="/%2e%2e%2f%2e%2e%2foutside.svg">',
387 );
388
389 const resolved = yield* resolver.resolvePath(cwd);
390
391 expect(resolved).toBeNull();
392 }),
393 );
394 });
395});