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
| 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 { ComposioUserContext } from 'src/services/user-context'; |
| 9 | import { cli, MockConsole, TestLive } from 'test/__utils__'; |
| 10 | import { agentsJsonResponse, makeAgentIdentity } from 'test/__utils__/models/agent'; |
| 11 | import { exitCodeOf } from 'test/__utils__/exit-code'; |
| 12 | |
| 13 | describe('CLI: composio agent signup', () => { |
| 14 | afterEach(() => { |
| 15 | vi.restoreAllMocks(); |
| 16 | }); |
| 17 | |
| 18 | layer(TestLive())(it => { |
| 19 | it.effect('agent signup works when the CLI is not signed in', () => |
| 20 | Effect.gen(function* () { |
| 21 | const fetchSpy = vi |
| 22 | .spyOn(globalThis, 'fetch') |
| 23 | .mockImplementation(async () => agentsJsonResponse(makeAgentIdentity())); |
| 24 | |
| 25 | yield* cli(['agent', 'signup']); |
| 26 | |
| 27 | expect(fetchSpy).toHaveBeenCalledExactlyOnceWith( |
| 28 | expect.stringContaining('/api/signup'), |
| 29 | expect.objectContaining({ method: 'POST' }) |
| 30 | ); |
| 31 | const output = (yield* MockConsole.getLines({ stripAnsi: true })).join('\n'); |
| 32 | expect(output).toContain('composio agent signup signs you up as a Composio agent'); |
| 33 | expect(output).toContain('fully non-interactive'); |
| 34 | |
| 35 | const ctx = yield* ComposioUserContext; |
| 36 | expect(Option.getOrUndefined(ctx.data.apiKey)).toBe('uak_agent'); |
| 37 | expect(Option.getOrUndefined(ctx.data.orgId)).toBe('org_agent'); |
| 38 | |
| 39 | const fs = yield* FileSystem.FileSystem; |
| 40 | const cacheDir = yield* setupCacheDir; |
| 41 | const userConfigRaw = yield* fs.readFileString( |
| 42 | path.join(cacheDir, constants.USER_CONFIG_FILE_NAME), |
| 43 | 'utf8' |
| 44 | ); |
| 45 | const agentConfigRaw = yield* fs.readFileString(path.join(cacheDir, 'agent.json'), 'utf8'); |
| 46 | |
| 47 | expect(JSON.parse(userConfigRaw)).toMatchObject({ |
| 48 | api_key: 'uak_agent', |
| 49 | org_id: 'org_agent', |
| 50 | }); |
| 51 | expect(JSON.parse(agentConfigRaw)).toMatchObject({ |
| 52 | composio_agent_key: 'cak_test_agent', |
| 53 | agent_key: 'cak_test_agent', |
| 54 | composio: { user_api_key: 'uak_agent' }, |
| 55 | }); |
| 56 | }) |
| 57 | ); |
| 58 | }); |
| 59 | |
| 60 | layer(TestLive())(it => { |
| 61 | it.effect('[Given] --no-wait -f [Then] starts a fresh signup and stays logged out', () => |
| 62 | Effect.gen(function* () { |
| 63 | const fetchSpy = vi |
| 64 | .spyOn(globalThis, 'fetch') |
| 65 | .mockImplementation(async () => |
| 66 | agentsJsonResponse(makeAgentIdentity({ status: 'PENDING', composio: undefined })) |
| 67 | ); |
| 68 | |
| 69 | yield* cli(['agent', 'signup', '--no-wait', '-f']); |
| 70 | |
| 71 | expect(fetchSpy).toHaveBeenCalledExactlyOnceWith( |
| 72 | expect.stringContaining('/api/signup?wait=0'), |
| 73 | expect.objectContaining({ method: 'POST' }) |
| 74 | ); |
| 75 | const stdout = yield* MockConsole.getLines({ stream: 'stdout', stripAnsi: true }); |
| 76 | expect(JSON.parse(stdout.at(-1) ?? '')).toMatchObject({ |
| 77 | status: 'PENDING', |
| 78 | logged_in: false, |
| 79 | }); |
| 80 | const ctx = yield* ComposioUserContext; |
| 81 | expect(Option.getOrUndefined(ctx.data.apiKey)).toBeUndefined(); |
| 82 | }) |
| 83 | ); |
| 84 | }); |
| 85 | |
| 86 | layer(TestLive({ fixture: 'user-config-example' }))(it => { |
| 87 | it.effect('agent signup shows a soft auth warning when signed in as a human user', () => |
| 88 | Effect.gen(function* () { |
| 89 | const exit = yield* Effect.exit(cli(['agent', 'signup'])); |
| 90 | const output = (yield* MockConsole.getLines({ stripAnsi: true })).join('\n'); |
| 91 | |
| 92 | expect(output).toContain('currently signed in to Composio as a regular user'); |
| 93 | expect(output).toContain('composio signup'); |
| 94 | expect(exitCodeOf(exit)).toBe(1); |
| 95 | }) |
| 96 | ); |
| 97 | }); |
| 98 | }); |