1
+import { describe, expect, it } from '@effect/vitest';
2
+import { Effect, Option } from 'effect';
3
+import { afterEach, vi } from 'vitest';
4
+import {
5
+ runAgentSignup,
6
+ type AgentSignupParams,
7
+} from 'src/commands/agent/signup/agent.signup.handler';
8
+import { readStoredAgentIdentity, writeStoredAgentIdentity } from 'src/services/agents';
9
+import { MockConsole } from 'test/__utils__';
10
+import { AgentHandlerTest } from 'test/__utils__/layers/agents';
11
+import { agentsJsonResponse, makeAgentIdentity } from 'test/__utils__/models/agent';
12
+
13
+const params = (overrides: Partial<AgentSignupParams> = {}): AgentSignupParams => ({
14
+ noWait: false,
15
+ noLogin: false,
16
+ force: false,
17
+ ...overrides,
18
+});
19
+
20
+const pendingIdentity = makeAgentIdentity({
21
+ status: 'PENDING',
22
+ composio_agent_key: 'cak_pending',
23
+ composio: undefined,
24
+});
25
+
26
+/** The note body for the default READY identity. */
27
+const readyNote = [
28
+ '[Composio Agent] Type: agent',
29
+ 'Status: READY',
30
+ 'Email: test-agent@agent.composio.ai',
31
+ 'Slug: test-agent',
32
+ 'Current Org ID: org_agent',
33
+ 'Project ID: proj_agent',
34
+ 'Agent key: stored in ~/.composio/agent.json',
35
+].join('\n');
36
+
37
+const INTRO = '-- composio agent signup --';
38
+const INFO =
39
+ 'composio agent signup signs you up as a Composio agent. This flow is fully non-interactive and does not open a browser.';
40
+const OUTRO = '-- Agent setup complete. --';
41
+
42
+const summaryJson = (identity: ReturnType<typeof makeAgentIdentity>, loggedIn: boolean) =>
43
+ JSON.stringify({
44
+ account_type: 'agent',
45
+ status: identity.status,
46
+ slug: identity.slug,
47
+ email: identity.email,
48
+ org_id: identity.composio?.org_id ?? null,
49
+ project_id: identity.composio?.project_id ?? null,
50
+ member_id: identity.composio?.member_id ?? null,
51
+ claimed_by: null,
52
+ claimed_at: null,
53
+ logged_in: loggedIn,
54
+ });
55
+
56
+describe('runAgentSignup', () => {
57
+ afterEach(() => {
58
+ vi.restoreAllMocks();
59
+ process.exitCode = undefined;
60
+ });
61
+
62
+ it.effect(
63
+ '[Given] no stored identity [Then] signs up, waits, stores the identity, and logs in',
64
+ () => {
65
+ const { layer, outputs, logins } = AgentHandlerTest();
66
+ return Effect.gen(function* () {
67
+ const fetchSpy = vi
68
+ .spyOn(globalThis, 'fetch')
69
+ .mockImplementation(async () => agentsJsonResponse(makeAgentIdentity()));
70
+
71
+ yield* runAgentSignup(params());
72
+
73
+ expect(fetchSpy).toHaveBeenCalledExactlyOnceWith(
74
+ 'https://agents.composio.dev/api/signup',
75
+ expect.objectContaining({ method: 'POST', body: '{}' })
76
+ );
77
+ expect(Option.getOrThrow(yield* readStoredAgentIdentity)).toMatchObject({
78
+ composio_agent_key: 'cak_test_agent',
79
+ agent_key: 'cak_test_agent',
80
+ });
81
+ expect(logins).toEqual([{ apiKey: 'uak_agent', orgId: 'org_agent' }]);
82
+ expect(yield* MockConsole.getLines({ stripAnsi: true })).toEqual([
83
+ INTRO,
84
+ INFO,
85
+ readyNote,
86
+ 'Logged in with the Composio agent identity.',
87
+ OUTRO,
88
+ ]);
89
+ expect(outputs).toEqual([{ data: summaryJson(makeAgentIdentity(), true), force: false }]);
90
+ }).pipe(Effect.provide(layer));
91
+ }
92
+ );
93
+
94
+ it.effect(
95
+ '[Given] --no-wait [Then] starts the signup, reports it pending, and stays logged out',
96
+ () => {
97
+ const { layer, outputs, logins } = AgentHandlerTest();
98
+ return Effect.gen(function* () {
99
+ const fetchSpy = vi
100
+ .spyOn(globalThis, 'fetch')
101
+ .mockImplementation(async () => agentsJsonResponse(pendingIdentity));
102
+
103
+ yield* runAgentSignup(params({ noWait: true }));
104
+
105
+ expect(fetchSpy).toHaveBeenCalledExactlyOnceWith(
106
+ 'https://agents.composio.dev/api/signup?wait=0',
107
+ expect.objectContaining({ method: 'POST' })
108
+ );
109
+ expect(Option.getOrThrow(yield* readStoredAgentIdentity).composio_agent_key).toBe(
110
+ 'cak_pending'
111
+ );
112
+ expect(logins).toEqual([]);
113
+ expect(yield* MockConsole.getLines({ stripAnsi: true })).toEqual([
114
+ INTRO,
115
+ INFO,
116
+ [
117
+ '[Composio Agent] Type: agent',
118
+ 'Status: PENDING',
119
+ 'Email: test-agent@agent.composio.ai',
120
+ 'Slug: test-agent',
121
+ 'Current Org ID: pending',
122
+ 'Project ID: pending',
123
+ 'Agent key: stored in ~/.composio/agent.json',
124
+ ].join('\n'),
125
+ 'Signup is pending. Run `composio agent whoami` to poll status.',
126
+ OUTRO,
127
+ ]);
128
+ expect(outputs).toEqual([{ data: summaryJson(pendingIdentity, false), force: false }]);
129
+ }).pipe(Effect.provide(layer));
130
+ }
131
+ );
132
+
133
+ it.effect('[Given] --no-login [Then] signs up without logging in or a pending note', () => {
134
+ const { layer, outputs, logins } = AgentHandlerTest();
135
+ return Effect.gen(function* () {
136
+ vi.spyOn(globalThis, 'fetch').mockImplementation(async () =>
137
+ agentsJsonResponse(makeAgentIdentity())
138
+ );
139
+
140
+ yield* runAgentSignup(params({ noLogin: true }));
141
+
142
+ expect(logins).toEqual([]);
143
+ expect(yield* MockConsole.getLines({ stripAnsi: true })).toEqual([
144
+ INTRO,
145
+ INFO,
146
+ readyNote,
147
+ OUTRO,
148
+ ]);
149
+ expect(outputs).toEqual([{ data: summaryJson(makeAgentIdentity(), false), force: false }]);
150
+ }).pipe(Effect.provide(layer));
151
+ });
152
+
153
+ it.effect(
154
+ '[Given] a stored READY identity [Then] refreshes and reuses it without signing up',
155
+ () => {
156
+ const { layer, logins } = AgentHandlerTest();
157
+ return Effect.gen(function* () {
158
+ yield* writeStoredAgentIdentity(makeAgentIdentity());
159
+ const fetchSpy = vi
160
+ .spyOn(globalThis, 'fetch')
161
+ .mockImplementation(async () => agentsJsonResponse(makeAgentIdentity()));
162
+
163
+ yield* runAgentSignup(params());
164
+
165
+ expect(fetchSpy).toHaveBeenCalledExactlyOnceWith(
166
+ 'https://agents.composio.dev/api/whoami',
167
+ expect.objectContaining({
168
+ method: 'GET',
169
+ headers: expect.objectContaining({ Authorization: 'Bearer cak_test_agent' }),
170
+ })
171
+ );
172
+ expect(logins).toEqual([{ apiKey: 'uak_agent', orgId: 'org_agent' }]);
173
+ }).pipe(Effect.provide(layer));
174
+ }
175
+ );
176
+
177
+ it.effect('[Given] a stored identity and --force [Then] signs up a new agent', () => {
178
+ const { layer } = AgentHandlerTest();
179
+ return Effect.gen(function* () {
180
+ yield* writeStoredAgentIdentity(makeAgentIdentity({ composio_agent_key: 'cak_old' }));
181
+ const fetchSpy = vi
182
+ .spyOn(globalThis, 'fetch')
183
+ .mockImplementation(async () => agentsJsonResponse(makeAgentIdentity()));
184
+
185
+ yield* runAgentSignup(params({ force: true, noLogin: true }));
186
+
187
+ expect(fetchSpy).toHaveBeenCalledExactlyOnceWith(
188
+ 'https://agents.composio.dev/api/signup',
189
+ expect.objectContaining({ method: 'POST' })
190
+ );
191
+ expect(Option.getOrThrow(yield* readStoredAgentIdentity).composio_agent_key).toBe(
192
+ 'cak_test_agent'
193
+ );
194
+ }).pipe(Effect.provide(layer));
195
+ });
196
+
197
+ it.effect('[Given] a human login [Then] warns, exits 1, and signs nothing up', () => {
198
+ const { layer, outputs, logins } = AgentHandlerTest({ apiKey: 'uak_human' });
199
+ return Effect.gen(function* () {
200
+ const fetchSpy = vi.spyOn(globalThis, 'fetch');
201
+
202
+ yield* runAgentSignup(params());
203
+
204
+ expect(yield* MockConsole.getLines({ stream: 'stderr', stripAnsi: true })).toEqual([
205
+ 'This CLI is currently signed in to Composio as a regular user, not an agent.',
206
+ ]);
207
+ expect(process.exitCode).toBe(1);
208
+ expect(fetchSpy).not.toHaveBeenCalled();
209
+ expect(logins).toEqual([]);
210
+ expect(outputs).toEqual([]);
211
+ }).pipe(Effect.provide(layer));
212
+ });
213
+
214
+ it.effect('[Given] the agents API refuses the signup [Then] fails and stores nothing', () => {
215
+ const { layer, outputs, logins } = AgentHandlerTest();
216
+ return Effect.gen(function* () {
217
+ vi.spyOn(globalThis, 'fetch').mockImplementation(async () =>
218
+ agentsJsonResponse({ message: 'Signup unavailable' }, 503)
219
+ );
220
+
221
+ const error = yield* Effect.flip(runAgentSignup(params()));
222
+
223
+ expect(error).toMatchObject({
224
+ _tag: 'services/AgentRequestError',
225
+ message: 'Signup unavailable',
226
+ pathname: '/api/signup',
227
+ status: 503,
228
+ });
229
+ expect(Option.isNone(yield* readStoredAgentIdentity)).toBe(true);
230
+ expect(logins).toEqual([]);
231
+ expect(outputs).toEqual([]);
232
+ }).pipe(Effect.provide(layer));
233
+ });
234
+});