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 | /** |
| 2 | * Which invocation hooks a command runs, and when: only for a command `effect/cli` selected, |
| 3 | * when it begins (before its invoked event) and before it checks a login, minus what its |
| 4 | * `CommandMeta` turns off. |
| 5 | */ |
| 6 | import { describe, expect, it, layer } from '@effect/vitest'; |
| 7 | import { Cause, Effect, Exit, Option, Schema } from 'effect'; |
| 8 | import { Command } from 'effect/cli'; |
| 9 | import { buildRootCommand } from 'src/commands'; |
| 10 | import { NotLoggedInError } from 'src/commands/errors'; |
| 11 | import { withRegisteredSubcommands } from 'src/commands/registration'; |
| 12 | import { |
| 13 | commandPathOf, |
| 14 | identityAt, |
| 15 | INVOCATION_HOOKS, |
| 16 | type InvocationHookName, |
| 17 | runsHook, |
| 18 | withCommandMeta, |
| 19 | } from 'src/invocation/command-identity'; |
| 20 | import { cliExit, recordedInvocationCalls, resetInvocationCalls, TestLive } from 'test/__utils__'; |
| 21 | |
| 22 | const HOOK_SERVICES: Readonly<Record<InvocationHookName, string>> = { |
| 23 | backendMismatch: 'BackendMismatchWarning', |
| 24 | updateNotice: 'UpdateNotice', |
| 25 | pluginHint: 'PluginHint', |
| 26 | backgroundUpdateCheck: 'BackgroundUpdateCheck', |
| 27 | }; |
| 28 | |
| 29 | /** The hook services and lifecycle events `cli(args)` recorded, in order. */ |
| 30 | const sequenceOf = (args: ReadonlyArray<string>) => |
| 31 | Effect.gen(function* () { |
| 32 | // Suites share one recorder. |
| 33 | yield* resetInvocationCalls; |
| 34 | const result = yield* cliExit(args); |
| 35 | const calls = yield* recordedInvocationCalls; |
| 36 | return { |
| 37 | ...result, |
| 38 | sequence: calls.map(call => |
| 39 | call.service === 'CommandLifecycleTelemetry' ? call.event : call.service |
| 40 | ), |
| 41 | hooked: calls.flatMap(call => |
| 42 | call.service === 'CommandLifecycleTelemetry' |
| 43 | ? [] |
| 44 | : [commandPathOf(call.invocation.identity)] |
| 45 | ), |
| 46 | }; |
| 47 | }); |
| 48 | |
| 49 | describe('invocation hooks', () => { |
| 50 | layer(TestLive())('when they run', it => { |
| 51 | it.effect( |
| 52 | '[Given] a selected command [Then] its hooks run before its invoked event, for its identity', |
| 53 | () => |
| 54 | Effect.gen(function* () { |
| 55 | const { exitCode, sequence, hooked } = yield* sequenceOf(['version']); |
| 56 | |
| 57 | expect(exitCode).toBe(0); |
| 58 | expect(sequence).toEqual([ |
| 59 | ...INVOCATION_HOOKS.map(hook => HOOK_SERVICES[hook]), |
| 60 | 'invoked', |
| 61 | 'succeeded', |
| 62 | ]); |
| 63 | expect(hooked).toEqual(INVOCATION_HOOKS.map(() => 'version')); |
| 64 | }) |
| 65 | ); |
| 66 | |
| 67 | it.effect( |
| 68 | '[Given] a login-gated command without a login [Then] the hooks run, then it fails with NotLoggedInError for its own path', |
| 69 | () => |
| 70 | Effect.gen(function* () { |
| 71 | const { exit, sequence, hooked } = yield* sequenceOf(['tools', 'list', 'gmail']); |
| 72 | const calls = yield* recordedInvocationCalls; |
| 73 | |
| 74 | expect( |
| 75 | Exit.isFailure(exit) && |
| 76 | Option.exists( |
| 77 | Cause.findErrorOption(exit.cause), |
| 78 | error => error instanceof NotLoggedInError |
| 79 | ) |
| 80 | ).toBe(true); |
| 81 | expect(sequence).toEqual([ |
| 82 | ...INVOCATION_HOOKS.map(hook => HOOK_SERVICES[hook]), |
| 83 | 'invoked', |
| 84 | 'failed', |
| 85 | ]); |
| 86 | expect(hooked).toEqual(INVOCATION_HOOKS.map(() => 'tools list')); |
| 87 | expect( |
| 88 | calls.flatMap(call => |
| 89 | call.service === 'CommandLifecycleTelemetry' && call.event === 'failed' |
| 90 | ? [call.context.commandPath] |
| 91 | : [] |
| 92 | ) |
| 93 | ).toEqual(['tools list']); |
| 94 | }) |
| 95 | ); |
| 96 | |
| 97 | it.effect.each([ |
| 98 | { name: '--help', args: ['--help'] }, |
| 99 | { name: 'a subcommand --help', args: ['upgrade', '--help'] }, |
| 100 | { name: '--version', args: ['--version'] }, |
| 101 | { name: 'a typo', args: ['whoamii'] }, |
| 102 | { name: 'a missing argument', args: ['dev', 'triggers', 'list'] }, |
| 103 | { name: 'the bare root', args: [] }, |
| 104 | { name: 'a bare group (`tools`)', args: ['tools'] }, |
| 105 | { name: 'a bare group (`orgs`)', args: ['orgs'] }, |
| 106 | { name: 'a bare nested group (`dev triggers`)', args: ['dev', 'triggers'] }, |
| 107 | { name: '`help`', args: ['help'] }, |
| 108 | { name: '`help <command>`', args: ['help', 'tools'] }, |
| 109 | ])('[Given] $name [Then] no hook runs', ({ args }) => |
| 110 | Effect.gen(function* () { |
| 111 | const { hooked } = yield* sequenceOf(args); |
| 112 | expect(hooked).toEqual([]); |
| 113 | }) |
| 114 | ); |
| 115 | |
| 116 | // `dev`'s own handler is the developer-mode toggle, an action of its own, not its help. |
| 117 | it.effect.each([ |
| 118 | { name: 'dev --mode off', args: ['dev', '--mode', 'off'] }, |
| 119 | { name: 'dev', args: ['dev'] }, |
| 120 | ])("[Given] a group's own action (`$name`) [Then] its hooks run, for the group", ({ args }) => |
| 121 | Effect.gen(function* () { |
| 122 | const { hooked } = yield* sequenceOf(args); |
| 123 | expect(hooked).toEqual(INVOCATION_HOOKS.map(() => 'dev')); |
| 124 | }) |
| 125 | ); |
| 126 | }); |
| 127 | }); |
| 128 | |
| 129 | describe('hook opt-outs from CommandMeta', () => { |
| 130 | const root = buildRootCommand({ |
| 131 | isDevModeEnabled: true, |
| 132 | isExperimentalFeatureEnabled: () => true, |
| 133 | }); |
| 134 | |
| 135 | /** Commands that turn hooks off (and some that do not), with the input `effect/cli` parses. */ |
| 136 | const COMMANDS: ReadonlyArray<{ |
| 137 | readonly name: string; |
| 138 | readonly path: ReadonlyArray<string>; |
| 139 | readonly input: unknown; |
| 140 | readonly off: ReadonlyArray<InvocationHookName>; |
| 141 | }> = [ |
| 142 | { name: 'version', path: ['version'], input: { check: false }, off: [] }, |
| 143 | { |
| 144 | name: 'version --check', |
| 145 | path: ['version'], |
| 146 | input: { check: true }, |
| 147 | off: ['backgroundUpdateCheck'], |
| 148 | }, |
| 149 | { |
| 150 | name: 'upgrade', |
| 151 | path: ['upgrade'], |
| 152 | input: {}, |
| 153 | off: ['updateNotice', 'backgroundUpdateCheck'], |
| 154 | }, |
| 155 | { name: 'setup', path: ['setup'], input: {}, off: ['pluginHint'] }, |
| 156 | { name: 'setup skill', path: ['setup', 'skill'], input: {}, off: ['pluginHint'] }, |
| 157 | { name: 'help', path: ['help'], input: {}, off: INVOCATION_HOOKS }, |
| 158 | { name: 'help tools', path: ['help', 'tools'], input: {}, off: INVOCATION_HOOKS }, |
| 159 | { name: 'whoami', path: ['whoami'], input: {}, off: [] }, |
| 160 | { name: 'dev triggers list', path: ['dev', 'triggers', 'list'], input: {}, off: [] }, |
| 161 | ]; |
| 162 | |
| 163 | const cases = COMMANDS.flatMap(command => |
| 164 | INVOCATION_HOOKS.map(hook => ({ ...command, hook, runs: !command.off.includes(hook) })) |
| 165 | ); |
| 166 | |
| 167 | it.each(cases)('`composio $name` runs $hook: $runs', ({ path, input, hook, runs }) => { |
| 168 | const identity = identityAt(root, ['composio', ...path]); |
| 169 | expect(identity.path).toEqual(path); |
| 170 | expect(runsHook(identity.meta, hook, input)).toBe(runs); |
| 171 | }); |
| 172 | |
| 173 | it('a subcommand inherits a boolean policy, never a predicate over its group input', () => { |
| 174 | const groupInput = Schema.is(Schema.Struct({ quiet: Schema.Literal(true) })); |
| 175 | const seen: Array<unknown> = []; |
| 176 | const group = Command.make('group').pipe( |
| 177 | withRegisteredSubcommands([Command.make('leaf', {}, () => Effect.void)]), |
| 178 | withCommandMeta({ |
| 179 | hooks: { |
| 180 | updateNotice: false, |
| 181 | pluginHint: input => { |
| 182 | seen.push(input); |
| 183 | return !groupInput(input); |
| 184 | }, |
| 185 | }, |
| 186 | }) |
| 187 | ); |
| 188 | const tree = Command.make('app').pipe(withRegisteredSubcommands([group])); |
| 189 | const groupMeta = identityAt(tree, ['app', 'group']).meta; |
| 190 | const leafMeta = identityAt(tree, ['app', 'group', 'leaf']).meta; |
| 191 | |
| 192 | expect(runsHook(groupMeta, 'pluginHint', { quiet: true })).toBe(false); |
| 193 | expect(runsHook(leafMeta, 'pluginHint', { quiet: true })).toBe(true); |
| 194 | expect(runsHook(leafMeta, 'updateNotice', {})).toBe(false); |
| 195 | expect(seen).toEqual([{ quiet: true }]); |
| 196 | }); |
| 197 | }); |