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 195 lines 7.03 KB
Raw
1 import { assert, describe, expect, layer } from '@effect/vitest';
2 import { isUsageFailure } from 'src/commands/errors';
3 import * as FileSystem from 'effect/FileSystem';
4 import { ConfigProvider, Effect, Option } from 'effect';
5 import path from 'node:path';
6 import * as constants from 'src/constants';
7 import { setupCacheDir } from 'src/effects/setup-cache-dir';
8 import { UserDataWithDefaults } from 'src/models/user-data';
9 import { writeStoredAgentIdentity } from 'src/services/agents';
10 import { ComposioUserContext } from 'src/services/user-context';
11 import { cli, TestLive, MockConsole } from 'test/__utils__';
12 import {
13 terminalUIWithConfirm,
14 terminalUIWithoutPrompt,
15 } from 'test/__utils__/services/terminal-ui-test';
16
17 /** Whether `~/.composio/agent.json` still holds the agent identity. */
18 const agentConfigExists = Effect.gen(function* () {
19 const fs = yield* FileSystem.FileSystem;
20 const cacheDir = yield* setupCacheDir;
21 return yield* fs.exists(path.join(cacheDir, 'agent.json'));
22 });
23
24 const setupLoggedInAgent = Effect.gen(function* () {
25 const ctx = yield* ComposioUserContext;
26 yield* writeStoredAgentIdentity({
27 status: 'READY',
28 slug: 'test-agent',
29 email: 'test-agent@agent.composio.ai',
30 composio_agent_key: 'cak_test_agent',
31 composio: {
32 org_id: 'org_agent',
33 project_id: 'proj_agent',
34 user_api_key: 'uak_agent',
35 },
36 });
37 yield* ctx.login('uak_agent', 'org_agent');
38 return ctx;
39 });
40
41 describe('CLI: composio logout', () => {
42 describe('[When] logged out', () => {
43 layer(TestLive())(it => {
44 it.effect('[Then] it is idempotent', () =>
45 Effect.gen(function* () {
46 const ctx = yield* ComposioUserContext;
47
48 expect(ctx.isLoggedIn()).toBeFalsy();
49 const expectedUserData = UserDataWithDefaults.make({
50 apiKey: Option.none(),
51 baseURL: 'https://backend.composio.dev',
52 webURL: 'https://dashboard.composio.dev/',
53 orgId: Option.none(),
54 projectId: Option.none(),
55 testUserId: Option.none(),
56 });
57
58 expect(ctx.data).toMatchObject(expectedUserData);
59
60 const args = ['logout'];
61 yield* cli(args);
62
63 const lines = yield* MockConsole.getLines();
64 const output = lines.join('\n');
65 expect(output).toMatchInlineSnapshot(
66 `"You are not logged in yet. Please run \`composio login\`."`
67 );
68 expect(ctx.isLoggedIn()).toBeFalsy();
69 expect(ctx.data).toMatchObject(expectedUserData);
70 })
71 );
72 });
73 });
74
75 describe('[When] logged in', () => {
76 const testConfigProvider = ConfigProvider.fromEnv({
77 env: { COMPOSIO_USER_API_KEY: 'api_key_already_logged_in' },
78 });
79
80 layer(TestLive({ baseConfigProvider: testConfigProvider }))(it => {
81 it.effect('[Then] it persists user data', () =>
82 Effect.gen(function* () {
83 const ctx = yield* ComposioUserContext;
84 expect(ctx.isLoggedIn()).toBeTruthy();
85 expect(ctx.data).toMatchObject(
86 UserDataWithDefaults.make({
87 apiKey: Option.some('api_key_already_logged_in'),
88 baseURL: 'https://backend.composio.dev',
89 webURL: 'https://dashboard.composio.dev/',
90 orgId: Option.none(),
91 projectId: Option.none(),
92 testUserId: Option.none(),
93 })
94 );
95
96 const args = ['logout'];
97 yield* cli(args);
98
99 const lines = yield* MockConsole.getLines();
100 const output = lines.join('\n');
101 expect(output).toMatchInlineSnapshot(`"Logged out successfully."`);
102 expect(ctx.isLoggedIn()).toBeFalsy();
103 })
104 );
105 });
106 });
107
108 describe('[When] logged in as an agent', () => {
109 layer(TestLive({ terminalUI: terminalUIWithConfirm(false) }))(it => {
110 it.effect('[Then] cancelling keeps user and agent credentials', () =>
111 Effect.gen(function* () {
112 const ctx = yield* setupLoggedInAgent;
113
114 yield* cli(['logout']);
115
116 const fs = yield* FileSystem.FileSystem;
117 const cacheDir = yield* setupCacheDir;
118 const agentConfigExists = yield* fs.exists(path.join(cacheDir, 'agent.json'));
119 const output = (yield* MockConsole.getLines({ stripAnsi: true })).join('\n');
120
121 expect(output).toContain('Agent Logout Warning');
122 expect(output).toContain('composio_agent_key');
123 expect(output).toContain('Agent logout cancelled');
124 expect(ctx.isLoggedIn()).toBeTruthy();
125 expect(agentConfigExists).toBe(true);
126 })
127 );
128 });
129
130 layer(TestLive())(it => {
131 it.effect('[Then] without a terminal and --yes it fails and removes nothing', () =>
132 Effect.gen(function* () {
133 const ctx = yield* setupLoggedInAgent;
134
135 const failure = yield* cli(['logout']).pipe(Effect.flip);
136
137 expect(failure).toMatchObject({
138 _tag: 'commands/CommandInputError',
139 command: 'logout',
140 option: '--yes',
141 message:
142 '`composio logout` needs --yes when not running interactively: it removes the stored Composio agent key.',
143 });
144 // The invocation prints the message on stderr, and nothing else.
145 assert(isUsageFailure(failure));
146 expect(yield* MockConsole.getLines()).toEqual([failure.message]);
147 expect(ctx.isLoggedIn()).toBeTruthy();
148 expect(yield* agentConfigExists).toBe(true);
149 })
150 );
151 });
152
153 layer(TestLive({ terminalUI: terminalUIWithoutPrompt }))(it => {
154 it.effect.each([['--yes'], ['-y'], ['-f']])(
155 '[Then] %s on a terminal logs out without asking',
156 ([flag]) =>
157 Effect.gen(function* () {
158 const ctx = yield* setupLoggedInAgent;
159
160 yield* cli(['logout', flag]);
161
162 const output = (yield* MockConsole.getLines({ stripAnsi: true })).join('\n');
163 expect(output).not.toContain('Agent Logout Warning');
164 expect(output).toContain('removed stored Composio agent key');
165 expect(ctx.isLoggedIn()).toBeFalsy();
166 expect(yield* agentConfigExists).toBe(false);
167 })
168 );
169 });
170
171 layer(TestLive())(it => {
172 it.effect('[Then] --force removes user and agent credentials', () =>
173 Effect.gen(function* () {
174 const ctx = yield* setupLoggedInAgent;
175
176 yield* cli(['logout', '--force']);
177
178 const fs = yield* FileSystem.FileSystem;
179 const cacheDir = yield* setupCacheDir;
180 const agentConfigExists = yield* fs.exists(path.join(cacheDir, 'agent.json'));
181 const userConfigRaw = yield* fs.readFileString(
182 path.join(cacheDir, constants.USER_CONFIG_FILE_NAME),
183 'utf8'
184 );
185 const output = (yield* MockConsole.getLines({ stripAnsi: true })).join('\n');
186
187 expect(output).toContain('removed stored Composio agent key');
188 expect(ctx.isLoggedIn()).toBeFalsy();
189 expect(agentConfigExists).toBe(false);
190 expect(JSON.parse(userConfigRaw)).toHaveProperty('api_key', null);
191 })
192 );
193 });
194 });
195 });