1
+import { describe, expect, it } from '@effect/vitest';
2
+import { Effect, Option } from 'effect';
3
+import {
4
+ buildAuthConfigCreateRequest,
5
+ parseScopes,
6
+ runAuthConfigsCreate,
7
+ type AuthConfigsCreateParams,
8
+} from 'src/commands/auth-configs/create/auth-configs.create.handler';
9
+import { MockConsole } from 'test/__utils__';
10
+import { AuthConfigsHandlerTest } from 'test/__utils__/layers/auth-configs';
11
+import { notFoundError } from 'test/__utils__/layers/not-found';
12
+
13
+const params: AuthConfigsCreateParams = {
14
+ name: Option.none(),
15
+ toolkit: 'gmail',
16
+ authScheme: Option.none(),
17
+ scopes: Option.none(),
18
+ customCredentials: Option.none(),
19
+};
20
+
21
+const createResponse = {
22
+ auth_config: { id: 'ac_created', auth_scheme: 'OAUTH2', is_composio_managed: false },
23
+ toolkit: { slug: 'gmail' },
24
+};
25
+
26
+describe('parseScopes', () => {
27
+ it.each([
28
+ { name: 'no flag', input: Option.none<string>(), expected: undefined },
29
+ { name: 'one scope', input: Option.some('send_email'), expected: ['send_email'] },
30
+ {
31
+ name: 'padded scopes and blanks',
32
+ input: Option.some(' send_email, ,read_email ,'),
33
+ expected: ['send_email', 'read_email'],
34
+ },
35
+ // An empty flag still sends an empty list, as before the move.
36
+ { name: 'an empty flag', input: Option.some(''), expected: [] },
37
+ ])('[Given] $name [Then] returns $expected', ({ input, expected }) => {
38
+ expect(parseScopes(input)).toEqual(expected);
39
+ });
40
+});
41
+
42
+describe('buildAuthConfigCreateRequest', () => {
43
+ const base = {
44
+ name: undefined,
45
+ toolkit: 'gmail',
46
+ authScheme: Option.none<string>(),
47
+ scopes: undefined,
48
+ customCredentials: undefined,
49
+ };
50
+
51
+ it.each([
52
+ {
53
+ name: 'no scheme',
54
+ input: { ...base, name: 'mine' },
55
+ expected: {
56
+ toolkit: { slug: 'gmail' },
57
+ auth_config: { type: 'use_composio_managed_auth', name: 'mine', credentials: undefined },
58
+ },
59
+ },
60
+ {
61
+ name: 'no scheme with scopes and custom credentials',
62
+ input: { ...base, scopes: ['send_email'], customCredentials: { client_id: 'ignored' } },
63
+ expected: {
64
+ toolkit: { slug: 'gmail' },
65
+ auth_config: {
66
+ type: 'use_composio_managed_auth',
67
+ name: undefined,
68
+ credentials: { scopes: ['send_email'] },
69
+ },
70
+ },
71
+ },
72
+ {
73
+ name: 'a scheme alone',
74
+ input: { ...base, authScheme: Option.some('API_KEY') },
75
+ expected: {
76
+ toolkit: { slug: 'gmail' },
77
+ auth_config: {
78
+ type: 'use_custom_auth',
79
+ authScheme: 'API_KEY',
80
+ name: undefined,
81
+ credentials: undefined,
82
+ },
83
+ },
84
+ },
85
+ {
86
+ name: 'a scheme with custom credentials and scopes',
87
+ input: {
88
+ ...base,
89
+ authScheme: Option.some('OAUTH2'),
90
+ scopes: ['send_email'],
91
+ customCredentials: { client_id: 'id', scopes: ['overridden'] },
92
+ },
93
+ expected: {
94
+ toolkit: { slug: 'gmail' },
95
+ auth_config: {
96
+ type: 'use_custom_auth',
97
+ authScheme: 'OAUTH2',
98
+ name: undefined,
99
+ credentials: { client_id: 'id', scopes: ['send_email'] },
100
+ },
101
+ },
102
+ },
103
+ ])('[Given] $name [Then] builds the matching request', ({ input, expected }) => {
104
+ expect(buildAuthConfigCreateRequest(input)).toStrictEqual(expected);
105
+ });
106
+});
107
+
108
+describe('runAuthConfigsCreate', () => {
109
+ it.effect('[Given] a successful create [Then] prints the API response JSON to stdout', () => {
110
+ const { layer, outputs, exitCode } = AuthConfigsHandlerTest({
111
+ authConfigsData: { createResponse },
112
+ });
113
+ return Effect.gen(function* () {
114
+ yield* runAuthConfigsCreate(params);
115
+
116
+ expect(outputs).toEqual([JSON.stringify(createResponse, null, 2)]);
117
+ const decoration = (yield* MockConsole.getLines({ stripAnsi: true })).join('\n');
118
+ expect(decoration).toContain('Auth config created.');
119
+ expect(decoration).toContain('[New Auth Config]');
120
+ expect(exitCode()).toBeUndefined();
121
+ }).pipe(Effect.provide(layer));
122
+ });
123
+
124
+ it.effect.each([
125
+ { name: 'malformed JSON', customCredentials: '{invalid json}' },
126
+ { name: 'a JSON array', customCredentials: '["client_id"]' },
127
+ ])(
128
+ '[Given] $name in --custom-credentials [Then] reports it, sends no request, and exits 0',
129
+ ({ customCredentials }) => {
130
+ const { layer, outputs, exitCode } = AuthConfigsHandlerTest({
131
+ repository: { createAuthConfig: () => Effect.die('createAuthConfig must not be called') },
132
+ });
133
+ return Effect.gen(function* () {
134
+ yield* runAuthConfigsCreate({
135
+ ...params,
136
+ authScheme: Option.some('OAUTH2'),
137
+ customCredentials: Option.some(customCredentials),
138
+ });
139
+
140
+ expect(yield* MockConsole.getLines({ stream: 'stderr' })).toEqual([
141
+ 'Invalid JSON in --custom-credentials. Please provide valid JSON.',
142
+ ]);
143
+ expect(outputs).toEqual([]);
144
+ expect(exitCode()).toBeUndefined();
145
+ }).pipe(Effect.provide(layer));
146
+ }
147
+ );
148
+
149
+ it.effect('[Given] the API rejects the create [Then] hints at the toolkit and exits 1', () => {
150
+ const { layer, outputs, exitCode } = AuthConfigsHandlerTest({
151
+ repository: {
152
+ createAuthConfig: () => Effect.fail(notFoundError('Toolkit not found')),
153
+ },
154
+ });
155
+ return Effect.gen(function* () {
156
+ yield* runAuthConfigsCreate({ ...params, toolkit: 'nope' });
157
+
158
+ const stderr = (yield* MockConsole.getLines({ stream: 'stderr', stripAnsi: true })).join(
159
+ '\n'
160
+ );
161
+ expect(stderr).toContain('Failed to create auth config.');
162
+ expect(stderr).toContain(
163
+ 'Check available auth schemes for "nope":\n> composio dev toolkits info "nope"'
164
+ );
165
+ expect(outputs).toEqual([]);
166
+ expect(exitCode()).toBe(1);
167
+ }).pipe(Effect.provide(layer));
168
+ });
169
+});