@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 267 lines 8.77 KB
Raw
1 /**
2 * Filesystem faults: the state under `~/.composio` missing, corrupt,
3 * read-only, or written by several invocations at once.
4 */
5
6 import { afterAll, beforeAll, describe, expect, it } from 'bun:test';
7 import {
8 CONSUMER_PROJECT,
9 CONSUMER_USER_ID,
10 MEMBER,
11 USER_API_KEY,
12 } from '@e2e-tests/cli-fake-backend';
13 import { z } from 'zod';
14 import { startCliRig, type CliRig } from '@e2e-tests/cli-fake-backend/rig';
15 import {
16 clean,
17 commandRequests,
18 EXECUTE_SLACK,
19 ExecutionResult,
20 readStoredLogin,
21 RIG_TIMEOUT_MS,
22 SEARCH_SLACK,
23 SearchResult,
24 stdoutJson,
25 TEST_TIMEOUT_MS,
26 writeDebugLog,
27 } from './support';
28
29 const Whoami = z.looseObject({ email: z.string() });
30
31 const NOT_LOGGED_IN = 'You are not logged in yet. Please run `composio login`.';
32 const STORED_LOGIN = '~/.composio/user_data.json';
33
34 describe('filesystem faults: the stored login', () => {
35 let rig: CliRig;
36
37 beforeAll(async () => {
38 rig = await startCliRig({ label: 'fs-login' });
39 }, RIG_TIMEOUT_MS);
40
41 afterAll(async () => {
42 await writeDebugLog(rig, import.meta.dir, 'fs-login');
43 await rig.stop();
44 });
45
46 for (const { name, damage } of [
47 { name: 'truncated JSON', damage: `printf '{"api_key":"uk_' > ${STORED_LOGIN}` },
48 { name: 'JSON of the wrong type', damage: `echo '[]' > ${STORED_LOGIN}` },
49 { name: 'an empty file', damage: `: > ${STORED_LOGIN}` },
50 { name: 'a missing file', damage: `rm -f ${STORED_LOGIN}` },
51 ]) {
52 it(
53 `${name}: commands report no login, and a new login repairs it`,
54 async () => {
55 await rig.login();
56 await rig.run(damage);
57
58 const result = await rig.run(SEARCH_SLACK);
59 expect(result.exitCode).toBe(1);
60 expect(result.stdout).toBe('');
61 expect(clean(result.stderr)).toBe(NOT_LOGGED_IN);
62 expect(commandRequests(result)).toEqual([]);
63
64 await rig.login();
65 expect((await readStoredLogin(rig)).api_key).toBe(USER_API_KEY);
66 expect((await rig.run(SEARCH_SLACK)).exitCode).toBe(0);
67 },
68 TEST_TIMEOUT_MS
69 );
70 }
71 });
72
73 describe('filesystem faults: cache files', () => {
74 let rig: CliRig;
75
76 beforeAll(async () => {
77 rig = await startCliRig({ label: 'fs-cache' });
78 await rig.login();
79 }, RIG_TIMEOUT_MS);
80
81 afterAll(async () => {
82 await writeDebugLog(rig, import.meta.dir, 'fs-cache');
83 await rig.stop();
84 });
85
86 it(
87 'corrupt update-check, config, and permission caches do not fail a command',
88 async () => {
89 await rig.run(SEARCH_SLACK);
90 await rig.run(
91 `for f in update-check.json config.json tool-permissions-cache.json; do echo '{corrupt' > ~/.composio/$f; done`
92 );
93 const result = await rig.run(SEARCH_SLACK);
94 expect(result.exitCode).toBe(0);
95 expect(stdoutJson(result, SearchResult).results).toHaveLength(1);
96 // The tests below corrupt one file each; leave nothing else broken for them.
97 await rig.run(
98 'cd ~/.composio && rm -f update-check.json config.json tool-permissions-cache.json'
99 );
100 },
101 TEST_TIMEOUT_MS
102 );
103
104 // CLI defect: a corrupt cached tool schema is fatal ("is not valid JSON") instead of being fetched again.
105 it.failing(
106 'a corrupt cached tool schema is fetched again',
107 async () => {
108 await rig.run(SEARCH_SLACK);
109 await rig.run(`echo '{corrupt' > ~/.composio/tool_definitions/SLACK_SEND_MESSAGE.json`);
110 const result = await rig.run(SEARCH_SLACK);
111 expect(result.exitCode).toBe(0);
112 },
113 TEST_TIMEOUT_MS
114 );
115
116 it(
117 'a corrupt cached tool schema is at least named, so it can be deleted',
118 async () => {
119 await rig.run(`mkdir -p ~/.composio/tool_definitions`);
120 await rig.run(`echo '{corrupt' > ~/.composio/tool_definitions/SLACK_SEND_MESSAGE.json`);
121 const result = await rig.run(SEARCH_SLACK);
122 expect(result.exitCode).toBe(1);
123 expect(result.stdout).toBe('');
124 expect(clean(result.stderr)).toContain('is not valid JSON');
125 expect(clean(result.stderr)).toContain(
126 `${rig.journey.home}/.composio/tool_definitions/SLACK_SEND_MESSAGE.json`
127 );
128 await rig.run(`rm -f ~/.composio/tool_definitions/SLACK_SEND_MESSAGE.json`);
129 expect((await rig.run(SEARCH_SLACK)).exitCode).toBe(0);
130 },
131 TEST_TIMEOUT_MS
132 );
133 });
134
135 describe('filesystem faults: a read-only home', () => {
136 let rig: CliRig;
137 // Root ignores file modes, so these commands run as an ordinary user.
138 const asUser = { user: '1000:1000' };
139
140 beforeAll(async () => {
141 rig = await startCliRig({ label: 'fs-readonly' });
142 await rig.login();
143 rig.fake.world.connect(CONSUMER_PROJECT.label, CONSUMER_USER_ID, 'slack');
144 await rig.run(SEARCH_SLACK);
145 await rig.run('chown -R 1000:1000 ~ && chmod -R a-w ~/.composio');
146 }, RIG_TIMEOUT_MS);
147
148 afterAll(async () => {
149 await writeDebugLog(rig, import.meta.dir, 'fs-readonly');
150 await rig.stop();
151 });
152
153 it(
154 'reads keep working: `whoami` and `search` succeed with data on stdout',
155 async () => {
156 const whoami = await rig.run('composio whoami', asUser);
157 expect(whoami.exitCode).toBe(0);
158 expect(stdoutJson(whoami, Whoami).email).toBe(MEMBER.email);
159 const search = await rig.run(SEARCH_SLACK, asUser);
160 expect(search.exitCode).toBe(0);
161 expect(stdoutJson(search, SearchResult).results).toHaveLength(1);
162 },
163 TEST_TIMEOUT_MS
164 );
165
166 it(
167 '`execute` still runs the tool; the failed cache write stays off stdout',
168 async () => {
169 const result = await rig.run(EXECUTE_SLACK, asUser);
170 expect(result.exitCode).toBe(0);
171 expect(stdoutJson(result, ExecutionResult).successful).toBe(true);
172 expect(result.stdout).not.toMatch(/EACCES|EROFS|permission denied/i);
173 expect(result.requests.filter(entry => entry.route === 'dashboard.execute')).toHaveLength(1);
174 },
175 TEST_TIMEOUT_MS
176 );
177
178 it(
179 '`logout` fails, names the file it could not write, and leaves the login intact',
180 async () => {
181 const result = await rig.run('composio logout', asUser);
182 expect(result.exitCode).toBe(1);
183 expect(result.stdout).toBe('');
184 const stderr = clean(result.stderr);
185 expect(stderr).toContain('PermissionDenied');
186 expect(stderr).toContain(`${rig.journey.home}/.composio/user_data.json`);
187 expect((await readStoredLogin(rig)).api_key).toBe(USER_API_KEY);
188 },
189 TEST_TIMEOUT_MS
190 );
191 });
192
193 describe('filesystem faults: concurrent invocations', () => {
194 let rig: CliRig;
195
196 beforeAll(async () => {
197 rig = await startCliRig({ label: 'fs-concurrent' });
198 await rig.login();
199 rig.fake.world.connect(CONSUMER_PROJECT.label, CONSUMER_USER_ID, 'slack');
200 }, RIG_TIMEOUT_MS);
201
202 afterAll(async () => {
203 await writeDebugLog(rig, import.meta.dir, 'fs-concurrent');
204 await rig.stop();
205 });
206
207 it(
208 'eight searches on a cold cache all succeed and leave valid state',
209 async () => {
210 const result = await rig.run(
211 [
212 'rm -rf ~/.composio/tool_definitions',
213 `for i in 1 2 3 4 5 6 7 8; do { ${SEARCH_SLACK} > /tmp/c$i.out 2> /tmp/c$i.err; echo $? > /tmp/c$i.rc; } & done`,
214 'wait',
215 'cat /tmp/c*.rc | tr -d "\\n"; echo',
216 'cat /tmp/c*.err',
217 'cat ~/.composio/tool_definitions/SLACK_SEND_MESSAGE.json > /dev/null',
218 ].join('\n')
219 );
220 expect(result.exitCode).toBe(0);
221 expect(result.stdout.trim()).toBe('00000000');
222 expect(result.stderr).toBe('');
223 const cached: unknown = JSON.parse(
224 await rig.journey.readFile(
225 `${rig.journey.home}/.composio/tool_definitions/SLACK_SEND_MESSAGE.json`
226 )
227 );
228 expect(cached).toBeObject();
229 expect((await readStoredLogin(rig)).api_key).toBe(USER_API_KEY);
230 },
231 TEST_TIMEOUT_MS
232 );
233
234 it(
235 'four executions at once each reach the Dashboard exactly once',
236 async () => {
237 const result = await rig.run(
238 [
239 `for i in 1 2 3 4; do { ${EXECUTE_SLACK} > /tmp/e$i.out 2> /tmp/e$i.err; echo $? > /tmp/e$i.rc; } & done`,
240 'wait',
241 'cat /tmp/e*.rc | tr -d "\\n"; echo',
242 ].join('\n')
243 );
244 expect(result.stdout.trim()).toBe('0000');
245 expect(result.requests.filter(entry => entry.route === 'dashboard.execute')).toHaveLength(4);
246 },
247 TEST_TIMEOUT_MS
248 );
249
250 it(
251 'a login racing other logins leaves one complete stored login',
252 async () => {
253 const result = await rig.run(
254 [
255 `for i in 1 2 3 4; do { composio login --user-api-key ${USER_API_KEY} --no-skill-install > /dev/null 2> /tmp/l$i.err; echo $? > /tmp/l$i.rc; } & done`,
256 'wait',
257 'cat /tmp/l*.rc | tr -d "\\n"; echo',
258 ].join('\n')
259 );
260 expect(result.stdout.trim()).toBe('0000');
261 expect((await readStoredLogin(rig)).api_key).toBe(USER_API_KEY);
262 const leftovers = await rig.run('ls -a ~/.composio | grep -c "composio-tmp" || true');
263 expect(leftovers.stdout.trim()).toBe('0');
264 },
265 TEST_TIMEOUT_MS
266 );
267 });