Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 304 lines 10.1 KB
Raw
1 import * as BunFileSystem from '@effect/platform-bun/BunFileSystem';
2 import * as BunPath from '@effect/platform-bun/BunPath';
3 import { describe, expect, it } from '@effect/vitest';
4 import { Cause, Effect, FileSystem, Layer, Path, Scope } from 'effect';
5 import { type RawSourceMap, SourceMapConsumer } from 'source-map-js';
6
7 import { captureErrors } from 'src/effect-errors/capture-errors';
8 import { getSourcesFromMapFile } from 'src/effect-errors/sourcemaps/get-sources-from-map-file';
9
10 const TestPlatform = Layer.mergeAll(BunFileSystem.layer, BunPath.layer);
11 const locationFor = (filePath: string) => ({ filePath, line: 1, column: 0 });
12
13 const makeSourceMapFixture = (
14 mapContents: string,
15 options: { readonly directory?: string; readonly sourceContents?: string } = {}
16 ) =>
17 Effect.gen(function* () {
18 const fs = yield* FileSystem.FileSystem;
19 const path = yield* Path.Path;
20 const directory = yield* fs.makeTempDirectoryScoped({
21 directory: options.directory,
22 prefix: 'composio-sourcemap-',
23 });
24 const generatedFile = path.join(directory, 'app.js');
25
26 yield* fs.writeFileString(`${generatedFile}.map`, mapContents);
27 if (options.sourceContents !== undefined) {
28 yield* fs.writeFileString(path.join(directory, 'app.ts'), options.sourceContents);
29 }
30
31 return { directory, generatedFile };
32 });
33
34 const run = <A, E>(effect: Effect.Effect<A, E, FileSystem.FileSystem | Path.Path | Scope.Scope>) =>
35 effect.pipe(Effect.provide(TestPlatform));
36
37 describe('getSourcesFromMapFile', () => {
38 it.effect('falls back to the full raw location when source-map JSON is malformed', () =>
39 run(
40 Effect.gen(function* () {
41 const { generatedFile } = yield* makeSourceMapFixture('{ this is not valid JSON');
42 const location = locationFor(generatedFile);
43
44 expect(yield* getSourcesFromMapFile('app.js', location)).toStrictEqual({
45 _tag: 'location',
46 name: 'app.js',
47 ...location,
48 });
49 })
50 )
51 );
52
53 it.effect('falls back when valid JSON is not a usable source map', () =>
54 run(
55 Effect.gen(function* () {
56 const invalidSourceMap = JSON.stringify({
57 version: 2,
58 sources: [],
59 names: [],
60 mappings: '',
61 });
62 const { generatedFile } = yield* makeSourceMapFixture(invalidSourceMap);
63 const location = locationFor(generatedFile);
64
65 expect(yield* getSourcesFromMapFile('app.js', location)).toStrictEqual({
66 _tag: 'location',
67 name: 'app.js',
68 ...location,
69 });
70 })
71 )
72 );
73
74 it.effect('falls back when source-map resolution rejects schema-valid data', () =>
75 run(
76 Effect.gen(function* () {
77 const invalidSourceMap = {
78 version: '3',
79 sources: ['app.ts'],
80 names: [],
81 mappings: '!',
82 } satisfies RawSourceMap;
83 expect(() =>
84 new SourceMapConsumer(invalidSourceMap).originalPositionFor({
85 line: 1,
86 column: 0,
87 })
88 ).toThrow('Invalid base64 digit');
89
90 const { generatedFile } = yield* makeSourceMapFixture(JSON.stringify(invalidSourceMap));
91 const location = locationFor(generatedFile);
92
93 expect(yield* getSourcesFromMapFile('app.js', location)).toStrictEqual({
94 _tag: 'location',
95 name: 'app.js',
96 ...location,
97 });
98 })
99 )
100 );
101
102 it.effect('falls back when the map points at a source file that was never shipped', () =>
103 run(
104 Effect.gen(function* () {
105 const validSourceMap = JSON.stringify({
106 version: 3,
107 file: 'app.js',
108 sources: ['app.ts'],
109 names: [],
110 mappings: 'AAAA',
111 });
112 // No `sourceContents`, so `app.ts` does not exist next to the map.
113 const { generatedFile } = yield* makeSourceMapFixture(validSourceMap);
114 const location = locationFor(generatedFile);
115
116 expect(yield* getSourcesFromMapFile('app.js', location)).toStrictEqual({
117 _tag: 'location',
118 name: 'app.js',
119 ...location,
120 });
121 })
122 )
123 );
124
125 it.effect('falls back when the .map path is a directory rather than a file', () =>
126 run(
127 Effect.gen(function* () {
128 const fs = yield* FileSystem.FileSystem;
129 const path = yield* Path.Path;
130 const directory = yield* fs.makeTempDirectoryScoped({ prefix: 'composio-sourcemap-' });
131 const generatedFile = path.join(directory, 'app.js');
132 yield* fs.makeDirectory(`${generatedFile}.map`);
133 const location = locationFor(generatedFile);
134
135 expect(yield* getSourcesFromMapFile('app.js', location)).toStrictEqual({
136 _tag: 'location',
137 name: 'app.js',
138 ...location,
139 });
140 })
141 )
142 );
143
144 it.effect('enriches maps that use null for the optional file and sourceRoot keys', () =>
145 run(
146 Effect.gen(function* () {
147 const sourceContents = "throw new Error('primary diagnostic');\n";
148 // `source-map-js` accepts both; rejecting them would silently drop
149 // enrichment for maps emitted without an output file name.
150 const nullableSourceMap = JSON.stringify({
151 version: 3,
152 file: null,
153 sourceRoot: null,
154 sources: ['app.ts'],
155 names: [],
156 mappings: 'AAAA',
157 });
158 const { directory, generatedFile } = yield* makeSourceMapFixture(nullableSourceMap, {
159 sourceContents,
160 });
161 const path = yield* Path.Path;
162
163 expect(yield* getSourcesFromMapFile('app.js', locationFor(generatedFile))).toMatchObject({
164 _tag: 'sources',
165 sourcesPath: `${path.join(directory, 'app.ts')}:1:1`,
166 });
167 })
168 )
169 );
170
171 it.effect('still enriches locations from valid source maps', () =>
172 run(
173 Effect.gen(function* () {
174 const sourceContents = "throw new Error('primary diagnostic');\n";
175 const validSourceMap = JSON.stringify({
176 version: 3,
177 file: 'app.js',
178 sources: ['app.ts'],
179 names: [],
180 mappings: 'AAAA',
181 });
182 const { directory, generatedFile } = yield* makeSourceMapFixture(validSourceMap, {
183 sourceContents,
184 });
185 const path = yield* Path.Path;
186
187 expect(yield* getSourcesFromMapFile('app.js', locationFor(generatedFile))).toStrictEqual({
188 _tag: 'sources',
189 name: 'app.js',
190 runPath: `${generatedFile}:1:0`,
191 sourcesPath: `${path.join(directory, 'app.ts')}:1:1`,
192 source: [
193 { line: 1, code: "throw new Error('primary diagnostic');", column: 1 },
194 { line: 2, code: '', column: undefined },
195 ],
196 });
197 })
198 )
199 );
200
201 it.effect('preserves the primary diagnostic through captureErrors', () =>
202 run(
203 Effect.gen(function* () {
204 const path = yield* Path.Path;
205 const workingDirectory = path.resolve('.');
206 const { generatedFile } = yield* makeSourceMapFixture('{ malformed', {
207 directory: workingDirectory,
208 });
209 const primary = new Error('primary diagnostic');
210 primary.stack = `Error: primary diagnostic\n at ${generatedFile}:1:0`;
211
212 const captured = yield* captureErrors(Cause.die(primary));
213
214 expect(captured.errors).toHaveLength(1);
215 expect(captured.errors[0]).toMatchObject({
216 errorType: 'Error',
217 message: ['primary diagnostic'],
218 location: [
219 {
220 name: '',
221 filePath: generatedFile.replace(workingDirectory, ''),
222 line: 1,
223 column: 0,
224 },
225 ],
226 });
227 })
228 )
229 );
230
231 it.effect('preserves the primary diagnostic when the original source is missing', () =>
232 run(
233 Effect.gen(function* () {
234 const path = yield* Path.Path;
235 const workingDirectory = path.resolve('.');
236 const validSourceMap = JSON.stringify({
237 version: 3,
238 file: 'app.js',
239 sources: ['app.ts'],
240 names: [],
241 mappings: 'AAAA',
242 });
243 // A valid map whose original source was never shipped — the common
244 // shape for a compiled binary — must not abort error capture either.
245 const { generatedFile } = yield* makeSourceMapFixture(validSourceMap, {
246 directory: workingDirectory,
247 });
248 const primary = new Error('primary diagnostic');
249 primary.stack = `Error: primary diagnostic\n at ${generatedFile}:1:0`;
250
251 const captured = yield* captureErrors(Cause.die(primary));
252
253 expect(captured.errors).toHaveLength(1);
254 expect(captured.errors[0]).toMatchObject({
255 errorType: 'Error',
256 message: ['primary diagnostic'],
257 location: [
258 {
259 name: '',
260 filePath: generatedFile.replace(workingDirectory, ''),
261 line: 1,
262 column: 0,
263 },
264 ],
265 });
266 })
267 )
268 );
269
270 it.effect('preserves the primary diagnostic when a .ts frame is unreadable', () =>
271 run(
272 Effect.gen(function* () {
273 const path = yield* Path.Path;
274 const fs = yield* FileSystem.FileSystem;
275 const workingDirectory = path.resolve('.');
276 const directory = yield* fs.makeTempDirectoryScoped({
277 directory: workingDirectory,
278 prefix: 'composio-sourcemap-',
279 });
280 // TypeScript frames skip source maps entirely and read the file
281 // directly; a deleted file must degrade rather than abort.
282 const missingSource = path.join(directory, 'missing.ts');
283 const primary = new Error('primary diagnostic');
284 primary.stack = `Error: primary diagnostic\n at ${missingSource}:1:0`;
285
286 const captured = yield* captureErrors(Cause.die(primary));
287
288 expect(captured.errors).toHaveLength(1);
289 expect(captured.errors[0]).toMatchObject({
290 errorType: 'Error',
291 message: ['primary diagnostic'],
292 location: [
293 {
294 name: '',
295 filePath: missingSource.replace(workingDirectory, ''),
296 line: 1,
297 column: 0,
298 },
299 ],
300 });
301 })
302 )
303 );
304 });