@devries-cameron20 / composio Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 04:45 UTC

next
ts 90 lines 3.34 KB
Raw
1 import { describe, expect, it } from '@effect/vitest';
2 import { Cause, Effect } from 'effect';
3 import { generationOutcome } from 'src/effects/generation-runtime';
4 import {
5 GenerateTypeFromJsonSchemaError,
6 rehydrateGenerationError,
7 SafeOutputPathError,
8 TypeScriptTranspileError,
9 } from 'src/generation/errors';
10
11 // The companion module ships its own copy of the error classes, so what comes
12 // back is an instance of a class the CLI has never seen: same tag, same fields,
13 // same stack, but `instanceof` the CLI's class is false. This clone reproduces
14 // exactly that.
15 const foreign = <E extends Error & { readonly _tag: string }>(error: E): E => {
16 const clone = Object.create(Error.prototype) as E;
17 Object.getOwnPropertyNames(error).forEach(key =>
18 Object.defineProperty(clone, key, Object.getOwnPropertyDescriptor(error, key)!)
19 );
20 // `stack` is a lazy accessor bound to the original instance; a real foreign
21 // instance owns its own, so give the clone a plain copy.
22 Object.defineProperty(clone, 'stack', { value: error.stack, configurable: true });
23 Object.defineProperty(clone, '_tag', { value: error._tag, enumerable: true });
24 return clone;
25 };
26
27 describe('generation-runtime', () => {
28 describe('rehydrateGenerationError', () => {
29 const cases = [
30 new SafeOutputPathError({
31 filename: '../x.ts',
32 outputDir: '/out',
33 resolvedPath: '/x.ts',
34 message: 'escapes',
35 }),
36 new TypeScriptTranspileError({ message: 'compile failed', cause: 'TS1005' }),
37 new GenerateTypeFromJsonSchemaError({ message: 'bad schema', cause: new Error('inner') }),
38 ] as const;
39
40 it.each(cases)(
41 'rebuilds $_tag as an instance of the CLI class, keeping fields and stack',
42 original => {
43 const crossed = foreign(original);
44 expect(crossed).not.toBeInstanceOf(original.constructor);
45
46 const rehydrated = rehydrateGenerationError(crossed);
47 expect(rehydrated).toBeInstanceOf(original.constructor);
48 expect(rehydrated._tag).toBe(original._tag);
49 expect(rehydrated.message).toBe(original.message);
50 expect(rehydrated.stack).toBe(original.stack);
51 }
52 );
53 });
54
55 describe('generationOutcome', () => {
56 it.effect('lifts a success outcome to the value', () =>
57 Effect.gen(function* () {
58 const value = yield* generationOutcome(() =>
59 Promise.resolve({ _tag: 'Success' as const, value: 42 })
60 );
61 expect(value).toBe(42);
62 })
63 );
64
65 it.effect('lifts a failure outcome to a typed failure of the CLI error class', () =>
66 Effect.gen(function* () {
67 const error = new TypeScriptTranspileError({ message: 'compile failed', cause: 'TS1005' });
68 const failure = yield* generationOutcome(() =>
69 Promise.resolve({ _tag: 'Failure' as const, error })
70 ).pipe(Effect.flip);
71
72 expect(failure).toBeInstanceOf(TypeScriptTranspileError);
73 expect(failure.cause).toBe('TS1005');
74 })
75 );
76
77 it.effect('keeps a rejected promise (a defect in the pipeline) as a defect', () =>
78 Effect.gen(function* () {
79 const boom = new Error('boom');
80 const cause = yield* generationOutcome<number>(() => Promise.reject(boom)).pipe(
81 Effect.sandbox,
82 Effect.flip
83 );
84
85 expect(Cause.hasDies(cause)).toBe(true);
86 expect(Cause.squash(cause)).toBe(boom);
87 })
88 );
89 });
90 });