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 { afterEach, vi } from 'vitest'; |
| 3 | import * as BunFileSystem from '@effect/platform-bun/BunFileSystem'; |
| 4 | import * as BunPath from '@effect/platform-bun/BunPath'; |
| 5 | import { Cause, Effect, Layer } from 'effect'; |
| 6 | import type { TelemetryFamily } from 'src/analytics/types'; |
| 7 | import { ChildProcessExited } from 'src/effects/child-process-exited'; |
| 8 | import { |
| 9 | CommandLifecycleTelemetry, |
| 10 | type CommandLifecycleTelemetryShape, |
| 11 | } from 'src/services/command-lifecycle-telemetry'; |
| 12 | import { NodeOs } from 'src/services/node-os'; |
| 13 | import { TerminalUITest } from 'test/__utils__/services/terminal-ui-test'; |
| 14 | import { commandTelemetryContext } from 'test/__utils__/telemetry-context'; |
| 15 | import { trackedEvents } from 'test/__utils__/tracked-events'; |
| 16 | |
| 17 | vi.mock('src/analytics/dispatch', async importOriginal => { |
| 18 | const { recordTrackedEvent } = await import('test/__utils__/tracked-events'); |
| 19 | return { |
| 20 | ...(await importOriginal<typeof import('src/analytics/dispatch')>()), |
| 21 | trackCliEventEffect: recordTrackedEvent, |
| 22 | }; |
| 23 | }); |
| 24 | |
| 25 | afterEach(() => { |
| 26 | trackedEvents.length = 0; |
| 27 | }); |
| 28 | |
| 29 | const TelemetryLive = Layer.provide( |
| 30 | CommandLifecycleTelemetry.Default, |
| 31 | Layer.mergeAll(BunFileSystem.layer, BunPath.layer, NodeOs.Default, TerminalUITest) |
| 32 | ); |
| 33 | |
| 34 | // The root commands these tests run are named after their families, except generic `whoami`. |
| 35 | const contextFor = (args: ReadonlyArray<string>) => { |
| 36 | const [name = 'whoami'] = args; |
| 37 | return commandTelemetryContext(args, { |
| 38 | path: [name], |
| 39 | family: name === 'whoami' ? 'command' : (name as TelemetryFamily), |
| 40 | input: {}, |
| 41 | }); |
| 42 | }; |
| 43 | |
| 44 | const report = ( |
| 45 | telemetry: CommandLifecycleTelemetryShape, |
| 46 | outcome: 'invoked' | 'succeeded' | 'failed', |
| 47 | args: ReadonlyArray<string> |
| 48 | ) => { |
| 49 | const context = contextFor(args); |
| 50 | switch (outcome) { |
| 51 | case 'invoked': |
| 52 | return telemetry.invoked(context); |
| 53 | case 'succeeded': |
| 54 | return telemetry.succeeded(context); |
| 55 | case 'failed': |
| 56 | return telemetry.failed(context, Cause.fail(new TypeError('boom'))); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | const cases = [ |
| 61 | { args: ['whoami'], family: 'CLI_COMMAND' }, |
| 62 | { args: ['execute', 'GITHUB_GET_THE_AUTHENTICATED_USER'], family: 'CLI_EXECUTE' }, |
| 63 | ].flatMap(({ args, family }) => |
| 64 | (['invoked', 'succeeded', 'failed'] as const).map(outcome => ({ |
| 65 | args, |
| 66 | command: args[0], |
| 67 | outcome, |
| 68 | event: `${family}_${outcome.toUpperCase()}`, |
| 69 | })) |
| 70 | ); |
| 71 | |
| 72 | describe('CommandLifecycleTelemetry', () => { |
| 73 | it.effect.each(cases)( |
| 74 | '[Given] `composio $command` $outcome [Then] tracks $event', |
| 75 | ({ args, outcome, event }) => |
| 76 | Effect.gen(function* () { |
| 77 | const telemetry = yield* CommandLifecycleTelemetry; |
| 78 | yield* report(telemetry, outcome, args); |
| 79 | expect(trackedEvents.map(tracked => tracked.name)).toEqual([event]); |
| 80 | }).pipe(Effect.provide(TelemetryLive)) |
| 81 | ); |
| 82 | |
| 83 | it.effect('[Given] a failure cause [Then] tracks the squashed error', () => |
| 84 | Effect.gen(function* () { |
| 85 | const telemetry = yield* CommandLifecycleTelemetry; |
| 86 | yield* telemetry.failed( |
| 87 | contextFor(['run', 'script.ts']), |
| 88 | Cause.fail(new ChildProcessExited({ code: 3 })) |
| 89 | ); |
| 90 | expect(trackedEvents).toEqual([ |
| 91 | { |
| 92 | name: 'CLI_RUN_FAILED', |
| 93 | properties: expect.objectContaining({ |
| 94 | failure_reason_code: 'child_exit', |
| 95 | }), |
| 96 | }, |
| 97 | ]); |
| 98 | }).pipe(Effect.provide(TelemetryLive)) |
| 99 | ); |
| 100 | }); |