@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 525 lines 17.3 KB
Raw
1 import { describe, expect, it, vi } from '@effect/vitest';
2 import { Config, ConfigProvider, Effect, FileSystem, Layer, Path, PlatformError } from 'effect';
3 import { FetchHttpClient, HttpClient } from 'effect/http';
4 import * as BunFileSystem from '@effect/platform-bun/BunFileSystem';
5 import * as BunPath from '@effect/platform-bun/BunPath';
6 import * as tempy from 'tempy';
7 import { TerminalUITest } from 'test/__utils__/services/terminal-ui-test';
8 import { startTestHttpServer } from 'test/__utils__/http-server';
9 import {
10 inferSkillReleaseChannel,
11 installSkill,
12 resolveInstalledSkillName,
13 resolveSkillReleaseTag,
14 resolveTargetSkillPath,
15 SKILL_RELEASE_TAG_FILENAME,
16 SkillInstallError,
17 type SkillReleaseChannel,
18 } from 'src/effects/install-skill';
19 import { GITHUB_CONFIG } from 'src/config';
20 import { CliReleaseResolutionError } from 'src/effects/resolve-cli-release';
21 import { defaultNodeOs, NodeOs } from 'src/services/node-os';
22
23 const TEST_RELEASE_TAG = '@composio/cli@0.3.0-test';
24 const TEST_SKILL_ZIP = Uint8Array.from(
25 atob(
26 'UEsDBAoAAAAAAEGX7lwAAAAAAAAAAAAAAAANABwAY29tcG9zaW8tY2xpL1VUCQADeU5WanlOVmp1eAsAAQT1AQAABBQAAABQSwMECgAAAAAAQZfuXMXglUQPAAAADwAAABUAHABjb21wb3Npby1jbGkvU0tJTEwubWRVVAkAA3lOVmp5TlZqdXgLAAEE9QEAAAQUAAAAIyBjb21wb3Npby1jbGkKUEsBAh4DCgAAAAAAQZfuXAAAAAAAAAAAAAAAAA0AGAAAAAAAAAAQAO1BAAAAAGNvbXBvc2lvLWNsaS9VVAUAA3lOVmp1eAsAAQT1AQAABBQAAABQSwECHgMKAAAAAABBl+5cxeCVRA8AAAAPAAAAFQAYAAAAAAABAAAApIFHAAAAY29tcG9zaW8tY2xpL1NLSUxMLm1kVVQFAAN5TlZqdXgLAAEE9QEAAAQUAAAAUEsFBgAAAAACAAIArgAAAKUAAAAAAA=='
27 ),
28 character => character.charCodeAt(0)
29 );
30
31 const TestPlatform = Layer.mergeAll(BunFileSystem.layer, BunPath.layer);
32
33 const makeInstallEffect = (
34 home: string,
35 apiBaseUrl: string,
36 options: { readonly releaseTag?: string } = { releaseTag: TEST_RELEASE_TAG }
37 ) =>
38 installSkill({ target: 'claude', ...options }).pipe(
39 Effect.provide(
40 Layer.mergeAll(
41 TestPlatform,
42 FetchHttpClient.layer,
43 TerminalUITest,
44 Layer.succeed(NodeOs, defaultNodeOs({ homedir: home })),
45 ConfigProvider.layer(
46 ConfigProvider.fromEnv({
47 env: {
48 COMPOSIO_GITHUB_API_BASE_URL: apiBaseUrl,
49 COMPOSIO_GITHUB_OWNER: 'test-owner',
50 COMPOSIO_GITHUB_REPO: 'test-repo',
51 },
52 })
53 )
54 )
55 ),
56 Effect.scoped
57 );
58
59 const startSkillReleaseServer = (skillZip: Uint8Array) =>
60 startTestHttpServer((req, res) => {
61 if (req.url === '/skill.zip') {
62 res.writeHead(200, { 'content-type': 'application/zip' });
63 res.end(skillZip);
64 return;
65 }
66
67 res.writeHead(200, { 'content-type': 'application/json' });
68 res.end(
69 JSON.stringify({
70 assets: [
71 {
72 name: 'composio-skill.zip',
73 browser_download_url: `http://${req.headers.host}/skill.zip`,
74 },
75 ],
76 })
77 );
78 });
79
80 /** Scoped stub of Bun.which that misses, so release resolution takes the HTTP path. */
81 const stubBunWhichMiss = Effect.acquireRelease(
82 Effect.sync(() => vi.stubGlobal('Bun', { which: vi.fn(() => null) })),
83 () =>
84 Effect.sync(() => {
85 vi.unstubAllGlobals();
86 vi.restoreAllMocks();
87 })
88 );
89
90 type TargetSetup = (
91 fs: FileSystem.FileSystem,
92 path: Path.Path,
93 home: string,
94 target: string
95 ) => Effect.Effect<void, PlatformError.PlatformError>;
96
97 const TARGET_SCENARIOS = [
98 ['a missing', (() => Effect.void) satisfies TargetSetup],
99 [
100 'an existing directory',
101 ((fs, _path, _home, target) =>
102 fs.makeDirectory(target, { recursive: true })) satisfies TargetSetup,
103 ],
104 [
105 'an existing symlink',
106 ((fs, path, home, target) =>
107 Effect.gen(function* () {
108 const existingTarget = path.join(home, 'existing-skill');
109 yield* fs.makeDirectory(existingTarget, { recursive: true });
110 yield* fs.makeDirectory(path.dirname(target), { recursive: true });
111 yield* fs.symlink(path.relative(path.dirname(target), existingTarget), target);
112 })) satisfies TargetSetup,
113 ],
114 [
115 'a broken symlink',
116 ((fs, path, home, target) =>
117 Effect.gen(function* () {
118 yield* fs.makeDirectory(path.dirname(target), { recursive: true });
119 yield* fs.symlink(
120 path.relative(path.dirname(target), path.join(home, 'missing-skill')),
121 target
122 );
123 })) satisfies TargetSetup,
124 ],
125 ] as const;
126
127 const makeResolveEffect = (
128 configEntries: ReadonlyArray<[string, string]>,
129 options: {
130 channel?: SkillReleaseChannel;
131 installedReleaseTag?: string;
132 releaseTag?: string;
133 } = {}
134 ) =>
135 Effect.gen(function* () {
136 const httpClient = yield* HttpClient.HttpClient;
137 const githubConfig = yield* GITHUB_CONFIG;
138
139 return yield* resolveSkillReleaseTag({
140 channel: options.channel,
141 githubConfig,
142 httpClient,
143 installedReleaseTag: options.installedReleaseTag,
144 releaseTag: options.releaseTag,
145 });
146 }).pipe(
147 Effect.provide(
148 Layer.mergeAll(
149 FetchHttpClient.layer,
150 ConfigProvider.layer(ConfigProvider.fromEnv({ env: Object.fromEntries(configEntries) }))
151 )
152 ),
153 Effect.scoped
154 );
155
156 describe('install-skill', () => {
157 it('infers the stable channel from stable versions', () => {
158 expect(inferSkillReleaseChannel('0.2.19')).toBe('stable');
159 expect(inferSkillReleaseChannel('@composio/cli@0.2.19')).toBe('stable');
160 });
161
162 it('infers the beta channel from beta versions', () => {
163 expect(inferSkillReleaseChannel('0.2.20-beta.4')).toBe('beta');
164 expect(inferSkillReleaseChannel('@composio/cli@0.2.20-beta.4')).toBe('beta');
165 });
166
167 it('defaults the installed skill name to composio-cli', () => {
168 expect(resolveInstalledSkillName()).toBe('composio-cli');
169 expect(resolveInstalledSkillName(' ')).toBe('composio-cli');
170 });
171
172 it('rejects unsafe installed skill names', () => {
173 expect(() => resolveInstalledSkillName('../bad')).toThrow(/Invalid skill name/);
174 expect(() => resolveInstalledSkillName('.')).toThrow(/Invalid skill name/);
175 expect(() => resolveInstalledSkillName('..')).toThrow(/Invalid skill name/);
176 });
177
178 it.effect('resolves the agent-specific skill path', () =>
179 Effect.gen(function* () {
180 const path = yield* Path.Path;
181 expect(
182 resolveTargetSkillPath({
183 home: '/tmp/test-home',
184 path,
185 skillName: 'composio-cli',
186 target: 'claude',
187 })
188 ).toBe('/tmp/test-home/.claude/skills/composio-cli');
189 expect(
190 resolveTargetSkillPath({
191 home: '/tmp/test-home',
192 path,
193 skillName: 'composio-cli',
194 target: 'codex',
195 })
196 ).toBe('/tmp/test-home/.codex/skills/composio-cli');
197 expect(
198 resolveTargetSkillPath({
199 home: '/tmp/test-home',
200 path,
201 skillName: 'composio-cli',
202 target: 'openclaw',
203 })
204 ).toBe('/tmp/test-home/.openclaw/skills/composio-cli');
205 }).pipe(Effect.provide(Path.layer))
206 );
207
208 it.effect('prefers an explicit running release tag over channel discovery', () =>
209 Effect.gen(function* () {
210 const releaseTag = '@composio/cli@0.3.0-beta.123';
211 const tag = yield* makeResolveEffect([], {
212 channel: 'stable',
213 installedReleaseTag: '@composio/cli@0.2.33-beta.322',
214 releaseTag,
215 });
216
217 expect(tag).toBe(releaseTag);
218 })
219 );
220
221 it.effect('prefers the configured release tag over packaged metadata', () =>
222 Effect.gen(function* () {
223 const tag = yield* makeResolveEffect(
224 [['COMPOSIO_GITHUB_TAG', '@composio/cli@0.2.34-beta.1']],
225 {
226 installedReleaseTag: '@composio/cli@0.2.33',
227 }
228 );
229
230 expect(tag).toBe('@composio/cli@0.2.34-beta.1');
231 })
232 );
233
234 it.effect('uses the packaged release tag when no explicit selector is provided', () =>
235 Effect.gen(function* () {
236 const tag = yield* makeResolveEffect([], {
237 installedReleaseTag: '@composio/cli@0.2.33-beta.322',
238 });
239
240 expect(tag).toBe('@composio/cli@0.2.33-beta.322');
241 })
242 );
243
244 it.effect('installs from the packaged release tag when package metadata differs', () => {
245 const installDir = tempy.temporaryDirectory();
246 const execPathSpy = vi
247 .spyOn(process, 'execPath', 'get')
248 .mockReturnValue(`${installDir}/composio`);
249 let requestedReleasePath = '';
250
251 return Effect.gen(function* () {
252 const fs = yield* FileSystem.FileSystem;
253 const path = yield* Path.Path;
254 const packagedReleaseTag = '@composio/cli@0.2.33';
255 yield* fs.writeFileString(path.join(installDir, 'release-tag.txt'), packagedReleaseTag);
256
257 const apiBaseUrl = yield* startTestHttpServer((req, res) => {
258 if (req.url === '/skill.zip') {
259 res.writeHead(200, { 'content-type': 'application/zip' });
260 res.end(TEST_SKILL_ZIP);
261 return;
262 }
263
264 requestedReleasePath = req.url ?? '';
265 res.writeHead(200, { 'content-type': 'application/json' });
266 res.end(
267 JSON.stringify({
268 tag_name: packagedReleaseTag,
269 assets: [
270 {
271 name: 'composio-skill.zip',
272 browser_download_url: `http://${req.headers.host}/skill.zip`,
273 },
274 ],
275 })
276 );
277 });
278
279 const home = tempy.temporaryDirectory();
280 yield* makeInstallEffect(home, apiBaseUrl, {});
281
282 expect(requestedReleasePath).toContain(encodeURIComponent(packagedReleaseTag));
283 expect(
284 yield* fs.readFileString(
285 path.join(home, '.agents', 'skills', 'composio-cli', SKILL_RELEASE_TAG_FILENAME)
286 )
287 ).toBe(`${packagedReleaseTag}\n`);
288 }).pipe(
289 Effect.provide(TestPlatform),
290 Effect.ensuring(Effect.sync(() => execPathSpy.mockRestore()))
291 );
292 });
293
294 it.effect('falls back to the latest inferred channel for source and development runs', () =>
295 Effect.gen(function* () {
296 yield* stubBunWhichMiss;
297 const apiBaseUrl = yield* startTestHttpServer((_req, res) => {
298 res.writeHead(200, { 'content-type': 'application/json' });
299 res.end(
300 JSON.stringify([
301 {
302 tag_name: '@composio/cli@0.2.33-beta.322',
303 draft: false,
304 prerelease: true,
305 assets: [
306 {
307 name: 'composio-skill.zip',
308 browser_download_url: 'http://127.0.0.1/beta-skill.zip',
309 },
310 ],
311 },
312 {
313 tag_name: '@composio/cli@0.2.33',
314 draft: false,
315 prerelease: false,
316 assets: [
317 {
318 name: 'composio-skill.zip',
319 browser_download_url: 'http://127.0.0.1/stable-skill.zip',
320 },
321 ],
322 },
323 ])
324 );
325 });
326
327 const tag = yield* makeResolveEffect([
328 ['COMPOSIO_GITHUB_API_BASE_URL', apiBaseUrl],
329 ['COMPOSIO_GITHUB_OWNER', 'test-owner'],
330 ['COMPOSIO_GITHUB_REPO', 'test-repo'],
331 ]);
332
333 expect(tag).toBe('@composio/cli@0.2.33');
334 })
335 );
336
337 it.effect('fails with a typed decode error for malformed GitHub release lists', () =>
338 Effect.gen(function* () {
339 const apiBaseUrl = yield* startTestHttpServer((_req, res) => {
340 res.writeHead(200, { 'content-type': 'application/json' });
341 res.end(JSON.stringify({ tag_name: '@composio/cli@0.3.0' }));
342 });
343
344 const error = yield* Effect.flip(
345 makeResolveEffect(
346 [
347 ['COMPOSIO_GITHUB_API_BASE_URL', apiBaseUrl],
348 ['COMPOSIO_GITHUB_OWNER', 'test-owner'],
349 ['COMPOSIO_GITHUB_REPO', 'test-repo'],
350 ],
351 { channel: 'stable' }
352 )
353 );
354
355 expect(error).toBeInstanceOf(CliReleaseResolutionError);
356 expect(error).toMatchObject({ reason: 'decode', cause: expect.anything() });
357 })
358 );
359
360 it.effect('fails with a typed decode error for malformed skill release metadata', () =>
361 Effect.gen(function* () {
362 const apiBaseUrl = yield* startTestHttpServer((_req, res) => {
363 res.writeHead(200, { 'content-type': 'application/json' });
364 res.end(JSON.stringify({ assets: [{ name: 'composio-skill.zip' }] }));
365 });
366
367 const home = tempy.temporaryDirectory();
368 const error = yield* Effect.flip(makeInstallEffect(home, apiBaseUrl));
369
370 expect(error).toBeInstanceOf(SkillInstallError);
371 expect(error).toMatchObject({ phase: 'release', cause: expect.anything() });
372 })
373 );
374
375 it.effect.each(TARGET_SCENARIOS)('installs over %s target', ([, prepareTarget]) =>
376 Effect.gen(function* () {
377 const apiBaseUrl = yield* startSkillReleaseServer(TEST_SKILL_ZIP);
378 const fs = yield* FileSystem.FileSystem;
379 const path = yield* Path.Path;
380 const home = tempy.temporaryDirectory();
381 const target = path.join(home, '.claude', 'skills', 'composio-cli');
382 const canonicalSkill = path.join(home, '.agents', 'skills', 'composio-cli');
383
384 yield* prepareTarget(fs, path, home, target);
385
386 yield* makeInstallEffect(home, apiBaseUrl);
387
388 expect(yield* fs.readFileString(path.join(target, 'SKILL.md'))).toBe('# composio-cli\n');
389 expect(yield* fs.readFileString(path.join(canonicalSkill, SKILL_RELEASE_TAG_FILENAME))).toBe(
390 `${TEST_RELEASE_TAG}\n`
391 );
392 expect(yield* fs.readLink(target)).toBe(path.relative(path.dirname(target), canonicalSkill));
393 expect(yield* fs.exists(path.join(home, '.agents', '.tmp-skill-install'))).toBe(false);
394 }).pipe(Effect.provide(TestPlatform))
395 );
396
397 it.effect('removes the temporary install directory after extraction fails', () =>
398 Effect.gen(function* () {
399 const apiBaseUrl = yield* startSkillReleaseServer(new TextEncoder().encode('not a zip'));
400 const home = tempy.temporaryDirectory();
401 const error = yield* Effect.flip(makeInstallEffect(home, apiBaseUrl));
402
403 expect(error).toBeInstanceOf(SkillInstallError);
404 expect(error).toMatchObject({ phase: 'extract' });
405
406 const fs = yield* FileSystem.FileSystem;
407 const path = yield* Path.Path;
408 expect(yield* fs.exists(path.join(home, '.agents', '.tmp-skill-install'))).toBe(false);
409 }).pipe(Effect.provide(TestPlatform))
410 );
411
412 it.effect('resolves the latest stable release when the stable channel is requested', () =>
413 Effect.gen(function* () {
414 yield* stubBunWhichMiss;
415 const apiBaseUrl = yield* startTestHttpServer((_req, res) => {
416 res.writeHead(200, { 'content-type': 'application/json' });
417 res.end(
418 JSON.stringify([
419 {
420 tag_name: '@composio/cli@0.2.20-beta.2',
421 draft: false,
422 prerelease: true,
423 assets: [
424 {
425 name: 'composio-skill.zip',
426 browser_download_url: 'http://127.0.0.1/beta-skill.zip',
427 },
428 ],
429 },
430 {
431 tag_name: '@composio/cli@0.2.19',
432 draft: false,
433 prerelease: false,
434 assets: [
435 {
436 name: 'composio-skill.zip',
437 browser_download_url: 'http://127.0.0.1/stable-skill.zip',
438 },
439 ],
440 },
441 {
442 tag_name: '@composio/cli@0.2.20',
443 draft: false,
444 prerelease: false,
445 assets: [
446 {
447 name: 'composio-linux-x64.zip',
448 browser_download_url: 'http://127.0.0.1/no-skill.zip',
449 },
450 ],
451 },
452 ])
453 );
454 });
455
456 const tag = yield* makeResolveEffect(
457 [
458 ['COMPOSIO_GITHUB_API_BASE_URL', apiBaseUrl],
459 ['COMPOSIO_GITHUB_OWNER', 'test-owner'],
460 ['COMPOSIO_GITHUB_REPO', 'test-repo'],
461 ],
462 { channel: 'stable' }
463 );
464
465 expect(tag).toBe('@composio/cli@0.2.19');
466 })
467 );
468
469 it.effect('resolves the latest beta release when the beta channel is requested', () =>
470 Effect.gen(function* () {
471 yield* stubBunWhichMiss;
472 const apiBaseUrl = yield* startTestHttpServer((_req, res) => {
473 res.writeHead(200, { 'content-type': 'application/json' });
474 res.end(
475 JSON.stringify([
476 {
477 tag_name: '@composio/cli@0.2.20-beta.1',
478 draft: false,
479 prerelease: true,
480 assets: [
481 {
482 name: 'composio-skill.zip',
483 browser_download_url: 'http://127.0.0.1/beta-1-skill.zip',
484 },
485 ],
486 },
487 {
488 tag_name: '@composio/cli@0.2.20-beta.3',
489 draft: false,
490 prerelease: true,
491 assets: [
492 {
493 name: 'composio-skill.zip',
494 browser_download_url: 'http://127.0.0.1/beta-3-skill.zip',
495 },
496 ],
497 },
498 {
499 tag_name: '@composio/cli@0.2.20',
500 draft: false,
501 prerelease: false,
502 assets: [
503 {
504 name: 'composio-skill.zip',
505 browser_download_url: 'http://127.0.0.1/stable-skill.zip',
506 },
507 ],
508 },
509 ])
510 );
511 });
512
513 const tag = yield* makeResolveEffect(
514 [
515 ['COMPOSIO_GITHUB_API_BASE_URL', apiBaseUrl],
516 ['COMPOSIO_GITHUB_OWNER', 'test-owner'],
517 ['COMPOSIO_GITHUB_REPO', 'test-repo'],
518 ],
519 { channel: 'beta' }
520 );
521
522 expect(tag).toBe('@composio/cli@0.2.20-beta.3');
523 })
524 );
525 });