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, it } from '@effect/vitest'; |
| 2 | import { Effect, Option } from 'effect'; |
| 3 | import { afterEach, vi } from 'vitest'; |
| 4 | import { |
| 5 | runAgentSignup, |
| 6 | type AgentSignupParams, |
| 7 | } from 'src/commands/agent/signup/agent.signup.handler'; |
| 8 | import { readStoredAgentIdentity, writeStoredAgentIdentity } from 'src/services/agents'; |
| 9 | import { MockConsole } from 'test/__utils__'; |
| 10 | import { AgentHandlerTest } from 'test/__utils__/layers/agents'; |
| 11 | import { agentsJsonResponse, makeAgentIdentity } from 'test/__utils__/models/agent'; |
| 12 | import { exitCodeOf } from 'test/__utils__/exit-code'; |
| 13 | |
| 14 | const params = (overrides: Partial<AgentSignupParams> = {}): AgentSignupParams => ({ |
| 15 | noWait: false, |
| 16 | noLogin: false, |
| 17 | force: false, |
| 18 | ...overrides, |
| 19 | }); |
| 20 | |
| 21 | const pendingIdentity = makeAgentIdentity({ |
| 22 | status: 'PENDING', |
| 23 | composio_agent_key: 'cak_pending', |
| 24 | composio: undefined, |
| 25 | }); |
| 26 | |
| 27 | /** The note body for the default READY identity. */ |
| 28 | const readyNote = [ |
| 29 | '[Composio Agent] Type: agent', |
| 30 | 'Status: READY', |
| 31 | 'Email: test-agent@agent.composio.ai', |
| 32 | 'Slug: test-agent', |
| 33 | 'Current Org ID: org_agent', |
| 34 | 'Project ID: proj_agent', |
| 35 | 'Agent key: stored in ~/.composio/agent.json', |
| 36 | ].join('\n'); |
| 37 | |
| 38 | const INTRO = '-- composio agent signup --'; |
| 39 | const INFO = |
| 40 | 'composio agent signup signs you up as a Composio agent. This flow is fully non-interactive and does not open a browser.'; |
| 41 | const OUTRO = '-- Agent setup complete. --'; |
| 42 | |
| 43 | const summaryJson = (identity: ReturnType<typeof makeAgentIdentity>, loggedIn: boolean) => |
| 44 | JSON.stringify({ |
| 45 | account_type: 'agent', |
| 46 | status: identity.status, |
| 47 | slug: identity.slug, |
| 48 | email: identity.email, |
| 49 | org_id: identity.composio?.org_id ?? null, |
| 50 | project_id: identity.composio?.project_id ?? null, |
| 51 | member_id: identity.composio?.member_id ?? null, |
| 52 | claimed_by: null, |
| 53 | claimed_at: null, |
| 54 | logged_in: loggedIn, |
| 55 | }); |
| 56 | |
| 57 | describe('runAgentSignup', () => { |
| 58 | afterEach(() => { |
| 59 | vi.restoreAllMocks(); |
| 60 | }); |
| 61 | |
| 62 | it.effect( |
| 63 | '[Given] no stored identity [Then] signs up, waits, stores the identity, and logs in', |
| 64 | () => { |
| 65 | const { layer, outputs, logins } = AgentHandlerTest(); |
| 66 | return Effect.gen(function* () { |
| 67 | const fetchSpy = vi |
| 68 | .spyOn(globalThis, 'fetch') |
| 69 | .mockImplementation(async () => agentsJsonResponse(makeAgentIdentity())); |
| 70 | |
| 71 | yield* runAgentSignup(params()); |
| 72 | |
| 73 | expect(fetchSpy).toHaveBeenCalledExactlyOnceWith( |
| 74 | 'https://agents.composio.dev/api/signup', |
| 75 | expect.objectContaining({ method: 'POST', body: '{}' }) |
| 76 | ); |
| 77 | expect(Option.getOrThrow(yield* readStoredAgentIdentity)).toMatchObject({ |
| 78 | composio_agent_key: 'cak_test_agent', |
| 79 | agent_key: 'cak_test_agent', |
| 80 | }); |
| 81 | expect(logins).toEqual([{ apiKey: 'uak_agent', orgId: 'org_agent' }]); |
| 82 | expect(yield* MockConsole.getLines({ stripAnsi: true })).toEqual([ |
| 83 | INTRO, |
| 84 | INFO, |
| 85 | readyNote, |
| 86 | 'Logged in with the Composio agent identity.', |
| 87 | OUTRO, |
| 88 | ]); |
| 89 | expect(outputs).toEqual([{ data: summaryJson(makeAgentIdentity(), true), force: false }]); |
| 90 | }).pipe(Effect.provide(layer)); |
| 91 | } |
| 92 | ); |
| 93 | |
| 94 | it.effect( |
| 95 | '[Given] --no-wait [Then] starts the signup, reports it pending, and stays logged out', |
| 96 | () => { |
| 97 | const { layer, outputs, logins } = AgentHandlerTest(); |
| 98 | return Effect.gen(function* () { |
| 99 | const fetchSpy = vi |
| 100 | .spyOn(globalThis, 'fetch') |
| 101 | .mockImplementation(async () => agentsJsonResponse(pendingIdentity)); |
| 102 | |
| 103 | yield* runAgentSignup(params({ noWait: true })); |
| 104 | |
| 105 | expect(fetchSpy).toHaveBeenCalledExactlyOnceWith( |
| 106 | 'https://agents.composio.dev/api/signup?wait=0', |
| 107 | expect.objectContaining({ method: 'POST' }) |
| 108 | ); |
| 109 | expect(Option.getOrThrow(yield* readStoredAgentIdentity).composio_agent_key).toBe( |
| 110 | 'cak_pending' |
| 111 | ); |
| 112 | expect(logins).toEqual([]); |
| 113 | expect(yield* MockConsole.getLines({ stripAnsi: true })).toEqual([ |
| 114 | INTRO, |
| 115 | INFO, |
| 116 | [ |
| 117 | '[Composio Agent] Type: agent', |
| 118 | 'Status: PENDING', |
| 119 | 'Email: test-agent@agent.composio.ai', |
| 120 | 'Slug: test-agent', |
| 121 | 'Current Org ID: pending', |
| 122 | 'Project ID: pending', |
| 123 | 'Agent key: stored in ~/.composio/agent.json', |
| 124 | ].join('\n'), |
| 125 | 'Signup is pending. Run `composio agent whoami` to poll status.', |
| 126 | OUTRO, |
| 127 | ]); |
| 128 | expect(outputs).toEqual([{ data: summaryJson(pendingIdentity, false), force: false }]); |
| 129 | }).pipe(Effect.provide(layer)); |
| 130 | } |
| 131 | ); |
| 132 | |
| 133 | it.effect('[Given] --no-login [Then] signs up without logging in or a pending note', () => { |
| 134 | const { layer, outputs, logins } = AgentHandlerTest(); |
| 135 | return Effect.gen(function* () { |
| 136 | vi.spyOn(globalThis, 'fetch').mockImplementation(async () => |
| 137 | agentsJsonResponse(makeAgentIdentity()) |
| 138 | ); |
| 139 | |
| 140 | yield* runAgentSignup(params({ noLogin: true })); |
| 141 | |
| 142 | expect(logins).toEqual([]); |
| 143 | expect(yield* MockConsole.getLines({ stripAnsi: true })).toEqual([ |
| 144 | INTRO, |
| 145 | INFO, |
| 146 | readyNote, |
| 147 | OUTRO, |
| 148 | ]); |
| 149 | expect(outputs).toEqual([{ data: summaryJson(makeAgentIdentity(), false), force: false }]); |
| 150 | }).pipe(Effect.provide(layer)); |
| 151 | }); |
| 152 | |
| 153 | it.effect( |
| 154 | '[Given] a stored READY identity [Then] refreshes and reuses it without signing up', |
| 155 | () => { |
| 156 | const { layer, logins } = AgentHandlerTest(); |
| 157 | return Effect.gen(function* () { |
| 158 | yield* writeStoredAgentIdentity(makeAgentIdentity()); |
| 159 | const fetchSpy = vi |
| 160 | .spyOn(globalThis, 'fetch') |
| 161 | .mockImplementation(async () => agentsJsonResponse(makeAgentIdentity())); |
| 162 | |
| 163 | yield* runAgentSignup(params()); |
| 164 | |
| 165 | expect(fetchSpy).toHaveBeenCalledExactlyOnceWith( |
| 166 | 'https://agents.composio.dev/api/whoami', |
| 167 | expect.objectContaining({ |
| 168 | method: 'GET', |
| 169 | headers: expect.objectContaining({ Authorization: 'Bearer cak_test_agent' }), |
| 170 | }) |
| 171 | ); |
| 172 | expect(logins).toEqual([{ apiKey: 'uak_agent', orgId: 'org_agent' }]); |
| 173 | }).pipe(Effect.provide(layer)); |
| 174 | } |
| 175 | ); |
| 176 | |
| 177 | it.effect('[Given] a stored identity and --force [Then] signs up a new agent', () => { |
| 178 | const { layer } = AgentHandlerTest(); |
| 179 | return Effect.gen(function* () { |
| 180 | yield* writeStoredAgentIdentity(makeAgentIdentity({ composio_agent_key: 'cak_old' })); |
| 181 | const fetchSpy = vi |
| 182 | .spyOn(globalThis, 'fetch') |
| 183 | .mockImplementation(async () => agentsJsonResponse(makeAgentIdentity())); |
| 184 | |
| 185 | yield* runAgentSignup(params({ force: true, noLogin: true })); |
| 186 | |
| 187 | expect(fetchSpy).toHaveBeenCalledExactlyOnceWith( |
| 188 | 'https://agents.composio.dev/api/signup', |
| 189 | expect.objectContaining({ method: 'POST' }) |
| 190 | ); |
| 191 | expect(Option.getOrThrow(yield* readStoredAgentIdentity).composio_agent_key).toBe( |
| 192 | 'cak_test_agent' |
| 193 | ); |
| 194 | }).pipe(Effect.provide(layer)); |
| 195 | }); |
| 196 | |
| 197 | it.effect('[Given] a human login [Then] warns, exits 1, and signs nothing up', () => { |
| 198 | const { layer, outputs, logins } = AgentHandlerTest({ apiKey: 'uak_human' }); |
| 199 | return Effect.gen(function* () { |
| 200 | const fetchSpy = vi.spyOn(globalThis, 'fetch'); |
| 201 | |
| 202 | const exit = yield* Effect.exit(runAgentSignup(params())); |
| 203 | |
| 204 | expect(yield* MockConsole.getLines({ stream: 'stderr', stripAnsi: true })).toEqual([ |
| 205 | 'This CLI is currently signed in to Composio as a regular user, not an agent.', |
| 206 | expect.stringMatching(/^Next steps:\n/), |
| 207 | ]); |
| 208 | expect(exitCodeOf(exit)).toBe(1); |
| 209 | expect(fetchSpy).not.toHaveBeenCalled(); |
| 210 | expect(logins).toEqual([]); |
| 211 | expect(outputs).toEqual([]); |
| 212 | }).pipe(Effect.provide(layer)); |
| 213 | }); |
| 214 | |
| 215 | it.effect('[Given] the agents API refuses the signup [Then] fails and stores nothing', () => { |
| 216 | const { layer, outputs, logins } = AgentHandlerTest(); |
| 217 | return Effect.gen(function* () { |
| 218 | vi.spyOn(globalThis, 'fetch').mockImplementation(async () => |
| 219 | agentsJsonResponse({ message: 'Signup unavailable' }, 503) |
| 220 | ); |
| 221 | |
| 222 | const error = yield* Effect.flip(runAgentSignup(params())); |
| 223 | |
| 224 | expect(error).toMatchObject({ |
| 225 | _tag: 'services/AgentRequestError', |
| 226 | message: 'Signup unavailable', |
| 227 | pathname: '/api/signup', |
| 228 | status: 503, |
| 229 | }); |
| 230 | expect(Option.isNone(yield* readStoredAgentIdentity)).toBe(true); |
| 231 | expect(logins).toEqual([]); |
| 232 | expect(outputs).toEqual([]); |
| 233 | }).pipe(Effect.provide(layer)); |
| 234 | }); |
| 235 | }); |