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 142 lines 5.18 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 { setupCacheDir } from 'src/effects/setup-cache-dir';
7 import { ComposioUserContext } from 'src/services/user-context';
8 import { cli, MockConsole, TestLive } from 'test/__utils__';
9 import { exitCodeOf } from 'test/__utils__/exit-code';
10
11 const existingAgent = {
12 status: 'READY',
13 slug: 'test-agent',
14 email: 'test-agent@agent.composio.ai',
15 composio_agent_key: 'cak_existing',
16 agent_key: 'cak_existing',
17 composio: {
18 member_id: 'mem_agent',
19 org_id: 'org_agent',
20 project_id: 'proj_agent',
21 user_api_key: 'uak_agent',
22 },
23 };
24
25 const jsonResponse = (body: unknown, status = 200) =>
26 new Response(JSON.stringify(body), {
27 status,
28 headers: { 'Content-Type': 'application/json' },
29 });
30
31 const agentConfigPath = Effect.map(setupCacheDir, cacheDir => path.join(cacheDir, 'agent.json'));
32
33 describe('CLI: composio agent login', () => {
34 afterEach(() => {
35 vi.restoreAllMocks();
36 });
37
38 layer(TestLive())(it => {
39 it.effect(
40 '[Given] a key the agents API knows [Then] stores the identity, logs in, and prints its summary',
41 () =>
42 Effect.gen(function* () {
43 const fetchSpy = vi
44 .spyOn(globalThis, 'fetch')
45 .mockImplementation(async () => jsonResponse(existingAgent));
46
47 yield* cli(['agent', 'login', 'cak_existing']);
48
49 expect(fetchSpy).toHaveBeenCalledExactlyOnceWith(
50 expect.stringContaining('/api/whoami'),
51 expect.objectContaining({
52 method: 'GET',
53 headers: expect.objectContaining({ Authorization: 'Bearer cak_existing' }),
54 })
55 );
56 const ctx = yield* ComposioUserContext;
57 expect(Option.getOrUndefined(ctx.data.apiKey)).toBe('uak_agent');
58 expect(Option.getOrUndefined(ctx.data.orgId)).toBe('org_agent');
59
60 const fs = yield* FileSystem.FileSystem;
61 const agentConfigRaw = yield* fs.readFileString(yield* agentConfigPath, 'utf8');
62 expect(JSON.parse(agentConfigRaw)).toMatchObject({
63 composio_agent_key: 'cak_existing',
64 agent_key: 'cak_existing',
65 composio: { user_api_key: 'uak_agent' },
66 });
67
68 const lines = yield* MockConsole.getLines({ stripAnsi: true });
69 expect(lines).toContain('Logged in as Composio agent test-agent@agent.composio.ai');
70 expect(lines.at(-1)).toBe(
71 JSON.stringify({
72 account_type: 'agent',
73 status: 'READY',
74 slug: 'test-agent',
75 email: 'test-agent@agent.composio.ai',
76 org_id: 'org_agent',
77 project_id: 'proj_agent',
78 member_id: 'mem_agent',
79 claimed_by: null,
80 claimed_at: null,
81 logged_in: true,
82 })
83 );
84 })
85 );
86 });
87
88 layer(TestLive())(it => {
89 it.effect('[Given] a key the agents API rejects [Then] fails and stores nothing', () =>
90 Effect.gen(function* () {
91 vi.spyOn(globalThis, 'fetch').mockImplementation(async () =>
92 jsonResponse({ message: 'Invalid agent key' }, 401)
93 );
94
95 const error = yield* cli(['agent', 'login', 'cak_revoked']).pipe(Effect.flip);
96
97 expect(error).toMatchObject({ message: 'Invalid agent key', status: 401 });
98 const ctx = yield* ComposioUserContext;
99 expect(Option.getOrUndefined(ctx.data.apiKey)).toBeUndefined();
100 const fs = yield* FileSystem.FileSystem;
101 expect(yield* fs.exists(yield* agentConfigPath)).toBe(false);
102 })
103 );
104 });
105
106 layer(TestLive())(it => {
107 it.effect(
108 '[Given] an identity without credentials yet [Then] warns, exits 1, and does not log in',
109 () =>
110 Effect.gen(function* () {
111 vi.spyOn(globalThis, 'fetch').mockImplementation(async () =>
112 jsonResponse({ ...existingAgent, status: 'PENDING', composio: undefined })
113 );
114
115 const exit = yield* Effect.exit(cli(['agent', 'login', 'cak_existing']));
116
117 const output = (yield* MockConsole.getLines({ stripAnsi: true })).join('\n');
118 expect(output).toContain('Agent identity is not ready yet.');
119 expect(output).not.toContain('"logged_in":true');
120 expect(exitCodeOf(exit)).toBe(1);
121 const ctx = yield* ComposioUserContext;
122 expect(Option.getOrUndefined(ctx.data.apiKey)).toBeUndefined();
123 })
124 );
125 });
126
127 layer(TestLive({ fixture: 'user-config-example' }))(it => {
128 it.effect('[Given] the CLI is signed in as a human user [Then] shows a soft auth warning', () =>
129 Effect.gen(function* () {
130 const fetchSpy = vi.spyOn(globalThis, 'fetch');
131
132 const exit = yield* Effect.exit(cli(['agent', 'login', 'cak_existing']));
133 const output = (yield* MockConsole.getLines({ stripAnsi: true })).join('\n');
134
135 expect(output).toContain('currently signed in to Composio as a regular user');
136 expect(output).toContain('composio logout');
137 expect(exitCodeOf(exit)).toBe(1);
138 expect(fetchSpy).not.toHaveBeenCalled();
139 })
140 );
141 });
142 });