@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 196 lines 6.89 KB
Raw
1 import { AuthenticationError, InternalServerError } from '@composio/client/effect';
2 import { assert, describe, expect, layer } from '@effect/vitest';
3 import { isUsageFailure } from 'src/commands/errors';
4 import { ConfigProvider, Effect } from 'effect';
5 import { cli, TestLive, MockConsole } from 'test/__utils__';
6 import { retryHint, TEMPORARY_FAILURE_EXIT_CODE } from 'src/effects/retry-advice';
7 import { exitCodeOf } from 'test/__utils__/exit-code';
8 import { makeSessionInfo } from 'test/__utils__/models/account';
9 import { redirectedTerminalUI } from 'test/__utils__/services/redirected-terminal-ui';
10
11 describe('CLI: composio whoami', () => {
12 layer(TestLive())('without a login', it => {
13 it.effect('[Given] no API key [Then] fails with NotLoggedInError and prints its message', () =>
14 Effect.gen(function* () {
15 const failure = yield* cli(['whoami']).pipe(Effect.flip);
16
17 expect(failure).toMatchObject({
18 _tag: 'commands/NotLoggedInError',
19 message: 'You are not logged in yet. Please run `composio login`.',
20 });
21 // The invocation prints the message on stderr, and nothing else.
22 assert(isUsageFailure(failure));
23 expect(yield* MockConsole.getLines()).toEqual([failure.message]);
24 })
25 );
26 });
27
28 const testConfigProvider = ConfigProvider.fromEnv({
29 env: { COMPOSIO_USER_API_KEY: 'api_key_from_test_config_provider' },
30 });
31
32 const readableSession = makeSessionInfo({
33 orgId: 'org_123',
34 orgName: 'Acme Org',
35 orgMemberId: 'om_123',
36 email: 'person@example.com',
37 name: 'Test Person',
38 });
39
40 layer(
41 TestLive({
42 baseConfigProvider: testConfigProvider,
43 accountData: { sessionInfo: readableSession },
44 })
45 )('with config override', it => {
46 it.effect('[Given] `COMPOSIO_USER_API_KEY` [Then] prints global user context JSON', () =>
47 Effect.gen(function* () {
48 const args = ['whoami'];
49 yield* cli(args);
50
51 const lines = yield* MockConsole.getLines();
52 const output = lines.join('\n');
53 expect(output).not.toContain(`api_key_from_test_config_provider`);
54 expect(output).not.toContain(`global_user_api_key`);
55 expect(output).toContain(`"email":"person@example.com"`);
56 expect(output).not.toContain(`"test_user_id"`);
57 })
58 );
59 });
60
61 layer(
62 TestLive({ fixture: 'user-config-example', accountData: { sessionInfo: readableSession } })
63 )('with fixture', it => {
64 it.effect('[Given] user_data.json in fixture [Then] prints global user context JSON', () =>
65 Effect.gen(function* () {
66 const args = ['whoami'];
67 yield* cli(args);
68
69 const lines = yield* MockConsole.getLines();
70 const output = lines.join('\n');
71 expect(output).not.toContain(`api_key_from_test_fixture`);
72 expect(output).not.toContain(`global_user_api_key`);
73 expect(output).toContain(`"email":"person@example.com"`);
74 expect(output).not.toContain(`"test_user_id"`);
75 })
76 );
77 });
78
79 // A 401 that is not the backend's `UserApiKey_Unauthorized`: the CLI cannot tell which
80 // credential it is about, so it names the login as the likely way out.
81 const rejectedKey = new AuthenticationError({
82 message: '401 Invalid API key',
83 method: 'GET',
84 url: 'https://mock.invalid/api/v3.1/auth/session/info',
85 status: 401,
86 requestId: undefined,
87 details: { message: 'Invalid API key', code: 401, slug: 'Auth_InvalidApiKey', status: 401 },
88 body: undefined,
89 });
90
91 layer(
92 TestLive({
93 fixture: 'user-config-example',
94 accountData: {
95 sessionInfo: () => {
96 throw rejectedKey;
97 },
98 },
99 })
100 )('with a stored key the backend rejects', it => {
101 it.effect('[Then] exits 1 with nothing on stdout and names `composio login` on stderr', () =>
102 Effect.gen(function* () {
103 const terminal = redirectedTerminalUI();
104 const exit = yield* Effect.exit(cli(['whoami']).pipe(Effect.provide(terminal.layer)));
105
106 expect(exitCodeOf(exit)).toBe(1);
107 expect(terminal.stdout()).toBe('');
108 expect(terminal.stderr()).toBe(
109 'Invalid API key\nIf your login expired, log in again, then retry:\n> composio logout && composio login\n'
110 );
111 })
112 );
113 });
114
115 // A backend that cannot answer: the command cannot vouch for the login, so it fails.
116 const serverError = new InternalServerError({
117 message: '500 Internal Server Error',
118 method: 'GET',
119 url: 'https://mock.invalid/api/v3.1/auth/session/info',
120 status: 500,
121 requestId: undefined,
122 details: { message: 'Internal Server Error', code: 500, slug: 'Internal', status: 500 },
123 body: undefined,
124 });
125
126 layer(
127 TestLive({
128 fixture: 'user-config-example',
129 accountData: {
130 sessionInfo: () => {
131 throw serverError;
132 },
133 },
134 })
135 )('when the backend fails to answer', it => {
136 it.effect('[Then] exits 75 with nothing on stdout and says it is safe to run again', () =>
137 Effect.gen(function* () {
138 const terminal = redirectedTerminalUI();
139 const exit = yield* Effect.exit(cli(['whoami']).pipe(Effect.provide(terminal.layer)));
140
141 expect(exitCodeOf(exit)).toBe(TEMPORARY_FAILURE_EXIT_CODE);
142 expect(terminal.stdout()).toBe('');
143 expect(terminal.stderr()).toContain('Internal Server Error');
144 expect(terminal.stderr()).toContain(retryHint('safe'));
145 })
146 );
147 });
148
149 // A session the CLI cannot fully read (org-level credentials have no project): the key
150 // works, so the fields print as unknown.
151 layer(
152 TestLive({
153 fixture: 'user-config-example',
154 accountData: { sessionInfo: { ...readableSession, project: null } },
155 })
156 )('with a session the CLI cannot fully read', it => {
157 it.effect('[Then] exits 0 and prints the fields as unknown', () =>
158 Effect.gen(function* () {
159 yield* cli(['whoami']);
160
161 const output = (yield* MockConsole.getLines()).join('\n');
162 expect(output).toContain(`"email":null`);
163 expect(output).toContain(`"current_org_name":null`);
164 })
165 );
166 });
167
168 layer(
169 TestLive({
170 baseConfigProvider: testConfigProvider,
171 accountData: {
172 sessionInfo: makeSessionInfo({
173 orgId: 'org_123',
174 orgName: 'Acme Org',
175 orgMemberId: 'om_123',
176 email: 'person@example.com',
177 name: 'Test Person',
178 }),
179 },
180 })
181 )('with session info', it => {
182 it.effect('[Given] session info is available [Then] prints email and org name', () =>
183 Effect.gen(function* () {
184 yield* cli(['whoami']);
185
186 const lines = yield* MockConsole.getLines();
187 const output = lines.join('\n');
188 expect(output).toContain(`"email":"person@example.com"`);
189 expect(output).toContain(`"current_org_name":"Acme Org"`);
190 expect(output).toContain('Current Org: Acme Org');
191 expect(output).not.toContain('Default Org');
192 expect(output).not.toContain('Test User ID');
193 })
194 );
195 });
196 });