Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { ThreadId } from "@t3tools/contracts";
3import { PROJECT_FAVICON_FALLBACK_MARKER } from "@t3tools/shared/projectFavicon";
4import { describe, expect, it } from "@effect/vitest";
5import * as Effect from "effect/Effect";
6import * as FileSystem from "effect/FileSystem";
7import * as Layer from "effect/Layer";
8import * as Path from "effect/Path";
9import * as PlatformError from "effect/PlatformError";
10
11import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
12import * as ServerConfig from "../config.ts";
13import * as ProjectFaviconResolver from "../project/ProjectFaviconResolver.ts";
14import * as T3ProjectFileLoader from "../project/T3ProjectFileLoader.ts";
15import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
16import { ASSET_ROUTE_PREFIX, issueAssetUrl, resolveAsset } from "./AssetAccess.ts";
17
18const configLayer = ServerConfig.ServerConfig.layerTest(process.cwd(), {
19 prefix: "t3-asset-access-test-",
20});
21const testLayer = Layer.mergeAll(
22 configLayer,
23 WorkspacePaths.layer,
24 ProjectFaviconResolver.layer.pipe(
25 Layer.provide(WorkspacePaths.layer),
26 Layer.provide(T3ProjectFileLoader.layer),
27 ),
28 ServerSecretStore.layer.pipe(Layer.provide(configLayer)),
29).pipe(Layer.provideMerge(NodeServices.layer));
30
31describe("AssetAccess", () => {
32 it.effect("issues workspace URLs that resolve the entry file and sibling assets", () =>
33 Effect.gen(function* () {
34 const fileSystem = yield* FileSystem.FileSystem;
35 const path = yield* Path.Path;
36 const root = yield* fileSystem.makeTempDirectoryScoped({
37 prefix: "t3-asset-workspace-",
38 });
39 const htmlPath = path.join(root, "report.html");
40 const cssPath = path.join(root, "report.css");
41 yield* fileSystem.writeFileString(htmlPath, '<link rel="stylesheet" href="report.css">');
42 yield* fileSystem.writeFileString(cssPath, "body { color: red; }");
43 yield* fileSystem.writeFileString(path.join(root, ".env"), "SECRET=value");
44 const canonicalHtmlPath = yield* fileSystem.realPath(htmlPath);
45 const canonicalCssPath = yield* fileSystem.realPath(cssPath);
46
47 const result = yield* issueAssetUrl({
48 resource: {
49 _tag: "workspace-file",
50 threadId: ThreadId.make("thread-1"),
51 path: htmlPath,
52 },
53 workspaceRoot: root,
54 });
55 const suffix = result.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length);
56 const separatorIndex = suffix.indexOf("/");
57 const token = suffix.slice(0, separatorIndex);
58
59 expect(yield* resolveAsset(token, "report.html")).toEqual({
60 kind: "file",
61 path: canonicalHtmlPath,
62 });
63 expect(yield* resolveAsset(token, "report.css")).toEqual({
64 kind: "file",
65 path: canonicalCssPath,
66 });
67 expect(yield* resolveAsset(token, "../secret.txt")).toBeNull();
68 expect(yield* resolveAsset(token, ".env")).toBeNull();
69 expect(yield* resolveAsset(`${token}tampered`, "report.html")).toBeNull();
70 }).pipe(Effect.provide(testLayer)),
71 );
72
73 it.effect("rejects workspace files outside the authorized root", () =>
74 Effect.gen(function* () {
75 const fileSystem = yield* FileSystem.FileSystem;
76 const path = yield* Path.Path;
77 const root = yield* fileSystem.makeTempDirectoryScoped({
78 prefix: "t3-asset-root-",
79 });
80 const outside = yield* fileSystem.makeTempDirectoryScoped({
81 prefix: "t3-asset-outside-",
82 });
83 const htmlPath = path.join(outside, "report.html");
84 yield* fileSystem.writeFileString(htmlPath, "<p>outside</p>");
85
86 const error = yield* issueAssetUrl({
87 resource: {
88 _tag: "workspace-file",
89 threadId: ThreadId.make("thread-1"),
90 path: htmlPath,
91 },
92 workspaceRoot: root,
93 }).pipe(Effect.flip);
94 expect(error.message).toBe("Workspace file path must be relative to the project root.");
95 expect(error).toMatchObject({
96 _tag: "AssetWorkspacePathValidationError",
97 resource: {
98 _tag: "workspace-file",
99 threadId: "thread-1",
100 path: htmlPath,
101 },
102 });
103 expect(error.cause).toBeInstanceOf(WorkspacePaths.WorkspacePathOutsideRootError);
104 }).pipe(Effect.provide(testLayer)),
105 );
106
107 it.effect("preserves non-missing canonical path failures when issuing asset URLs", () =>
108 Effect.gen(function* () {
109 const fileSystem = yield* FileSystem.FileSystem;
110 const path = yield* Path.Path;
111 const root = yield* fileSystem.makeTempDirectoryScoped({
112 prefix: "t3-asset-permission-root-",
113 });
114 const htmlPath = path.join(root, "report.html");
115 yield* fileSystem.writeFileString(htmlPath, "<p>report</p>");
116 const cause = PlatformError.systemError({
117 _tag: "PermissionDenied",
118 module: "FileSystem",
119 method: "realPath",
120 pathOrDescriptor: htmlPath,
121 });
122 const failingFileSystem = FileSystem.FileSystem.of({
123 ...fileSystem,
124 realPath: () => Effect.fail(cause),
125 });
126
127 const error = yield* issueAssetUrl({
128 resource: {
129 _tag: "workspace-file",
130 threadId: ThreadId.make("thread-1"),
131 path: htmlPath,
132 },
133 workspaceRoot: root,
134 }).pipe(Effect.provideService(FileSystem.FileSystem, failingFileSystem), Effect.flip);
135
136 expect(error.message).toBe("Failed to inspect the workspace asset.");
137 expect(error).toMatchObject({
138 _tag: "AssetWorkspaceAssetInspectionError",
139 resource: {
140 _tag: "workspace-file",
141 threadId: "thread-1",
142 path: htmlPath,
143 },
144 });
145 expect(error.cause).toBe(cause);
146 }).pipe(Effect.provide(testLayer)),
147 );
148
149 it.effect("issues exact workspace URLs for image previews", () =>
150 Effect.gen(function* () {
151 const fileSystem = yield* FileSystem.FileSystem;
152 const path = yield* Path.Path;
153 const root = yield* fileSystem.makeTempDirectoryScoped({
154 prefix: "t3-asset-image-workspace-",
155 });
156 const assetsDirectory = path.join(root, "assets");
157 const imagePath = path.join(assetsDirectory, "icon.png");
158 const siblingPath = path.join(assetsDirectory, "other.png");
159 yield* fileSystem.makeDirectory(assetsDirectory, { recursive: true });
160 yield* fileSystem.writeFile(imagePath, new Uint8Array([137, 80, 78, 71]));
161 yield* fileSystem.writeFile(siblingPath, new Uint8Array([137, 80, 78, 71]));
162 const canonicalImagePath = yield* fileSystem.realPath(imagePath);
163
164 const result = yield* issueAssetUrl({
165 resource: {
166 _tag: "workspace-file",
167 threadId: ThreadId.make("thread-1"),
168 path: imagePath,
169 },
170 workspaceRoot: root,
171 });
172 const suffix = result.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length);
173 const separatorIndex = suffix.indexOf("/");
174 const token = suffix.slice(0, separatorIndex);
175
176 expect(yield* resolveAsset(token, "icon.png")).toEqual({
177 kind: "file",
178 path: canonicalImagePath,
179 });
180 expect(yield* resolveAsset(token, "other.png")).toBeNull();
181 expect(yield* resolveAsset(token, "../icon.png")).toBeNull();
182 }).pipe(Effect.provide(testLayer)),
183 );
184
185 it.effect("issues exact attachment capabilities by attachment id", () =>
186 Effect.gen(function* () {
187 const config = yield* ServerConfig.ServerConfig;
188 const fileSystem = yield* FileSystem.FileSystem;
189 const path = yield* Path.Path;
190 const attachmentId = "thread-1-00000000-0000-4000-8000-000000000001";
191 const attachmentPath = path.join(config.attachmentsDir, `${attachmentId}.png`);
192 yield* fileSystem.makeDirectory(config.attachmentsDir, { recursive: true });
193 yield* fileSystem.writeFile(attachmentPath, new Uint8Array([1, 2, 3]));
194
195 const result = yield* issueAssetUrl({
196 resource: { _tag: "attachment", attachmentId },
197 });
198 const suffix = result.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length);
199 const separatorIndex = suffix.indexOf("/");
200 const token = suffix.slice(0, separatorIndex);
201
202 expect(yield* resolveAsset(token, "ignored.png")).toEqual({
203 kind: "file",
204 path: attachmentPath,
205 });
206 }).pipe(Effect.provide(testLayer)),
207 );
208
209 it.effect("issues project favicon capabilities with a signed fallback", () =>
210 Effect.gen(function* () {
211 const fileSystem = yield* FileSystem.FileSystem;
212 const path = yield* Path.Path;
213 const root = yield* fileSystem.makeTempDirectoryScoped({
214 prefix: "t3-asset-favicon-",
215 });
216 const faviconPath = path.join(root, "favicon.svg");
217 yield* fileSystem.writeFileString(faviconPath, "<svg />");
218 const canonicalFaviconPath = yield* fileSystem.realPath(faviconPath);
219
220 const faviconResult = yield* issueAssetUrl({
221 resource: { _tag: "project-favicon", cwd: root },
222 });
223 const faviconSuffix = faviconResult.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length);
224 const faviconSeparatorIndex = faviconSuffix.indexOf("/");
225 expect(
226 yield* resolveAsset(
227 faviconSuffix.slice(0, faviconSeparatorIndex),
228 faviconSuffix.slice(faviconSeparatorIndex + 1),
229 ),
230 ).toEqual({ kind: "file", path: canonicalFaviconPath });
231
232 yield* fileSystem.remove(faviconPath);
233 const fallbackResult = yield* issueAssetUrl({
234 resource: { _tag: "project-favicon", cwd: root },
235 });
236 expect(fallbackResult.relativeUrl.endsWith(`/${PROJECT_FAVICON_FALLBACK_MARKER}`)).toBe(true);
237 const fallbackSuffix = fallbackResult.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length);
238 const fallbackSeparatorIndex = fallbackSuffix.indexOf("/");
239 expect(
240 yield* resolveAsset(
241 fallbackSuffix.slice(0, fallbackSeparatorIndex),
242 fallbackSuffix.slice(fallbackSeparatorIndex + 1),
243 ),
244 ).toBeNull();
245 }).pipe(Effect.provide(testLayer)),
246 );
247
248 it.effect("preserves structured project favicon resolution causes", () =>
249 Effect.gen(function* () {
250 const fileSystem = yield* FileSystem.FileSystem;
251 const root = yield* fileSystem.makeTempDirectoryScoped({
252 prefix: "t3-asset-favicon-error-",
253 });
254 const platformCause = PlatformError.systemError({
255 _tag: "PermissionDenied",
256 module: "FileSystem",
257 method: "stat",
258 });
259 const resolutionCause = new ProjectFaviconResolver.ProjectFaviconResolutionError({
260 operation: "stat-candidate",
261 workspaceRoot: root,
262 relativePath: "favicon.svg",
263 cause: platformCause,
264 });
265 const resolver = ProjectFaviconResolver.ProjectFaviconResolver.of({
266 resolvePath: () => Effect.fail(resolutionCause),
267 });
268
269 const error = yield* issueAssetUrl({
270 resource: { _tag: "project-favicon", cwd: root },
271 }).pipe(
272 Effect.provideService(ProjectFaviconResolver.ProjectFaviconResolver, resolver),
273 Effect.flip,
274 );
275
276 expect(error.message).toBe("Failed to resolve project favicon.");
277 expect(error._tag).toBe("AssetProjectFaviconResolutionError");
278 expect(error.cause).toBe(resolutionCause);
279 }).pipe(Effect.provide(testLayer)),
280 );
281});