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 172 lines 6.07 KB
Raw
1 import { describe, expect, it } from '@effect/vitest';
2 import { Cause, Console, Data, Effect, Exit, Layer } from 'effect';
3 import * as BunFileSystem from '@effect/platform-bun/BunFileSystem';
4 import * as BunPath from '@effect/platform-bun/BunPath';
5 import { APIConnectionTimeoutError, AuthenticationError } from '@composio/client/effect';
6 import { ExpectedFailure } from 'src/effects/expected-failure';
7 import { ReportedFailure } from 'src/effects/report-failure';
8 import { requestError } from 'src/effects/request-error';
9 import { retryHint, TEMPORARY_FAILURE_EXIT_CODE } from 'src/effects/retry-advice';
10 import { reportInvocationFailure } from 'src/invocation/invocation';
11 import { MockConsole } from 'test/__utils__';
12 import { exitCodeOf } from 'test/__utils__/exit-code';
13 import { TerminalUITest } from 'test/__utils__/services/terminal-ui-test';
14
15 // A fresh console per test, so one test's output never shows up in another's lines.
16 const TestLayer = Layer.mergeAll(
17 Layer.effect(Console.Console, MockConsole.make),
18 TerminalUITest,
19 BunFileSystem.layer,
20 BunPath.layer
21 );
22
23 const report = (cause: Cause.Cause<unknown>) =>
24 Effect.gen(function* () {
25 const exit = yield* Effect.exit(reportInvocationFailure(cause));
26 const stderr = yield* MockConsole.getLines({ stream: 'stderr' });
27 return { exit, stderr };
28 }).pipe(Effect.provide(TestLayer));
29
30 // Standalone controls (BEL, DEL, NEL) and line breaks, as a remote message could carry them.
31 const remoteText = 'remote\u0007text\u007f\u0085';
32
33 class RemoteExpectedFailure extends Data.TaggedError('test/RemoteExpectedFailure')<{
34 readonly message: string;
35 }> {
36 get [ExpectedFailure]() {
37 return true;
38 }
39 }
40
41 const failureOf = (exit: Exit.Exit<unknown, unknown>) =>
42 Exit.isFailure(exit) ? Cause.squash(exit.cause) : undefined;
43
44 describe('reportInvocationFailure', () => {
45 it.effect('[Given] an interruption alone [Then] it stays an interruption and exits 130', () =>
46 Effect.gen(function* () {
47 const { exit, stderr } = yield* report(Cause.interrupt());
48
49 expect(Exit.hasInterrupts(exit)).toBe(true);
50 expect(exitCodeOf(exit)).toBe(130);
51 expect(stderr).toEqual([]);
52 })
53 );
54
55 it.effect('[Given] a failure already on screen [Then] it is re-failed without printing', () =>
56 Effect.gen(function* () {
57 const failure = new ReportedFailure({ message: 'already shown' });
58
59 const { exit, stderr } = yield* report(Cause.fail(failure));
60
61 expect(failureOf(exit)).toBe(failure);
62 expect(exitCodeOf(exit)).toBe(1);
63 expect(stderr).toEqual([]);
64 })
65 );
66
67 it.effect('[Given] an unreported defect [Then] it is printed once and exits 1', () =>
68 Effect.gen(function* () {
69 const defect = new Error('boom');
70
71 const { exit, stderr } = yield* report(Cause.die(defect));
72
73 expect(failureOf(exit)).toBe(defect);
74 expect(exitCodeOf(exit)).toBe(1);
75 expect(stderr).toHaveLength(1);
76 expect(stderr[0]).toContain('boom');
77 })
78 );
79
80 it.effect('[Given] a request failure safe to send again [Then] it says so and exits 75', () =>
81 Effect.gen(function* () {
82 class FetchError extends requestError('test/FetchError') {}
83 const failure = new FetchError({
84 message: 'Failed to fetch toolkits.',
85 cause: new APIConnectionTimeoutError({
86 message: 'Request timed out.',
87 method: 'GET',
88 url: 'https://backend.composio.dev/api/v3.1/toolkits',
89 }),
90 });
91
92 const { exit, stderr } = yield* report(Cause.fail(failure));
93
94 expect(failureOf(exit)).toBe(failure);
95 expect(exitCodeOf(exit)).toBe(TEMPORARY_FAILURE_EXIT_CODE);
96 expect(stderr.at(-1)).toBe(retryHint('safe'));
97 })
98 );
99
100 it.effect('[Given] a POST that timed out [Then] it exits 1 with no retry advice', () =>
101 Effect.gen(function* () {
102 const failure = new APIConnectionTimeoutError({
103 message: 'Request timed out.',
104 method: 'POST',
105 url: 'https://backend.composio.dev/api/v3.1/auth_configs',
106 });
107
108 const { exit, stderr } = yield* report(Cause.fail(failure));
109
110 expect(exitCodeOf(exit)).toBe(1);
111 expect(stderr.join('\n')).not.toContain(retryHint('safe'));
112 })
113 );
114
115 it.effect(
116 '[Given] an expected failure quoting remote text [Then] its controls print as escapes and it exits 1',
117 () =>
118 Effect.gen(function* () {
119 const failure = new RemoteExpectedFailure({
120 message: `Request failed: ${remoteText}\r\nRun the command again.\rdone`,
121 });
122
123 const { exit, stderr } = yield* report(Cause.fail(failure));
124
125 expect(failureOf(exit)).toBe(failure);
126 expect(exitCodeOf(exit)).toBe(1);
127 // CRLF stays a line break; a lone CR does not.
128 expect(stderr).toEqual([
129 'Request failed: remote\\u0007text\\u007f\\u0085\nRun the command again.\\u000ddone',
130 ]);
131 })
132 );
133
134 it.effect(
135 '[Given] a 401 whose backend message has controls [Then] it prints as one escaped line',
136 () =>
137 Effect.gen(function* () {
138 const failure = new AuthenticationError({
139 message: '401 Unauthorized',
140 method: 'GET',
141 url: 'https://backend.composio.dev/api/v3.1/toolkits',
142 status: 401,
143 requestId: undefined,
144 details: {
145 message: `Key\nrejected ${remoteText}`,
146 code: 401,
147 slug: 'unauthorized',
148 status: 401,
149 },
150 body: undefined,
151 });
152
153 const { exit, stderr } = yield* report(Cause.fail(failure));
154
155 expect(exitCodeOf(exit)).toBe(1);
156 expect(stderr[0]).toBe('Key\\u000arejected remote\\u0007text\\u007f\\u0085');
157 })
158 );
159
160 it.effect(
161 '[Given] a multiline defect message [Then] the error title preserves line breaks and escapes unsafe controls',
162 () =>
163 Effect.gen(function* () {
164 const { exit, stderr } = yield* report(Cause.die(new Error(`boom\n${remoteText}\rdone`)));
165
166 expect(exitCodeOf(exit)).toBe(1);
167 expect(stderr).toHaveLength(1);
168 expect(stderr[0]).toContain('boom\nremote\\u0007text\\u007f\\u0085\\u000ddone');
169 expect(stderr[0]).not.toMatch(/[\u0007\u007f\u0085]/u);
170 })
171 );
172 });