1
+import { Predicate } from 'effect';
2
+import { describe, expect, it } from 'vitest';
3
+import {
4
+ ConnectionPollingError,
5
+ MissingLinkUserIdError,
6
+} from 'src/commands/connected-accounts/link/connected-accounts.link.errors';
7
+import { commandInputError, isUsageFailure, notLoggedIn } from 'src/commands/errors';
8
+import { InvalidOrganizationError } from 'src/commands/login/login.errors';
9
+import { ProxyCommandError, ReportedProxyCommandError } from 'src/commands/proxy.cmd';
10
+import {
11
+ ReportedToolExecutionError,
12
+ ToolExecutionError,
13
+} from 'src/commands/tools/commands/tools.execute.cmd';
14
+import { LoginSessionError } from 'src/effects/browser-login';
15
+import { ExpectedFailure } from 'src/effects/expected-failure';
16
+import { ComposioCorePkgNotFound } from 'src/effects/find-composio-core-generated';
17
+import {
18
+ InvalidToolkitVersionsValidationError,
19
+ ToolkitVersionValidationError,
20
+} from 'src/effects/validate-toolkit-versions';
21
+import { ProjectResolutionError } from 'src/services/command-project';
22
+import { ConnectedAccountResolutionError } from 'src/services/connected-account-selection';
23
+import { PendingLoginError } from 'src/services/pending-login-session';
24
+import { ProjectEnvironmentDetectorError } from 'src/services/project-environment-detector';
25
+
26
+const expected = [
27
+ { name: 'a usage mistake', error: commandInputError('execute')('Bad flag.', '--flag') },
28
+ { name: 'a missing login', error: notLoggedIn() },
29
+ {
30
+ name: 'an unresolved project',
31
+ error: new ProjectResolutionError({ reason: 'missing_org', message: 'No current org.' }),
32
+ },
33
+ {
34
+ name: 'an --account that matches no connected account',
35
+ error: new ConnectedAccountResolutionError({ message: 'No match.', toolkitSlug: 'gmail' }),
36
+ },
37
+ {
38
+ name: 'execute without a user id',
39
+ error: new ToolExecutionError({ reason: 'missing_user_id', message: 'Missing user id.' }),
40
+ },
41
+ {
42
+ name: 'execute with a --file the tool cannot take',
43
+ error: new ToolExecutionError({ reason: 'file_input', message: 'No file input.' }),
44
+ },
45
+ {
46
+ name: 'execute with a toolkit that is not connected',
47
+ error: new ToolExecutionError({ reason: 'connection_check', message: 'Not connected.' }),
48
+ },
49
+ {
50
+ name: 'proxy without a consumer user',
51
+ error: new ProxyCommandError({ message: 'No user.' }),
52
+ },
53
+ {
54
+ name: 'link without a user id',
55
+ error: new MissingLinkUserIdError({ message: 'Missing user id.', flow: 'list' }),
56
+ },
57
+ {
58
+ name: 'link with an authorization not finished in time',
59
+ error: new ConnectionPollingError({
60
+ message: "Connection status is still 'INITIATED', waiting for 'ACTIVE'",
61
+ connectedAccountId: 'ca_1',
62
+ status: 'INITIATED',
63
+ }),
64
+ },
65
+ {
66
+ name: 'login with an unknown --org',
67
+ error: new InvalidOrganizationError({ message: 'Invalid organization.', requestedOrg: 'x' }),
68
+ },
69
+ {
70
+ name: 'login with an expired session',
71
+ error: new LoginSessionError({ message: 'Session not found.', operation: 'load' }),
72
+ },
73
+ {
74
+ name: 'login --poll without a pending login',
75
+ error: new PendingLoginError({ message: 'No pending login found.', reason: 'missing' }),
76
+ },
77
+ {
78
+ name: 'login --poll with an expired pending login',
79
+ error: new PendingLoginError({ message: 'Pending login expired.', reason: 'expired' }),
80
+ },
81
+ {
82
+ name: 'generate without @composio/core installed',
83
+ error: new ComposioCorePkgNotFound({
84
+ message: '@composio/core not found',
85
+ cause: '@composio/core not installed',
86
+ }),
87
+ },
88
+ {
89
+ name: 'generate outside a recognizable project',
90
+ error: new ProjectEnvironmentDetectorError({
91
+ cause: new Error('No project language detected'),
92
+ message: 'No recognizable project indicators found.',
93
+ checkedPaths: [],
94
+ }),
95
+ },
96
+ {
97
+ name: 'a toolkit version override naming an unknown version',
98
+ error: new InvalidToolkitVersionsValidationError([
99
+ { toolkit: 'gmail', requestedVersion: 'nope', availableVersions: ['20260101_00'] },
100
+ ]),
101
+ },
102
+ {
103
+ name: 'a toolkit version override naming an unknown toolkit',
104
+ error: new ToolkitVersionValidationError({
105
+ message: 'Invalid toolkit(s) in version overrides: gmal.',
106
+ reason: 'invalid-toolkit',
107
+ }),
108
+ },
109
+];
110
+
111
+const defects = [
112
+ {
113
+ name: 'a toolkit version check whose request failed',
114
+ error: new ToolkitVersionValidationError({ message: 'Failed.', reason: 'request' }),
115
+ },
116
+ {
117
+ name: 'a toolkit version check whose response did not decode',
118
+ error: new ToolkitVersionValidationError({ message: 'Failed.', reason: 'decode' }),
119
+ },
120
+ {
121
+ name: 'a toolkit version check without a client',
122
+ error: new ToolkitVersionValidationError({
123
+ message: 'API client not initialized',
124
+ reason: 'client-unavailable',
125
+ }),
126
+ },
127
+ {
128
+ name: 'a pending login cache that cannot be read',
129
+ error: new PendingLoginError({
130
+ message: 'Failed to read pending login cache',
131
+ reason: 'io',
132
+ cause: new Error('EACCES'),
133
+ }),
134
+ },
135
+ {
136
+ name: 'a pending login cache that does not decode',
137
+ error: new PendingLoginError({ message: 'Pending login cache is invalid', reason: 'invalid' }),
138
+ },
139
+ {
140
+ name: 'an execute failure already printed',
141
+ error: new ReportedToolExecutionError({ reason: 'execution_failed', message: 'Failed.' }),
142
+ },
143
+ {
144
+ name: 'a proxy failure already printed',
145
+ error: new ReportedProxyCommandError({
146
+ message: 'Not connected.',
147
+ toolkit: 'gmail',
148
+ endpoint: 'https://gmail.googleapis.com',
149
+ }),
150
+ },
151
+ { name: 'a plain Error', error: new Error('boom') },
152
+];
153
+
154
+describe('isUsageFailure', () => {
155
+ it.each(expected)('[Given] $name [Then] it is a usage failure', ({ error }) => {
156
+ expect(isUsageFailure(error)).toBe(true);
157
+ });
158
+
159
+ it.each(defects)('[Given] $name [Then] it is not a usage failure', ({ error }) => {
160
+ expect(isUsageFailure(error)).toBe(false);
161
+ });
162
+
163
+ // The marker is a prototype getter: declaring it must not add an own property, which would
164
+ // change the error's structural equality and what a reporter prints for it.
165
+ it.each(expected)(
166
+ '[Given] $name [Then] its class declares the marker off the instance',
167
+ ({ error }) => {
168
+ expect(Predicate.hasProperty(error, ExpectedFailure)).toBe(true);
169
+ expect(Object.getOwnPropertySymbols(error)).not.toContain(ExpectedFailure);
170
+ }
171
+ );
172
+
173
+ it('[Given] an object that only claims the marker [Then] it still needs a string message', () => {
174
+ expect(isUsageFailure({ [ExpectedFailure]: true })).toBe(false);
175
+ expect(isUsageFailure({ [ExpectedFailure]: true, message: 'Fix it.' })).toBe(true);
176
+ });
177
+});