@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 428 lines 14.6 KB
Raw
1 /**
2 * Identity: which project and which user a command acts on.
3 *
4 * The root tree acts on the consumer project as the consumer user; the dev
5 * tree acts on a developer project as a test user. Each claim below is checked
6 * against what the fake backend received, not against what the CLI printed.
7 */
8
9 import { afterAll, beforeAll, describe, expect, it } from 'bun:test';
10 import {
11 CONSUMER_PROJECT,
12 CONSUMER_USER_ID,
13 DEVELOPER_PROJECT,
14 GLOBAL_TEST_USER_ID,
15 OTHER_DEVELOPER_PROJECT,
16 ORG,
17 } from '@e2e-tests/cli-fake-backend';
18 import { startCliRig, type CliRig } from '@e2e-tests/cli-fake-backend/rig';
19 import {
20 clean,
21 commandRequests,
22 DEV_DIRECTORY,
23 EXECUTE_SLACK,
24 ExecutionResult,
25 initDeveloperDirectory,
26 PLAYGROUND_EXECUTE_SLACK,
27 RIG_TIMEOUT_MS,
28 routesOf,
29 SLACK_ARGUMENTS,
30 stdoutJson,
31 TEST_TIMEOUT_MS,
32 writeDebugLog,
33 } from './support';
34
35 const inDevDirectory = (command: string) => `cd ${DEV_DIRECTORY} && ${command}`;
36
37 /** A row of the help printed with the error: the name indented, then its description or type. */
38 const helpRow = (name: string) =>
39 new RegExp(`^\\s+${name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\s`, 'm');
40
41 describe('identity: the two command trees', () => {
42 let rig: CliRig;
43
44 beforeAll(async () => {
45 rig = await startCliRig({ label: 'identity' });
46 await rig.login();
47 await initDeveloperDirectory(rig);
48 }, RIG_TIMEOUT_MS);
49
50 afterAll(async () => {
51 await writeDebugLog(rig, import.meta.dir, 'identity');
52 await rig.stop();
53 });
54
55 describe('the same tool on each tree', () => {
56 it(
57 'root `execute` acts on the consumer project as the consumer user',
58 async () => {
59 rig.fake.world.connect(CONSUMER_PROJECT.label, CONSUMER_USER_ID, 'slack');
60 const result = await rig.run(inDevDirectory(EXECUTE_SLACK));
61
62 expect(result.exitCode).toBe(0);
63 expect(stdoutJson(result, ExecutionResult).successful).toBe(true);
64
65 const execution = result.requests.filter(entry => entry.route === 'dashboard.execute');
66 expect(execution).toHaveLength(1);
67 expect(execution[0]).toMatchObject({
68 role: 'dashboard',
69 keyClass: 'user',
70 orgId: ORG.id,
71 project: CONSUMER_PROJECT.label,
72 userId: CONSUMER_USER_ID,
73 });
74 // Run from an initialized developer directory on purpose: the
75 // directory's project must not leak into a root-tree command.
76 const projects = commandRequests(result).map(entry => entry.project);
77 expect(projects).not.toContain(DEVELOPER_PROJECT.label);
78 expect(routesOf(result)).not.toContain('toolRouter.session.execute');
79 },
80 TEST_TIMEOUT_MS
81 );
82
83 it(
84 'dev `playground-execute` acts on the developer project as the global test user',
85 async () => {
86 rig.fake.world.connect(DEVELOPER_PROJECT.label, GLOBAL_TEST_USER_ID, 'slack');
87 const result = await rig.run(inDevDirectory(PLAYGROUND_EXECUTE_SLACK));
88
89 expect(result.exitCode).toBe(0);
90 expect(stdoutJson(result, ExecutionResult).successful).toBe(true);
91
92 const execution = result.requests.filter(
93 entry => entry.route === 'toolRouter.session.execute'
94 );
95 expect(execution).toHaveLength(1);
96 expect(execution[0]).toMatchObject({
97 role: 'backend',
98 keyClass: 'user',
99 project: DEVELOPER_PROJECT.label,
100 userId: GLOBAL_TEST_USER_ID,
101 });
102 expect(routesOf(result)).not.toContain('dashboard.execute');
103 expect(commandRequests(result).map(entry => entry.project)).not.toContain(
104 CONSUMER_PROJECT.label
105 );
106 },
107 TEST_TIMEOUT_MS
108 );
109
110 it(
111 'dev `--user-id` replaces the test user',
112 async () => {
113 rig.fake.world.connect(DEVELOPER_PROJECT.label, 'alice', 'slack');
114 const result = await rig.run(inDevDirectory(`${PLAYGROUND_EXECUTE_SLACK} --user-id alice`));
115
116 expect(result.exitCode).toBe(0);
117 const execution = result.requests.find(
118 entry => entry.route === 'toolRouter.session.execute'
119 );
120 expect(execution).toMatchObject({ project: DEVELOPER_PROJECT.label, userId: 'alice' });
121 },
122 TEST_TIMEOUT_MS
123 );
124
125 it(
126 'dev `--project-name` targets the named developer project',
127 async () => {
128 rig.fake.world.connect(OTHER_DEVELOPER_PROJECT.label, GLOBAL_TEST_USER_ID, 'slack');
129 const result = await rig.run(
130 inDevDirectory(
131 `${PLAYGROUND_EXECUTE_SLACK} --project-name ${OTHER_DEVELOPER_PROJECT.name}`
132 )
133 );
134
135 expect(result.exitCode).toBe(0);
136 const execution = result.requests.find(
137 entry => entry.route === 'toolRouter.session.execute'
138 );
139 expect(execution).toMatchObject({
140 project: OTHER_DEVELOPER_PROJECT.label,
141 userId: GLOBAL_TEST_USER_ID,
142 });
143 },
144 TEST_TIMEOUT_MS
145 );
146 });
147
148 describe('connections do not cross trees', () => {
149 it(
150 'an account linked on the root tree is not connected on the dev tree',
151 async () => {
152 const link = await rig.run(inDevDirectory('composio link github'));
153 expect(link.exitCode).toBe(0);
154 expect(
155 link.requests.find(entry => entry.route === 'toolRouter.session.link')
156 ).toMatchObject({ project: CONSUMER_PROJECT.label, userId: CONSUMER_USER_ID });
157
158 const execution = await rig.run(
159 inDevDirectory(
160 `composio dev playground-execute GITHUB_GET_THE_AUTHENTICATED_USER -d '{}'`
161 )
162 );
163 expect(execution.exitCode).toBe(1);
164 expect(stdoutJson(execution, ExecutionResult)).toMatchObject({
165 successful: false,
166 slug: 'ToolRouterV2_NoActiveConnection',
167 });
168 },
169 TEST_TIMEOUT_MS
170 );
171
172 it(
173 'an account linked on the dev tree is not connected on the root tree',
174 async () => {
175 const link = await rig.run(inDevDirectory('composio dev connected-accounts link gmail'));
176 expect(link.exitCode).toBe(0);
177 expect(
178 link.requests.find(entry => entry.route === 'toolRouter.session.link')
179 ).toMatchObject({ project: DEVELOPER_PROJECT.label, userId: GLOBAL_TEST_USER_ID });
180
181 const execution = await rig.run(
182 `composio execute GMAIL_SEND_EMAIL -d '{"recipient_email":"a@b.test"}'`
183 );
184 expect(execution.exitCode).toBe(1);
185 expect(stdoutJson(execution, ExecutionResult)).toMatchObject({
186 successful: false,
187 slug: 'ToolRouterV2_NoActiveConnection',
188 });
189 },
190 TEST_TIMEOUT_MS
191 );
192
193 it(
194 'root `execute` without a connection names the root command that fixes it',
195 async () => {
196 const result = await rig.run(
197 `composio execute GMAIL_SEND_EMAIL -d '{"recipient_email":"a@b.test"}'`
198 );
199 expect(result.exitCode).toBe(1);
200 expect(stdoutJson(result, ExecutionResult).error).toContain('composio link gmail');
201 },
202 TEST_TIMEOUT_MS
203 );
204
205 it(
206 'dev `playground-execute` without a connection names a dev-tree command that fixes it',
207 async () => {
208 const result = await rig.run(
209 inDevDirectory(
210 `composio dev playground-execute GITHUB_GET_THE_AUTHENTICATED_USER -d '{}'`
211 )
212 );
213 const { error } = stdoutJson(result, ExecutionResult);
214 expect(error).toContain('composio dev connected-accounts link github');
215 expect(error).not.toContain('composio link github');
216 },
217 TEST_TIMEOUT_MS
218 );
219 });
220
221 describe('the developer project of the directory', () => {
222 const OTHER_DIRECTORY = '/work/other-app';
223 const listInOtherDirectory = async () => {
224 await rig.journey.writeFile(
225 `${OTHER_DIRECTORY}/.composio/project.json`,
226 JSON.stringify({
227 org_id: ORG.id,
228 project_id: OTHER_DEVELOPER_PROJECT.nanoId,
229 project_name: OTHER_DEVELOPER_PROJECT.name,
230 })
231 );
232 const result = await rig.run(`cd ${OTHER_DIRECTORY} && composio dev auth-configs list`);
233 return { result, listing: result.requests.find(entry => entry.route === 'authConfigs.list') };
234 };
235
236 it(
237 '`dev auth-configs list` is scoped to the project the directory is set up for',
238 async () => {
239 const { result, listing } = await listInOtherDirectory();
240 expect(result.exitCode).toBe(0);
241 expect(listing).toMatchObject({
242 project: OTHER_DEVELOPER_PROJECT.label,
243 projectFrom: 'scope-headers',
244 });
245 },
246 TEST_TIMEOUT_MS
247 );
248 });
249
250 describe('a command from the wrong tree', () => {
251 const cases = [
252 {
253 command: `composio dev execute SLACK_SEND_MESSAGE -d ${SLACK_ARGUMENTS}`,
254 message: 'Unknown subcommand "execute" for "composio dev"',
255 listed: 'playground-execute',
256 },
257 {
258 command: 'composio dev link slack',
259 message: 'Unknown subcommand "link" for "composio dev"',
260 listed: 'connected-accounts',
261 },
262 {
263 command: `composio playground-execute SLACK_SEND_MESSAGE -d ${SLACK_ARGUMENTS}`,
264 message: 'Unknown subcommand "playground-execute" for "composio"',
265 listed: 'execute',
266 },
267 {
268 command: 'composio auth-configs list',
269 message: 'Unknown subcommand "auth-configs" for "composio"',
270 listed: 'dev',
271 },
272 {
273 command: `${EXECUTE_SLACK} --user-id alice`,
274 message: 'Unrecognized flag: --user-id in command composio execute',
275 listed: '--account',
276 },
277 ] as const;
278
279 for (const { command, message, listed } of cases) {
280 it(
281 `\`${command.split(' -d ')[0]}\` fails before any request, naming what was wrong`,
282 async () => {
283 const result = await rig.run(command);
284 expect(result.exitCode).toBe(1);
285 expect(result.stdout).toBe('');
286 const stderr = clean(result.stderr);
287 expect(stderr).toContain(message);
288 // The help printed with the error is what lets an agent find the real command.
289 expect(stderr).toMatch(helpRow(listed));
290 expect(commandRequests(result)).toEqual([]);
291 },
292 TEST_TIMEOUT_MS
293 );
294 }
295
296 // The two tests below pin what the CLI does today, not what it should do.
297 // Nothing in the repo says what a wrong-tree command should answer, so they
298 // are listed as known gaps in the README instead of as expected failures.
299 it(
300 'today: `composio dev link --help` prints the `dev` help and exits 0, as if the command existed',
301 async () => {
302 const result = await rig.run('composio dev link --help');
303 expect(result.exitCode).toBe(0);
304 expect(`${result.stdout}${clean(result.stderr)}`).toContain('connected-accounts');
305 },
306 TEST_TIMEOUT_MS
307 );
308
309 it(
310 'today: `composio dev link` lists `connected-accounts` but never names `connected-accounts link`',
311 async () => {
312 const result = await rig.run('composio dev link slack');
313 expect(clean(result.stderr)).not.toContain('composio dev connected-accounts link');
314 },
315 TEST_TIMEOUT_MS
316 );
317 });
318
319 describe('a help page teaches its own tree', () => {
320 // One `link` definition serves both trees, so its examples must follow the page they are on.
321 const examplesOf = (help: string) =>
322 (help.split('EXAMPLES')[1] ?? '')
323 .split('\n')
324 .map(line => line.trim())
325 .filter(line => line.startsWith('composio '));
326
327 for (const page of ['composio link', 'composio dev connected-accounts link']) {
328 it(
329 `\`${page} --help\` shows examples of \`${page}\` only`,
330 async () => {
331 const result = await rig.run(`${page} --help`);
332 expect(result.exitCode).toBe(0);
333 const examples = examplesOf(clean(`${result.stdout}${result.stderr}`));
334 expect(examples.length).toBeGreaterThan(0);
335 expect(examples.filter(example => !example.startsWith(`${page} `))).toEqual([]);
336 expect(commandRequests(result)).toEqual([]);
337 },
338 TEST_TIMEOUT_MS
339 );
340 }
341 });
342
343 // Last, so it covers every command above: links, auth configs, both executions.
344 it(
345 'never sends the project API key `dev init` minted',
346 async () => {
347 expect(rig.fake.world.mintedProjectKeys).toHaveLength(1);
348 const classes = new Set(
349 rig.fake.journal
350 .entries()
351 .filter(entry => entry.role === 'backend' || entry.role === 'dashboard')
352 .map(entry => entry.keyClass)
353 );
354 expect([...classes]).toEqual(['user']);
355 },
356 TEST_TIMEOUT_MS
357 );
358 });
359
360 describe('identity: a missing acting user', () => {
361 let rig: CliRig;
362
363 beforeAll(async () => {
364 rig = await startCliRig({ label: 'acting-user' });
365 await rig.login();
366 }, RIG_TIMEOUT_MS);
367
368 afterAll(async () => {
369 await writeDebugLog(rig, import.meta.dir, 'acting-user');
370 await rig.stop();
371 });
372
373 it(
374 'root tree: an empty consumer user id fails with the consumer message',
375 async () => {
376 const disarm = rig.fake.faults.inject({
377 route: '_internal.org.consumer.project.resolve',
378 fault: {
379 kind: 'schema-drift',
380 transform: body => ({ ...(body as Record<string, unknown>), consumer_user_id: '' }),
381 },
382 });
383 const result = await rig.run(EXECUTE_SLACK);
384 disarm();
385
386 expect(result.exitCode).toBe(1);
387 expect(result.stdout).toBe('');
388 expect(clean(result.stderr)).toBe(
389 'Missing consumer user id. Run `composio login` and try again.'
390 );
391 expect(routesOf(result)).not.toContain('dashboard.execute');
392 },
393 TEST_TIMEOUT_MS
394 );
395
396 it(
397 'dev tree: no developer project for the directory fails with the dev message',
398 async () => {
399 const result = await rig.run(PLAYGROUND_EXECUTE_SLACK);
400 expect(result.exitCode).toBe(1);
401 expect(result.stdout).toBe('');
402 expect(clean(result.stderr)).toBe(
403 'No developer project configured for this directory. Run `composio dev init`.'
404 );
405 expect(commandRequests(result)).toEqual([]);
406 },
407 TEST_TIMEOUT_MS
408 );
409
410 it(
411 'dev tree: `--project-name` without any test user fails with the dev message',
412 async () => {
413 await rig.run(
414 `sed -i 's/"test_user_id":"[^"]*"/"test_user_id":null/' ~/.composio/user_data.json`
415 );
416 const result = await rig.run(
417 `${PLAYGROUND_EXECUTE_SLACK} --project-name ${DEVELOPER_PROJECT.name}`
418 );
419 expect(result.exitCode).toBe(1);
420 expect(result.stdout).toBe('');
421 expect(clean(result.stderr)).toBe(
422 'Missing user id. Provide --user-id, or run `composio login` to store a global test user id.'
423 );
424 expect(routesOf(result)).not.toContain('toolRouter.session.execute');
425 },
426 TEST_TIMEOUT_MS
427 );
428 });