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 103 lines 3.71 KB
Raw
1 import { describe, expect, it } from '@effect/vitest';
2 import { Effect } from 'effect';
3 import { afterEach, vi } from 'vitest';
4 import { runAgentInbox } from 'src/commands/agent/inbox/agent.inbox.handler';
5 import { 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 const inbox = {
12 count: 1,
13 messages: [
14 {
15 id: 'msg_123',
16 from: 'person@example.com',
17 subject: 'Hello',
18 preview: 'Hi agent',
19 labels: ['inbox'],
20 },
21 ],
22 next_cursor: 'cursor_2',
23 };
24
25 describe('runAgentInbox', () => {
26 afterEach(() => {
27 vi.restoreAllMocks();
28 });
29
30 it.effect(
31 '[Given] a stored agent key [Then] fetches up to --limit messages and forces the JSON to stdout',
32 () => {
33 const { layer, outputs } = AgentHandlerTest();
34 return Effect.gen(function* () {
35 yield* writeStoredAgentIdentity(makeAgentIdentity());
36 const fetchSpy = vi
37 .spyOn(globalThis, 'fetch')
38 .mockImplementation(async () => agentsJsonResponse(inbox));
39
40 yield* runAgentInbox({ limit: 7 });
41
42 expect(fetchSpy).toHaveBeenCalledExactlyOnceWith(
43 'https://agents.composio.dev/api/mail?limit=7',
44 expect.objectContaining({
45 method: 'GET',
46 headers: expect.objectContaining({ Authorization: 'Bearer cak_test_agent' }),
47 })
48 );
49 // Fields the CLI does not model pass through unchanged.
50 expect(outputs).toEqual([{ data: JSON.stringify(inbox), force: true }]);
51 expect(yield* MockConsole.getLines()).toEqual([]);
52 }).pipe(Effect.provide(layer));
53 }
54 );
55
56 it.effect('[Given] a human login [Then] warns, exits 1, and reads no mail', () => {
57 const { layer, outputs } = AgentHandlerTest({ apiKey: 'uak_human' });
58 return Effect.gen(function* () {
59 yield* writeStoredAgentIdentity(makeAgentIdentity());
60 const fetchSpy = vi.spyOn(globalThis, 'fetch');
61
62 const exit = yield* Effect.exit(runAgentInbox({ limit: 50 }));
63
64 expect(yield* MockConsole.getLines({ stream: 'stderr', stripAnsi: true })).toEqual([
65 'This CLI is currently signed in to Composio as a regular user, not an agent.',
66 expect.stringMatching(/^Next steps:\n/),
67 ]);
68 expect(exitCodeOf(exit)).toBe(1);
69 expect(fetchSpy).not.toHaveBeenCalled();
70 expect(outputs).toEqual([]);
71 }).pipe(Effect.provide(layer));
72 });
73
74 it.effect('[Given] the logged-in key is the stored agent [Then] reads its inbox', () => {
75 const { layer, outputs } = AgentHandlerTest({ apiKey: 'uak_agent' });
76 return Effect.gen(function* () {
77 yield* writeStoredAgentIdentity(makeAgentIdentity());
78 vi.spyOn(globalThis, 'fetch').mockImplementation(async () => agentsJsonResponse(inbox));
79
80 yield* runAgentInbox({ limit: 50 });
81
82 expect(outputs).toEqual([{ data: JSON.stringify(inbox), force: true }]);
83 }).pipe(Effect.provide(layer));
84 });
85
86 it.effect('[Given] a response that is not an inbox [Then] fails to decode it', () => {
87 const { layer, outputs } = AgentHandlerTest();
88 return Effect.gen(function* () {
89 yield* writeStoredAgentIdentity(makeAgentIdentity());
90 vi.spyOn(globalThis, 'fetch').mockImplementation(async () =>
91 agentsJsonResponse({ count: 'one' })
92 );
93
94 const error = yield* Effect.flip(runAgentInbox({ limit: 50 }));
95
96 expect(error).toMatchObject({
97 _tag: 'services/AgentResponseDecodeError',
98 pathname: '/api/mail?limit=50',
99 });
100 expect(outputs).toEqual([]);
101 }).pipe(Effect.provide(layer));
102 });
103 });