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, layer } from '@effect/vitest'; |
| 2 | import { afterEach, beforeEach, vi } from 'vitest'; |
| 3 | import { ConfigProvider, Effect, Option } from 'effect'; |
| 4 | import * as FileSystem from 'effect/FileSystem'; |
| 5 | import * as Path from 'effect/Path'; |
| 6 | import * as tempy from 'tempy'; |
| 7 | import { |
| 8 | appendCliSessionHistory, |
| 9 | resolveArtifactsRoot, |
| 10 | resolveCliSessionArtifacts, |
| 11 | storeCliSessionArtifact, |
| 12 | } from 'src/services/cli-session-artifacts'; |
| 13 | import { ComposioCliUserConfig } from 'src/services/cli-user-config'; |
| 14 | import { defaultNodeOs, NodeOs } from 'src/services/node-os'; |
| 15 | import { TestLive } from 'test/__utils__'; |
| 16 | |
| 17 | // Backed by the live `process.env` record so `vi.stubEnv` calls made inside a test are |
| 18 | // observed by later config reads; `ConfigProvider.fromEnv()` would snapshot the environment. |
| 19 | const environmentTestConfigProvider = ConfigProvider.fromEnvRecord(process.env); |
| 20 | |
| 21 | const cacheEnabledTestConfigProvider = ConfigProvider.fromEnvRecord({ |
| 22 | COMPOSIO_DISABLE_CONNECTED_ACCOUNT_CACHE: 'false', |
| 23 | }).pipe(ConfigProvider.orElse(ConfigProvider.fromEnvRecord(process.env))); |
| 24 | |
| 25 | describe('CLI session artifacts', () => { |
| 26 | beforeEach(() => { |
| 27 | vi.stubEnv('COMPOSIO_SESSION_DIR', undefined); |
| 28 | vi.stubEnv('COMPOSIO_CACHE_DIR', undefined); |
| 29 | }); |
| 30 | |
| 31 | afterEach(() => { |
| 32 | vi.unstubAllEnvs(); |
| 33 | }); |
| 34 | |
| 35 | layer(TestLive({ baseConfigProvider: environmentTestConfigProvider }))(it => { |
| 36 | it.effect('resolves artifact roots in documented precedence order', () => |
| 37 | Effect.gen(function* () { |
| 38 | const path = yield* Path.Path; |
| 39 | const config = yield* ComposioCliUserConfig; |
| 40 | const sessionRoot = tempy.temporaryDirectory(); |
| 41 | const cacheRoot = tempy.temporaryDirectory(); |
| 42 | const configuredRoot = tempy.temporaryDirectory(); |
| 43 | const temporaryRoot = tempy.temporaryDirectory(); |
| 44 | const root = resolveArtifactsRoot.pipe( |
| 45 | Effect.provideService( |
| 46 | NodeOs, |
| 47 | defaultNodeOs({ homedir: tempy.temporaryDirectory(), tmpdir: temporaryRoot }) |
| 48 | ) |
| 49 | ); |
| 50 | |
| 51 | yield* config.update({ artifactDirectory: Option.some(configuredRoot) }); |
| 52 | vi.stubEnv('COMPOSIO_SESSION_DIR', sessionRoot); |
| 53 | vi.stubEnv('COMPOSIO_CACHE_DIR', cacheRoot); |
| 54 | expect(yield* root).toBe(sessionRoot); |
| 55 | |
| 56 | vi.stubEnv('COMPOSIO_SESSION_DIR', ' '); |
| 57 | expect(yield* root).toBe(cacheRoot); |
| 58 | |
| 59 | vi.stubEnv('COMPOSIO_CACHE_DIR', ' '); |
| 60 | expect(yield* root).toBe(configuredRoot); |
| 61 | |
| 62 | yield* config.update({ artifactDirectory: Option.none() }); |
| 63 | expect(yield* root).toBe(path.join(temporaryRoot, 'composio')); |
| 64 | }) |
| 65 | ); |
| 66 | |
| 67 | it.effect('stores a sanitized artifact with the requested extension', () => |
| 68 | Effect.gen(function* () { |
| 69 | const fs = yield* FileSystem.FileSystem; |
| 70 | const path = yield* Path.Path; |
| 71 | const directoryPath = yield* fs.makeTempDirectoryScoped(); |
| 72 | |
| 73 | const filePath = yield* storeCliSessionArtifact({ |
| 74 | contents: '{"ok":true}', |
| 75 | name: 'github issue output', |
| 76 | extension: '.json', |
| 77 | directoryPath, |
| 78 | }).pipe(Effect.flatMap(Effect.fromNullishOr)); |
| 79 | |
| 80 | expect(path.dirname(filePath)).toBe(directoryPath); |
| 81 | expect(path.basename(filePath)).toMatch(/^github_issue_output_[a-f0-9]{8}\.json$/i); |
| 82 | expect(yield* fs.readFileString(filePath)).toBe('{"ok":true}'); |
| 83 | }) |
| 84 | ); |
| 85 | |
| 86 | it.effect('returns undefined when the artifact directory cannot be created', () => |
| 87 | Effect.gen(function* () { |
| 88 | const fs = yield* FileSystem.FileSystem; |
| 89 | const filePath = yield* fs.makeTempFileScoped(); |
| 90 | |
| 91 | expect( |
| 92 | yield* storeCliSessionArtifact({ |
| 93 | contents: '{}', |
| 94 | name: 'output', |
| 95 | directoryPath: filePath, |
| 96 | }) |
| 97 | ).toBeUndefined(); |
| 98 | }) |
| 99 | ); |
| 100 | |
| 101 | it.effect('stores an adhoc artifact when no CLI session is available', () => |
| 102 | Effect.gen(function* () { |
| 103 | const path = yield* Path.Path; |
| 104 | const artifactsRoot = tempy.temporaryDirectory(); |
| 105 | vi.stubEnv('COMPOSIO_SESSION_DIR', artifactsRoot); |
| 106 | |
| 107 | const filePath = yield* storeCliSessionArtifact({ |
| 108 | contents: '{}', |
| 109 | name: 'adhoc output', |
| 110 | }).pipe(Effect.flatMap(Effect.fromNullishOr)); |
| 111 | |
| 112 | const directoryPath = path.dirname(filePath); |
| 113 | expect(path.dirname(directoryPath)).toBe(artifactsRoot); |
| 114 | expect(path.basename(directoryPath)).toMatch(/^adhoc_[a-f0-9]{12}$/i); |
| 115 | }) |
| 116 | ); |
| 117 | }); |
| 118 | |
| 119 | layer(TestLive({ baseConfigProvider: cacheEnabledTestConfigProvider }))(it => { |
| 120 | const sessionScope = { |
| 121 | orgId: 'org_test', |
| 122 | consumerUserId: 'consumer-user-test', |
| 123 | } as const; |
| 124 | |
| 125 | it.effect('stores artifacts under the current CLI session without a directory override', () => |
| 126 | Effect.gen(function* () { |
| 127 | const path = yield* Path.Path; |
| 128 | const artifactsRoot = tempy.temporaryDirectory(); |
| 129 | vi.stubEnv('COMPOSIO_SESSION_DIR', artifactsRoot); |
| 130 | |
| 131 | const filePath = yield* storeCliSessionArtifact({ |
| 132 | contents: '{"session":true}', |
| 133 | name: 'session output', |
| 134 | ...sessionScope, |
| 135 | }).pipe(Effect.flatMap(Effect.fromNullishOr)); |
| 136 | |
| 137 | const directoryPath = path.dirname(filePath); |
| 138 | expect(path.dirname(directoryPath)).toBe(artifactsRoot); |
| 139 | expect(path.basename(directoryPath)).toMatch(/^cli_s_[a-z0-9]+_[a-f0-9]{10}$/i); |
| 140 | }) |
| 141 | ); |
| 142 | |
| 143 | it.effect('appends multiple JSONL history entries to the same session', () => |
| 144 | Effect.gen(function* () { |
| 145 | const fs = yield* FileSystem.FileSystem; |
| 146 | const artifactsRoot = tempy.temporaryDirectory(); |
| 147 | vi.stubEnv('COMPOSIO_SESSION_DIR', artifactsRoot); |
| 148 | |
| 149 | yield* appendCliSessionHistory({ entry: { sequence: 1 }, ...sessionScope }); |
| 150 | yield* appendCliSessionHistory({ entry: { sequence: 2 }, ...sessionScope }); |
| 151 | const artifacts = yield* resolveCliSessionArtifacts(sessionScope).pipe( |
| 152 | Effect.flatMap(Effect.fromOption) |
| 153 | ); |
| 154 | const lines = (yield* fs.readFileString(artifacts.historyFilePath, 'utf8')) |
| 155 | .trim() |
| 156 | .split('\n') |
| 157 | .map(line => JSON.parse(line) as { sessionId: string; sequence: number }); |
| 158 | expect(lines).toEqual([ |
| 159 | expect.objectContaining({ sessionId: artifacts.sessionId, sequence: 1 }), |
| 160 | expect.objectContaining({ sessionId: artifacts.sessionId, sequence: 2 }), |
| 161 | ]); |
| 162 | }) |
| 163 | ); |
| 164 | |
| 165 | it.effect('keeps history writes best-effort when the artifact root is not a directory', () => |
| 166 | Effect.gen(function* () { |
| 167 | const fs = yield* FileSystem.FileSystem; |
| 168 | const rootFile = yield* fs.makeTempFileScoped(); |
| 169 | vi.stubEnv('COMPOSIO_SESSION_DIR', rootFile); |
| 170 | |
| 171 | yield* appendCliSessionHistory({ entry: { sequence: 1 }, ...sessionScope }); |
| 172 | |
| 173 | expect(yield* fs.readFileString(rootFile, 'utf8')).toBe(''); |
| 174 | }) |
| 175 | ); |
| 176 | }); |
| 177 | }); |