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 117 lines 4.22 KB
Raw
1 import { describe, expect, it } from '@effect/vitest';
2 import { Effect } from 'effect';
3 import { afterEach, vi } from 'vitest';
4 import { runAgentClaim } from 'src/commands/agent/claim/agent.claim.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 describe('runAgentClaim', () => {
12 afterEach(() => {
13 vi.restoreAllMocks();
14 });
15
16 it.effect(
17 '[Given] a stored agent key [Then] posts the invite and prints what the API returned',
18 () => {
19 const { layer, outputs } = AgentHandlerTest();
20 return Effect.gen(function* () {
21 yield* writeStoredAgentIdentity(makeAgentIdentity());
22 const fetchSpy = vi.spyOn(globalThis, 'fetch').mockImplementation(async () =>
23 agentsJsonResponse({
24 status: 'pending',
25 email: 'Boss@Example.com',
26 org_id: 'org_agent',
27 invite_code: 'inv_123',
28 })
29 );
30
31 yield* runAgentClaim({ email: 'boss@example.com' });
32
33 expect(fetchSpy).toHaveBeenCalledExactlyOnceWith(
34 'https://agents.composio.dev/api/claim',
35 expect.objectContaining({
36 method: 'POST',
37 body: JSON.stringify({ email: 'boss@example.com' }),
38 headers: expect.objectContaining({ Authorization: 'Bearer cak_test_agent' }),
39 })
40 );
41 expect(yield* MockConsole.getLines({ stripAnsi: true })).toEqual([
42 'Sent Composio org claim invite to Boss@Example.com.',
43 ]);
44 expect(outputs).toEqual([
45 {
46 data: JSON.stringify({
47 status: 'pending',
48 email: 'Boss@Example.com',
49 org_id: 'org_agent',
50 invite_code: 'inv_123',
51 }),
52 force: false,
53 },
54 ]);
55 }).pipe(Effect.provide(layer));
56 }
57 );
58
59 it.effect('[Given] an empty API response [Then] falls back to the requested email', () => {
60 const { layer, outputs } = AgentHandlerTest();
61 return Effect.gen(function* () {
62 yield* writeStoredAgentIdentity(makeAgentIdentity());
63 vi.spyOn(globalThis, 'fetch').mockImplementation(async () => agentsJsonResponse({}));
64
65 yield* runAgentClaim({ email: 'boss@example.com' });
66
67 expect(yield* MockConsole.getLines({ stripAnsi: true })).toEqual([
68 'Sent Composio org claim invite to boss@example.com.',
69 ]);
70 expect(outputs.map(output => output.data)).toEqual([
71 JSON.stringify({
72 status: 'invited',
73 email: 'boss@example.com',
74 org_id: null,
75 invite_code: null,
76 }),
77 ]);
78 }).pipe(Effect.provide(layer));
79 });
80
81 it.effect('[Given] no stored identity [Then] warns, exits 1, and sends no invite', () => {
82 const { layer, outputs } = AgentHandlerTest();
83 return Effect.gen(function* () {
84 const fetchSpy = vi.spyOn(globalThis, 'fetch');
85
86 const exit = yield* Effect.exit(runAgentClaim({ email: 'boss@example.com' }));
87
88 expect(yield* MockConsole.getLines({ stream: 'stderr', stripAnsi: true })).toEqual([
89 'No Composio agent identity is saved on this machine.',
90 expect.stringMatching(/^Next steps:\n/),
91 ]);
92 expect(exitCodeOf(exit)).toBe(1);
93 expect(fetchSpy).not.toHaveBeenCalled();
94 expect(outputs).toEqual([]);
95 }).pipe(Effect.provide(layer));
96 });
97
98 it.effect('[Given] the agents API refuses the invite [Then] fails with its message', () => {
99 const { layer, outputs } = AgentHandlerTest();
100 return Effect.gen(function* () {
101 yield* writeStoredAgentIdentity(makeAgentIdentity());
102 vi.spyOn(globalThis, 'fetch').mockImplementation(async () =>
103 agentsJsonResponse({ message: 'Org already claimed' }, 409)
104 );
105
106 const error = yield* Effect.flip(runAgentClaim({ email: 'boss@example.com' }));
107
108 expect(error).toMatchObject({
109 _tag: 'services/AgentRequestError',
110 message: 'Org already claimed',
111 pathname: '/api/claim',
112 status: 409,
113 });
114 expect(outputs).toEqual([]);
115 }).pipe(Effect.provide(layer));
116 });
117 });