1
+import {
2
+ APIConnectionError,
3
+ APIConnectionTimeoutError,
4
+ APIError,
5
+ BadRequestError,
6
+ ConflictError,
7
+ DecodeError,
8
+ InternalServerError,
9
+ RateLimitError,
10
+ type APIErrorFields,
11
+} from '@composio/client/effect';
12
+import { describe, expect, it } from '@effect/vitest';
13
+import { Option } from 'effect';
14
+import { requestError } from 'src/effects/request-error';
15
+import { retryAdviceFor } from 'src/effects/retry-advice';
16
+import { DashboardToolExecutionError } from 'src/services/dashboard-tool-execution';
17
+
18
+const url = 'https://backend.composio.dev/api/v3.1/tools/{tool_slug}';
19
+
20
+const status = (method: string, code: number): APIErrorFields => ({
21
+ message: `${code}`,
22
+ method,
23
+ url,
24
+ status: code,
25
+ requestId: undefined,
26
+ details: undefined,
27
+ body: undefined,
28
+});
29
+
30
+const connectionError = (method: string, reason = 'TransportError') =>
31
+ new APIConnectionError({
32
+ message: 'Connection error.',
33
+ method,
34
+ url,
35
+ reason,
36
+ description: undefined,
37
+ });
38
+
39
+const timeout = (method: string) =>
40
+ new APIConnectionTimeoutError({ message: 'Request timed out.', method, url });
41
+
42
+class WrappingError extends requestError('test/WrappingError') {}
43
+
44
+describe('retryAdviceFor', () => {
45
+ it.each([
46
+ { name: 'a GET that timed out', error: timeout('GET'), advice: 'safe' },
47
+ { name: 'a GET whose connection failed', error: connectionError('GET'), advice: 'safe' },
48
+ {
49
+ name: 'a GET answered 503',
50
+ error: new InternalServerError(status('GET', 503)),
51
+ advice: 'safe',
52
+ },
53
+ {
54
+ name: 'a DELETE answered 502',
55
+ error: new InternalServerError(status('DELETE', 502)),
56
+ advice: 'safe',
57
+ },
58
+ { name: 'a GET answered 408', error: new APIError(status('GET', 408)), advice: 'safe' },
59
+ { name: 'a POST answered 429', error: new RateLimitError(status('POST', 429)), advice: 'safe' },
60
+ { name: 'a POST that timed out', error: timeout('POST'), advice: 'check-first' },
61
+ {
62
+ name: 'a POST whose connection failed',
63
+ error: connectionError('POST'),
64
+ advice: 'check-first',
65
+ },
66
+ {
67
+ name: 'a PATCH answered 500',
68
+ error: new InternalServerError(status('PATCH', 500)),
69
+ advice: 'check-first',
70
+ },
71
+ ])('[Given] $name [Then] the advice is $advice', ({ error, advice }) => {
72
+ expect(retryAdviceFor(error)).toStrictEqual(Option.some(advice));
73
+ });
74
+
75
+ it.each([
76
+ { name: 'a 400', error: new BadRequestError(status('GET', 400)) },
77
+ { name: 'a 409', error: new ConflictError(status('GET', 409)) },
78
+ {
79
+ name: 'a read whose response failed decoding',
80
+ error: new DecodeError({
81
+ message: 'The response body is not valid JSON.',
82
+ method: 'GET',
83
+ url,
84
+ status: 200,
85
+ requestId: undefined,
86
+ operation: 'tools.retrieve',
87
+ issues: [],
88
+ }),
89
+ },
90
+ { name: 'a redirect the client refused', error: connectionError('GET', 'TooManyRedirects') },
91
+ { name: 'an error that is not a request', error: new Error('boom') },
92
+ ])('[Given] $name [Then] there is no advice', ({ error }) => {
93
+ expect(retryAdviceFor(error)).toStrictEqual(Option.none());
94
+ });
95
+
96
+ it('[Given] an execution whose response failed decoding [Then] it may have run', () => {
97
+ const error = new DecodeError({
98
+ message: 'The response body is not valid JSON.',
99
+ method: 'POST',
100
+ url,
101
+ status: 200,
102
+ requestId: undefined,
103
+ operation: 'toolRouter.session.execute',
104
+ issues: [],
105
+ });
106
+
107
+ expect(retryAdviceFor(error)).toStrictEqual(Option.some('check-first'));
108
+ });
109
+
110
+ it('[Given] a command error that keeps the request failure as its cause [Then] it reads the cause', () => {
111
+ const wrapped = new WrappingError({ message: 'Failed to fetch.', cause: timeout('GET') });
112
+
113
+ expect(retryAdviceFor(wrapped)).toStrictEqual(Option.some('safe'));
114
+ });
115
+
116
+ it.each([
117
+ { reason: 'request', status: undefined, advice: Option.some('check-first') },
118
+ { reason: 'timeout', status: undefined, advice: Option.some('check-first') },
119
+ { reason: 'response', status: 503, advice: Option.some('check-first') },
120
+ { reason: 'response', status: 429, advice: Option.some('safe') },
121
+ { reason: 'dashboard', status: 429, advice: Option.some('safe') },
122
+ { reason: 'dashboard', status: 500, advice: Option.some('check-first') },
123
+ { reason: 'dashboard', status: 400, advice: Option.none() },
124
+ { reason: 'unauthorized', status: 401, advice: Option.none() },
125
+ ] as const)(
126
+ '[Given] a Dashboard execution that failed with $reason ($status) [Then] the advice is $advice',
127
+ ({ reason, status: httpStatus, advice }) => {
128
+ const error = new DashboardToolExecutionError({
129
+ reason,
130
+ message: 'failed',
131
+ ...(httpStatus === undefined ? {} : { status: httpStatus }),
132
+ });
133
+
134
+ expect(retryAdviceFor(error)).toStrictEqual(advice);
135
+ }
136
+ );
137
+});