Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | import { describe, expect, it } from '@effect/vitest'; |
| 2 | import { afterEach, beforeEach, vi } from 'vitest'; |
| 3 | import * as BunFileSystem from '@effect/platform-bun/BunFileSystem'; |
| 4 | import * as BunPath from '@effect/platform-bun/BunPath'; |
| 5 | import { ConfigProvider, Effect, FileSystem, Layer, Path } from 'effect'; |
| 6 | import { mkdirSync, rmSync, writeFileSync } from 'node:fs'; |
| 7 | import path from 'node:path'; |
| 8 | import { FetchHttpClient } from 'effect/http'; |
| 9 | import * as tempy from 'tempy'; |
| 10 | import { |
| 11 | clearApolloIdentityForAnalytics, |
| 12 | emitPostHogAlias, |
| 13 | getCurrentCwdSessionId, |
| 14 | linkApolloIdentityForAnalytics, |
| 15 | readApiBaseUrl, |
| 16 | runBackgroundWorkerFromArgv, |
| 17 | trackCliCodactFailureEffect, |
| 18 | trackCliEventEffect, |
| 19 | } from 'src/analytics/dispatch'; |
| 20 | import { CLI_ANALYTICS_EVENTS } from 'src/analytics/events'; |
| 21 | import { CommandInvocation } from 'src/invocation/command-identity'; |
| 22 | import { APP_VERSION, USER_CONFIG_FILE_NAME } from 'src/constants'; |
| 23 | import { defaultNodeOs, NodeOs } from 'src/services/node-os'; |
| 24 | import { TerminalUITest } from 'test/__utils__/services/terminal-ui-test'; |
| 25 | import { liveEnvConfigProvider } from 'test/__utils__/live-env-config-provider'; |
| 26 | |
| 27 | const childProcessMocks = vi.hoisted(() => ({ |
| 28 | once: vi.fn(), |
| 29 | removeListener: vi.fn(), |
| 30 | spawn: vi.fn(), |
| 31 | unref: vi.fn(), |
| 32 | })); |
| 33 | |
| 34 | vi.mock('node:child_process', () => ({ |
| 35 | spawn: childProcessMocks.spawn, |
| 36 | })); |
| 37 | |
| 38 | const originalArgv = [...process.argv]; |
| 39 | |
| 40 | /** The cache key of `/workspace/project`: its djb2 hash in base 36. */ |
| 41 | const WORKSPACE_PROJECT_CWD_HASH = '1uj8qfp'; |
| 42 | |
| 43 | // Design contract: `FetchHttpClient.Fetch` is an `effect` `Context.Reference` |
| 44 | // whose default value (`globalThis.fetch`) is memoized on first read (see the |
| 45 | // vendored `effect` source, `Context.ts`'s `getOrCreateDefaultValue`) — once |
| 46 | // any test in this process reads the reference, later `vi.spyOn(globalThis, |
| 47 | // 'fetch')` swaps are invisible to `FetchHttpClient`. Mutating the global is |
| 48 | // therefore never a reliable way to observe or stub fetch calls made through |
| 49 | // this reference, in this file or any other. Every test that asserts on fetch |
| 50 | // — including tests asserting fetch was *not* called — must inject its own |
| 51 | // mock explicitly via `Layer.succeed(FetchHttpClient.Fetch, fetchImpl)`, |
| 52 | // which `makePlatformLayerWithFetch` below wires up. Tests that never |
| 53 | // exercise a fetch-reaching code path use the plain `makePlatformLayer` and |
| 54 | // don't touch `globalThis.fetch` at all. |
| 55 | // `vi.stubEnv` drives every scenario, so config reads go through a provider that re-reads |
| 56 | // the environment per lookup instead of the process-wide snapshot `ConfigProvider` defaults to. |
| 57 | const makePlatformLayer = (home: string) => |
| 58 | Layer.mergeAll( |
| 59 | BunFileSystem.layer, |
| 60 | BunPath.layer, |
| 61 | FetchHttpClient.layer, |
| 62 | ConfigProvider.layer(liveEnvConfigProvider), |
| 63 | TerminalUITest, |
| 64 | Layer.succeed(NodeOs, defaultNodeOs({ homedir: home })) |
| 65 | ); |
| 66 | |
| 67 | const makePlatformLayerWithFetch = (home: string, fetchImpl: typeof fetch) => |
| 68 | Layer.mergeAll( |
| 69 | BunFileSystem.layer, |
| 70 | BunPath.layer, |
| 71 | FetchHttpClient.layer, |
| 72 | ConfigProvider.layer(liveEnvConfigProvider), |
| 73 | Layer.succeed(FetchHttpClient.Fetch, fetchImpl), |
| 74 | TerminalUITest, |
| 75 | Layer.succeed(NodeOs, defaultNodeOs({ homedir: home })) |
| 76 | ); |
| 77 | |
| 78 | const enableTelemetry = (apiKey = 'uak_test') => { |
| 79 | vi.stubEnv('COMPOSIO_BASE_URL', 'https://backend.example.test'); |
| 80 | vi.stubEnv('COMPOSIO_USER_API_KEY', apiKey); |
| 81 | vi.stubEnv('NODE_ENV', 'development'); |
| 82 | vi.stubEnv('CI', 'false'); |
| 83 | vi.stubEnv('COMPOSIO_CLI_TELEMETRY_DISABLED', 'false'); |
| 84 | vi.stubEnv('TELEMETRY_DISABLED', 'false'); |
| 85 | vi.stubEnv('COMPOSIO_DISABLE_TELEMETRY', 'false'); |
| 86 | // A configured project key is what gates delivery/worker spawning; enabled |
| 87 | // telemetry implies a target. The no-key path is covered explicitly below. |
| 88 | vi.stubEnv('COMPOSIO_POSTHOG_PROJECT_API_KEY', 'phc_test_key'); |
| 89 | }; |
| 90 | |
| 91 | const decodeWorkerPayload = <A>(encodedPayload: string): A => { |
| 92 | const normalized = encodedPayload.replace(/-/gu, '+').replace(/_/gu, '/'); |
| 93 | const padded = normalized.padEnd(Math.ceil(normalized.length / 4) * 4, '='); |
| 94 | return JSON.parse(atob(padded)) as A; |
| 95 | }; |
| 96 | |
| 97 | const encodeWorkerPayload = (payload: unknown): string => |
| 98 | btoa(JSON.stringify(payload)).replace(/\+/gu, '-').replace(/\//gu, '_').replace(/=+$/u, ''); |
| 99 | |
| 100 | describe('CLI analytics dispatch', () => { |
| 101 | beforeEach(() => { |
| 102 | const child = { |
| 103 | once: childProcessMocks.once, |
| 104 | removeListener: childProcessMocks.removeListener, |
| 105 | unref: childProcessMocks.unref, |
| 106 | }; |
| 107 | // spawnDetached resolves only after Node's 'spawn' confirmation event, so |
| 108 | // the mock child fires it synchronously on registration. |
| 109 | childProcessMocks.once.mockReset().mockImplementation((event: string, listener: () => void) => { |
| 110 | if (event === 'spawn') { |
| 111 | listener(); |
| 112 | } |
| 113 | return child; |
| 114 | }); |
| 115 | childProcessMocks.removeListener.mockReset().mockReturnValue(child); |
| 116 | childProcessMocks.spawn.mockReset().mockReturnValue(child); |
| 117 | childProcessMocks.unref.mockReset(); |
| 118 | }); |
| 119 | |
| 120 | afterEach(() => { |
| 121 | vi.restoreAllMocks(); |
| 122 | vi.unstubAllEnvs(); |
| 123 | process.argv = [...originalArgv]; |
| 124 | }); |
| 125 | |
| 126 | it.effect( |
| 127 | 'reads configuration and the freshest current-directory session via platform services', |
| 128 | () => { |
| 129 | const home = tempy.temporaryDirectory(); |
| 130 | const cacheDir = tempy.temporaryDirectory(); |
| 131 | const cwd = '/workspace/project'; |
| 132 | vi.stubEnv('COMPOSIO_CACHE_DIR', cacheDir); |
| 133 | vi.stubEnv('COMPOSIO_BASE_URL', 'https://backend.example.test///'); |
| 134 | |
| 135 | return Effect.gen(function* () { |
| 136 | const fs = yield* FileSystem.FileSystem; |
| 137 | const path = yield* Path.Path; |
| 138 | yield* fs.writeFileString( |
| 139 | path.join(cacheDir, 'consumer-short-term-cache.json'), |
| 140 | JSON.stringify({ |
| 141 | older: { |
| 142 | probablyMyCliSessionsByCwdHash: { |
| 143 | [WORKSPACE_PROJECT_CWD_HASH]: { |
| 144 | id: 'cli_s_older', |
| 145 | // The TestClock sits at epoch 0, so these fixed instants are |
| 146 | // deterministically 30s and 60s in the future. |
| 147 | expiresAt: '1970-01-01T00:00:30.000Z', |
| 148 | }, |
| 149 | }, |
| 150 | }, |
| 151 | newer: { |
| 152 | probablyMyCliSessionsByCwdHash: { |
| 153 | [WORKSPACE_PROJECT_CWD_HASH]: { |
| 154 | id: 'cli_s_newer', |
| 155 | expiresAt: '1970-01-01T00:01:00.000Z', |
| 156 | }, |
| 157 | }, |
| 158 | }, |
| 159 | }) |
| 160 | ); |
| 161 | |
| 162 | expect(yield* readApiBaseUrl).toBe('https://backend.example.test'); |
| 163 | expect(yield* getCurrentCwdSessionId(cwd)).toBe('cli_s_newer'); |
| 164 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 165 | } |
| 166 | ); |
| 167 | |
| 168 | it.effect('sends failures to the backend the stored login was issued by', () => { |
| 169 | const home = tempy.temporaryDirectory(); |
| 170 | const cacheDir = tempy.temporaryDirectory(); |
| 171 | vi.stubEnv('COMPOSIO_CACHE_DIR', cacheDir); |
| 172 | // Blank reads as unset, whatever the host environment holds. |
| 173 | vi.stubEnv('COMPOSIO_BASE_URL', ''); |
| 174 | vi.stubEnv('COMPOSIO_ENVIRONMENT', ''); |
| 175 | vi.stubEnv('COMPOSIO_USER_API_KEY', ''); |
| 176 | writeFileSync( |
| 177 | path.join(cacheDir, USER_CONFIG_FILE_NAME), |
| 178 | JSON.stringify({ api_key: 'uak_staging', base_url: 'https://staging-backend.composio.dev/' }) |
| 179 | ); |
| 180 | |
| 181 | return Effect.gen(function* () { |
| 182 | expect(yield* readApiBaseUrl).toBe('https://staging-backend.composio.dev'); |
| 183 | vi.stubEnv('COMPOSIO_ENVIRONMENT', 'production'); |
| 184 | expect(yield* readApiBaseUrl).toBe('https://backend.composio.dev'); |
| 185 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 186 | }); |
| 187 | |
| 188 | it.effect('ignores malformed worker payloads', () => { |
| 189 | const home = tempy.temporaryDirectory(); |
| 190 | |
| 191 | return runBackgroundWorkerFromArgv([ |
| 192 | process.execPath, |
| 193 | 'composio', |
| 194 | '__analytics-worker', |
| 195 | 'not-base64', |
| 196 | ]).pipe(Effect.provide(makePlatformLayer(home))); |
| 197 | }); |
| 198 | |
| 199 | it.effect('delivers analytics worker payloads directly to PostHog', () => { |
| 200 | const home = tempy.temporaryDirectory(); |
| 201 | const envelope = { |
| 202 | event: 'cli_command_invoked', |
| 203 | properties: { cli_version: '9.9.9' }, |
| 204 | sentAt: '2026-07-16T00:00:00.000Z', |
| 205 | source: 'cli' as const, |
| 206 | distinctId: 'install_test', |
| 207 | installId: 'install_test', |
| 208 | }; |
| 209 | const encodedPayload = btoa(JSON.stringify(envelope)) |
| 210 | .replace(/\+/gu, '-') |
| 211 | .replace(/\//gu, '_') |
| 212 | .replace(/=+$/u, ''); |
| 213 | enableTelemetry(); |
| 214 | // Direct-to-PostHog transport does NOT read the Composio base URL. Overriding |
| 215 | // the ingest URL + a fake public key keeps the test off the real project. |
| 216 | vi.stubEnv('COMPOSIO_BASE_URL', ''); |
| 217 | vi.stubEnv('COMPOSIO_POSTHOG_INGEST_URL', 'https://posthog.example.test/i/v0/e/'); |
| 218 | vi.stubEnv('COMPOSIO_POSTHOG_PROJECT_API_KEY', 'phc_test_key'); |
| 219 | const fetchSpy = vi.fn().mockResolvedValue(new Response(null, { status: 200 })); |
| 220 | |
| 221 | return Effect.gen(function* () { |
| 222 | yield* runBackgroundWorkerFromArgv([ |
| 223 | process.execPath, |
| 224 | 'composio', |
| 225 | '__analytics-worker', |
| 226 | encodedPayload, |
| 227 | ]).pipe( |
| 228 | Effect.provide(makePlatformLayerWithFetch(home, fetchSpy as unknown as typeof fetch)) |
| 229 | ); |
| 230 | |
| 231 | expect(fetchSpy).toHaveBeenCalledTimes(1); |
| 232 | const [endpoint, request] = fetchSpy.mock.calls[0]!; |
| 233 | expect(String(endpoint)).toBe('https://posthog.example.test/i/v0/e/'); |
| 234 | expect(request).toMatchObject({ |
| 235 | method: 'POST', |
| 236 | headers: expect.objectContaining({ |
| 237 | 'content-type': 'application/json', |
| 238 | 'x-composio-analytics-source': 'cli', |
| 239 | }), |
| 240 | }); |
| 241 | // The public write key authenticates in-band; no user API key is attached. |
| 242 | expect((request?.headers as Record<string, string>)['x-user-api-key']).toBeUndefined(); |
| 243 | const body = JSON.parse(new TextDecoder().decode(request?.body as Uint8Array)); |
| 244 | expect(body).toMatchObject({ |
| 245 | api_key: 'phc_test_key', |
| 246 | event: 'cli_command_invoked', |
| 247 | distinct_id: 'install_test', |
| 248 | timestamp: '2026-07-16T00:00:00.000Z', |
| 249 | properties: expect.objectContaining({ |
| 250 | cli_version: '9.9.9', |
| 251 | install_id: 'install_test', |
| 252 | $lib: 'composio-cli', |
| 253 | }), |
| 254 | }); |
| 255 | }); |
| 256 | }); |
| 257 | |
| 258 | it.effect('skips PostHog delivery when no project key is configured', () => { |
| 259 | const home = tempy.temporaryDirectory(); |
| 260 | const envelope = { |
| 261 | event: 'cli_command_invoked', |
| 262 | sentAt: '2026-07-16T00:00:00.000Z', |
| 263 | source: 'cli' as const, |
| 264 | distinctId: 'install_test', |
| 265 | installId: 'install_test', |
| 266 | }; |
| 267 | const encodedPayload = btoa(JSON.stringify(envelope)) |
| 268 | .replace(/\+/gu, '-') |
| 269 | .replace(/\//gu, '_') |
| 270 | .replace(/=+$/u, ''); |
| 271 | enableTelemetry(); |
| 272 | // No project key -> the empty embedded placeholder, so the worker's delivery |
| 273 | // is a safe no-op until the real key is baked in. |
| 274 | vi.stubEnv('COMPOSIO_POSTHOG_PROJECT_API_KEY', ''); |
| 275 | const fetchSpy = vi |
| 276 | .spyOn(globalThis, 'fetch') |
| 277 | .mockResolvedValue(new Response(null, { status: 200 })); |
| 278 | |
| 279 | return Effect.gen(function* () { |
| 280 | yield* runBackgroundWorkerFromArgv([ |
| 281 | process.execPath, |
| 282 | 'composio', |
| 283 | '__analytics-worker', |
| 284 | encodedPayload, |
| 285 | ]).pipe(Effect.provide(makePlatformLayer(home))); |
| 286 | |
| 287 | expect(fetchSpy).not.toHaveBeenCalled(); |
| 288 | }); |
| 289 | }); |
| 290 | |
| 291 | it.effect('does not spawn a worker when no project key is configured', () => { |
| 292 | const home = tempy.temporaryDirectory(); |
| 293 | const scriptPath = `${home}/composio.ts`; |
| 294 | enableTelemetry(); |
| 295 | // Empty key: forks / local builds without a baked key must not spawn a |
| 296 | // detached worker on every command just to have it no-op. |
| 297 | vi.stubEnv('COMPOSIO_POSTHOG_PROJECT_API_KEY', ''); |
| 298 | process.argv[1] = scriptPath; |
| 299 | |
| 300 | return Effect.gen(function* () { |
| 301 | const fs = yield* FileSystem.FileSystem; |
| 302 | yield* fs.writeFileString(scriptPath, ''); |
| 303 | yield* trackCliEventEffect({ name: 'producer_event', properties: { sample: 'value' } }); |
| 304 | |
| 305 | expect(childProcessMocks.spawn).not.toHaveBeenCalled(); |
| 306 | expect(yield* fs.exists(path.join(home, '.composio', 'analytics.json'))).toBe(false); |
| 307 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 308 | }); |
| 309 | |
| 310 | it.effect('spawns a detached analytics worker with a decodable envelope', () => { |
| 311 | const home = tempy.temporaryDirectory(); |
| 312 | const scriptPath = `${home}/composio.ts`; |
| 313 | enableTelemetry(); |
| 314 | vi.stubEnv('CLAUDECODE', ''); |
| 315 | vi.stubEnv('CODEX_THREAD_ID', ''); |
| 316 | vi.stubEnv('CODEX_SANDBOX', ''); |
| 317 | process.argv[1] = scriptPath; |
| 318 | |
| 319 | return Effect.gen(function* () { |
| 320 | const fs = yield* FileSystem.FileSystem; |
| 321 | yield* fs.writeFileString(scriptPath, ''); |
| 322 | yield* trackCliEventEffect({ |
| 323 | name: 'producer_event', |
| 324 | properties: { sample: 'value' }, |
| 325 | }); |
| 326 | |
| 327 | expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1); |
| 328 | const [command, args, options] = childProcessMocks.spawn.mock.calls[0] as unknown as [ |
| 329 | string, |
| 330 | string[], |
| 331 | { detached: boolean; stdio: unknown }, |
| 332 | ]; |
| 333 | expect(command).toBe(process.execPath); |
| 334 | expect(args.slice(0, 2)).toEqual([scriptPath, '__analytics-worker']); |
| 335 | expect(args).toHaveLength(3); |
| 336 | expect(decodeWorkerPayload(args[2]!)).toMatchObject({ |
| 337 | event: 'producer_event', |
| 338 | properties: { cli_version: APP_VERSION, sample: 'value', agent_host_env: 'none' }, |
| 339 | source: 'cli', |
| 340 | }); |
| 341 | expect(options).toMatchObject({ |
| 342 | detached: true, |
| 343 | stdio: 'ignore', |
| 344 | }); |
| 345 | expect(options).not.toHaveProperty('env'); |
| 346 | expect(childProcessMocks.once).toHaveBeenCalledWith('error', expect.any(Function)); |
| 347 | expect(childProcessMocks.unref).toHaveBeenCalledTimes(1); |
| 348 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 349 | }); |
| 350 | |
| 351 | it.effect('stamps the enqueued envelope with the agent host it runs under', () => { |
| 352 | const home = tempy.temporaryDirectory(); |
| 353 | const scriptPath = `${home}/composio.ts`; |
| 354 | enableTelemetry(); |
| 355 | vi.stubEnv('CLAUDECODE', '1'); |
| 356 | process.argv[1] = scriptPath; |
| 357 | |
| 358 | return Effect.gen(function* () { |
| 359 | const fs = yield* FileSystem.FileSystem; |
| 360 | yield* fs.writeFileString(scriptPath, ''); |
| 361 | yield* trackCliEventEffect({ name: 'producer_event' }); |
| 362 | |
| 363 | const [, args] = childProcessMocks.spawn.mock.calls[0] as unknown as [string, string[]]; |
| 364 | expect(decodeWorkerPayload(args[2]!)).toMatchObject({ |
| 365 | properties: { agent_host_env: 'claude' }, |
| 366 | }); |
| 367 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 368 | }); |
| 369 | |
| 370 | it.effect('propagates telemetry debug to the detached worker without an env write', () => { |
| 371 | const home = tempy.temporaryDirectory(); |
| 372 | const scriptPath = `${home}/composio.ts`; |
| 373 | enableTelemetry(); |
| 374 | vi.stubEnv('COMPOSIO_CLI_TELEMETRY_DEBUG', 'true'); |
| 375 | process.argv[1] = scriptPath; |
| 376 | |
| 377 | return Effect.gen(function* () { |
| 378 | const fs = yield* FileSystem.FileSystem; |
| 379 | yield* fs.writeFileString(scriptPath, ''); |
| 380 | yield* trackCliEventEffect({ name: 'producer_event' }); |
| 381 | |
| 382 | const [, args, options] = childProcessMocks.spawn.mock.calls[0] as unknown as [ |
| 383 | string, |
| 384 | string[], |
| 385 | { detached: boolean; stdio: unknown }, |
| 386 | ]; |
| 387 | expect(args).toHaveLength(4); |
| 388 | expect(args.at(-1)).toBe('--telemetry-debug'); |
| 389 | expect(options.stdio).toEqual(['ignore', 'ignore', 'inherit']); |
| 390 | expect(options).not.toHaveProperty('env'); |
| 391 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 392 | }); |
| 393 | |
| 394 | it.effect('spawns a detached codact worker with a decodable failure body', () => { |
| 395 | const home = tempy.temporaryDirectory(); |
| 396 | const scriptPath = `${home}/composio.ts`; |
| 397 | enableTelemetry(); |
| 398 | vi.stubEnv('COMPOSIO_CLI_INVOCATION_ORIGIN', 'agent'); |
| 399 | vi.stubEnv('COMPOSIO_CLI_PARENT_RUN_ID', 'run_parent'); |
| 400 | process.argv[1] = scriptPath; |
| 401 | |
| 402 | return Effect.gen(function* () { |
| 403 | const fs = yield* FileSystem.FileSystem; |
| 404 | yield* fs.writeFileString(scriptPath, ''); |
| 405 | yield* trackCliCodactFailureEffect({ |
| 406 | failureType: 'wrong_tool_input_param', |
| 407 | toolInfo: { |
| 408 | toolkit: 'github', |
| 409 | tool: { slug: 'GITHUB_CREATE_ISSUE', version: 'latest' }, |
| 410 | }, |
| 411 | ctx: { invalidKey: 'repo' }, |
| 412 | requestId: 'req_producer', |
| 413 | }); |
| 414 | |
| 415 | expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1); |
| 416 | const [command, args, options] = childProcessMocks.spawn.mock.calls[0] as unknown as [ |
| 417 | string, |
| 418 | string[], |
| 419 | { detached: boolean; stdio: unknown }, |
| 420 | ]; |
| 421 | expect(command).toBe(process.execPath); |
| 422 | expect(args.slice(0, 2)).toEqual([scriptPath, '__codact-failure-worker']); |
| 423 | expect(args).toHaveLength(3); |
| 424 | expect(decodeWorkerPayload(args[2]!)).toMatchObject({ |
| 425 | failure_type: 'wrong_tool_input_param', |
| 426 | tool_info: { |
| 427 | toolkit: 'github', |
| 428 | tool: { slug: 'GITHUB_CREATE_ISSUE', version: 'latest' }, |
| 429 | }, |
| 430 | ctx: { invalidKey: 'repo' }, |
| 431 | request_id: 'req_producer', |
| 432 | session: { |
| 433 | cli_version: APP_VERSION, |
| 434 | invocation_origin: 'agent', |
| 435 | parent_run_id: 'run_parent', |
| 436 | source: 'cli', |
| 437 | }, |
| 438 | }); |
| 439 | expect(options).toMatchObject({ |
| 440 | detached: true, |
| 441 | stdio: 'ignore', |
| 442 | }); |
| 443 | expect(options).not.toHaveProperty('env'); |
| 444 | expect(childProcessMocks.once).toHaveBeenCalledWith('error', expect.any(Function)); |
| 445 | expect(childProcessMocks.unref).toHaveBeenCalledTimes(1); |
| 446 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 447 | }); |
| 448 | |
| 449 | it.effect( |
| 450 | 'stamps codact failures with the run id the command minted when no parent run id is inherited', |
| 451 | () => { |
| 452 | const home = tempy.temporaryDirectory(); |
| 453 | const scriptPath = `${home}/composio.ts`; |
| 454 | enableTelemetry(); |
| 455 | process.argv[1] = scriptPath; |
| 456 | |
| 457 | return Effect.gen(function* () { |
| 458 | const fs = yield* FileSystem.FileSystem; |
| 459 | yield* fs.writeFileString(scriptPath, ''); |
| 460 | yield* trackCliCodactFailureEffect({ |
| 461 | failureType: 'wrong_tool_slug', |
| 462 | ctx: { slug: 'MISSING_TOOL' }, |
| 463 | }); |
| 464 | |
| 465 | expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1); |
| 466 | const args = childProcessMocks.spawn.mock.calls[0]![1] as string[]; |
| 467 | expect(decodeWorkerPayload(args[2]!)).toMatchObject({ |
| 468 | failure_type: 'wrong_tool_slug', |
| 469 | session: { |
| 470 | parent_run_id: 'run_minted', |
| 471 | }, |
| 472 | }); |
| 473 | }).pipe( |
| 474 | Effect.provideService(CommandInvocation, { |
| 475 | begin: () => Effect.void, |
| 476 | enter: () => Effect.void, |
| 477 | runId: Effect.succeedSome('run_minted'), |
| 478 | }), |
| 479 | Effect.provide(makePlatformLayer(home)) |
| 480 | ); |
| 481 | } |
| 482 | ); |
| 483 | |
| 484 | it.effect('keeps both producer effects non-fatal when spawn throws', () => { |
| 485 | const home = tempy.temporaryDirectory(); |
| 486 | enableTelemetry(); |
| 487 | childProcessMocks.spawn.mockImplementation(() => { |
| 488 | throw new Error('spawn failed'); |
| 489 | }); |
| 490 | |
| 491 | return Effect.gen(function* () { |
| 492 | yield* trackCliEventEffect({ name: 'producer_event' }); |
| 493 | yield* trackCliCodactFailureEffect({ |
| 494 | failureType: 'wrong_tool_slug', |
| 495 | ctx: { slug: 'MISSING_TOOL' }, |
| 496 | }); |
| 497 | |
| 498 | expect(childProcessMocks.spawn).toHaveBeenCalledTimes(2); |
| 499 | expect(childProcessMocks.unref).not.toHaveBeenCalled(); |
| 500 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 501 | }); |
| 502 | |
| 503 | it.effect('honors Effect boolean config when telemetry is disabled', () => { |
| 504 | const home = tempy.temporaryDirectory(); |
| 505 | enableTelemetry(); |
| 506 | vi.stubEnv('COMPOSIO_CLI_TELEMETRY_DISABLED', 'yes'); |
| 507 | |
| 508 | return Effect.gen(function* () { |
| 509 | yield* trackCliEventEffect({ name: 'producer_event' }); |
| 510 | yield* trackCliCodactFailureEffect({ |
| 511 | failureType: 'wrong_tool_slug', |
| 512 | ctx: { slug: 'MISSING_TOOL' }, |
| 513 | }); |
| 514 | yield* emitPostHogAlias('install_disabled', 'apollo_user_disabled'); |
| 515 | yield* linkApolloIdentityForAnalytics('apollo_user_disabled'); |
| 516 | |
| 517 | expect(childProcessMocks.spawn).not.toHaveBeenCalled(); |
| 518 | |
| 519 | // Disabled telemetry must not even create the local identity file. |
| 520 | const fs = yield* FileSystem.FileSystem; |
| 521 | const path = yield* Path.Path; |
| 522 | const exists = yield* fs.exists(path.join(home, '.composio', 'analytics.json')); |
| 523 | expect(exists).toBe(false); |
| 524 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 525 | }); |
| 526 | |
| 527 | it.effect('keys pre-login events on the bare install_id', () => { |
| 528 | const home = tempy.temporaryDirectory(); |
| 529 | const scriptPath = `${home}/composio.ts`; |
| 530 | // No persisted apollo_user_id and no user API key -> anonymous device identity. |
| 531 | enableTelemetry(''); |
| 532 | process.argv[1] = scriptPath; |
| 533 | |
| 534 | return Effect.gen(function* () { |
| 535 | const fs = yield* FileSystem.FileSystem; |
| 536 | yield* fs.writeFileString(scriptPath, ''); |
| 537 | yield* trackCliEventEffect({ name: CLI_ANALYTICS_EVENTS.CLI_SETUP_SUCCEEDED }); |
| 538 | |
| 539 | expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1); |
| 540 | const args = childProcessMocks.spawn.mock.calls[0]![1] as string[]; |
| 541 | const payload = decodeWorkerPayload<{ distinctId: string; installId: string }>(args[2]!); |
| 542 | // Pre-login, distinct_id is the raw install_id (not `anon_<id>`), so the |
| 543 | // login-time alias can merge this exact person into the Apollo user. |
| 544 | expect(payload.installId.length).toBeGreaterThan(0); |
| 545 | expect(payload.distinctId).toBe(payload.installId); |
| 546 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 547 | }); |
| 548 | |
| 549 | it.effect('uses one install_id across concurrent first-run events', () => { |
| 550 | const home = tempy.temporaryDirectory(); |
| 551 | const scriptPath = `${home}/composio.ts`; |
| 552 | enableTelemetry(''); |
| 553 | process.argv[1] = scriptPath; |
| 554 | |
| 555 | return Effect.gen(function* () { |
| 556 | const fs = yield* FileSystem.FileSystem; |
| 557 | const path = yield* Path.Path; |
| 558 | yield* fs.writeFileString(scriptPath, ''); |
| 559 | |
| 560 | yield* Effect.all( |
| 561 | Array.from({ length: 16 }, (_, index) => |
| 562 | trackCliEventEffect({ |
| 563 | name: 'producer_event', |
| 564 | properties: { index }, |
| 565 | }) |
| 566 | ), |
| 567 | { concurrency: 'unbounded' } |
| 568 | ); |
| 569 | |
| 570 | expect(childProcessMocks.spawn).toHaveBeenCalledTimes(16); |
| 571 | const installIds = childProcessMocks.spawn.mock.calls.map(call => { |
| 572 | const args = call[1] as string[]; |
| 573 | return decodeWorkerPayload<{ installId: string }>(args[2]!).installId; |
| 574 | }); |
| 575 | expect(new Set(installIds).size).toBe(1); |
| 576 | |
| 577 | const persisted = JSON.parse( |
| 578 | yield* fs.readFileString(path.join(home, '.composio', 'analytics.json'), 'utf8') |
| 579 | ) as { install_id: string }; |
| 580 | expect(persisted.install_id).toBe(installIds[0]); |
| 581 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 582 | }); |
| 583 | |
| 584 | it.effect('waits for a live analytics-state lock instead of exhausting retries', () => { |
| 585 | const home = tempy.temporaryDirectory(); |
| 586 | const scriptPath = path.join(home, 'composio.ts'); |
| 587 | const composioDir = path.join(home, '.composio'); |
| 588 | const lockPath = path.join(composioDir, 'analytics.json.lock'); |
| 589 | enableTelemetry(''); |
| 590 | process.argv[1] = scriptPath; |
| 591 | mkdirSync(composioDir, { recursive: true }); |
| 592 | writeFileSync(scriptPath, ''); |
| 593 | writeFileSync(lockPath, 'other-process'); |
| 594 | const releaseTimer = setTimeout(() => rmSync(lockPath, { force: true }), 25); |
| 595 | |
| 596 | return Effect.gen(function* () { |
| 597 | yield* trackCliEventEffect({ name: CLI_ANALYTICS_EVENTS.CLI_SETUP_SUCCEEDED }); |
| 598 | |
| 599 | const fs = yield* FileSystem.FileSystem; |
| 600 | expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1); |
| 601 | expect(yield* fs.exists(lockPath)).toBe(false); |
| 602 | }).pipe( |
| 603 | Effect.ensuring(Effect.sync(() => clearTimeout(releaseTimer))), |
| 604 | Effect.provide(makePlatformLayer(home)) |
| 605 | ); |
| 606 | }); |
| 607 | |
| 608 | it.effect('bounds the delay when another process leaves a live state lock behind', () => { |
| 609 | const home = tempy.temporaryDirectory(); |
| 610 | const scriptPath = path.join(home, 'composio.ts'); |
| 611 | const composioDir = path.join(home, '.composio'); |
| 612 | const lockPath = path.join(composioDir, 'analytics.json.lock'); |
| 613 | enableTelemetry(''); |
| 614 | process.argv[1] = scriptPath; |
| 615 | mkdirSync(composioDir, { recursive: true }); |
| 616 | writeFileSync(scriptPath, ''); |
| 617 | writeFileSync(lockPath, 'other-process'); |
| 618 | |
| 619 | return Effect.gen(function* () { |
| 620 | const completedBeforeDeadline = yield* Effect.race( |
| 621 | trackCliEventEffect({ name: CLI_ANALYTICS_EVENTS.CLI_SETUP_SUCCEEDED }).pipe( |
| 622 | Effect.as(true) |
| 623 | ), |
| 624 | Effect.promise( |
| 625 | () => |
| 626 | new Promise<boolean>(resolve => { |
| 627 | setTimeout(() => resolve(false), 1_500); |
| 628 | }) |
| 629 | ) |
| 630 | ); |
| 631 | |
| 632 | expect(completedBeforeDeadline).toBe(true); |
| 633 | expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1); |
| 634 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 635 | }); |
| 636 | |
| 637 | it.effect('adopts the persisted install_id when the bounded lock wait expires', () => { |
| 638 | const home = tempy.temporaryDirectory(); |
| 639 | const scriptPath = path.join(home, 'composio.ts'); |
| 640 | const composioDir = path.join(home, '.composio'); |
| 641 | const lockPath = path.join(composioDir, 'analytics.json.lock'); |
| 642 | enableTelemetry(''); |
| 643 | process.argv[1] = scriptPath; |
| 644 | mkdirSync(composioDir, { recursive: true }); |
| 645 | writeFileSync(scriptPath, ''); |
| 646 | // A live lock that is never released drives this process through the full |
| 647 | // bounded wait; the identity another run already persisted must still win |
| 648 | // over minting a fresh install_id, and the fallback must not rewrite state |
| 649 | // without holding the lock. |
| 650 | writeFileSync(lockPath, 'other-process'); |
| 651 | writeFileSync( |
| 652 | path.join(composioDir, 'analytics.json'), |
| 653 | JSON.stringify({ install_id: 'install_persisted' }) |
| 654 | ); |
| 655 | |
| 656 | return Effect.gen(function* () { |
| 657 | const fs = yield* FileSystem.FileSystem; |
| 658 | yield* trackCliEventEffect({ name: CLI_ANALYTICS_EVENTS.CLI_SETUP_SUCCEEDED }); |
| 659 | |
| 660 | expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1); |
| 661 | const args = childProcessMocks.spawn.mock.calls[0]![1] as string[]; |
| 662 | const payload = decodeWorkerPayload<{ installId: string }>(args[2]!); |
| 663 | expect(payload.installId).toBe('install_persisted'); |
| 664 | |
| 665 | const persisted = JSON.parse( |
| 666 | yield* fs.readFileString(path.join(composioDir, 'analytics.json'), 'utf8') |
| 667 | ) as { install_id: string }; |
| 668 | expect(persisted.install_id).toBe('install_persisted'); |
| 669 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 670 | }); |
| 671 | |
| 672 | it.effect('keys post-login events on the persisted apollo_user_id', () => { |
| 673 | const home = tempy.temporaryDirectory(); |
| 674 | const scriptPath = `${home}/composio.ts`; |
| 675 | enableTelemetry('uak_persisted'); |
| 676 | process.argv[1] = scriptPath; |
| 677 | |
| 678 | return Effect.gen(function* () { |
| 679 | const fs = yield* FileSystem.FileSystem; |
| 680 | yield* fs.writeFileString(scriptPath, ''); |
| 681 | |
| 682 | // Establish the identity under this credential; the id is only trusted |
| 683 | // while that same credential is active. |
| 684 | yield* linkApolloIdentityForAnalytics('om_apollo_123'); |
| 685 | childProcessMocks.spawn.mockClear(); |
| 686 | |
| 687 | yield* trackCliEventEffect({ name: CLI_ANALYTICS_EVENTS.CLI_EXECUTE_SUCCEEDED }); |
| 688 | |
| 689 | expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1); |
| 690 | const args = childProcessMocks.spawn.mock.calls[0]![1] as string[]; |
| 691 | const payload = decodeWorkerPayload<{ distinctId: string }>(args[2]!); |
| 692 | expect(payload.distinctId).toBe('om_apollo_123'); |
| 693 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 694 | }); |
| 695 | |
| 696 | it.effect('persists apollo_user_id and emits exactly one $create_alias at login', () => { |
| 697 | const home = tempy.temporaryDirectory(); |
| 698 | const scriptPath = `${home}/composio.ts`; |
| 699 | enableTelemetry(''); |
| 700 | process.argv[1] = scriptPath; |
| 701 | |
| 702 | return Effect.gen(function* () { |
| 703 | const fs = yield* FileSystem.FileSystem; |
| 704 | const path = yield* Path.Path; |
| 705 | yield* fs.writeFileString(scriptPath, ''); |
| 706 | const composioDir = path.join(home, '.composio'); |
| 707 | yield* fs.makeDirectory(composioDir, { recursive: true }); |
| 708 | yield* fs.writeFileString( |
| 709 | path.join(composioDir, 'analytics.json'), |
| 710 | JSON.stringify({ install_id: 'install_login', created_at: '2026-01-01T00:00:00.000Z' }) |
| 711 | ); |
| 712 | |
| 713 | yield* linkApolloIdentityForAnalytics('om_apollo_login'); |
| 714 | |
| 715 | // Exactly one alias event is spawned. |
| 716 | expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1); |
| 717 | const args = childProcessMocks.spawn.mock.calls[0]![1] as string[]; |
| 718 | const payload = decodeWorkerPayload<{ |
| 719 | event: string; |
| 720 | distinctId: string; |
| 721 | installId: string; |
| 722 | properties: { distinct_id: string; alias: string }; |
| 723 | }>(args[2]!); |
| 724 | expect(payload.event).toBe('$create_alias'); |
| 725 | expect(payload.distinctId).toBe('om_apollo_login'); |
| 726 | expect(payload.installId).toBe('install_login'); |
| 727 | expect(payload.properties).toMatchObject({ |
| 728 | distinct_id: 'om_apollo_login', |
| 729 | alias: 'install_login', |
| 730 | }); |
| 731 | |
| 732 | // The Apollo id and pending alias are persisted while keeping the |
| 733 | // original install_id/created_at. |
| 734 | const persisted = JSON.parse( |
| 735 | yield* fs.readFileString(path.join(composioDir, 'analytics.json'), 'utf8') |
| 736 | ) as { |
| 737 | install_id: string; |
| 738 | apollo_user_id: string; |
| 739 | pending_alias_apollo_user_id: string; |
| 740 | created_at: string; |
| 741 | }; |
| 742 | expect(persisted).toMatchObject({ |
| 743 | install_id: 'install_login', |
| 744 | apollo_user_id: 'om_apollo_login', |
| 745 | pending_alias_apollo_user_id: 'om_apollo_login', |
| 746 | created_at: '2026-01-01T00:00:00.000Z', |
| 747 | }); |
| 748 | |
| 749 | // A repeat login while delivery is pending does not re-emit the alias. |
| 750 | yield* linkApolloIdentityForAnalytics('om_apollo_login'); |
| 751 | expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1); |
| 752 | |
| 753 | vi.stubEnv('COMPOSIO_POSTHOG_INGEST_URL', 'https://posthog.example.test/i/v0/e/'); |
| 754 | const fetchMock = vi.fn().mockResolvedValue(new Response(null, { status: 200 })); |
| 755 | yield* runBackgroundWorkerFromArgv([ |
| 756 | process.execPath, |
| 757 | 'composio', |
| 758 | '__analytics-worker', |
| 759 | args[2]!, |
| 760 | ]).pipe( |
| 761 | Effect.provide(makePlatformLayerWithFetch(home, fetchMock as unknown as typeof fetch)) |
| 762 | ); |
| 763 | |
| 764 | const acknowledged = JSON.parse( |
| 765 | yield* fs.readFileString(path.join(composioDir, 'analytics.json'), 'utf8') |
| 766 | ) as { |
| 767 | aliased_apollo_user_id?: string; |
| 768 | pending_alias_apollo_user_id?: string; |
| 769 | }; |
| 770 | expect(acknowledged.aliased_apollo_user_id).toBe('om_apollo_login'); |
| 771 | expect(acknowledged.pending_alias_apollo_user_id).toBeUndefined(); |
| 772 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 773 | }); |
| 774 | |
| 775 | it.effect('retries an alias after the worker reports a failed delivery', () => { |
| 776 | const home = tempy.temporaryDirectory(); |
| 777 | const scriptPath = `${home}/composio.ts`; |
| 778 | enableTelemetry(''); |
| 779 | process.argv[1] = scriptPath; |
| 780 | |
| 781 | return Effect.gen(function* () { |
| 782 | const fs = yield* FileSystem.FileSystem; |
| 783 | yield* fs.writeFileString(scriptPath, ''); |
| 784 | |
| 785 | yield* linkApolloIdentityForAnalytics('om_alias_retry'); |
| 786 | expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1); |
| 787 | const args = childProcessMocks.spawn.mock.calls[0]![1] as string[]; |
| 788 | |
| 789 | vi.stubEnv('COMPOSIO_POSTHOG_INGEST_URL', 'https://posthog.example.test/i/v0/e/'); |
| 790 | const fetchMock = vi.fn().mockResolvedValue(new Response(null, { status: 500 })); |
| 791 | yield* runBackgroundWorkerFromArgv([ |
| 792 | process.execPath, |
| 793 | 'composio', |
| 794 | '__analytics-worker', |
| 795 | args[2]!, |
| 796 | ]).pipe( |
| 797 | Effect.provide(makePlatformLayerWithFetch(home, fetchMock as unknown as typeof fetch)) |
| 798 | ); |
| 799 | |
| 800 | const afterFailure = JSON.parse( |
| 801 | yield* fs.readFileString(path.join(home, '.composio', 'analytics.json'), 'utf8') |
| 802 | ) as { |
| 803 | aliased_apollo_user_id?: string; |
| 804 | pending_alias_apollo_user_id?: string; |
| 805 | }; |
| 806 | expect(afterFailure.aliased_apollo_user_id).toBeUndefined(); |
| 807 | expect(afterFailure.pending_alias_apollo_user_id).toBeUndefined(); |
| 808 | |
| 809 | yield* linkApolloIdentityForAnalytics('om_alias_retry'); |
| 810 | expect(childProcessMocks.spawn).toHaveBeenCalledTimes(2); |
| 811 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 812 | }); |
| 813 | |
| 814 | it.effect('emits one alias across concurrent identity links', () => { |
| 815 | const home = tempy.temporaryDirectory(); |
| 816 | const scriptPath = `${home}/composio.ts`; |
| 817 | enableTelemetry('uak_concurrent'); |
| 818 | process.argv[1] = scriptPath; |
| 819 | |
| 820 | return Effect.gen(function* () { |
| 821 | const fs = yield* FileSystem.FileSystem; |
| 822 | const path = yield* Path.Path; |
| 823 | yield* fs.writeFileString(scriptPath, ''); |
| 824 | |
| 825 | yield* Effect.all( |
| 826 | Array.from({ length: 8 }, () => |
| 827 | linkApolloIdentityForAnalytics('om_concurrent', 'uak_concurrent') |
| 828 | ), |
| 829 | { concurrency: 'unbounded' } |
| 830 | ); |
| 831 | |
| 832 | expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1); |
| 833 | const persisted = JSON.parse( |
| 834 | yield* fs.readFileString(path.join(home, '.composio', 'analytics.json'), 'utf8') |
| 835 | ) as { |
| 836 | apollo_user_id: string; |
| 837 | pending_alias_apollo_user_id: string; |
| 838 | api_key_fingerprint: string; |
| 839 | }; |
| 840 | expect(persisted.apollo_user_id).toBe('om_concurrent'); |
| 841 | expect(persisted.pending_alias_apollo_user_id).toBe('om_concurrent'); |
| 842 | expect(persisted.api_key_fingerprint.length).toBeGreaterThan(0); |
| 843 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 844 | }); |
| 845 | |
| 846 | it.effect('retries the alias when the first attempt could not be enqueued', () => { |
| 847 | const home = tempy.temporaryDirectory(); |
| 848 | const scriptPath = `${home}/composio.ts`; |
| 849 | enableTelemetry(); |
| 850 | vi.stubEnv('COMPOSIO_POSTHOG_PROJECT_API_KEY', ''); |
| 851 | process.argv[1] = scriptPath; |
| 852 | |
| 853 | return Effect.gen(function* () { |
| 854 | const fs = yield* FileSystem.FileSystem; |
| 855 | const path = yield* Path.Path; |
| 856 | yield* fs.writeFileString(scriptPath, ''); |
| 857 | |
| 858 | // No key -> no analytics state or worker is created. |
| 859 | yield* linkApolloIdentityForAnalytics('om_apollo_retry'); |
| 860 | expect(childProcessMocks.spawn).not.toHaveBeenCalled(); |
| 861 | expect(yield* fs.exists(path.join(home, '.composio', 'analytics.json'))).toBe(false); |
| 862 | |
| 863 | // Once a key is configured the alias is attempted again. |
| 864 | vi.stubEnv('COMPOSIO_POSTHOG_PROJECT_API_KEY', 'phc_test_key'); |
| 865 | yield* linkApolloIdentityForAnalytics('om_apollo_retry'); |
| 866 | expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1); |
| 867 | const afterRetry = JSON.parse( |
| 868 | yield* fs.readFileString(path.join(home, '.composio', 'analytics.json'), 'utf8') |
| 869 | ) as { pending_alias_apollo_user_id?: string }; |
| 870 | expect(afterRetry.pending_alias_apollo_user_id).toBe('om_apollo_retry'); |
| 871 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 872 | }); |
| 873 | |
| 874 | it.effect('clears the Apollo identity on logout so events revert to install_id', () => { |
| 875 | const home = tempy.temporaryDirectory(); |
| 876 | const scriptPath = `${home}/composio.ts`; |
| 877 | enableTelemetry(''); |
| 878 | process.argv[1] = scriptPath; |
| 879 | |
| 880 | return Effect.gen(function* () { |
| 881 | const fs = yield* FileSystem.FileSystem; |
| 882 | const path = yield* Path.Path; |
| 883 | yield* fs.writeFileString(scriptPath, ''); |
| 884 | const composioDir = path.join(home, '.composio'); |
| 885 | yield* fs.makeDirectory(composioDir, { recursive: true }); |
| 886 | yield* fs.writeFileString( |
| 887 | path.join(composioDir, 'analytics.json'), |
| 888 | JSON.stringify({ install_id: 'install_logout', apollo_user_id: 'om_apollo_gone' }) |
| 889 | ); |
| 890 | |
| 891 | yield* clearApolloIdentityForAnalytics; |
| 892 | |
| 893 | const persisted = JSON.parse( |
| 894 | yield* fs.readFileString(path.join(composioDir, 'analytics.json'), 'utf8') |
| 895 | ) as { install_id: string; apollo_user_id?: string }; |
| 896 | expect(persisted.install_id).toBe('install_logout'); |
| 897 | expect(persisted.apollo_user_id).toBeUndefined(); |
| 898 | |
| 899 | // Later events fall back to the anonymous install identity. |
| 900 | yield* trackCliEventEffect({ name: 'producer_event' }); |
| 901 | const args = childProcessMocks.spawn.mock.calls[0]![1] as string[]; |
| 902 | const payload = decodeWorkerPayload<{ distinctId: string }>(args[2]!); |
| 903 | expect(payload.distinctId).toBe('install_logout'); |
| 904 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 905 | }); |
| 906 | |
| 907 | it.effect('delivers codact worker failures with the user key and session body', () => { |
| 908 | const home = tempy.temporaryDirectory(); |
| 909 | const failureBody = { |
| 910 | failure_type: 'wrong_tool_input_param', |
| 911 | tool_info: { |
| 912 | toolkit: 'github', |
| 913 | tool: { slug: 'GITHUB_CREATE_ISSUE', version: 'latest' }, |
| 914 | }, |
| 915 | ctx: { invalidKey: 'repository' }, |
| 916 | session: { |
| 917 | source: 'cli', |
| 918 | id: 'cli_s_worker', |
| 919 | cli_version: '0.3.0-test', |
| 920 | invocation_origin: 'agent', |
| 921 | parent_run_id: 'run_parent', |
| 922 | }, |
| 923 | request_id: 'req_worker', |
| 924 | }; |
| 925 | enableTelemetry('uak_worker'); |
| 926 | const fetchSpy = vi.fn().mockResolvedValue(new Response(null, { status: 204 })); |
| 927 | |
| 928 | return Effect.gen(function* () { |
| 929 | yield* runBackgroundWorkerFromArgv([ |
| 930 | process.execPath, |
| 931 | 'composio', |
| 932 | '__codact-failure-worker', |
| 933 | encodeWorkerPayload(failureBody), |
| 934 | ]).pipe( |
| 935 | Effect.provide(makePlatformLayerWithFetch(home, fetchSpy as unknown as typeof fetch)) |
| 936 | ); |
| 937 | |
| 938 | expect(fetchSpy).toHaveBeenCalledTimes(1); |
| 939 | const [endpoint, request] = fetchSpy.mock.calls[0]!; |
| 940 | expect(String(endpoint)).toBe('https://backend.example.test/api/v3/cli/codact_failures'); |
| 941 | expect(request).toMatchObject({ |
| 942 | method: 'POST', |
| 943 | headers: expect.objectContaining({ |
| 944 | 'content-type': 'application/json', |
| 945 | 'x-user-api-key': 'uak_worker', |
| 946 | }), |
| 947 | }); |
| 948 | expect(JSON.parse(new TextDecoder().decode(request?.body as Uint8Array))).toEqual( |
| 949 | failureBody |
| 950 | ); |
| 951 | }); |
| 952 | }); |
| 953 | |
| 954 | it.effect('ignores malformed codact worker payloads', () => { |
| 955 | const home = tempy.temporaryDirectory(); |
| 956 | enableTelemetry(); |
| 957 | const fetchSpy = vi.fn().mockResolvedValue(new Response(null, { status: 204 })); |
| 958 | |
| 959 | return Effect.gen(function* () { |
| 960 | yield* runBackgroundWorkerFromArgv([ |
| 961 | process.execPath, |
| 962 | 'composio', |
| 963 | '__codact-failure-worker', |
| 964 | 'not-base64', |
| 965 | ]).pipe( |
| 966 | Effect.provide(makePlatformLayerWithFetch(home, fetchSpy as unknown as typeof fetch)) |
| 967 | ); |
| 968 | |
| 969 | expect(fetchSpy).not.toHaveBeenCalled(); |
| 970 | }); |
| 971 | }); |
| 972 | |
| 973 | it.effect('skips codact worker delivery without a user key', () => { |
| 974 | const home = tempy.temporaryDirectory(); |
| 975 | enableTelemetry(''); |
| 976 | const fetchSpy = vi.fn().mockResolvedValue(new Response(null, { status: 204 })); |
| 977 | const failureBody = { |
| 978 | failure_type: 'wrong_tool_slug', |
| 979 | ctx: { slug: 'MISSING_TOOL' }, |
| 980 | session: { source: 'cli' }, |
| 981 | }; |
| 982 | |
| 983 | return Effect.gen(function* () { |
| 984 | yield* runBackgroundWorkerFromArgv([ |
| 985 | process.execPath, |
| 986 | 'composio', |
| 987 | '__codact-failure-worker', |
| 988 | encodeWorkerPayload(failureBody), |
| 989 | ]).pipe( |
| 990 | Effect.provide(makePlatformLayerWithFetch(home, fetchSpy as unknown as typeof fetch)) |
| 991 | ); |
| 992 | |
| 993 | expect(fetchSpy).not.toHaveBeenCalled(); |
| 994 | }); |
| 995 | }); |
| 996 | |
| 997 | describe('CLI analytics identity safety', () => { |
| 998 | it.effect('does not attribute events to a stale identity when the active key differs', () => { |
| 999 | const home = tempy.temporaryDirectory(); |
| 1000 | const scriptPath = `${home}/composio.ts`; |
| 1001 | enableTelemetry('uak_user_a'); |
| 1002 | process.argv[1] = scriptPath; |
| 1003 | |
| 1004 | return Effect.gen(function* () { |
| 1005 | const fs = yield* FileSystem.FileSystem; |
| 1006 | yield* fs.writeFileString(scriptPath, ''); |
| 1007 | yield* linkApolloIdentityForAnalytics('om_user_a'); |
| 1008 | childProcessMocks.spawn.mockClear(); |
| 1009 | |
| 1010 | // A script running under a different credential must not inherit user A. |
| 1011 | vi.stubEnv('COMPOSIO_USER_API_KEY', 'uak_user_b'); |
| 1012 | yield* trackCliEventEffect({ name: 'producer_event' }); |
| 1013 | |
| 1014 | const args = childProcessMocks.spawn.mock.calls[0]![1] as string[]; |
| 1015 | const payload = decodeWorkerPayload<{ distinctId: string; installId: string }>(args[2]!); |
| 1016 | expect(payload.distinctId).not.toBe('om_user_a'); |
| 1017 | // The install_id is already merged into user A's PostHog person, so |
| 1018 | // reusing it would still attribute to A. Must be a never-merged id. |
| 1019 | expect(payload.distinctId).not.toBe(payload.installId); |
| 1020 | expect(payload.distinctId.startsWith('user_')).toBe(true); |
| 1021 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 1022 | }); |
| 1023 | |
| 1024 | it.effect('binds identity to the credential the caller passes, not one read back', () => { |
| 1025 | const home = tempy.temporaryDirectory(); |
| 1026 | const scriptPath = `${home}/composio.ts`; |
| 1027 | // Mirrors real login: identity is resolved BEFORE the credential is persisted, |
| 1028 | // so nothing readable from disk/env identifies the user yet. |
| 1029 | enableTelemetry(''); |
| 1030 | process.argv[1] = scriptPath; |
| 1031 | |
| 1032 | return Effect.gen(function* () { |
| 1033 | const fs = yield* FileSystem.FileSystem; |
| 1034 | yield* fs.writeFileString(scriptPath, ''); |
| 1035 | |
| 1036 | yield* linkApolloIdentityForAnalytics('om_login_user', 'uak_fresh'); |
| 1037 | childProcessMocks.spawn.mockClear(); |
| 1038 | |
| 1039 | // The credential is active from here on. |
| 1040 | vi.stubEnv('COMPOSIO_USER_API_KEY', 'uak_fresh'); |
| 1041 | yield* trackCliEventEffect({ name: 'producer_event' }); |
| 1042 | |
| 1043 | const args = childProcessMocks.spawn.mock.calls[0]![1] as string[]; |
| 1044 | const payload = decodeWorkerPayload<{ distinctId: string }>(args[2]!); |
| 1045 | expect(payload.distinctId).toBe('om_login_user'); |
| 1046 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 1047 | }); |
| 1048 | |
| 1049 | it.effect('does not reuse the aliased install_id after logout', () => { |
| 1050 | const home = tempy.temporaryDirectory(); |
| 1051 | const scriptPath = `${home}/composio.ts`; |
| 1052 | enableTelemetry('uak_logged_in'); |
| 1053 | process.argv[1] = scriptPath; |
| 1054 | |
| 1055 | return Effect.gen(function* () { |
| 1056 | const fs = yield* FileSystem.FileSystem; |
| 1057 | yield* fs.writeFileString(scriptPath, ''); |
| 1058 | |
| 1059 | yield* linkApolloIdentityForAnalytics('om_logged_in', 'uak_logged_in'); |
| 1060 | yield* clearApolloIdentityForAnalytics; |
| 1061 | childProcessMocks.spawn.mockClear(); |
| 1062 | |
| 1063 | // Signed out: no credential at all. |
| 1064 | vi.stubEnv('COMPOSIO_USER_API_KEY', ''); |
| 1065 | yield* trackCliEventEffect({ name: 'producer_event' }); |
| 1066 | |
| 1067 | const args = childProcessMocks.spawn.mock.calls[0]![1] as string[]; |
| 1068 | const payload = decodeWorkerPayload<{ distinctId: string; installId: string }>(args[2]!); |
| 1069 | expect(payload.distinctId).not.toBe('om_logged_in'); |
| 1070 | // Bare install_id now resolves to the logged-out user's person. |
| 1071 | expect(payload.distinctId).not.toBe(payload.installId); |
| 1072 | expect(payload.distinctId).toBe(`anon_${payload.installId}`); |
| 1073 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 1074 | }); |
| 1075 | |
| 1076 | it.effect('attaches org_id from user config to events', () => { |
| 1077 | const home = tempy.temporaryDirectory(); |
| 1078 | const scriptPath = `${home}/composio.ts`; |
| 1079 | enableTelemetry(); |
| 1080 | vi.stubEnv('COMPOSIO_CACHE_DIR', `${home}/.composio`); |
| 1081 | process.argv[1] = scriptPath; |
| 1082 | |
| 1083 | return Effect.gen(function* () { |
| 1084 | const fs = yield* FileSystem.FileSystem; |
| 1085 | const path = yield* Path.Path; |
| 1086 | yield* fs.writeFileString(scriptPath, ''); |
| 1087 | const composioDir = path.join(home, '.composio'); |
| 1088 | yield* fs.makeDirectory(composioDir, { recursive: true }); |
| 1089 | yield* fs.writeFileString( |
| 1090 | path.join(composioDir, USER_CONFIG_FILE_NAME), |
| 1091 | JSON.stringify({ api_key: 'uak_test', org_id: 'org_acme' }) |
| 1092 | ); |
| 1093 | |
| 1094 | yield* trackCliEventEffect({ name: 'producer_event' }); |
| 1095 | |
| 1096 | const args = childProcessMocks.spawn.mock.calls[0]![1] as string[]; |
| 1097 | const payload = decodeWorkerPayload<{ properties?: { org_id?: string } }>(args[2]!); |
| 1098 | expect(payload.properties?.org_id).toBe('org_acme'); |
| 1099 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 1100 | }); |
| 1101 | |
| 1102 | it.effect('reads the user config from COMPOSIO_CACHE_DIR rather than the homedir', () => { |
| 1103 | const home = tempy.temporaryDirectory(); |
| 1104 | const cacheDir = tempy.temporaryDirectory(); |
| 1105 | const scriptPath = `${home}/composio.ts`; |
| 1106 | enableTelemetry(); |
| 1107 | vi.stubEnv('COMPOSIO_CACHE_DIR', cacheDir); |
| 1108 | process.argv[1] = scriptPath; |
| 1109 | |
| 1110 | return Effect.gen(function* () { |
| 1111 | const fs = yield* FileSystem.FileSystem; |
| 1112 | const path = yield* Path.Path; |
| 1113 | yield* fs.writeFileString(scriptPath, ''); |
| 1114 | // The user config exists only in the relocated cache dir; ~/.composio |
| 1115 | // never receives a copy. |
| 1116 | yield* fs.writeFileString( |
| 1117 | path.join(cacheDir, USER_CONFIG_FILE_NAME), |
| 1118 | JSON.stringify({ api_key: 'uak_test', org_id: 'org_cache_dir' }) |
| 1119 | ); |
| 1120 | |
| 1121 | yield* trackCliEventEffect({ name: 'producer_event' }); |
| 1122 | |
| 1123 | const args = childProcessMocks.spawn.mock.calls[0]![1] as string[]; |
| 1124 | const payload = decodeWorkerPayload<{ properties?: { org_id?: string } }>(args[2]!); |
| 1125 | expect(payload.properties?.org_id).toBe('org_cache_dir'); |
| 1126 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 1127 | }); |
| 1128 | |
| 1129 | it.effect('does not create the identity file when logging out with telemetry disabled', () => { |
| 1130 | const home = tempy.temporaryDirectory(); |
| 1131 | enableTelemetry(); |
| 1132 | vi.stubEnv('COMPOSIO_DISABLE_TELEMETRY', 'true'); |
| 1133 | |
| 1134 | return Effect.gen(function* () { |
| 1135 | yield* clearApolloIdentityForAnalytics; |
| 1136 | |
| 1137 | const fs = yield* FileSystem.FileSystem; |
| 1138 | const path = yield* Path.Path; |
| 1139 | const exists = yield* fs.exists(path.join(home, '.composio', 'analytics.json')); |
| 1140 | expect(exists).toBe(false); |
| 1141 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 1142 | }); |
| 1143 | |
| 1144 | it.effect('rotates to a fresh install_id when a second Apollo user links', () => { |
| 1145 | const home = tempy.temporaryDirectory(); |
| 1146 | const scriptPath = `${home}/composio.ts`; |
| 1147 | enableTelemetry(); |
| 1148 | process.argv[1] = scriptPath; |
| 1149 | |
| 1150 | return Effect.gen(function* () { |
| 1151 | const fs = yield* FileSystem.FileSystem; |
| 1152 | const path = yield* Path.Path; |
| 1153 | yield* fs.writeFileString(scriptPath, ''); |
| 1154 | |
| 1155 | yield* linkApolloIdentityForAnalytics('om_first_user'); |
| 1156 | expect(childProcessMocks.spawn).toHaveBeenCalledTimes(1); |
| 1157 | const firstArgs = childProcessMocks.spawn.mock.calls[0]![1] as string[]; |
| 1158 | const firstPayload = decodeWorkerPayload<{ installId: string }>(firstArgs[2]!); |
| 1159 | |
| 1160 | // Logout, then log in as a different user on the same device: the |
| 1161 | // second user is aliased to a fresh install_id, never the one already |
| 1162 | // merged into the first user's PostHog person. |
| 1163 | yield* clearApolloIdentityForAnalytics; |
| 1164 | yield* linkApolloIdentityForAnalytics('om_second_user'); |
| 1165 | expect(childProcessMocks.spawn).toHaveBeenCalledTimes(2); |
| 1166 | const secondArgs = childProcessMocks.spawn.mock.calls[1]![1] as string[]; |
| 1167 | const secondPayload = decodeWorkerPayload<{ |
| 1168 | event: string; |
| 1169 | distinctId: string; |
| 1170 | installId: string; |
| 1171 | properties: { alias: string }; |
| 1172 | }>(secondArgs[2]!); |
| 1173 | expect(secondPayload.event).toBe('$create_alias'); |
| 1174 | expect(secondPayload.distinctId).toBe('om_second_user'); |
| 1175 | expect(secondPayload.installId).not.toBe(firstPayload.installId); |
| 1176 | expect(secondPayload.properties.alias).toBe(secondPayload.installId); |
| 1177 | |
| 1178 | const persisted = JSON.parse( |
| 1179 | yield* fs.readFileString(path.join(home, '.composio', 'analytics.json'), 'utf8') |
| 1180 | ) as { |
| 1181 | install_id: string; |
| 1182 | apollo_user_id: string; |
| 1183 | pending_alias_apollo_user_id: string; |
| 1184 | }; |
| 1185 | expect(persisted.install_id).toBe(secondPayload.installId); |
| 1186 | expect(persisted.apollo_user_id).toBe('om_second_user'); |
| 1187 | expect(persisted.pending_alias_apollo_user_id).toBe('om_second_user'); |
| 1188 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 1189 | }); |
| 1190 | |
| 1191 | it.effect('trusts the persisted identity when the api key lives in the OS keyring', () => { |
| 1192 | const home = tempy.temporaryDirectory(); |
| 1193 | const scriptPath = `${home}/composio.ts`; |
| 1194 | // No env key and no plaintext api_key on disk -> no fingerprint. |
| 1195 | enableTelemetry(''); |
| 1196 | vi.stubEnv('COMPOSIO_CACHE_DIR', `${home}/.composio`); |
| 1197 | process.argv[1] = scriptPath; |
| 1198 | |
| 1199 | return Effect.gen(function* () { |
| 1200 | const fs = yield* FileSystem.FileSystem; |
| 1201 | const path = yield* Path.Path; |
| 1202 | yield* fs.writeFileString(scriptPath, ''); |
| 1203 | const composioDir = path.join(home, '.composio'); |
| 1204 | yield* fs.makeDirectory(composioDir, { recursive: true }); |
| 1205 | yield* fs.writeFileString( |
| 1206 | path.join(composioDir, 'config.json'), |
| 1207 | JSON.stringify({ security: 'keychain-subprocess' }) |
| 1208 | ); |
| 1209 | yield* fs.writeFileString( |
| 1210 | path.join(composioDir, USER_CONFIG_FILE_NAME), |
| 1211 | JSON.stringify({ base_url: 'https://backend.example.test', org_id: 'org_keychain' }) |
| 1212 | ); |
| 1213 | yield* fs.writeFileString( |
| 1214 | path.join(composioDir, 'analytics.json'), |
| 1215 | JSON.stringify({ |
| 1216 | install_id: 'install_keychain', |
| 1217 | apollo_user_id: 'om_keychain_user', |
| 1218 | aliased_apollo_user_id: 'om_keychain_user', |
| 1219 | api_key_fingerprint: 'abc.5', |
| 1220 | }) |
| 1221 | ); |
| 1222 | |
| 1223 | yield* trackCliEventEffect({ name: 'producer_event' }); |
| 1224 | |
| 1225 | const args = childProcessMocks.spawn.mock.calls[0]![1] as string[]; |
| 1226 | const payload = decodeWorkerPayload<{ distinctId: string }>(args[2]!); |
| 1227 | expect(payload.distinctId).toBe('om_keychain_user'); |
| 1228 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 1229 | }); |
| 1230 | |
| 1231 | it.effect('does not reuse a stale keychain identity when logout loses the state lock', () => { |
| 1232 | const home = tempy.temporaryDirectory(); |
| 1233 | const scriptPath = `${home}/composio.ts`; |
| 1234 | enableTelemetry(''); |
| 1235 | vi.stubEnv('COMPOSIO_CACHE_DIR', `${home}/.composio`); |
| 1236 | process.argv[1] = scriptPath; |
| 1237 | |
| 1238 | return Effect.gen(function* () { |
| 1239 | const fs = yield* FileSystem.FileSystem; |
| 1240 | const path = yield* Path.Path; |
| 1241 | yield* fs.writeFileString(scriptPath, ''); |
| 1242 | const composioDir = path.join(home, '.composio'); |
| 1243 | yield* fs.makeDirectory(composioDir, { recursive: true }); |
| 1244 | yield* fs.writeFileString( |
| 1245 | path.join(composioDir, 'config.json'), |
| 1246 | JSON.stringify({ security: 'keychain-subprocess' }) |
| 1247 | ); |
| 1248 | // Logout persists an empty credential record but intentionally keeps |
| 1249 | // user_data.json in place. |
| 1250 | yield* fs.writeFileString( |
| 1251 | path.join(composioDir, USER_CONFIG_FILE_NAME), |
| 1252 | JSON.stringify({ base_url: 'https://backend.example.test' }) |
| 1253 | ); |
| 1254 | yield* fs.writeFileString( |
| 1255 | path.join(composioDir, 'analytics.json'), |
| 1256 | JSON.stringify({ |
| 1257 | install_id: 'install_keychain_logout', |
| 1258 | apollo_user_id: 'om_logged_out_keychain_user', |
| 1259 | aliased_apollo_user_id: 'om_logged_out_keychain_user', |
| 1260 | }) |
| 1261 | ); |
| 1262 | const lockPath = path.join(composioDir, 'analytics.json.lock'); |
| 1263 | yield* fs.writeFileString(lockPath, 'other-process'); |
| 1264 | |
| 1265 | // The clear is best-effort and times out while another process owns |
| 1266 | // the lock, leaving the old identity on disk. |
| 1267 | yield* clearApolloIdentityForAnalytics; |
| 1268 | yield* fs.remove(lockPath); |
| 1269 | childProcessMocks.spawn.mockClear(); |
| 1270 | |
| 1271 | yield* trackCliEventEffect({ name: 'producer_event' }); |
| 1272 | |
| 1273 | const args = childProcessMocks.spawn.mock.calls[0]![1] as string[]; |
| 1274 | const payload = decodeWorkerPayload<{ distinctId: string; installId: string }>(args[2]!); |
| 1275 | expect(payload.distinctId).not.toBe('om_logged_out_keychain_user'); |
| 1276 | expect(payload.distinctId).toBe(`anon_${payload.installId}`); |
| 1277 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 1278 | }); |
| 1279 | |
| 1280 | it.effect('does not trust a fingerprint-less identity outside keychain modes', () => { |
| 1281 | const home = tempy.temporaryDirectory(); |
| 1282 | const scriptPath = `${home}/composio.ts`; |
| 1283 | enableTelemetry(''); |
| 1284 | vi.stubEnv('COMPOSIO_CACHE_DIR', `${home}/.composio`); |
| 1285 | process.argv[1] = scriptPath; |
| 1286 | |
| 1287 | return Effect.gen(function* () { |
| 1288 | const fs = yield* FileSystem.FileSystem; |
| 1289 | const path = yield* Path.Path; |
| 1290 | yield* fs.writeFileString(scriptPath, ''); |
| 1291 | const composioDir = path.join(home, '.composio'); |
| 1292 | yield* fs.makeDirectory(composioDir, { recursive: true }); |
| 1293 | yield* fs.writeFileString( |
| 1294 | path.join(composioDir, 'config.json'), |
| 1295 | JSON.stringify({ security: 'json' }) |
| 1296 | ); |
| 1297 | yield* fs.writeFileString( |
| 1298 | path.join(composioDir, USER_CONFIG_FILE_NAME), |
| 1299 | JSON.stringify({ org_id: 'org_json' }) |
| 1300 | ); |
| 1301 | yield* fs.writeFileString( |
| 1302 | path.join(composioDir, 'analytics.json'), |
| 1303 | JSON.stringify({ |
| 1304 | install_id: 'install_json', |
| 1305 | apollo_user_id: 'om_json_user', |
| 1306 | aliased_apollo_user_id: 'om_json_user', |
| 1307 | api_key_fingerprint: 'abc.5', |
| 1308 | }) |
| 1309 | ); |
| 1310 | |
| 1311 | yield* trackCliEventEffect({ name: 'producer_event' }); |
| 1312 | |
| 1313 | const args = childProcessMocks.spawn.mock.calls[0]![1] as string[]; |
| 1314 | const payload = decodeWorkerPayload<{ distinctId: string }>(args[2]!); |
| 1315 | expect(payload.distinctId).toBe('anon_install_json'); |
| 1316 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 1317 | }); |
| 1318 | |
| 1319 | it.effect('redacts secret-shaped tokens from event properties', () => { |
| 1320 | const home = tempy.temporaryDirectory(); |
| 1321 | const scriptPath = `${home}/composio.ts`; |
| 1322 | enableTelemetry(); |
| 1323 | process.argv[1] = scriptPath; |
| 1324 | |
| 1325 | return Effect.gen(function* () { |
| 1326 | const fs = yield* FileSystem.FileSystem; |
| 1327 | yield* fs.writeFileString(scriptPath, ''); |
| 1328 | |
| 1329 | yield* trackCliEventEffect({ |
| 1330 | name: CLI_ANALYTICS_EVENTS.CLI_COMMAND_FAILED, |
| 1331 | properties: { |
| 1332 | error_message: 'Auth failed for uak_AbC123xyz_secret via Bearer eyJhbGciOi.Jt0ken', |
| 1333 | nested: { tokens: ['ghp_ABCdef1234567890'] }, |
| 1334 | duration_ms: 42, |
| 1335 | }, |
| 1336 | }); |
| 1337 | |
| 1338 | const args = childProcessMocks.spawn.mock.calls[0]![1] as string[]; |
| 1339 | const payload = decodeWorkerPayload<{ |
| 1340 | properties: { |
| 1341 | error_message: string; |
| 1342 | nested: { tokens: string[] }; |
| 1343 | duration_ms: number; |
| 1344 | }; |
| 1345 | }>(args[2]!); |
| 1346 | expect(payload.properties.error_message).toBe('Auth failed for [redacted] via [redacted]'); |
| 1347 | expect(payload.properties.nested.tokens).toEqual(['[redacted]']); |
| 1348 | expect(payload.properties.duration_ms).toBe(42); |
| 1349 | }).pipe(Effect.provide(makePlatformLayer(home))); |
| 1350 | }); |
| 1351 | }); |
| 1352 | }); |