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 199 lines 7.2 KB
Raw
1 import { describe, expect, it } from '@effect/vitest';
2 import * as BunFileSystem from '@effect/platform-bun/BunFileSystem';
3 import * as BunPath from '@effect/platform-bun/BunPath';
4 import { Cause, Data, Effect } from 'effect';
5
6 import { captureErrors } from 'src/effect-errors/capture-errors';
7 import { captureErrorsFrom } from 'src/effect-errors/logic/errors/capture-errors-from-cause';
8 import { prettyPrintFromCapturedErrors } from 'src/effect-errors/pretty-print-from-captured-errors';
9
10 // Characterization tests pinning the v3 Cause traversal and rendering
11 // behavior, so the Effect v4 migration (flat failure list instead of the
12 // Empty/Fail/Die/Interrupt/Sequential/Parallel tree) has an executable
13 // equivalence oracle instead of eyeballing error output.
14
15 class DbError extends Data.TaggedError('DbError')<{
16 readonly message: string;
17 }> {}
18
19 class HttpError extends Data.TaggedError('HttpError')<{
20 readonly message: string;
21 }> {}
22
23 const stripAnsi = (value: string): string => value.replace(/\x1b\[[0-9;]*m/g, '');
24
25 const render = (captured: Effect.Success<ReturnType<typeof capture>>): string =>
26 stripAnsi(
27 prettyPrintFromCapturedErrors(captured, {
28 enabled: true,
29 stripCwd: true,
30 hideStackTrace: true,
31 })
32 );
33
34 const capture = <E>(cause: Cause.Cause<E>) =>
35 captureErrors(cause).pipe(Effect.provide([BunFileSystem.layer, BunPath.layer]));
36
37 describe('captureErrorsFrom (structural Cause traversal)', () => {
38 it('returns no errors for an empty cause', () => {
39 expect(captureErrorsFrom(Cause.empty)).toStrictEqual([]);
40 });
41
42 it('captures a tagged failure with its type and message', () => {
43 const [error, ...rest] = captureErrorsFrom(Cause.fail(new DbError({ message: 'no db' })));
44
45 expect(rest).toStrictEqual([]);
46 expect(error?.errorType).toBe('DbError');
47 expect(error?.message).toBe('no db');
48 expect(error?.isPlainString).toBe(false);
49 });
50
51 it('captures a plain-string failure as a plain string', () => {
52 const [error] = captureErrorsFrom(Cause.fail('boom'));
53
54 expect(error?.isPlainString).toBe(true);
55 expect(error?.message).toBe('boom');
56 });
57
58 it('captures a defect', () => {
59 const [error, ...rest] = captureErrorsFrom(Cause.die(new Error('kaboom')));
60
61 expect(rest).toStrictEqual([]);
62 expect(error?.errorType).toBe('Error');
63 expect(error?.message).toStrictEqual(['kaboom']);
64 });
65
66 it('captures nothing for an interruption', () => {
67 expect(captureErrorsFrom(Cause.interrupt())).toStrictEqual([]);
68 });
69
70 it('flattens a sequential cause left to right', () => {
71 const cause = Cause.combine(
72 Cause.fail(new DbError({ message: 'first' })),
73 Cause.die(new Error('second'))
74 );
75
76 const errors = captureErrorsFrom(cause);
77
78 expect(errors.map(error => error.errorType)).toStrictEqual(['DbError', 'Error']);
79 });
80
81 it('flattens a parallel cause left to right', () => {
82 const cause = Cause.combine(
83 Cause.fail(new DbError({ message: 'left' })),
84 Cause.fail(new HttpError({ message: 'right' }))
85 );
86
87 const errors = captureErrorsFrom(cause);
88
89 expect(errors.map(error => error.message)).toStrictEqual(['left', 'right']);
90 });
91
92 it('flattens nested parallel-in-sequential causes in traversal order', () => {
93 const cause = Cause.combine(
94 Cause.combine(
95 Cause.fail(new DbError({ message: 'a' })),
96 Cause.fail(new HttpError({ message: 'b' }))
97 ),
98 Cause.fail(new DbError({ message: 'c' }))
99 );
100
101 const errors = captureErrorsFrom(cause);
102
103 expect(errors.map(error => error.message)).toStrictEqual(['a', 'b', 'c']);
104 });
105
106 it('ignores interruptions mixed with failures', () => {
107 const cause = Cause.combine(
108 Cause.interrupt(),
109 Cause.fail(new DbError({ message: 'still there' }))
110 );
111
112 const errors = captureErrorsFrom(cause);
113
114 expect(errors.map(error => error.message)).toStrictEqual(['still there']);
115 });
116
117 // Effect v4 dropped the `effect/SpanAnnotation` internal that let `Effect.fail`/
118 // `Effect.die` stamp the ambient span directly onto the raised error value. The
119 // analogous v4 mechanism recovers the span one level up, from the `Cause`
120 // `Reason`'s `Cause.StackTrace` annotation (see
121 // `src/effect-errors/logic/errors/span-annotation.ts` and the canaries in
122 // `test/src/effect-errors/span-annotation.test.ts`), so `captureErrorsFrom`
123 // still threads the ambient span into the captured error — just as a name
124 // chain rather than the old v3 `Span` object.
125 it.effect('threads the ambient span from a runtime failure into the captured error', () =>
126 Effect.gen(function* () {
127 const cause = yield* Effect.fail(new DbError({ message: 'no db' })).pipe(
128 Effect.withSpan('characterization-span'),
129 Effect.sandbox,
130 Effect.flip
131 );
132
133 const [error] = captureErrorsFrom(cause);
134
135 expect(error?.spans.map(span => span.name)).toEqual(['characterization-span']);
136 })
137 );
138 });
139
140 describe('captureErrors + prettyPrintFromCapturedErrors (rendering)', () => {
141 it.effect(
142 'reports interrupt-only causes as interrupted and renders the interruption message',
143 () =>
144 Effect.gen(function* () {
145 const captured = yield* capture(Cause.interrupt());
146
147 expect(captured).toStrictEqual({ interrupted: true, errors: [] });
148 expect(render(captured)).toBe('✅ All fibers interrupted without errors.');
149 })
150 );
151
152 it.effect('renders a single failure without the multi-error header', () =>
153 Effect.gen(function* () {
154 const captured = yield* capture(Cause.fail(new DbError({ message: 'no db' })));
155 const output = render(captured);
156
157 expect(captured.interrupted).toBe(false);
158 expect(output).toContain('💥 DbError • no db');
159 expect(output).not.toContain('errors occurred');
160 expect(output).not.toContain('#1 -');
161 })
162 );
163
164 // Same v4 mechanism as above, exercised end to end: the span recovered from the
165 // `Cause.StackTrace` reason annotation now renders as a (duration-less) span
166 // timeline instead of falling back to the "consider using spans" hint.
167 it.effect('renders a runtime failure with a span timeline recovered from the Cause', () =>
168 Effect.gen(function* () {
169 const cause = yield* Effect.fail(new HttpError({ message: 'request failed' })).pipe(
170 Effect.withSpan('characterization-span'),
171 Effect.sandbox,
172 Effect.flip
173 );
174
175 const output = render(yield* capture(cause));
176
177 expect(output).toContain('💥 HttpError • request failed');
178 expect(output).toContain('◯');
179 expect(output).toContain('characterization-span');
180 expect(output).not.toContain('Consider using spans to improve errors reporting');
181 })
182 );
183
184 it.effect('renders multiple failures with a count header and per-error indexes in order', () =>
185 Effect.gen(function* () {
186 const cause = Cause.combine(
187 Cause.fail(new DbError({ message: 'left' })),
188 Cause.fail(new HttpError({ message: 'right' }))
189 );
190
191 const output = render(yield* capture(cause));
192
193 expect(output).toContain('2 errors occurred');
194 expect(output).toContain('💥 #1 - DbError • left');
195 expect(output).toContain('💥 #2 - HttpError • right');
196 expect(output.indexOf('DbError')).toBeLessThan(output.indexOf('HttpError'));
197 })
198 );
199 });