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 } from '@effect/vitest'; |
| 2 | import { ChildProcessSpawner } from 'effect/process'; |
| 3 | import { ConfigProvider, Effect, Layer, PlatformError } from 'effect'; |
| 4 | import { detectPermissionCallerAgentEffect } from 'src/services/permission-ui'; |
| 5 | import { HOST_CONFIG } from 'src/config'; |
| 6 | |
| 7 | const loadFlag = (entries: Record<string, string>) => |
| 8 | HOST_CONFIG.INTERACTIVE_PERMISSION_UI_DISABLED.pipe( |
| 9 | Effect.provideService(ConfigProvider.ConfigProvider, ConfigProvider.fromEnv({ env: entries })) |
| 10 | ); |
| 11 | |
| 12 | const executorLayer = (spawn: Parameters<typeof ChildProcessSpawner.make>[0]) => |
| 13 | Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, ChildProcessSpawner.make(spawn)); |
| 14 | |
| 15 | // Environment-based detection must resolve before `ps` is ever spawned. |
| 16 | const processTreeMustNotRun = executorLayer(() => |
| 17 | Effect.die(new Error('the process tree must not be consulted')) |
| 18 | ); |
| 19 | |
| 20 | // Spawn failures during the process-tree walk resolve to the default agent. |
| 21 | const processTreeUnavailable = executorLayer(() => |
| 22 | Effect.fail(PlatformError.systemError({ _tag: 'NotFound', module: 'Command', method: 'spawn' })) |
| 23 | ); |
| 24 | |
| 25 | const detectAgent = ( |
| 26 | env: Record<string, string>, |
| 27 | layer: Layer.Layer<ChildProcessSpawner.ChildProcessSpawner> |
| 28 | ) => |
| 29 | Effect.gen(function* () { |
| 30 | const signals = yield* HOST_CONFIG.CALLER_AGENT_SIGNALS.pipe( |
| 31 | Effect.provideService(ConfigProvider.ConfigProvider, ConfigProvider.fromEnv({ env })) |
| 32 | ); |
| 33 | return yield* detectPermissionCallerAgentEffect(signals); |
| 34 | }).pipe(Effect.provide(layer)); |
| 35 | |
| 36 | describe('permission UI', () => { |
| 37 | it.effect('disables interactive permission UI in CI and Vitest environments', () => |
| 38 | Effect.gen(function* () { |
| 39 | expect(yield* loadFlag({})).toBe(false); |
| 40 | expect(yield* loadFlag({ CI: 'true' })).toBe(true); |
| 41 | expect(yield* loadFlag({ CI: '1' })).toBe(true); |
| 42 | expect(yield* loadFlag({ CI: 'woohoo' })).toBe(true); |
| 43 | expect(yield* loadFlag({ CI: 'false' })).toBe(false); |
| 44 | expect(yield* loadFlag({ CI: '' })).toBe(false); |
| 45 | expect(yield* loadFlag({ VITEST: 'true' })).toBe(true); |
| 46 | }) |
| 47 | ); |
| 48 | |
| 49 | it.effect( |
| 50 | 'lets COMPOSIO_DISABLE_PERMISSION_UI override environment detection in both directions', |
| 51 | () => |
| 52 | Effect.gen(function* () { |
| 53 | expect(yield* loadFlag({ COMPOSIO_DISABLE_PERMISSION_UI: '1' })).toBe(true); |
| 54 | expect(yield* loadFlag({ COMPOSIO_DISABLE_PERMISSION_UI: 'true' })).toBe(true); |
| 55 | expect( |
| 56 | yield* loadFlag({ COMPOSIO_DISABLE_PERMISSION_UI: '0', CI: 'true', VITEST: 'true' }) |
| 57 | ).toBe(false); |
| 58 | expect(yield* loadFlag({ COMPOSIO_DISABLE_PERMISSION_UI: '', CI: 'true' })).toBe(true); |
| 59 | }) |
| 60 | ); |
| 61 | |
| 62 | it.effect('honours explicit caller-agent overrides without consulting the process tree', () => |
| 63 | Effect.gen(function* () { |
| 64 | expect(yield* detectAgent({ COMPOSIO_CALLER_AGENT: 'Claude' }, processTreeMustNotRun)).toBe( |
| 65 | 'claude' |
| 66 | ); |
| 67 | expect(yield* detectAgent({ COMPOSIO_AGENT: 'open-claw' }, processTreeMustNotRun)).toBe( |
| 68 | 'openclaw' |
| 69 | ); |
| 70 | expect( |
| 71 | yield* detectAgent( |
| 72 | { COMPOSIO_CALLER_AGENT: 'codex', CLAUDE_CODE: '1' }, |
| 73 | processTreeMustNotRun |
| 74 | ) |
| 75 | ).toBe('codex'); |
| 76 | }) |
| 77 | ); |
| 78 | |
| 79 | it.effect('detects the caller agent from environment prefixes', () => |
| 80 | Effect.gen(function* () { |
| 81 | expect(yield* detectAgent({ OPENCLAW_FUTURE_MARKER: '1' }, processTreeMustNotRun)).toBe( |
| 82 | 'openclaw' |
| 83 | ); |
| 84 | expect(yield* detectAgent({ CLAUDE_FUTURE_MARKER: 'cli' }, processTreeMustNotRun)).toBe( |
| 85 | 'claude' |
| 86 | ); |
| 87 | expect(yield* detectAgent({ CODEX_FUTURE_MARKER: '1' }, processTreeMustNotRun)).toBe('codex'); |
| 88 | }) |
| 89 | ); |
| 90 | |
| 91 | it.effect( |
| 92 | 'falls back to composio when nothing matches and the process tree cannot be walked', |
| 93 | () => |
| 94 | Effect.gen(function* () { |
| 95 | expect(yield* detectAgent({}, processTreeUnavailable)).toBe('composio'); |
| 96 | }) |
| 97 | ); |
| 98 | }); |