@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 04:45 UTC

next
ts 191 lines 6.13 KB
Raw
1 import { describe, it, expect, vi, beforeEach } from 'vitest';
2 import ComposioClient from '@composio/client';
3 import { Toolkits } from '../../src/models/Toolkits';
4 import { ValidationError } from '../../src/errors/ValidationErrors';
5
6 vi.mock('../../src/telemetry/Telemetry', () => ({
7 telemetry: {
8 instrument: vi.fn(),
9 },
10 }));
11
12 const createMockClient = () => ({
13 toolkits: {
14 recommendScopes: vi.fn(),
15 retrieveScopesGrantContext: vi.fn(),
16 },
17 });
18
19 const rawRecommendation = {
20 auth_scheme: 'OAUTH2',
21 toolkit_version: '20250909_00',
22 grant_context: { account_type: 'Google Workspace' },
23 scopes: {
24 least_privilege: ['https://www.googleapis.com/auth/gmail.send'],
25 fewest: ['https://mail.google.com/'],
26 conditional: [
27 {
28 scope: 'https://www.googleapis.com/auth/gmail.modify',
29 when: { label_ids: ['TRASH'] },
30 for: ['GMAIL_FETCH_EMAILS'],
31 },
32 ],
33 },
34 };
35
36 describe('Toolkits scope recommendation', () => {
37 let toolkits: Toolkits;
38 let mockClient: ReturnType<typeof createMockClient>;
39
40 beforeEach(() => {
41 vi.clearAllMocks();
42 mockClient = createMockClient();
43 toolkits = new Toolkits(mockClient as unknown as ComposioClient);
44 });
45
46 describe('recommendScopes', () => {
47 it('covers every tool when tools is empty', async () => {
48 mockClient.toolkits.recommendScopes.mockResolvedValue(rawRecommendation);
49
50 await toolkits.recommendScopes('gmail', { tools: [] });
51
52 expect(mockClient.toolkits.recommendScopes).toHaveBeenCalledWith(
53 'gmail',
54 {
55 tools: [],
56 auth_scheme: undefined,
57 toolkit_version: undefined,
58 grant_context: undefined,
59 include: undefined,
60 exclude: undefined,
61 available_scopes: undefined,
62 },
63 undefined
64 );
65 });
66
67 it('maps params to the wire and transforms the response', async () => {
68 mockClient.toolkits.recommendScopes.mockResolvedValue(rawRecommendation);
69 const signal = new AbortController().signal;
70
71 const result = await toolkits.recommendScopes(
72 'gmail',
73 {
74 tools: ['GMAIL_SEND_EMAIL', 'GMAIL_FETCH_EMAILS'],
75 authScheme: 'OAUTH2',
76 toolkitVersion: '20250909_00',
77 grantContext: { account_type: 'Google Workspace' },
78 include: ['openid'],
79 exclude: ['https://mail.google.com/'],
80 availableScopes: ['openid', 'https://www.googleapis.com/auth/gmail.send'],
81 },
82 { signal }
83 );
84
85 expect(mockClient.toolkits.recommendScopes).toHaveBeenCalledWith(
86 'gmail',
87 {
88 tools: ['GMAIL_SEND_EMAIL', 'GMAIL_FETCH_EMAILS'],
89 auth_scheme: 'OAUTH2',
90 toolkit_version: '20250909_00',
91 grant_context: { account_type: 'Google Workspace' },
92 include: ['openid'],
93 exclude: ['https://mail.google.com/'],
94 available_scopes: ['openid', 'https://www.googleapis.com/auth/gmail.send'],
95 },
96 { signal }
97 );
98 expect(result).toEqual({
99 authScheme: 'OAUTH2',
100 toolkitVersion: '20250909_00',
101 grantContext: { account_type: 'Google Workspace' },
102 scopes: {
103 leastPrivilege: ['https://www.googleapis.com/auth/gmail.send'],
104 fewest: ['https://mail.google.com/'],
105 conditional: [
106 {
107 scope: 'https://www.googleapis.com/auth/gmail.modify',
108 when: { label_ids: ['TRASH'] },
109 for: ['GMAIL_FETCH_EMAILS'],
110 },
111 ],
112 },
113 });
114 });
115
116 it('rejects a missing tools list before calling the API', async () => {
117 await expect(
118 toolkits.recommendScopes('gmail', {} as Parameters<Toolkits['recommendScopes']>[1])
119 ).rejects.toThrow(ValidationError);
120 expect(mockClient.toolkits.recommendScopes).not.toHaveBeenCalled();
121 });
122
123 it('rejects an unknown auth scheme before calling the API', async () => {
124 await expect(
125 toolkits.recommendScopes('gmail', { tools: [], authScheme: 'NOT_A_SCHEME' as never })
126 ).rejects.toThrow(ValidationError);
127 expect(mockClient.toolkits.recommendScopes).not.toHaveBeenCalled();
128 });
129 });
130
131 describe('listGrantContexts', () => {
132 const rawGrantContexts = {
133 auth_scheme: 'OAUTH2',
134 toolkit_version: '20250909_00',
135 grant_context_dimensions: [
136 {
137 dimension: 'account_type',
138 description: 'Kind of Google account the user connects',
139 values: ['Google Workspace', 'Personal'],
140 },
141 ],
142 default_grant_context: { account_type: 'Personal' },
143 };
144
145 it('sends an empty query when called without params', async () => {
146 mockClient.toolkits.retrieveScopesGrantContext.mockResolvedValue(rawGrantContexts);
147
148 await toolkits.listGrantContexts('gmail');
149
150 expect(mockClient.toolkits.retrieveScopesGrantContext).toHaveBeenCalledWith(
151 'gmail',
152 { auth_scheme: undefined, toolkit_version: undefined },
153 undefined
154 );
155 });
156
157 it('maps params to the wire and transforms the response', async () => {
158 mockClient.toolkits.retrieveScopesGrantContext.mockResolvedValue(rawGrantContexts);
159
160 const result = await toolkits.listGrantContexts('gmail', {
161 authScheme: 'OAUTH2',
162 toolkitVersion: '20250909_00',
163 });
164
165 expect(mockClient.toolkits.retrieveScopesGrantContext).toHaveBeenCalledWith(
166 'gmail',
167 { auth_scheme: 'OAUTH2', toolkit_version: '20250909_00' },
168 undefined
169 );
170 expect(result).toEqual({
171 authScheme: 'OAUTH2',
172 toolkitVersion: '20250909_00',
173 grantContextDimensions: [
174 {
175 dimension: 'account_type',
176 description: 'Kind of Google account the user connects',
177 values: ['Google Workspace', 'Personal'],
178 },
179 ],
180 defaultGrantContext: { account_type: 'Personal' },
181 });
182 });
183
184 it('rejects an empty toolkit version before calling the API', async () => {
185 await expect(toolkits.listGrantContexts('gmail', { toolkitVersion: '' })).rejects.toThrow(
186 ValidationError
187 );
188 expect(mockClient.toolkits.retrieveScopesGrantContext).not.toHaveBeenCalled();
189 });
190 });
191 });