@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 82 lines 3.4 KB
Raw
1 import { describe, expect, it } from '@effect/vitest';
2 import { Effect, Option } from 'effect';
3 import { afterEach, vi } from 'vitest';
4 import { runAgentLogin } from 'src/commands/agent/login/agent.login.handler';
5 import { readStoredAgentIdentity, writeStoredAgentIdentity } from 'src/services/agents';
6 import { MockConsole } from 'test/__utils__';
7 import { AgentHandlerTest } from 'test/__utils__/layers/agents';
8 import { agentsJsonResponse, makeAgentIdentity } from 'test/__utils__/models/agent';
9 import { exitCodeOf } from 'test/__utils__/exit-code';
10
11 describe('runAgentLogin', () => {
12 afterEach(() => {
13 vi.restoreAllMocks();
14 });
15
16 it.effect(
17 '[Given] a key the agents API knows [Then] stores the identity under that key and logs in',
18 () => {
19 const { layer, outputs, logins } = AgentHandlerTest();
20 return Effect.gen(function* () {
21 // The API answers without the key; the stored identity keeps the one the user passed.
22 const fetchSpy = vi
23 .spyOn(globalThis, 'fetch')
24 .mockImplementation(async () =>
25 agentsJsonResponse(makeAgentIdentity({ composio_agent_key: undefined }))
26 );
27
28 yield* runAgentLogin({ composioAgentKey: 'cak_existing' });
29
30 expect(fetchSpy).toHaveBeenCalledExactlyOnceWith(
31 'https://agents.composio.dev/api/whoami',
32 expect.objectContaining({
33 headers: expect.objectContaining({ Authorization: 'Bearer cak_existing' }),
34 })
35 );
36 expect(Option.getOrThrow(yield* readStoredAgentIdentity)).toMatchObject({
37 composio_agent_key: 'cak_existing',
38 agent_key: 'cak_existing',
39 });
40 expect(logins).toEqual([{ apiKey: 'uak_agent', orgId: 'org_agent' }]);
41 expect(yield* MockConsole.getLines({ stripAnsi: true })).toEqual([
42 'Logged in as Composio agent test-agent@agent.composio.ai',
43 ]);
44 expect(outputs.map(output => JSON.parse(output.data))).toEqual([
45 expect.objectContaining({ account_type: 'agent', logged_in: true }),
46 ]);
47 }).pipe(Effect.provide(layer));
48 }
49 );
50
51 it.effect('[Given] the CLI is logged in as this agent [Then] logs in again', () => {
52 const { layer, logins } = AgentHandlerTest({ apiKey: 'uak_agent' });
53 return Effect.gen(function* () {
54 yield* writeStoredAgentIdentity(makeAgentIdentity());
55 vi.spyOn(globalThis, 'fetch').mockImplementation(async () =>
56 agentsJsonResponse(makeAgentIdentity())
57 );
58
59 yield* runAgentLogin({ composioAgentKey: 'cak_test_agent' });
60
61 expect(logins).toEqual([{ apiKey: 'uak_agent', orgId: 'org_agent' }]);
62 }).pipe(Effect.provide(layer));
63 });
64
65 it.effect('[Given] a human login [Then] warns, exits 1, and looks nothing up', () => {
66 const { layer, outputs, logins } = AgentHandlerTest({ apiKey: 'uak_human' });
67 return Effect.gen(function* () {
68 const fetchSpy = vi.spyOn(globalThis, 'fetch');
69
70 const exit = yield* Effect.exit(runAgentLogin({ composioAgentKey: 'cak_existing' }));
71
72 expect(yield* MockConsole.getLines({ stream: 'stderr', stripAnsi: true })).toEqual([
73 'This CLI is currently signed in to Composio as a regular user, not an agent.',
74 expect.stringMatching(/^Next steps:\n/),
75 ]);
76 expect(exitCodeOf(exit)).toBe(1);
77 expect(fetchSpy).not.toHaveBeenCalled();
78 expect(logins).toEqual([]);
79 expect(outputs).toEqual([]);
80 }).pipe(Effect.provide(layer));
81 });
82 });