1
+import { describe, expect, layer } from '@effect/vitest';
2
+import * as FileSystem from 'effect/FileSystem';
3
+import { Effect, Option } from 'effect';
4
+import path from 'node:path';
5
+import { afterEach, vi } from 'vitest';
6
+import * as constants from 'src/constants';
7
+import { setupCacheDir } from 'src/effects/setup-cache-dir';
8
+import type { AgentIdentity } from 'src/services/agents';
9
+import { ComposioUserContext } from 'src/services/user-context';
10
+import { cli, MockConsole, TestLive } from 'test/__utils__';
11
+
12
+const readyIdentity: AgentIdentity = {
13
+ status: 'READY',
14
+ slug: 'test-agent',
15
+ email: 'test-agent@agent.composio.ai',
16
+ composio_agent_key: 'cak_test_agent',
17
+ composio: {
18
+ member_id: 'mem_agent',
19
+ org_id: 'org_agent',
20
+ project_id: 'proj_agent',
21
+ user_api_key: 'uak_agent',
22
+ },
23
+};
24
+
25
+const pendingIdentity: AgentIdentity = {
26
+ ...readyIdentity,
27
+ status: 'PENDING',
28
+ composio: undefined,
29
+};
30
+
31
+/** Answers every agents API request with `body`. */
32
+const stubAgentsApi = (body: AgentIdentity) =>
33
+ vi.spyOn(globalThis, 'fetch').mockImplementation(
34
+ async () =>
35
+ new Response(JSON.stringify(body), {
36
+ status: 200,
37
+ headers: { 'Content-Type': 'application/json' },
38
+ })
39
+ );
40
+
41
+describe('CLI: composio signup', () => {
42
+ afterEach(() => {
43
+ vi.restoreAllMocks();
44
+ process.exitCode = undefined;
45
+ });
46
+
47
+ layer(TestLive())('[Given] no login', it => {
48
+ it.effect('[Then] signs up an agent, waits for it, and logs the CLI in', () =>
49
+ Effect.gen(function* () {
50
+ const fetchSpy = stubAgentsApi(readyIdentity);
51
+
52
+ yield* cli(['signup']);
53
+
54
+ expect(fetchSpy).toHaveBeenCalledExactlyOnceWith(
55
+ expect.stringMatching(/\/api\/signup$/),
56
+ expect.objectContaining({ method: 'POST' })
57
+ );
58
+ const stderr = yield* MockConsole.getLines({ stream: 'stderr', stripAnsi: true });
59
+ expect(stderr).toContain('-- composio signup --');
60
+ expect(stderr).toContain(
61
+ 'composio signup signs you up as a Composio agent. This flow is fully non-interactive and does not open a browser.'
62
+ );
63
+ const stdout = yield* MockConsole.getLines({ stream: 'stdout' });
64
+ expect(JSON.parse(stdout.at(-1) ?? '')).toMatchObject({
65
+ status: 'READY',
66
+ org_id: 'org_agent',
67
+ logged_in: true,
68
+ });
69
+
70
+ const ctx = yield* ComposioUserContext;
71
+ expect(Option.getOrUndefined(ctx.data.apiKey)).toBe('uak_agent');
72
+ const fs = yield* FileSystem.FileSystem;
73
+ const cacheDir = yield* setupCacheDir;
74
+ const userConfig = yield* fs.readFileString(
75
+ path.join(cacheDir, constants.USER_CONFIG_FILE_NAME),
76
+ 'utf8'
77
+ );
78
+ expect(JSON.parse(userConfig)).toMatchObject({ api_key: 'uak_agent', org_id: 'org_agent' });
79
+ })
80
+ );
81
+ });
82
+
83
+ layer(TestLive())('[Given] --no-wait -f', it => {
84
+ it.effect('[Then] starts a fresh signup without waiting and stays logged out', () =>
85
+ Effect.gen(function* () {
86
+ const fetchSpy = stubAgentsApi(pendingIdentity);
87
+
88
+ yield* cli(['signup', '--no-wait', '-f']);
89
+
90
+ expect(fetchSpy).toHaveBeenCalledExactlyOnceWith(
91
+ expect.stringMatching(/\/api\/signup\?wait=0$/),
92
+ expect.objectContaining({ method: 'POST' })
93
+ );
94
+ const stdout = yield* MockConsole.getLines({ stream: 'stdout' });
95
+ expect(JSON.parse(stdout.at(-1) ?? '')).toMatchObject({
96
+ status: 'PENDING',
97
+ logged_in: false,
98
+ });
99
+ const ctx = yield* ComposioUserContext;
100
+ expect(Option.isNone(ctx.data.apiKey)).toBe(true);
101
+ })
102
+ );
103
+ });
104
+
105
+ layer(TestLive())('[Given] --no-login', it => {
106
+ it.effect('[Then] signs up a ready agent but leaves the CLI logged out', () =>
107
+ Effect.gen(function* () {
108
+ stubAgentsApi(readyIdentity);
109
+
110
+ yield* cli(['signup', '--no-login']);
111
+
112
+ const stdout = yield* MockConsole.getLines({ stream: 'stdout' });
113
+ expect(JSON.parse(stdout.at(-1) ?? '')).toMatchObject({
114
+ status: 'READY',
115
+ logged_in: false,
116
+ });
117
+ const ctx = yield* ComposioUserContext;
118
+ expect(Option.isNone(ctx.data.apiKey)).toBe(true);
119
+ })
120
+ );
121
+ });
122
+
123
+ layer(TestLive({ fixture: 'user-config-example' }))('[Given] a human login', it => {
124
+ it.effect('[Then] warns, exits 1, and signs nothing up', () =>
125
+ Effect.gen(function* () {
126
+ const fetchSpy = vi.spyOn(globalThis, 'fetch');
127
+
128
+ yield* cli(['signup']);
129
+
130
+ const output = (yield* MockConsole.getLines({ stripAnsi: true })).join('\n');
131
+ expect(output).toContain('currently signed in to Composio as a regular user');
132
+ expect(process.exitCode).toBe(1);
133
+ expect(fetchSpy).not.toHaveBeenCalled();
134
+ })
135
+ );
136
+ });
137
+});