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