@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 137 lines 4.71 KB
Raw
1 import { describe, expect, layer } from '@effect/vitest';
2 import * as FileSystem from 'effect/FileSystem';
3 import { Effect, Option } from 'effect';
4 import path from 'node:path';
5 import { afterEach, vi } from 'vitest';
6 import * as constants from 'src/constants';
7 import { setupCacheDir } from 'src/effects/setup-cache-dir';
8 import type { AgentIdentity } from 'src/services/agents';
9 import { ComposioUserContext } from 'src/services/user-context';
10 import { cli, MockConsole, TestLive } from 'test/__utils__';
11 import { exitCodeOf } from 'test/__utils__/exit-code';
12
13 const readyIdentity: AgentIdentity = {
14 status: 'READY',
15 slug: 'test-agent',
16 email: 'test-agent@agent.composio.ai',
17 composio_agent_key: 'cak_test_agent',
18 composio: {
19 member_id: 'mem_agent',
20 org_id: 'org_agent',
21 project_id: 'proj_agent',
22 user_api_key: 'uak_agent',
23 },
24 };
25
26 const pendingIdentity: AgentIdentity = {
27 ...readyIdentity,
28 status: 'PENDING',
29 composio: undefined,
30 };
31
32 /** Answers every agents API request with `body`. */
33 const stubAgentsApi = (body: AgentIdentity) =>
34 vi.spyOn(globalThis, 'fetch').mockImplementation(
35 async () =>
36 new Response(JSON.stringify(body), {
37 status: 200,
38 headers: { 'Content-Type': 'application/json' },
39 })
40 );
41
42 describe('CLI: composio signup', () => {
43 afterEach(() => {
44 vi.restoreAllMocks();
45 });
46
47 layer(TestLive())('[Given] no login', it => {
48 it.effect('[Then] signs up an agent, waits for it, and logs the CLI in', () =>
49 Effect.gen(function* () {
50 const fetchSpy = stubAgentsApi(readyIdentity);
51
52 yield* cli(['signup']);
53
54 expect(fetchSpy).toHaveBeenCalledExactlyOnceWith(
55 expect.stringMatching(/\/api\/signup$/),
56 expect.objectContaining({ method: 'POST' })
57 );
58 const stderr = yield* MockConsole.getLines({ stream: 'stderr', stripAnsi: true });
59 expect(stderr).toContain('-- composio signup --');
60 expect(stderr).toContain(
61 'composio signup signs you up as a Composio agent. This flow is fully non-interactive and does not open a browser.'
62 );
63 const stdout = yield* MockConsole.getLines({ stream: 'stdout' });
64 expect(JSON.parse(stdout.at(-1) ?? '')).toMatchObject({
65 status: 'READY',
66 org_id: 'org_agent',
67 logged_in: true,
68 });
69
70 const ctx = yield* ComposioUserContext;
71 expect(Option.getOrUndefined(ctx.data.apiKey)).toBe('uak_agent');
72 const fs = yield* FileSystem.FileSystem;
73 const cacheDir = yield* setupCacheDir;
74 const userConfig = yield* fs.readFileString(
75 path.join(cacheDir, constants.USER_CONFIG_FILE_NAME),
76 'utf8'
77 );
78 expect(JSON.parse(userConfig)).toMatchObject({ api_key: 'uak_agent', org_id: 'org_agent' });
79 })
80 );
81 });
82
83 layer(TestLive())('[Given] --no-wait -f', it => {
84 it.effect('[Then] starts a fresh signup without waiting and stays logged out', () =>
85 Effect.gen(function* () {
86 const fetchSpy = stubAgentsApi(pendingIdentity);
87
88 yield* cli(['signup', '--no-wait', '-f']);
89
90 expect(fetchSpy).toHaveBeenCalledExactlyOnceWith(
91 expect.stringMatching(/\/api\/signup\?wait=0$/),
92 expect.objectContaining({ method: 'POST' })
93 );
94 const stdout = yield* MockConsole.getLines({ stream: 'stdout' });
95 expect(JSON.parse(stdout.at(-1) ?? '')).toMatchObject({
96 status: 'PENDING',
97 logged_in: false,
98 });
99 const ctx = yield* ComposioUserContext;
100 expect(Option.isNone(ctx.data.apiKey)).toBe(true);
101 })
102 );
103 });
104
105 layer(TestLive())('[Given] --no-login', it => {
106 it.effect('[Then] signs up a ready agent but leaves the CLI logged out', () =>
107 Effect.gen(function* () {
108 stubAgentsApi(readyIdentity);
109
110 yield* cli(['signup', '--no-login']);
111
112 const stdout = yield* MockConsole.getLines({ stream: 'stdout' });
113 expect(JSON.parse(stdout.at(-1) ?? '')).toMatchObject({
114 status: 'READY',
115 logged_in: false,
116 });
117 const ctx = yield* ComposioUserContext;
118 expect(Option.isNone(ctx.data.apiKey)).toBe(true);
119 })
120 );
121 });
122
123 layer(TestLive({ fixture: 'user-config-example' }))('[Given] a human login', it => {
124 it.effect('[Then] warns, exits 1, and signs nothing up', () =>
125 Effect.gen(function* () {
126 const fetchSpy = vi.spyOn(globalThis, 'fetch');
127
128 const exit = yield* Effect.exit(cli(['signup']));
129
130 const output = (yield* MockConsole.getLines({ stripAnsi: true })).join('\n');
131 expect(output).toContain('currently signed in to Composio as a regular user');
132 expect(exitCodeOf(exit)).toBe(1);
133 expect(fetchSpy).not.toHaveBeenCalled();
134 })
135 );
136 });
137 });