Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { describe, expect, it, vi } from '@effect/vitest'; |
| 2 | import * as BunFileSystem from '@effect/platform-bun/BunFileSystem'; |
| 3 | import * as BunPath from '@effect/platform-bun/BunPath'; |
| 4 | import { Effect, Exit, Layer } from 'effect'; |
| 5 | import { FetchHttpClient } from 'effect/http'; |
| 6 | import { ChildProcess, ChildProcessSpawner } from 'effect/process'; |
| 7 | import { CommandRunner } from 'src/services/command-runner'; |
| 8 | import { ComposioToolkitsRepository } from 'src/services/composio-clients'; |
| 9 | import { NodeOs } from 'src/services/node-os'; |
| 10 | import { SetupSkillInstaller } from 'src/services/setup-skill-installer'; |
| 11 | import { |
| 12 | getSessionInfoByUserApiKey, |
| 13 | listOrganizations, |
| 14 | resolveConsumerProject, |
| 15 | } from 'src/services/composio-account'; |
| 16 | import type { AuthConfigItem } from 'src/models/auth-configs'; |
| 17 | import type { ConnectedAccountItem } from 'src/models/connected-accounts'; |
| 18 | import { |
| 19 | CommandRunnerTest, |
| 20 | ConnectedAccountsTest, |
| 21 | SetupTest, |
| 22 | TestLive, |
| 23 | ToolkitsTest, |
| 24 | } from 'test/__utils__'; |
| 25 | import { makeSessionInfo } from 'test/__utils__/models/account'; |
| 26 | import { makeToolkitFixture } from 'test/__utils__/models/toolkits'; |
| 27 | import { TerminalUITest } from 'test/__utils__/services/terminal-ui-test'; |
| 28 | import { DIRECTORY_PROJECT_SCOPE } from 'test/__utils__/layers/developer-project'; |
| 29 | |
| 30 | describe('TestLayer', () => { |
| 31 | it.effect('leaves globalThis.fetch untouched', () => |
| 32 | Effect.gen(function* () { |
| 33 | const originalFetch = globalThis.fetch; |
| 34 | |
| 35 | yield* Effect.provide(Effect.void, TestLive()).pipe(Effect.scoped); |
| 36 | |
| 37 | expect(globalThis.fetch).toBe(originalFetch); |
| 38 | }) |
| 39 | ); |
| 40 | |
| 41 | it.effect('serves account endpoints from the mock client without calling fetch', () => |
| 42 | Effect.gen(function* () { |
| 43 | const fetchSpy = vi.spyOn(globalThis, 'fetch'); |
| 44 | |
| 45 | const [sessionInfo, consumerProject] = yield* Effect.all([ |
| 46 | getSessionInfoByUserApiKey({ userApiKey: 'uak_test', orgId: 'org_selected' }), |
| 47 | resolveConsumerProject({ apiKey: 'uak_test', orgId: 'org_selected' }), |
| 48 | ]).pipe( |
| 49 | Effect.provide( |
| 50 | TestLive({ |
| 51 | accountData: { |
| 52 | sessionInfo: scope => makeSessionInfo({ orgId: scope.orgId }), |
| 53 | }, |
| 54 | }) |
| 55 | ), |
| 56 | Effect.scoped |
| 57 | ); |
| 58 | |
| 59 | expect(sessionInfo.project.org.id).toBe('org_selected'); |
| 60 | expect(consumerProject.consumer_user_id).toBe('consumer-user-org_selected'); |
| 61 | expect(fetchSpy).not.toHaveBeenCalled(); |
| 62 | vi.restoreAllMocks(); |
| 63 | }) |
| 64 | ); |
| 65 | |
| 66 | it.effect('rejects an account endpoint without data as an unreachable backend would', () => |
| 67 | Effect.gen(function* () { |
| 68 | const error = yield* listOrganizations({ apiKey: 'uak_test' }).pipe( |
| 69 | Effect.provide(TestLive()), |
| 70 | Effect.scoped, |
| 71 | Effect.flip |
| 72 | ); |
| 73 | |
| 74 | expect(error).toMatchObject({ _tag: '@composio/client/APIConnectionError' }); |
| 75 | }) |
| 76 | ); |
| 77 | }); |
| 78 | |
| 79 | describe('ToolkitsTest', () => { |
| 80 | const toolkitsData = { |
| 81 | toolkits: [makeToolkitFixture('gmail', 'Gmail'), makeToolkitFixture('slack', 'Slack')], |
| 82 | }; |
| 83 | |
| 84 | it.effect.each([ |
| 85 | { name: 'ToolkitsTest', layer: ToolkitsTest(toolkitsData) }, |
| 86 | { name: 'TestLive', layer: TestLive({ toolkitsData }) }, |
| 87 | ])('serves the same catalog answers through $name', ({ layer }) => |
| 88 | Effect.gen(function* () { |
| 89 | const repo = yield* ComposioToolkitsRepository; |
| 90 | const search = yield* repo.searchToolkits({ search: 'gma' }); |
| 91 | const missing = yield* repo.getToolkitDetailed('nope').pipe(Effect.flip); |
| 92 | |
| 93 | expect(search.items.map(toolkit => toolkit.slug)).toEqual(['gmail']); |
| 94 | expect(missing).toMatchObject({ _tag: '@composio/client/NotFoundError', status: 404 }); |
| 95 | }).pipe(Effect.provide(layer), Effect.scoped) |
| 96 | ); |
| 97 | |
| 98 | it.effect('dies on a repository method another family serves', () => |
| 99 | Effect.gen(function* () { |
| 100 | const repo = yield* ComposioToolkitsRepository; |
| 101 | const exit = yield* repo.listAuthConfigs(DIRECTORY_PROJECT_SCOPE, {}).pipe(Effect.exit); |
| 102 | |
| 103 | expect(Exit.hasDies(exit)).toBe(true); |
| 104 | }).pipe(Effect.provide(ToolkitsTest())) |
| 105 | ); |
| 106 | }); |
| 107 | |
| 108 | describe('ConnectedAccountsTest', () => { |
| 109 | const authConfig = (id: string, toolkitSlug: string): AuthConfigItem => ({ |
| 110 | id, |
| 111 | name: id, |
| 112 | no_of_connections: 0, |
| 113 | status: 'ENABLED', |
| 114 | type: 'default', |
| 115 | uuid: `uuid-${id}`, |
| 116 | toolkit: { logo: '', slug: toolkitSlug }, |
| 117 | auth_scheme: 'OAUTH2', |
| 118 | is_composio_managed: true, |
| 119 | is_enabled_for_tool_router: true, |
| 120 | created_at: '2026-01-01T00:00:00Z', |
| 121 | }); |
| 122 | const connectedAccount: ConnectedAccountItem = { |
| 123 | id: 'con_gmail', |
| 124 | status: 'ACTIVE', |
| 125 | status_reason: null, |
| 126 | is_disabled: false, |
| 127 | user_id: 'default', |
| 128 | toolkit: { slug: 'gmail' }, |
| 129 | auth_config: { |
| 130 | id: 'ac_gmail', |
| 131 | auth_scheme: 'OAUTH2', |
| 132 | is_composio_managed: true, |
| 133 | is_disabled: false, |
| 134 | }, |
| 135 | created_at: '2026-01-01T00:00:00Z', |
| 136 | updated_at: '2026-01-01T00:00:00Z', |
| 137 | test_request_endpoint: '', |
| 138 | }; |
| 139 | const input = { |
| 140 | authConfigsData: { items: [authConfig('ac_gmail', 'gmail'), authConfig('ac_slack', 'slack')] }, |
| 141 | connectedAccountsData: { items: [connectedAccount] }, |
| 142 | }; |
| 143 | |
| 144 | it.effect.each([ |
| 145 | { name: 'ConnectedAccountsTest', layer: ConnectedAccountsTest(input) }, |
| 146 | { name: 'TestLive', layer: TestLive(input) }, |
| 147 | ])('serves the same auth-config and connected-account answers through $name', ({ layer }) => |
| 148 | Effect.gen(function* () { |
| 149 | const repo = yield* ComposioToolkitsRepository; |
| 150 | const authConfigs = yield* repo.listAuthConfigs(DIRECTORY_PROJECT_SCOPE, { |
| 151 | toolkit_slug: 'slack', |
| 152 | }); |
| 153 | const found = yield* repo.getConnectedAccount(DIRECTORY_PROJECT_SCOPE, 'con_gmail'); |
| 154 | const missing = yield* repo |
| 155 | .getAuthConfig(DIRECTORY_PROJECT_SCOPE, 'ac_nope') |
| 156 | .pipe(Effect.flip); |
| 157 | |
| 158 | expect(authConfigs.items.map(item => item.id)).toEqual(['ac_slack']); |
| 159 | expect(found.toolkit.slug).toBe('gmail'); |
| 160 | expect(missing).toMatchObject({ _tag: '@composio/client/NotFoundError', status: 404 }); |
| 161 | }).pipe(Effect.provide(layer), Effect.scoped) |
| 162 | ); |
| 163 | |
| 164 | it.effect('dies on a repository method another family serves', () => |
| 165 | Effect.gen(function* () { |
| 166 | const repo = yield* ComposioToolkitsRepository; |
| 167 | const exit = yield* repo.getToolkits().pipe(Effect.exit); |
| 168 | |
| 169 | expect(Exit.hasDies(exit)).toBe(true); |
| 170 | }).pipe(Effect.provide(ConnectedAccountsTest())) |
| 171 | ); |
| 172 | }); |
| 173 | |
| 174 | // The runner doubles never spawn; a spawner that dies on use proves it. |
| 175 | const noSpawn = Layer.succeed( |
| 176 | ChildProcessSpawner.ChildProcessSpawner, |
| 177 | ChildProcessSpawner.make(() => Effect.die('a runner double spawned a process')) |
| 178 | ); |
| 179 | const failingRunner = CommandRunner.of({ |
| 180 | run: () => Effect.succeed(3), |
| 181 | capture: () => Effect.succeed({ exitCode: 3, stdout: 'out', stderr: 'err' }), |
| 182 | }); |
| 183 | |
| 184 | describe('CommandRunnerTest', () => { |
| 185 | const command = ChildProcess.make('false'); |
| 186 | |
| 187 | it.effect.each([ |
| 188 | { name: 'CommandRunnerTest', layer: CommandRunnerTest() }, |
| 189 | { name: 'TestLive', layer: TestLive() }, |
| 190 | ])('exits 0 with empty output without spawning through $name', ({ layer }) => |
| 191 | Effect.gen(function* () { |
| 192 | const runner = yield* CommandRunner; |
| 193 | const exitCode = yield* runner.run(command); |
| 194 | const captured = yield* runner.capture(command); |
| 195 | |
| 196 | expect(exitCode).toBe(0); |
| 197 | expect(captured).toEqual({ exitCode: 0, stdout: '', stderr: '' }); |
| 198 | }).pipe(Effect.provide(Layer.merge(layer, noSpawn)), Effect.scoped) |
| 199 | ); |
| 200 | |
| 201 | it.effect.each([ |
| 202 | { name: 'CommandRunnerTest', layer: CommandRunnerTest(failingRunner) }, |
| 203 | { name: 'TestLive', layer: TestLive({ commandRunner: failingRunner }) }, |
| 204 | ])('answers with the runner the test passes through $name', ({ layer }) => |
| 205 | Effect.gen(function* () { |
| 206 | const runner = yield* CommandRunner; |
| 207 | const captured = yield* runner.capture(command); |
| 208 | |
| 209 | expect(captured).toEqual({ exitCode: 3, stdout: 'out', stderr: 'err' }); |
| 210 | }).pipe(Effect.provide(Layer.merge(layer, noSpawn)), Effect.scoped) |
| 211 | ); |
| 212 | }); |
| 213 | |
| 214 | describe('SetupTest', () => { |
| 215 | // `SetupSkillInstallerShape` names the services the real installer reads; the doubles read none. |
| 216 | const withInstallerServices = (layer: ReturnType<typeof SetupTest>) => |
| 217 | Layer.mergeAll( |
| 218 | layer, |
| 219 | BunFileSystem.layer, |
| 220 | BunPath.layer, |
| 221 | FetchHttpClient.layer, |
| 222 | NodeOs.Default, |
| 223 | TerminalUITest |
| 224 | ); |
| 225 | const readyInstaller = SetupSkillInstaller.of({ |
| 226 | isClaudeSkillReady: Effect.succeed(true), |
| 227 | hasManagedClaudeSkill: Effect.succeed(true), |
| 228 | ensureClaudeSkill: Effect.succeed(false), |
| 229 | removeClaudeSkill: Effect.succeed(true), |
| 230 | }); |
| 231 | |
| 232 | it.effect.each([ |
| 233 | { name: 'SetupTest', layer: withInstallerServices(SetupTest()) }, |
| 234 | { name: 'TestLive', layer: TestLive() }, |
| 235 | ])('installs no Claude skill and changes nothing, however often, through $name', ({ layer }) => |
| 236 | Effect.gen(function* () { |
| 237 | const installer = yield* SetupSkillInstaller; |
| 238 | const answers = yield* Effect.all([ |
| 239 | installer.ensureClaudeSkill, |
| 240 | installer.ensureClaudeSkill, |
| 241 | installer.isClaudeSkillReady, |
| 242 | installer.hasManagedClaudeSkill, |
| 243 | installer.removeClaudeSkill, |
| 244 | ]); |
| 245 | |
| 246 | expect(answers).toEqual([false, false, false, false, false]); |
| 247 | }).pipe(Effect.provide(layer), Effect.scoped) |
| 248 | ); |
| 249 | |
| 250 | it.effect.each([ |
| 251 | { |
| 252 | name: 'SetupTest', |
| 253 | layer: withInstallerServices( |
| 254 | SetupTest({ commandRunner: failingRunner, setupSkillInstaller: readyInstaller }) |
| 255 | ), |
| 256 | }, |
| 257 | { |
| 258 | name: 'TestLive', |
| 259 | layer: TestLive({ commandRunner: failingRunner, setupSkillInstaller: readyInstaller }), |
| 260 | }, |
| 261 | ])('answers with the runner and installer the test passes through $name', ({ layer }) => |
| 262 | Effect.gen(function* () { |
| 263 | const installer = yield* SetupSkillInstaller; |
| 264 | const runner = yield* CommandRunner; |
| 265 | const ready = yield* installer.isClaudeSkillReady; |
| 266 | const exitCode = yield* runner.run(ChildProcess.make('claude', ['--version'])); |
| 267 | |
| 268 | expect(ready).toBe(true); |
| 269 | expect(exitCode).toBe(3); |
| 270 | }).pipe(Effect.provide(Layer.merge(layer, noSpawn)), Effect.scoped) |
| 271 | ); |
| 272 | }); |