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 86 lines 2.68 KB
Raw
1 import { afterEach, vi } from 'vitest';
2 import { describe, expect, it } from '@effect/vitest';
3 import { Effect } from 'effect';
4 import {
5 AgentRequestError,
6 AgentResponseDecodeError,
7 fetchAgentInbox,
8 normalizeAgentIdentity,
9 } from 'src/services/agents';
10
11 describe('agent service response boundaries', () => {
12 afterEach(() => {
13 vi.restoreAllMocks();
14 });
15
16 it('normalizes known keys while preserving forward-compatible fields', () => {
17 const identity = normalizeAgentIdentity({
18 agent_key: 'agent_test_key',
19 status: 'READY',
20 future_field: { enabled: true },
21 composio: {
22 user_api_key: 'uak_test',
23 org_id: 'org_test',
24 future_credential_metadata: 'preserved',
25 },
26 });
27
28 expect(identity.composio_agent_key).toBe('agent_test_key');
29 expect(identity.future_field).toEqual({ enabled: true });
30 expect(identity.composio?.future_credential_metadata).toBe('preserved');
31 });
32
33 it.effect('decodes inbox responses instead of asserting their shape', () =>
34 Effect.gen(function* () {
35 vi.spyOn(globalThis, 'fetch').mockResolvedValue(
36 new Response(
37 JSON.stringify({
38 count: 1,
39 messages: [{ subject: 'Hello', future_field: 'preserved' }],
40 next_cursor: 'cursor_test',
41 }),
42 { status: 200 }
43 )
44 );
45
46 const inbox = yield* fetchAgentInbox({ agentKey: 'agent_test_key' });
47
48 expect(inbox.count).toBe(1);
49 expect(inbox.messages?.[0]?.subject).toBe('Hello');
50 expect(inbox.messages?.[0]?.future_field).toBe('preserved');
51 expect(inbox.next_cursor).toBe('cursor_test');
52 })
53 );
54
55 it.effect('fails with structured context when an inbox response is invalid', () =>
56 Effect.gen(function* () {
57 vi.spyOn(globalThis, 'fetch').mockResolvedValue(
58 new Response(JSON.stringify({ messages: [{ subject: 42 }] }), { status: 200 })
59 );
60
61 const error = yield* fetchAgentInbox({ agentKey: 'agent_test_key', limit: 5 }).pipe(
62 Effect.flip
63 );
64
65 expect(error).toBeInstanceOf(AgentResponseDecodeError);
66 expect(error.pathname).toBe('/api/mail?limit=5');
67 })
68 );
69
70 it.effect('preserves an agent API error message and HTTP status', () =>
71 Effect.gen(function* () {
72 vi.spyOn(globalThis, 'fetch').mockResolvedValue(
73 new Response(JSON.stringify({ message: 'Agent key expired' }), { status: 401 })
74 );
75
76 const error = yield* fetchAgentInbox({ agentKey: 'expired_key' }).pipe(Effect.flip);
77
78 expect(error).toBeInstanceOf(AgentRequestError);
79 expect(error).toMatchObject({
80 message: 'Agent key expired',
81 status: 401,
82 pathname: '/api/mail?limit=50',
83 });
84 })
85 );
86 });