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 * as fs from 'node:fs'; |
| 2 | import * as os from 'node:os'; |
| 3 | import * as path from 'node:path'; |
| 4 | import * as BunFileSystem from '@effect/platform-bun/BunFileSystem'; |
| 5 | import * as BunPath from '@effect/platform-bun/BunPath'; |
| 6 | import { describe, expect, it } from '@effect/vitest'; |
| 7 | import { afterEach, vi } from 'vitest'; |
| 8 | import { isAPIError } from '@composio/client/effect'; |
| 9 | import { ConfigProvider, Deferred, Effect, Fiber, Layer, Option } from 'effect'; |
| 10 | import { TestClock } from 'effect/testing'; |
| 11 | import { |
| 12 | FetchHttpClient, |
| 13 | HttpClient, |
| 14 | HttpClientError, |
| 15 | HttpClientResponse, |
| 16 | type HttpClientRequest, |
| 17 | } from 'effect/http'; |
| 18 | import { |
| 19 | DashboardToolExecution, |
| 20 | DashboardToolExecutionError, |
| 21 | resolveConsumerExecutionTransport, |
| 22 | } from 'src/services/dashboard-tool-execution'; |
| 23 | import { cliRequestHeaders } from 'src/services/client-provenance'; |
| 24 | import { defaultNodeOs, NodeOs } from 'src/services/node-os'; |
| 25 | import { ComposioUserContext } from 'src/services/user-context'; |
| 26 | import { extractApiErrorDetails } from 'src/utils/api-error-extraction'; |
| 27 | |
| 28 | interface RecordedRequest { |
| 29 | readonly method: string; |
| 30 | readonly url: string; |
| 31 | readonly headers: Record<string, string>; |
| 32 | readonly body: unknown; |
| 33 | readonly signal: AbortSignal; |
| 34 | } |
| 35 | |
| 36 | type Respond = ( |
| 37 | request: HttpClientRequest.HttpClientRequest |
| 38 | ) => Effect.Effect<Response, HttpClientError.HttpClientError>; |
| 39 | |
| 40 | const jsonResponse = (status: number, body: unknown) => |
| 41 | new Response(JSON.stringify(body), { |
| 42 | status, |
| 43 | headers: { 'content-type': 'application/json' }, |
| 44 | }); |
| 45 | |
| 46 | const succeedWith = |
| 47 | (outcome: unknown): Respond => |
| 48 | () => |
| 49 | Effect.succeed(jsonResponse(200, { result: { data: { json: outcome } } })); |
| 50 | |
| 51 | const requestBody = (request: HttpClientRequest.HttpClientRequest): unknown => |
| 52 | request.body._tag === 'Uint8Array' |
| 53 | ? JSON.parse(new TextDecoder().decode(request.body.body)) |
| 54 | : null; |
| 55 | |
| 56 | /** The filesystem the service reads the analytics state from, rooted at `home`. */ |
| 57 | const platform = (home: string) => |
| 58 | Layer.mergeAll( |
| 59 | BunFileSystem.layer, |
| 60 | BunPath.layer, |
| 61 | Layer.succeed(NodeOs, defaultNodeOs({ homedir: home })) |
| 62 | ); |
| 63 | type Platform = Layer.Success<ReturnType<typeof platform>>; |
| 64 | |
| 65 | const temporaryHomes: Array<string> = []; |
| 66 | const emptyHome = () => { |
| 67 | const home = fs.mkdtempSync(path.join(os.tmpdir(), 'composio-cli-test-home-')); |
| 68 | temporaryHomes.push(home); |
| 69 | return home; |
| 70 | }; |
| 71 | |
| 72 | afterEach(() => { |
| 73 | temporaryHomes.forEach(home => fs.rmSync(home, { recursive: true, force: true })); |
| 74 | temporaryHomes.length = 0; |
| 75 | }); |
| 76 | |
| 77 | const userContext = ( |
| 78 | overrides: { webURL?: string; apiKey?: Option.Option<string>; home?: string } = {} |
| 79 | ) => |
| 80 | Layer.succeed( |
| 81 | ComposioUserContext, |
| 82 | ComposioUserContext.of({ |
| 83 | data: { |
| 84 | apiKey: overrides.apiKey ?? Option.some('uak_test_key'), |
| 85 | baseURL: 'https://backend.example.test', |
| 86 | webURL: overrides.webURL ?? 'https://dashboard.example.test/', |
| 87 | orgId: Option.some('org_test'), |
| 88 | projectId: Option.none(), |
| 89 | testUserId: Option.none(), |
| 90 | }, |
| 91 | backendMismatch: Option.none(), |
| 92 | isLoggedIn: () => true, |
| 93 | logout: Effect.void, |
| 94 | login: () => Effect.void, |
| 95 | update: () => Effect.void, |
| 96 | }) |
| 97 | ); |
| 98 | |
| 99 | /** Runs `use` against the real service over a recording HTTP client. */ |
| 100 | const withDashboard = <A, E>( |
| 101 | respond: Respond, |
| 102 | use: ( |
| 103 | dashboard: DashboardToolExecution['Service'], |
| 104 | requests: ReadonlyArray<RecordedRequest> |
| 105 | ) => Effect.Effect<A, E, Platform>, |
| 106 | overrides?: Parameters<typeof userContext>[0] |
| 107 | ) => { |
| 108 | const requests: Array<RecordedRequest> = []; |
| 109 | const httpClient = HttpClient.make((request, url, signal) => { |
| 110 | requests.push({ |
| 111 | method: request.method, |
| 112 | url: url.toString(), |
| 113 | headers: { ...request.headers }, |
| 114 | body: requestBody(request), |
| 115 | signal, |
| 116 | }); |
| 117 | return respond(request).pipe( |
| 118 | Effect.map(response => HttpClientResponse.fromWeb(request, response)) |
| 119 | ); |
| 120 | }); |
| 121 | |
| 122 | return Effect.gen(function* () { |
| 123 | const dashboard = yield* DashboardToolExecution; |
| 124 | return yield* use(dashboard, requests); |
| 125 | }).pipe( |
| 126 | Effect.provide( |
| 127 | DashboardToolExecution.Default.pipe( |
| 128 | Layer.provide( |
| 129 | Layer.mergeAll(Layer.succeed(HttpClient.HttpClient, httpClient), userContext(overrides)) |
| 130 | ) |
| 131 | ) |
| 132 | ), |
| 133 | Effect.provide(platform(overrides?.home ?? emptyHome())), |
| 134 | Effect.provide( |
| 135 | ConfigProvider.layer( |
| 136 | ConfigProvider.fromEnv({ |
| 137 | env: Object.fromEntries( |
| 138 | Object.entries(process.env).filter( |
| 139 | (entry): entry is [string, string] => entry[1] !== undefined |
| 140 | ) |
| 141 | ), |
| 142 | }) |
| 143 | ) |
| 144 | ) |
| 145 | ); |
| 146 | }; |
| 147 | |
| 148 | const successOutcome = { |
| 149 | ok: true, |
| 150 | response: { |
| 151 | result_type: 'completed', |
| 152 | data: { id: 'msg_1' }, |
| 153 | error: null, |
| 154 | log_id: 'log_dashboard', |
| 155 | }, |
| 156 | }; |
| 157 | |
| 158 | const executeRequest = { |
| 159 | slug: 'GMAIL_SEND_EMAIL', |
| 160 | arguments: { recipient: 'a@example.com' }, |
| 161 | orgId: 'org_test', |
| 162 | }; |
| 163 | |
| 164 | describe('DashboardToolExecution', () => { |
| 165 | it.effect('posts a tool execution to the execute procedure and returns the relayed body', () => |
| 166 | withDashboard(succeedWith(successOutcome), (dashboard, requests) => |
| 167 | Effect.gen(function* () { |
| 168 | const response = yield* dashboard.execute(executeRequest); |
| 169 | |
| 170 | expect(response).toEqual({ |
| 171 | result_type: 'completed', |
| 172 | data: { id: 'msg_1' }, |
| 173 | error: null, |
| 174 | log_id: 'log_dashboard', |
| 175 | }); |
| 176 | expect(requests).toHaveLength(1); |
| 177 | const [request] = requests; |
| 178 | expect(request?.method).toBe('POST'); |
| 179 | expect(request?.url).toBe('https://dashboard.example.test/api/cli/trpc/execute'); |
| 180 | expect(request?.headers['authorization']).toBe('Bearer uak_test_key'); |
| 181 | expect(request?.headers['x-org-id']).toBe('org_test'); |
| 182 | expect(request?.headers['content-type']).toBe('application/json'); |
| 183 | expect(request?.headers['x-source']).toBe('CLI'); |
| 184 | expect(request?.headers['x-user-api-key']).toBeUndefined(); |
| 185 | // Telemetry is off under test unless a case turns it on. |
| 186 | expect(request?.headers['x-cli-install-id']).toBeUndefined(); |
| 187 | expect(request?.body).toEqual({ |
| 188 | json: { |
| 189 | tool_slug: 'GMAIL_SEND_EMAIL', |
| 190 | arguments: { recipient: 'a@example.com' }, |
| 191 | }, |
| 192 | }); |
| 193 | }) |
| 194 | ) |
| 195 | ); |
| 196 | |
| 197 | it.effect('sends the selected connected account with the execution, and omits it otherwise', () => |
| 198 | withDashboard(succeedWith(successOutcome), (dashboard, requests) => |
| 199 | Effect.gen(function* () { |
| 200 | yield* dashboard.execute({ ...executeRequest, account: 'ca_1' }); |
| 201 | yield* dashboard.execute(executeRequest); |
| 202 | |
| 203 | expect(requests[0]?.body).toEqual({ |
| 204 | json: { |
| 205 | tool_slug: 'GMAIL_SEND_EMAIL', |
| 206 | arguments: { recipient: 'a@example.com' }, |
| 207 | account: 'ca_1', |
| 208 | }, |
| 209 | }); |
| 210 | expect(requests[1]?.body).toEqual({ |
| 211 | json: { tool_slug: 'GMAIL_SEND_EMAIL', arguments: { recipient: 'a@example.com' } }, |
| 212 | }); |
| 213 | }) |
| 214 | ) |
| 215 | ); |
| 216 | |
| 217 | it.effect('posts a meta tool to the executeMeta procedure under `slug`', () => |
| 218 | withDashboard(succeedWith(successOutcome), (dashboard, requests) => |
| 219 | Effect.gen(function* () { |
| 220 | yield* dashboard.executeMeta({ |
| 221 | slug: 'COMPOSIO_SEARCH_TOOLS', |
| 222 | arguments: { query: 'email' }, |
| 223 | orgId: 'org_test', |
| 224 | }); |
| 225 | |
| 226 | expect(requests[0]?.url).toBe('https://dashboard.example.test/api/cli/trpc/executeMeta'); |
| 227 | expect(requests[0]?.body).toEqual({ |
| 228 | json: { slug: 'COMPOSIO_SEARCH_TOOLS', arguments: { query: 'email' } }, |
| 229 | }); |
| 230 | }) |
| 231 | ) |
| 232 | ); |
| 233 | |
| 234 | describe.each(['https://web.example.test', 'https://web.example.test/'])('%s', webURL => { |
| 235 | it.effect(`builds the same request URL from ${webURL}`, () => |
| 236 | withDashboard( |
| 237 | succeedWith(successOutcome), |
| 238 | (dashboard, requests) => |
| 239 | Effect.gen(function* () { |
| 240 | yield* dashboard.execute(executeRequest); |
| 241 | expect(requests[0]?.url).toBe('https://web.example.test/api/cli/trpc/execute'); |
| 242 | }), |
| 243 | { webURL } |
| 244 | ) |
| 245 | ); |
| 246 | }); |
| 247 | |
| 248 | it.effect('fails a relayed backend failure with the client status error', () => |
| 249 | withDashboard( |
| 250 | succeedWith({ |
| 251 | ok: false, |
| 252 | status: 402, |
| 253 | error: { |
| 254 | message: 'Insufficient wallet balance', |
| 255 | code: 4020, |
| 256 | slug: 'Wallet_InsufficientBalance', |
| 257 | status: 402, |
| 258 | request_id: 'req_wallet', |
| 259 | suggested_fix: 'Add credits and retry.', |
| 260 | }, |
| 261 | }), |
| 262 | dashboard => |
| 263 | Effect.gen(function* () { |
| 264 | const failure = yield* dashboard.execute(executeRequest).pipe(Effect.flip); |
| 265 | |
| 266 | // The client's own status class for a 402, as the backend transport fails with. |
| 267 | expect(isAPIError(failure) && failure._tag).toBe('@composio/client/APIError'); |
| 268 | expect(extractApiErrorDetails(failure)).toEqual({ |
| 269 | message: 'Insufficient wallet balance', |
| 270 | code: 4020, |
| 271 | slug: 'Wallet_InsufficientBalance', |
| 272 | status: 402, |
| 273 | request_id: 'req_wallet', |
| 274 | suggested_fix: 'Add credits and retry.', |
| 275 | }); |
| 276 | }) |
| 277 | ) |
| 278 | ); |
| 279 | |
| 280 | it.effect('suggests logging in when the Dashboard rejects the credentials', () => |
| 281 | withDashboard( |
| 282 | () => |
| 283 | Effect.succeed( |
| 284 | jsonResponse(401, { |
| 285 | error: { |
| 286 | json: { |
| 287 | message: 'Invalid API key', |
| 288 | data: { code: 'UNAUTHORIZED', httpStatus: 401 }, |
| 289 | }, |
| 290 | }, |
| 291 | }) |
| 292 | ), |
| 293 | (dashboard, requests) => |
| 294 | Effect.gen(function* () { |
| 295 | const failure = yield* dashboard.execute(executeRequest).pipe(Effect.flip); |
| 296 | |
| 297 | expect(failure).toBeInstanceOf(DashboardToolExecutionError); |
| 298 | expect(failure).toMatchObject({ reason: 'unauthorized', status: 401 }); |
| 299 | expect(failure.message).toContain('Invalid API key'); |
| 300 | expect(failure.message).toContain('composio login'); |
| 301 | expect(requests).toHaveLength(1); |
| 302 | }) |
| 303 | ) |
| 304 | ); |
| 305 | |
| 306 | it.effect("reports the Dashboard's own message for its other failures", () => |
| 307 | withDashboard( |
| 308 | () => |
| 309 | Effect.succeed( |
| 310 | jsonResponse(502, { |
| 311 | error: { |
| 312 | json: { |
| 313 | message: 'Tool execution is temporarily unavailable.', |
| 314 | data: { code: 'BAD_GATEWAY', httpStatus: 502 }, |
| 315 | }, |
| 316 | }, |
| 317 | }) |
| 318 | ), |
| 319 | (dashboard, requests) => |
| 320 | Effect.gen(function* () { |
| 321 | const failure = yield* dashboard.execute(executeRequest).pipe(Effect.flip); |
| 322 | |
| 323 | expect(failure).toMatchObject({ |
| 324 | reason: 'dashboard', |
| 325 | status: 502, |
| 326 | code: 'BAD_GATEWAY', |
| 327 | message: 'Tool execution is temporarily unavailable.', |
| 328 | }); |
| 329 | expect(requests).toHaveLength(1); |
| 330 | }) |
| 331 | ) |
| 332 | ); |
| 333 | |
| 334 | it.effect('names the Dashboard host on a network failure and sends the request once', () => |
| 335 | withDashboard( |
| 336 | request => |
| 337 | Effect.fail( |
| 338 | new HttpClientError.HttpClientError({ |
| 339 | reason: new HttpClientError.TransportError({ |
| 340 | request, |
| 341 | cause: new Error('connect ECONNREFUSED'), |
| 342 | }), |
| 343 | }) |
| 344 | ), |
| 345 | (dashboard, requests) => |
| 346 | Effect.gen(function* () { |
| 347 | const failure = yield* dashboard.execute(executeRequest).pipe(Effect.flip); |
| 348 | |
| 349 | expect(failure).toMatchObject({ reason: 'request' }); |
| 350 | expect(failure.message).toContain('dashboard.example.test'); |
| 351 | expect(requests).toHaveLength(1); |
| 352 | }) |
| 353 | ) |
| 354 | ); |
| 355 | |
| 356 | it.effect('names the Dashboard host when the response is not JSON', () => |
| 357 | withDashboard( |
| 358 | () => Effect.succeed(new Response('<html>Bad gateway</html>', { status: 502 })), |
| 359 | (dashboard, requests) => |
| 360 | Effect.gen(function* () { |
| 361 | const failure = yield* dashboard.execute(executeRequest).pipe(Effect.flip); |
| 362 | |
| 363 | expect(failure).toMatchObject({ reason: 'response', status: 502 }); |
| 364 | expect(failure.message).toContain('dashboard.example.test'); |
| 365 | expect(requests).toHaveLength(1); |
| 366 | }) |
| 367 | ) |
| 368 | ); |
| 369 | |
| 370 | it.effect('sends nothing when no user API key is stored', () => |
| 371 | withDashboard( |
| 372 | succeedWith(successOutcome), |
| 373 | (dashboard, requests) => |
| 374 | Effect.gen(function* () { |
| 375 | const failure = yield* dashboard.execute(executeRequest).pipe(Effect.flip); |
| 376 | |
| 377 | expect(failure).toMatchObject({ reason: 'unauthorized' }); |
| 378 | expect(failure.message).toContain('composio login'); |
| 379 | expect(requests).toHaveLength(0); |
| 380 | }), |
| 381 | { apiKey: Option.none() } |
| 382 | ) |
| 383 | ); |
| 384 | |
| 385 | it.effect('tells fetch to fail on a redirect instead of re-posting the execution', () => |
| 386 | Effect.gen(function* () { |
| 387 | const inits: Array<RequestInit | undefined> = []; |
| 388 | const fetchStub: typeof globalThis.fetch = Object.assign( |
| 389 | (_input: RequestInfo | URL, init?: RequestInit) => { |
| 390 | inits.push(init); |
| 391 | // What fetch does with `redirect: 'error'` when the server answers 307. |
| 392 | return Promise.reject(new TypeError('unexpected redirect')); |
| 393 | }, |
| 394 | { preconnect: () => undefined } |
| 395 | ); |
| 396 | |
| 397 | const failure = yield* Effect.gen(function* () { |
| 398 | const dashboard = yield* DashboardToolExecution; |
| 399 | return yield* dashboard.execute(executeRequest).pipe(Effect.flip); |
| 400 | }).pipe( |
| 401 | Effect.provide( |
| 402 | DashboardToolExecution.Default.pipe( |
| 403 | Layer.provide(Layer.mergeAll(FetchHttpClient.layer, userContext())) |
| 404 | ) |
| 405 | ), |
| 406 | Effect.provideService(FetchHttpClient.Fetch, fetchStub), |
| 407 | Effect.provide(platform(emptyHome())) |
| 408 | ); |
| 409 | |
| 410 | expect(inits).toHaveLength(1); |
| 411 | expect(inits[0]?.redirect).toBe('error'); |
| 412 | expect(inits[0]?.method).toBe('POST'); |
| 413 | expect(failure).toMatchObject({ reason: 'request' }); |
| 414 | }) |
| 415 | ); |
| 416 | |
| 417 | const rejectedURLs: ReadonlyArray<{ readonly name: string; readonly webURL: string }> = [ |
| 418 | { name: 'plain http to a remote host', webURL: 'http://dashboard.example.test' }, |
| 419 | { name: 'embedded credentials', webURL: 'https://user:s3cret-pass@dashboard.example.test' }, |
| 420 | { name: 'a query string', webURL: 'https://dashboard.example.test/?token=s3cret-token' }, |
| 421 | { name: 'a fragment', webURL: 'https://dashboard.example.test/#s3cret-fragment' }, |
| 422 | { name: 'a non-http scheme', webURL: 'ftp://dashboard.example.test' }, |
| 423 | { name: 'an unparseable value', webURL: 'not a url s3cret-value' }, |
| 424 | ]; |
| 425 | |
| 426 | describe.each(rejectedURLs)('$name', testCase => { |
| 427 | it.effect(`sends nothing to a Dashboard URL with ${testCase.name}`, () => |
| 428 | withDashboard( |
| 429 | succeedWith(successOutcome), |
| 430 | (dashboard, requests) => |
| 431 | Effect.gen(function* () { |
| 432 | const failure = yield* dashboard.execute(executeRequest).pipe(Effect.flip); |
| 433 | |
| 434 | expect(failure).toMatchObject({ reason: 'configuration' }); |
| 435 | expect(failure.message).toContain('COMPOSIO_WEB_URL'); |
| 436 | expect(failure.message).not.toContain('s3cret'); |
| 437 | expect(failure.message).not.toContain('example.test'); |
| 438 | expect(requests).toHaveLength(0); |
| 439 | }), |
| 440 | { webURL: testCase.webURL } |
| 441 | ) |
| 442 | ); |
| 443 | }); |
| 444 | |
| 445 | describe.each(['http://localhost:3000', 'http://127.0.0.1:3000/', 'http://[::1]:3000'])( |
| 446 | '%s', |
| 447 | webURL => { |
| 448 | it.effect(`allows plain http for the loopback host in ${webURL}`, () => |
| 449 | withDashboard( |
| 450 | succeedWith(successOutcome), |
| 451 | (dashboard, requests) => |
| 452 | Effect.gen(function* () { |
| 453 | yield* dashboard.execute(executeRequest); |
| 454 | expect(requests.map(request => request.url)).toEqual([ |
| 455 | `${webURL.replace(/\/$/, '')}/api/cli/trpc/execute`, |
| 456 | ]); |
| 457 | }), |
| 458 | { webURL } |
| 459 | ) |
| 460 | ); |
| 461 | } |
| 462 | ); |
| 463 | |
| 464 | it.effect('names only the origin of the Dashboard in a transport error', () => |
| 465 | withDashboard( |
| 466 | () => Effect.succeed(new Response('<html>Bad gateway</html>', { status: 502 })), |
| 467 | dashboard => |
| 468 | Effect.gen(function* () { |
| 469 | const failure = yield* dashboard.execute(executeRequest).pipe(Effect.flip); |
| 470 | |
| 471 | expect(failure.message).toContain('https://dashboard.example.test '); |
| 472 | expect(failure.message).not.toContain('tenant-path'); |
| 473 | }), |
| 474 | { webURL: 'https://dashboard.example.test/tenant-path/' } |
| 475 | ) |
| 476 | ); |
| 477 | |
| 478 | it.effect('gives up after the deadline, aborts the request and does not resend it', () => |
| 479 | Effect.gen(function* () { |
| 480 | const started = yield* Deferred.make<void>(); |
| 481 | |
| 482 | yield* withDashboard( |
| 483 | () => Deferred.succeed(started, undefined).pipe(Effect.andThen(Effect.never)), |
| 484 | (dashboard, requests) => |
| 485 | Effect.gen(function* () { |
| 486 | const fiber = yield* Effect.forkChild( |
| 487 | dashboard.execute(executeRequest).pipe(Effect.flip) |
| 488 | ); |
| 489 | yield* Deferred.await(started); |
| 490 | |
| 491 | yield* TestClock.adjust('14 minutes'); |
| 492 | expect(requests[0]?.signal.aborted).toBe(false); |
| 493 | yield* TestClock.adjust('1 minute'); |
| 494 | const failure = yield* Fiber.join(fiber); |
| 495 | |
| 496 | expect(failure).toMatchObject({ reason: 'timeout' }); |
| 497 | expect(failure.message).toContain('may still have run'); |
| 498 | expect(failure.message).toContain('check before running it again'); |
| 499 | expect(requests).toHaveLength(1); |
| 500 | expect(requests[0]?.signal.aborted).toBe(true); |
| 501 | }) |
| 502 | ); |
| 503 | }) |
| 504 | ); |
| 505 | |
| 506 | it.effect('aborts the in-flight request when the calling fiber is interrupted', () => |
| 507 | Effect.gen(function* () { |
| 508 | const started = yield* Deferred.make<void>(); |
| 509 | |
| 510 | yield* withDashboard( |
| 511 | () => Deferred.succeed(started, undefined).pipe(Effect.andThen(Effect.never)), |
| 512 | (dashboard, requests) => |
| 513 | Effect.gen(function* () { |
| 514 | const fiber = yield* Effect.forkChild(dashboard.execute(executeRequest)); |
| 515 | yield* Deferred.await(started); |
| 516 | expect(requests[0]?.signal.aborted).toBe(false); |
| 517 | |
| 518 | yield* Fiber.interrupt(fiber); |
| 519 | |
| 520 | expect(requests[0]?.signal.aborted).toBe(true); |
| 521 | expect(requests).toHaveLength(1); |
| 522 | }) |
| 523 | ); |
| 524 | }) |
| 525 | ); |
| 526 | }); |
| 527 | |
| 528 | describe('DashboardToolExecution install ID header', () => { |
| 529 | const INSTALL_ID = '6f1d2c1e-8a4b-4c0e-9f55-2f0d6d1b7a31'; |
| 530 | |
| 531 | afterEach(() => { |
| 532 | vi.unstubAllEnvs(); |
| 533 | }); |
| 534 | |
| 535 | /** The environment in which the CLI sends its own analytics events. */ |
| 536 | const enableTelemetry = () => { |
| 537 | vi.stubEnv('NODE_ENV', 'development'); |
| 538 | vi.stubEnv('CI', 'false'); |
| 539 | vi.stubEnv('COMPOSIO_CLI_TELEMETRY_DISABLED', 'false'); |
| 540 | vi.stubEnv('TELEMETRY_DISABLED', 'false'); |
| 541 | vi.stubEnv('COMPOSIO_DISABLE_TELEMETRY', 'false'); |
| 542 | vi.stubEnv('COMPOSIO_POSTHOG_PROJECT_API_KEY', 'phc_test_key'); |
| 543 | }; |
| 544 | |
| 545 | const analyticsStatePath = (home: string) => path.join(home, '.composio', 'analytics.json'); |
| 546 | |
| 547 | const homeWithAnalyticsState = (contents: string) => { |
| 548 | const home = emptyHome(); |
| 549 | fs.mkdirSync(path.join(home, '.composio'), { recursive: true }); |
| 550 | fs.writeFileSync(analyticsStatePath(home), contents); |
| 551 | return home; |
| 552 | }; |
| 553 | |
| 554 | const sentInstallId = (home: string) => |
| 555 | withDashboard( |
| 556 | succeedWith(successOutcome), |
| 557 | (dashboard, requests) => |
| 558 | Effect.gen(function* () { |
| 559 | const response = yield* dashboard.execute(executeRequest); |
| 560 | yield* dashboard.executeMeta({ ...executeRequest, slug: 'COMPOSIO_SEARCH_TOOLS' }); |
| 561 | expect(response.log_id).toBe('log_dashboard'); |
| 562 | expect(requests).toHaveLength(2); |
| 563 | return requests.map(request => request.headers['x-cli-install-id']); |
| 564 | }), |
| 565 | { home } |
| 566 | ); |
| 567 | |
| 568 | it.effect('sends the stored install ID verbatim on both procedures when telemetry is on', () => |
| 569 | Effect.gen(function* () { |
| 570 | enableTelemetry(); |
| 571 | const home = homeWithAnalyticsState( |
| 572 | JSON.stringify({ install_id: INSTALL_ID, apollo_user_id: 'user_linked' }) |
| 573 | ); |
| 574 | |
| 575 | expect(yield* sentInstallId(home)).toEqual([INSTALL_ID, INSTALL_ID]); |
| 576 | }) |
| 577 | ); |
| 578 | |
| 579 | describe.each([ |
| 580 | 'COMPOSIO_CLI_TELEMETRY_DISABLED', |
| 581 | 'TELEMETRY_DISABLED', |
| 582 | 'COMPOSIO_DISABLE_TELEMETRY', |
| 583 | 'CI', |
| 584 | ])('%s', optOut => { |
| 585 | it.effect(`omits the header when ${optOut} opts out of telemetry`, () => |
| 586 | Effect.gen(function* () { |
| 587 | enableTelemetry(); |
| 588 | vi.stubEnv(optOut, 'true'); |
| 589 | const home = homeWithAnalyticsState(JSON.stringify({ install_id: INSTALL_ID })); |
| 590 | |
| 591 | expect(yield* sentInstallId(home)).toEqual([undefined, undefined]); |
| 592 | }) |
| 593 | ); |
| 594 | }); |
| 595 | |
| 596 | it.effect('omits the header and creates no install ID when none is stored', () => |
| 597 | Effect.gen(function* () { |
| 598 | enableTelemetry(); |
| 599 | const home = emptyHome(); |
| 600 | |
| 601 | expect(yield* sentInstallId(home)).toEqual([undefined, undefined]); |
| 602 | expect(fs.existsSync(analyticsStatePath(home))).toBe(false); |
| 603 | }) |
| 604 | ); |
| 605 | |
| 606 | it.effect('still executes, without the header, when the analytics state is unreadable', () => |
| 607 | Effect.gen(function* () { |
| 608 | enableTelemetry(); |
| 609 | const home = homeWithAnalyticsState('{ not json'); |
| 610 | |
| 611 | expect(yield* sentInstallId(home)).toEqual([undefined, undefined]); |
| 612 | }) |
| 613 | ); |
| 614 | |
| 615 | it('is not part of the headers shared with backend requests', () => { |
| 616 | enableTelemetry(); |
| 617 | expect(Object.keys(cliRequestHeaders())).not.toContain('x-cli-install-id'); |
| 618 | }); |
| 619 | }); |
| 620 | |
| 621 | describe('resolveConsumerExecutionTransport', () => { |
| 622 | const resolveWith = (env: Record<string, string>) => |
| 623 | resolveConsumerExecutionTransport.pipe( |
| 624 | Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env }))) |
| 625 | ); |
| 626 | |
| 627 | const cases: ReadonlyArray<{ |
| 628 | readonly name: string; |
| 629 | readonly env: Record<string, string>; |
| 630 | readonly expected: 'dashboard' | 'backend'; |
| 631 | }> = [ |
| 632 | { name: 'no overrides', env: {}, expected: 'dashboard' }, |
| 633 | { |
| 634 | name: 'the staging environment', |
| 635 | env: { COMPOSIO_ENVIRONMENT: 'staging' }, |
| 636 | expected: 'dashboard', |
| 637 | }, |
| 638 | { |
| 639 | name: 'the default backend URL spelled out', |
| 640 | env: { COMPOSIO_BASE_URL: 'https://backend.composio.dev/' }, |
| 641 | expected: 'dashboard', |
| 642 | }, |
| 643 | { |
| 644 | name: 'a custom backend URL without a web URL', |
| 645 | env: { COMPOSIO_BASE_URL: 'https://composio.internal.example' }, |
| 646 | expected: 'backend', |
| 647 | }, |
| 648 | { |
| 649 | name: 'a backend URL that does not match the selected environment', |
| 650 | env: { |
| 651 | COMPOSIO_ENVIRONMENT: 'staging', |
| 652 | COMPOSIO_BASE_URL: 'https://backend.composio.dev', |
| 653 | }, |
| 654 | expected: 'backend', |
| 655 | }, |
| 656 | { |
| 657 | name: 'a custom backend URL with a web URL', |
| 658 | env: { |
| 659 | COMPOSIO_BASE_URL: 'https://composio.internal.example', |
| 660 | COMPOSIO_WEB_URL: 'https://dashboard.internal.example', |
| 661 | }, |
| 662 | expected: 'dashboard', |
| 663 | }, |
| 664 | ]; |
| 665 | |
| 666 | describe.each(cases)('$name', testCase => { |
| 667 | it.effect(`uses the ${testCase.expected} transport for ${testCase.name}`, () => |
| 668 | Effect.gen(function* () { |
| 669 | expect(yield* resolveWith(testCase.env)).toBe(testCase.expected); |
| 670 | }) |
| 671 | ); |
| 672 | }); |
| 673 | }); |