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 * as PlatformError from "effect/PlatformError";
8
9import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
10import * as ProjectFaviconResolver from "./ProjectFaviconResolver.ts";
11
12const TestLayer = Layer.empty.pipe(
13 Layer.provideMerge(ProjectFaviconResolver.layer.pipe(Layer.provide(WorkspacePaths.layer))),
14 Layer.provideMerge(NodeServices.layer),
15);
16
17const makeTempDir = Effect.gen(function* () {
18 const fileSystem = yield* FileSystem.FileSystem;
19 return yield* fileSystem.makeTempDirectoryScoped({
20 prefix: "t3code-project-favicon-",
21 });
22});
23
24const writeTextFile = Effect.fn("writeTextFile")(function* (
25 cwd: string,
26 relativePath: string,
27 contents: string,
28) {
29 const fileSystem = yield* FileSystem.FileSystem;
30 const path = yield* Path.Path;
31 const absolutePath = path.join(cwd, relativePath);
32 yield* fileSystem
33 .makeDirectory(path.dirname(absolutePath), { recursive: true })
34 .pipe(Effect.orDie);
35 yield* fileSystem.writeFileString(absolutePath, contents).pipe(Effect.orDie);
36});
37
38const makeResolverWithFileSystem = (fileSystem: FileSystem.FileSystem) =>
39 ProjectFaviconResolver.make.pipe(
40 Effect.provide(WorkspacePaths.layer),
41 Effect.provideService(FileSystem.FileSystem, fileSystem),
42 );
43
44it.layer(TestLayer)("ProjectFaviconResolverLive", (it) => {
45 describe("resolvePath", () => {
46 it.effect("prefers well-known favicon files", () =>
47 Effect.gen(function* () {
48 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
49 const cwd = yield* makeTempDir;
50 yield* writeTextFile(cwd, "favicon.svg", "<svg>favicon</svg>");
51
52 const resolved = yield* resolver.resolvePath(cwd);
53
54 expect(resolved).not.toBeNull();
55 expect(resolved).toContain("favicon.svg");
56 }),
57 );
58
59 it.effect("resolves icon hrefs from project source files", () =>
60 Effect.gen(function* () {
61 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
62 const cwd = yield* makeTempDir;
63 yield* writeTextFile(cwd, "index.html", '<link rel="icon" href="/brand/logo.svg">');
64 yield* writeTextFile(cwd, "public/brand/logo.svg", "<svg>brand</svg>");
65
66 const resolved = yield* resolver.resolvePath(cwd);
67
68 expect(resolved).not.toBeNull();
69 expect(resolved).toContain("public/brand/logo.svg");
70 }),
71 );
72
73 it.effect("returns null when no icon is present", () =>
74 Effect.gen(function* () {
75 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
76 const cwd = yield* makeTempDir;
77
78 const resolved = yield* resolver.resolvePath(cwd);
79
80 expect(resolved).toBeNull();
81 }),
82 );
83
84 it.effect("preserves workspace normalization context", () =>
85 Effect.gen(function* () {
86 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
87 const cwd = yield* makeTempDir;
88 const missingCwd = `${cwd}/missing`;
89
90 const error = yield* resolver.resolvePath(missingCwd).pipe(Effect.flip);
91
92 expect(error).toMatchObject({
93 _tag: "ProjectFaviconResolutionError",
94 operation: "normalize-workspace",
95 workspaceRoot: missingCwd,
96 });
97 expect(error.cause).toBeInstanceOf(WorkspacePaths.WorkspaceRootNotExistsError);
98 }),
99 );
100
101 it.effect("preserves non-missing candidate stat failures", () =>
102 Effect.gen(function* () {
103 const fileSystem = yield* FileSystem.FileSystem;
104 const path = yield* Path.Path;
105 const cwd = yield* makeTempDir;
106 const faviconPath = path.join(cwd, "favicon.svg");
107 const cause = PlatformError.systemError({
108 _tag: "PermissionDenied",
109 module: "FileSystem",
110 method: "stat",
111 pathOrDescriptor: faviconPath,
112 });
113 const resolver = yield* makeResolverWithFileSystem(
114 FileSystem.FileSystem.of({
115 ...fileSystem,
116 stat: (filePath) =>
117 filePath === faviconPath ? Effect.fail(cause) : fileSystem.stat(filePath),
118 }),
119 );
120
121 const error = yield* resolver.resolvePath(cwd).pipe(Effect.flip);
122
123 expect(error).toMatchObject({
124 _tag: "ProjectFaviconResolutionError",
125 operation: "stat-candidate",
126 workspaceRoot: cwd,
127 relativePath: "favicon.svg",
128 absolutePath: faviconPath,
129 });
130 expect(error.cause).toBe(cause);
131 }),
132 );
133
134 it.effect("preserves icon source read failures", () =>
135 Effect.gen(function* () {
136 const fileSystem = yield* FileSystem.FileSystem;
137 const path = yield* Path.Path;
138 const cwd = yield* makeTempDir;
139 const sourcePath = path.join(cwd, "index.html");
140 yield* writeTextFile(cwd, "index.html", '<link rel="icon" href="/favicon.svg">');
141 const cause = PlatformError.systemError({
142 _tag: "PermissionDenied",
143 module: "FileSystem",
144 method: "readFileString",
145 pathOrDescriptor: sourcePath,
146 });
147 const resolver = yield* makeResolverWithFileSystem(
148 FileSystem.FileSystem.of({
149 ...fileSystem,
150 readFileString: (filePath, options) =>
151 filePath === sourcePath
152 ? Effect.fail(cause)
153 : fileSystem.readFileString(filePath, options),
154 }),
155 );
156
157 const error = yield* resolver.resolvePath(cwd).pipe(Effect.flip);
158
159 expect(error).toMatchObject({
160 _tag: "ProjectFaviconResolutionError",
161 operation: "read-source",
162 workspaceRoot: cwd,
163 relativePath: "index.html",
164 absolutePath: sourcePath,
165 });
166 expect(error.cause).toBe(cause);
167 }),
168 );
169
170 it.effect("skips icon metadata paths outside the workspace", () =>
171 Effect.gen(function* () {
172 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
173 const cwd = yield* makeTempDir;
174 yield* writeTextFile(cwd, "index.html", '<link rel="icon" href="../../secret.svg">');
175
176 const resolved = yield* resolver.resolvePath(cwd);
177
178 expect(resolved).toBeNull();
179 }),
180 );
181
182 it.effect("continues to later sources after an outside-root icon href", () =>
183 Effect.gen(function* () {
184 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
185 const cwd = yield* makeTempDir;
186 yield* writeTextFile(cwd, "index.html", '<link rel="icon" href="../../secret.svg">');
187 yield* writeTextFile(cwd, "public/index.html", '<link rel="icon" href="/brand/logo.svg">');
188 yield* writeTextFile(cwd, "public/brand/logo.svg", "<svg>brand</svg>");
189
190 const resolved = yield* resolver.resolvePath(cwd);
191
192 expect(resolved).not.toBeNull();
193 expect(resolved).toContain("public/brand/logo.svg");
194 }),
195 );
196 });
197});