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

ProjectFaviconResolver.test.ts

260 lines9.4 KB

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";
11import * as T3ProjectFileLoader from "./T3ProjectFileLoader.ts";
12​
13const TestLayer = 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([WorkspacePaths.layer, T3ProjectFileLoader.layer]),
47 Effect.provideService(FileSystem.FileSystem, fileSystem),
48 );
49​
50it.layer(TestLayer)("ProjectFaviconResolverLive", (it) => {
51 describe("resolvePath", () => {
52 it.effect("prefers well-known favicon files", () =>
53 Effect.gen(function* () {
54 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
55 const cwd = yield* makeTempDir;
56 yield* writeTextFile(cwd, "favicon.svg", "<svg>favicon</svg>");
57​
58 const resolved = yield* resolver.resolvePath(cwd);
59​
60 expect(resolved).not.toBeNull();
61 expect(resolved).toContain("favicon.svg");
62 }),
63 );
64​
65 it.effect("prefers a t3.json iconPath over well-known files", () =>
66 Effect.gen(function* () {
67 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
68 const cwd = yield* makeTempDir;
69 yield* writeTextFile(cwd, "t3.json", '{ "iconPath": "brand/mark.svg" }');
70 yield* writeTextFile(cwd, "brand/mark.svg", "<svg>mark</svg>");
71 yield* writeTextFile(cwd, "favicon.svg", "<svg>favicon</svg>");
72​
73 const resolved = yield* resolver.resolvePath(cwd);
74​
75 expect(resolved).not.toBeNull();
76 expect(resolved).toContain("brand/mark.svg");
77 }),
78 );
79​
80 it.effect("falls back to well-known files when the t3.json iconPath does not exist", () =>
81 Effect.gen(function* () {
82 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
83 const cwd = yield* makeTempDir;
84 yield* writeTextFile(cwd, "t3.json", '{ "iconPath": "brand/missing.svg" }');
85 yield* writeTextFile(cwd, "favicon.svg", "<svg>favicon</svg>");
86​
87 const resolved = yield* resolver.resolvePath(cwd);
88​
89 expect(resolved).not.toBeNull();
90 expect(resolved).toContain("favicon.svg");
91 }),
92 );
93​
94 it.effect("ignores invalid t3.json files", () =>
95 Effect.gen(function* () {
96 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
97 const cwd = yield* makeTempDir;
98 yield* writeTextFile(cwd, "t3.json", "{ not json");
99 yield* writeTextFile(cwd, "favicon.svg", "<svg>favicon</svg>");
100​
101 const resolved = yield* resolver.resolvePath(cwd);
102​
103 expect(resolved).not.toBeNull();
104 expect(resolved).toContain("favicon.svg");
105 }),
106 );
107​
108 it.effect("does not resolve a t3.json iconPath outside the workspace root", () =>
109 Effect.gen(function* () {
110 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
111 const parent = yield* makeTempDir;
112 const cwd = `${parent}/app`;
113 yield* writeTextFile(parent, "secret.svg", "<svg>secret</svg>");
114 yield* writeTextFile(cwd, "t3.json", '{ "iconPath": "../secret.svg" }');
115​
116 const resolved = yield* resolver.resolvePath(cwd);
117​
118 expect(resolved).toBeNull();
119 }),
120 );
121​
122 it.effect("resolves icon hrefs from project source files", () =>
123 Effect.gen(function* () {
124 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
125 const cwd = yield* makeTempDir;
126 yield* writeTextFile(cwd, "index.html", '<link rel="icon" href="/brand/logo.svg">');
127 yield* writeTextFile(cwd, "public/brand/logo.svg", "<svg>brand</svg>");
128​
129 const resolved = yield* resolver.resolvePath(cwd);
130​
131 expect(resolved).not.toBeNull();
132 expect(resolved).toContain("public/brand/logo.svg");
133 }),
134 );
135​
136 it.effect("returns null when no icon is present", () =>
137 Effect.gen(function* () {
138 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
139 const cwd = yield* makeTempDir;
140​
141 const resolved = yield* resolver.resolvePath(cwd);
142​
143 expect(resolved).toBeNull();
144 }),
145 );
146​
147 it.effect("preserves workspace normalization context", () =>
148 Effect.gen(function* () {
149 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
150 const cwd = yield* makeTempDir;
151 const missingCwd = `${cwd}/missing`;
152​
153 const error = yield* resolver.resolvePath(missingCwd).pipe(Effect.flip);
154​
155 expect(error).toMatchObject({
156 _tag: "ProjectFaviconResolutionError",
157 operation: "normalize-workspace",
158 workspaceRoot: missingCwd,
159 });
160 expect(error.cause).toBeInstanceOf(WorkspacePaths.WorkspaceRootNotExistsError);
161 }),
162 );
163​
164 it.effect("preserves non-missing candidate stat failures", () =>
165 Effect.gen(function* () {
166 const fileSystem = yield* FileSystem.FileSystem;
167 const path = yield* Path.Path;
168 const cwd = yield* makeTempDir;
169 const faviconPath = path.join(cwd, "favicon.svg");
170 const cause = PlatformError.systemError({
171 _tag: "PermissionDenied",
172 module: "FileSystem",
173 method: "stat",
174 pathOrDescriptor: faviconPath,
175 });
176 const resolver = yield* makeResolverWithFileSystem(
177 FileSystem.FileSystem.of({
178 ...fileSystem,
179 stat: (filePath) =>
180 filePath === faviconPath ? Effect.fail(cause) : fileSystem.stat(filePath),
181 }),
182 );
183​
184 const error = yield* resolver.resolvePath(cwd).pipe(Effect.flip);
185​
186 expect(error).toMatchObject({
187 _tag: "ProjectFaviconResolutionError",
188 operation: "stat-candidate",
189 workspaceRoot: cwd,
190 relativePath: "favicon.svg",
191 absolutePath: faviconPath,
192 });
193 expect(error.cause).toBe(cause);
194 }),
195 );
196​
197 it.effect("preserves icon source read failures", () =>
198 Effect.gen(function* () {
199 const fileSystem = yield* FileSystem.FileSystem;
200 const path = yield* Path.Path;
201 const cwd = yield* makeTempDir;
202 const sourcePath = path.join(cwd, "index.html");
203 yield* writeTextFile(cwd, "index.html", '<link rel="icon" href="/favicon.svg">');
204 const cause = PlatformError.systemError({
205 _tag: "PermissionDenied",
206 module: "FileSystem",
207 method: "readFileString",
208 pathOrDescriptor: sourcePath,
209 });
210 const resolver = yield* makeResolverWithFileSystem(
211 FileSystem.FileSystem.of({
212 ...fileSystem,
213 readFileString: (filePath, options) =>
214 filePath === sourcePath
215 ? Effect.fail(cause)
216 : fileSystem.readFileString(filePath, options),
217 }),
218 );
219​
220 const error = yield* resolver.resolvePath(cwd).pipe(Effect.flip);
221​
222 expect(error).toMatchObject({
223 _tag: "ProjectFaviconResolutionError",
224 operation: "read-source",
225 workspaceRoot: cwd,
226 relativePath: "index.html",
227 absolutePath: sourcePath,
228 });
229 expect(error.cause).toBe(cause);
230 }),
231 );
232​
233 it.effect("skips icon metadata paths outside the workspace", () =>
234 Effect.gen(function* () {
235 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
236 const cwd = yield* makeTempDir;
237 yield* writeTextFile(cwd, "index.html", '<link rel="icon" href="../../secret.svg">');
238​
239 const resolved = yield* resolver.resolvePath(cwd);
240​
241 expect(resolved).toBeNull();
242 }),
243 );
244​
245 it.effect("continues to later sources after an outside-root icon href", () =>
246 Effect.gen(function* () {
247 const resolver = yield* ProjectFaviconResolver.ProjectFaviconResolver;
248 const cwd = yield* makeTempDir;
249 yield* writeTextFile(cwd, "index.html", '<link rel="icon" href="../../secret.svg">');
250 yield* writeTextFile(cwd, "public/index.html", '<link rel="icon" href="/brand/logo.svg">');
251 yield* writeTextFile(cwd, "public/brand/logo.svg", "<svg>brand</svg>");
252​
253 const resolved = yield* resolver.resolvePath(cwd);
254​
255 expect(resolved).not.toBeNull();
256 expect(resolved).toContain("public/brand/logo.svg");
257 }),
258 );
259 });
260});