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 { runAgentWhoami } from 'src/commands/agent/whoami/agent.whoami.handler'; |
| 5 | import { readStoredAgentIdentity, writeStoredAgentIdentity } from 'src/services/agents'; |
| 6 | import { MockConsole } from 'test/__utils__'; |
| 7 | import { AgentHandlerTest } from 'test/__utils__/layers/agents'; |
| 8 | import { agentsJsonResponse, makeAgentIdentity } from 'test/__utils__/models/agent'; |
| 9 | import { exitCodeOf } from 'test/__utils__/exit-code'; |
| 10 | |
| 11 | const decoration = Effect.map(MockConsole.getLines({ stripAnsi: true }), lines => lines.join('\n')); |
| 12 | |
| 13 | describe('runAgentWhoami', () => { |
| 14 | afterEach(() => { |
| 15 | vi.restoreAllMocks(); |
| 16 | }); |
| 17 | |
| 18 | it.effect( |
| 19 | '[Given] a stored agent key [Then] refreshes and saves the identity, and prints its summary', |
| 20 | () => { |
| 21 | const { layer, outputs } = AgentHandlerTest(); |
| 22 | return Effect.gen(function* () { |
| 23 | yield* writeStoredAgentIdentity(makeAgentIdentity({ status: 'PENDING', composio: {} })); |
| 24 | const fetchSpy = vi |
| 25 | .spyOn(globalThis, 'fetch') |
| 26 | .mockImplementation(async () => |
| 27 | agentsJsonResponse(makeAgentIdentity({ claimed_by: 'owner@example.com' })) |
| 28 | ); |
| 29 | |
| 30 | yield* runAgentWhoami(); |
| 31 | |
| 32 | expect(fetchSpy).toHaveBeenCalledExactlyOnceWith( |
| 33 | 'https://agents.composio.dev/api/whoami', |
| 34 | expect.objectContaining({ |
| 35 | method: 'GET', |
| 36 | headers: expect.objectContaining({ Authorization: 'Bearer cak_test_agent' }), |
| 37 | }) |
| 38 | ); |
| 39 | const stored = Option.getOrThrow(yield* readStoredAgentIdentity); |
| 40 | expect(stored).toMatchObject({ status: 'READY', claimed_by: 'owner@example.com' }); |
| 41 | expect(yield* decoration).toBe( |
| 42 | [ |
| 43 | '[Composio Agent] Type: agent', |
| 44 | 'Status: READY', |
| 45 | 'Email: test-agent@agent.composio.ai', |
| 46 | 'Slug: test-agent', |
| 47 | 'Current Org ID: org_agent', |
| 48 | 'Project ID: proj_agent', |
| 49 | 'Claimed By: owner@example.com', |
| 50 | ].join('\n') |
| 51 | ); |
| 52 | expect(outputs).toEqual([ |
| 53 | { |
| 54 | data: JSON.stringify({ |
| 55 | account_type: 'agent', |
| 56 | status: 'READY', |
| 57 | slug: 'test-agent', |
| 58 | email: 'test-agent@agent.composio.ai', |
| 59 | org_id: 'org_agent', |
| 60 | project_id: 'proj_agent', |
| 61 | member_id: 'mem_agent', |
| 62 | claimed_by: 'owner@example.com', |
| 63 | claimed_at: null, |
| 64 | }), |
| 65 | force: false, |
| 66 | }, |
| 67 | ]); |
| 68 | }).pipe(Effect.provide(layer)); |
| 69 | } |
| 70 | ); |
| 71 | |
| 72 | it.effect('[Given] a pending identity [Then] prints placeholders for the missing fields', () => { |
| 73 | const { layer } = AgentHandlerTest(); |
| 74 | return Effect.gen(function* () { |
| 75 | yield* writeStoredAgentIdentity(makeAgentIdentity()); |
| 76 | vi.spyOn(globalThis, 'fetch').mockImplementation(async () => |
| 77 | agentsJsonResponse({ status: 'pending' }) |
| 78 | ); |
| 79 | |
| 80 | yield* runAgentWhoami(); |
| 81 | |
| 82 | expect(yield* decoration).toBe( |
| 83 | [ |
| 84 | '[Composio Agent] Type: agent', |
| 85 | 'Status: PENDING', |
| 86 | 'Email: unknown', |
| 87 | 'Slug: unknown', |
| 88 | 'Current Org ID: pending', |
| 89 | 'Project ID: pending', |
| 90 | 'Claimed By: not claimed', |
| 91 | ].join('\n') |
| 92 | ); |
| 93 | // The key the request used is kept on the refreshed identity. |
| 94 | const stored = Option.getOrThrow(yield* readStoredAgentIdentity); |
| 95 | expect(stored.composio_agent_key).toBe('cak_test_agent'); |
| 96 | }).pipe(Effect.provide(layer)); |
| 97 | }); |
| 98 | |
| 99 | it.effect.each([ |
| 100 | { |
| 101 | name: 'no stored identity', |
| 102 | apiKey: undefined, |
| 103 | stored: Option.none(), |
| 104 | message: 'No Composio agent identity is saved on this machine.', |
| 105 | }, |
| 106 | { |
| 107 | name: 'a stored identity without its key', |
| 108 | apiKey: undefined, |
| 109 | stored: Option.some(makeAgentIdentity({ composio_agent_key: undefined })), |
| 110 | message: 'The saved Composio agent identity is missing its composio_agent_key.', |
| 111 | }, |
| 112 | { |
| 113 | name: 'a human login', |
| 114 | apiKey: 'uak_human', |
| 115 | stored: Option.some(makeAgentIdentity()), |
| 116 | message: 'This CLI is currently signed in to Composio as a regular user, not an agent.', |
| 117 | }, |
| 118 | ])( |
| 119 | '[Given] $name [Then] warns with next steps, exits 1, and calls nothing', |
| 120 | ({ apiKey, stored, message }) => { |
| 121 | const { layer, outputs } = AgentHandlerTest({ apiKey }); |
| 122 | return Effect.gen(function* () { |
| 123 | yield* Effect.forEach(Option.toArray(stored), writeStoredAgentIdentity); |
| 124 | const fetchSpy = vi.spyOn(globalThis, 'fetch'); |
| 125 | |
| 126 | const exit = yield* Effect.exit(runAgentWhoami()); |
| 127 | |
| 128 | // Plain lines on stderr: a headless terminal drops `ui.log.*` decoration. |
| 129 | const stderr = yield* MockConsole.getLines({ stream: 'stderr', stripAnsi: true }); |
| 130 | expect(stderr).toEqual([message, expect.stringMatching(/^Next steps:\n/)]); |
| 131 | expect(exitCodeOf(exit)).toBe(1); |
| 132 | expect(fetchSpy).not.toHaveBeenCalled(); |
| 133 | expect(outputs).toEqual([]); |
| 134 | }).pipe(Effect.provide(layer)); |
| 135 | } |
| 136 | ); |
| 137 | |
| 138 | it.effect( |
| 139 | '[Given] a key the agents API rejects [Then] fails and keeps the stored identity', |
| 140 | () => { |
| 141 | const { layer, outputs } = AgentHandlerTest(); |
| 142 | return Effect.gen(function* () { |
| 143 | const original = yield* writeStoredAgentIdentity(makeAgentIdentity()); |
| 144 | vi.spyOn(globalThis, 'fetch').mockImplementation(async () => |
| 145 | agentsJsonResponse({ message: 'Invalid agent key' }, 401) |
| 146 | ); |
| 147 | |
| 148 | const error = yield* Effect.flip(runAgentWhoami()); |
| 149 | |
| 150 | expect(error).toMatchObject({ |
| 151 | _tag: 'services/AgentRequestError', |
| 152 | message: 'Invalid agent key', |
| 153 | status: 401, |
| 154 | }); |
| 155 | expect(Option.getOrThrow(yield* readStoredAgentIdentity)).toEqual(original); |
| 156 | expect(outputs).toEqual([]); |
| 157 | }).pipe(Effect.provide(layer)); |
| 158 | } |
| 159 | ); |
| 160 | }); |