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 path from 'node:path'; |
| 2 | import * as tempy from 'tempy'; |
| 3 | import { describe, expect, layer, assert } from '@effect/vitest'; |
| 4 | import { beforeEach, vi } from 'vitest'; |
| 5 | import { ConfigProvider, Effect, Layer, Result } from 'effect'; |
| 6 | import { liveEnvConfigProvider } from 'test/__utils__'; |
| 7 | import * as FileSystem from 'effect/FileSystem'; |
| 8 | import * as BunFileSystem from '@effect/platform-bun/BunFileSystem'; |
| 9 | import { |
| 10 | ProjectEnvironmentDetector, |
| 11 | ProjectEnvironmentDetectorError, |
| 12 | } from 'src/services/project-environment-detector'; |
| 13 | |
| 14 | // The package-manager cases below set `npm_config_user_agent` on the live process environment. |
| 15 | const testLayer = Layer.provideMerge( |
| 16 | ProjectEnvironmentDetector.Default, |
| 17 | Layer.mergeAll(BunFileSystem.layer, ConfigProvider.layer(liveEnvConfigProvider)) |
| 18 | ); |
| 19 | |
| 20 | const writeFile = (fs: FileSystem.FileSystem, filePath: string, content: string) => |
| 21 | Effect.gen(function* () { |
| 22 | const dir = path.dirname(filePath); |
| 23 | yield* fs.makeDirectory(dir, { recursive: true }).pipe(Effect.catch(() => Effect.void)); |
| 24 | yield* fs.writeFileString(filePath, content); |
| 25 | }); |
| 26 | |
| 27 | describe('ProjectEnvironmentDetector', () => { |
| 28 | // `unstubEnvs` in the Vitest config restores the inherited value after each test. |
| 29 | beforeEach(() => { |
| 30 | vi.stubEnv('npm_config_user_agent', undefined); |
| 31 | }); |
| 32 | |
| 33 | describe('detectProjectEnvironment', () => { |
| 34 | // -- TypeScript detection -- |
| 35 | |
| 36 | layer(testLayer)('TypeScript detection', it => { |
| 37 | it.effect('[Given] package.json + tsconfig.json [Then] detects typescript + npm', () => |
| 38 | Effect.gen(function* () { |
| 39 | const fs = yield* FileSystem.FileSystem; |
| 40 | const detector = yield* ProjectEnvironmentDetector; |
| 41 | const cwd = tempy.temporaryDirectory(); |
| 42 | |
| 43 | yield* writeFile(fs, path.join(cwd, 'package.json'), JSON.stringify({ name: 'test' })); |
| 44 | yield* writeFile(fs, path.join(cwd, 'tsconfig.json'), '{}'); |
| 45 | |
| 46 | const result = yield* detector.detectProjectEnvironment(cwd); |
| 47 | |
| 48 | expect(result.kind).toBe('js'); |
| 49 | expect(result.language).toBe('typescript'); |
| 50 | expect(result.packageManager).toBe('npm'); |
| 51 | expect(result.rootDir).toBe(cwd); |
| 52 | }) |
| 53 | ); |
| 54 | |
| 55 | it.effect( |
| 56 | '[Given] pnpm-lock.yaml + package.json with packageManager:pnpm@ [Then] detects typescript + pnpm', |
| 57 | () => |
| 58 | Effect.gen(function* () { |
| 59 | const fs = yield* FileSystem.FileSystem; |
| 60 | const detector = yield* ProjectEnvironmentDetector; |
| 61 | const cwd = tempy.temporaryDirectory(); |
| 62 | |
| 63 | yield* writeFile( |
| 64 | fs, |
| 65 | path.join(cwd, 'package.json'), |
| 66 | JSON.stringify({ |
| 67 | name: 'test-mono', |
| 68 | packageManager: 'pnpm@9.15.0', |
| 69 | devDependencies: { typescript: '^5.0.0' }, |
| 70 | }) |
| 71 | ); |
| 72 | yield* writeFile(fs, path.join(cwd, 'pnpm-lock.yaml'), 'lockfileVersion: 9'); |
| 73 | yield* writeFile(fs, path.join(cwd, 'tsconfig.json'), '{}'); |
| 74 | |
| 75 | const result = yield* detector.detectProjectEnvironment(cwd); |
| 76 | |
| 77 | expect(result.kind).toBe('js'); |
| 78 | expect(result.language).toBe('typescript'); |
| 79 | expect(result.packageManager).toBe('pnpm'); |
| 80 | }) |
| 81 | ); |
| 82 | |
| 83 | it.effect('[Given] bun.lockb [Then] detects js + bun', () => |
| 84 | Effect.gen(function* () { |
| 85 | const fs = yield* FileSystem.FileSystem; |
| 86 | const detector = yield* ProjectEnvironmentDetector; |
| 87 | const cwd = tempy.temporaryDirectory(); |
| 88 | |
| 89 | yield* writeFile(fs, path.join(cwd, 'package.json'), JSON.stringify({ name: 'test' })); |
| 90 | yield* writeFile(fs, path.join(cwd, 'bun.lockb'), ''); |
| 91 | |
| 92 | const result = yield* detector.detectProjectEnvironment(cwd); |
| 93 | |
| 94 | expect(result.kind).toBe('js'); |
| 95 | expect(result.packageManager).toBe('bun'); |
| 96 | }) |
| 97 | ); |
| 98 | |
| 99 | it.effect('[Given] bun.lock (text format) [Then] detects bun', () => |
| 100 | Effect.gen(function* () { |
| 101 | const fs = yield* FileSystem.FileSystem; |
| 102 | const detector = yield* ProjectEnvironmentDetector; |
| 103 | const cwd = tempy.temporaryDirectory(); |
| 104 | |
| 105 | yield* writeFile(fs, path.join(cwd, 'package.json'), JSON.stringify({ name: 'test' })); |
| 106 | yield* writeFile(fs, path.join(cwd, 'bun.lock'), '{}'); |
| 107 | |
| 108 | const result = yield* detector.detectProjectEnvironment(cwd); |
| 109 | |
| 110 | expect(result.kind).toBe('js'); |
| 111 | expect(result.packageManager).toBe('bun'); |
| 112 | }) |
| 113 | ); |
| 114 | |
| 115 | it.effect('[Given] package.json with typescript dep [Then] detects typescript', () => |
| 116 | Effect.gen(function* () { |
| 117 | const fs = yield* FileSystem.FileSystem; |
| 118 | const detector = yield* ProjectEnvironmentDetector; |
| 119 | const cwd = tempy.temporaryDirectory(); |
| 120 | |
| 121 | yield* writeFile( |
| 122 | fs, |
| 123 | path.join(cwd, 'package.json'), |
| 124 | JSON.stringify({ name: 'test', devDependencies: { typescript: '^5.0.0' } }) |
| 125 | ); |
| 126 | |
| 127 | const result = yield* detector.detectProjectEnvironment(cwd); |
| 128 | |
| 129 | expect(result.kind).toBe('js'); |
| 130 | expect(result.language).toBe('typescript'); |
| 131 | }) |
| 132 | ); |
| 133 | |
| 134 | it.effect('[Given] monorepo root with pnpm-workspace.yaml [Then] detects pnpm', () => |
| 135 | Effect.gen(function* () { |
| 136 | const fs = yield* FileSystem.FileSystem; |
| 137 | const detector = yield* ProjectEnvironmentDetector; |
| 138 | const cwd = tempy.temporaryDirectory(); |
| 139 | |
| 140 | yield* writeFile( |
| 141 | fs, |
| 142 | path.join(cwd, 'package.json'), |
| 143 | JSON.stringify({ name: 'monorepo' }) |
| 144 | ); |
| 145 | yield* writeFile( |
| 146 | fs, |
| 147 | path.join(cwd, 'pnpm-workspace.yaml'), |
| 148 | "packages:\n - 'packages/*'" |
| 149 | ); |
| 150 | yield* writeFile(fs, path.join(cwd, 'tsconfig.json'), '{}'); |
| 151 | |
| 152 | const result = yield* detector.detectProjectEnvironment(cwd); |
| 153 | |
| 154 | expect(result.kind).toBe('js'); |
| 155 | expect(result.packageManager).toBe('pnpm'); |
| 156 | }) |
| 157 | ); |
| 158 | }); |
| 159 | |
| 160 | // -- Python detection -- |
| 161 | |
| 162 | layer(testLayer)('Python detection', it => { |
| 163 | it.effect('[Given] pyproject.toml with [tool.uv] + uv.lock [Then] detects python + uv', () => |
| 164 | Effect.gen(function* () { |
| 165 | const fs = yield* FileSystem.FileSystem; |
| 166 | const detector = yield* ProjectEnvironmentDetector; |
| 167 | const cwd = tempy.temporaryDirectory(); |
| 168 | |
| 169 | yield* writeFile( |
| 170 | fs, |
| 171 | path.join(cwd, 'pyproject.toml'), |
| 172 | '[project]\nname = "test"\n\n[tool.uv]\ndev-dependencies = []' |
| 173 | ); |
| 174 | yield* writeFile(fs, path.join(cwd, 'uv.lock'), 'version = 1'); |
| 175 | |
| 176 | const result = yield* detector.detectProjectEnvironment(cwd); |
| 177 | |
| 178 | expect(result.kind).toBe('python'); |
| 179 | expect(result.language).toBe('python'); |
| 180 | expect(result.packageManager).toBe('uv'); |
| 181 | }) |
| 182 | ); |
| 183 | |
| 184 | it.effect('[Given] requirements.txt only [Then] detects python + pip', () => |
| 185 | Effect.gen(function* () { |
| 186 | const fs = yield* FileSystem.FileSystem; |
| 187 | const detector = yield* ProjectEnvironmentDetector; |
| 188 | const cwd = tempy.temporaryDirectory(); |
| 189 | |
| 190 | yield* writeFile(fs, path.join(cwd, 'requirements.txt'), 'composio'); |
| 191 | |
| 192 | const result = yield* detector.detectProjectEnvironment(cwd); |
| 193 | |
| 194 | expect(result.kind).toBe('python'); |
| 195 | expect(result.language).toBe('python'); |
| 196 | expect(result.packageManager).toBe('pip'); |
| 197 | }) |
| 198 | ); |
| 199 | |
| 200 | it.effect('[Given] setup.py only [Then] detects python + pip', () => |
| 201 | Effect.gen(function* () { |
| 202 | const fs = yield* FileSystem.FileSystem; |
| 203 | const detector = yield* ProjectEnvironmentDetector; |
| 204 | const cwd = tempy.temporaryDirectory(); |
| 205 | |
| 206 | yield* writeFile( |
| 207 | fs, |
| 208 | path.join(cwd, 'setup.py'), |
| 209 | 'from setuptools import setup\nsetup(name="test")' |
| 210 | ); |
| 211 | |
| 212 | const result = yield* detector.detectProjectEnvironment(cwd); |
| 213 | |
| 214 | expect(result.kind).toBe('python'); |
| 215 | expect(result.packageManager).toBe('pip'); |
| 216 | }) |
| 217 | ); |
| 218 | }); |
| 219 | |
| 220 | // -- Ambiguity -- |
| 221 | |
| 222 | layer(testLayer)('Ambiguity handling', it => { |
| 223 | it.effect( |
| 224 | '[Given] both package.json + pyproject.toml at CWD [Then] fails with ambiguity error', |
| 225 | () => |
| 226 | Effect.gen(function* () { |
| 227 | const fs = yield* FileSystem.FileSystem; |
| 228 | const detector = yield* ProjectEnvironmentDetector; |
| 229 | const cwd = tempy.temporaryDirectory(); |
| 230 | |
| 231 | yield* writeFile(fs, path.join(cwd, 'package.json'), JSON.stringify({ name: 'test' })); |
| 232 | yield* writeFile(fs, path.join(cwd, 'pyproject.toml'), '[project]\nname = "test"'); |
| 233 | yield* writeFile(fs, path.join(cwd, 'requirements.txt'), 'composio'); |
| 234 | |
| 235 | const result = yield* detector.detectProjectEnvironment(cwd).pipe(Effect.result); |
| 236 | |
| 237 | assert(Result.isFailure(result)); |
| 238 | expect(result.failure).toBeInstanceOf(ProjectEnvironmentDetectorError); |
| 239 | expect(result.failure.message).toContain('both'); |
| 240 | }) |
| 241 | ); |
| 242 | }); |
| 243 | |
| 244 | // -- Edge cases -- |
| 245 | |
| 246 | layer(testLayer)('Edge cases', it => { |
| 247 | it.effect('[Given] empty directory [Then] fails with no-detection error', () => |
| 248 | Effect.gen(function* () { |
| 249 | const fs = yield* FileSystem.FileSystem; |
| 250 | const detector = yield* ProjectEnvironmentDetector; |
| 251 | const tempRoot = tempy.temporaryDirectory(); |
| 252 | const emptyDir = path.join(tempRoot, 'a', 'b', 'c'); |
| 253 | yield* fs.makeDirectory(emptyDir, { recursive: true }); |
| 254 | |
| 255 | const result = yield* detector.detectProjectEnvironment(emptyDir).pipe(Effect.result); |
| 256 | |
| 257 | assert(Result.isFailure(result)); |
| 258 | expect(result.failure.message).toContain('No recognizable'); |
| 259 | }) |
| 260 | ); |
| 261 | |
| 262 | it.effect('[Given] indicators at parent directory [Then] detects from parent', () => |
| 263 | Effect.gen(function* () { |
| 264 | const fs = yield* FileSystem.FileSystem; |
| 265 | const detector = yield* ProjectEnvironmentDetector; |
| 266 | const parentDir = tempy.temporaryDirectory(); |
| 267 | const childDir = path.join(parentDir, 'subdir'); |
| 268 | yield* fs.makeDirectory(childDir, { recursive: true }); |
| 269 | |
| 270 | yield* writeFile( |
| 271 | fs, |
| 272 | path.join(parentDir, 'package.json'), |
| 273 | JSON.stringify({ name: 'parent', packageManager: 'pnpm@9.0.0' }) |
| 274 | ); |
| 275 | yield* writeFile(fs, path.join(parentDir, 'pnpm-lock.yaml'), ''); |
| 276 | yield* writeFile(fs, path.join(parentDir, 'tsconfig.json'), '{}'); |
| 277 | |
| 278 | const result = yield* detector.detectProjectEnvironment(childDir); |
| 279 | |
| 280 | expect(result.kind).toBe('js'); |
| 281 | expect(result.packageManager).toBe('pnpm'); |
| 282 | expect(result.rootDir).toBe(parentDir); |
| 283 | }) |
| 284 | ); |
| 285 | }); |
| 286 | }); |
| 287 | |
| 288 | // -- detectJsPackageManager -- |
| 289 | |
| 290 | describe('detectJsPackageManager', () => { |
| 291 | layer(testLayer)('JS package manager detection', it => { |
| 292 | it.effect('[Given] packageManager field in package.json [Then] uses that', () => |
| 293 | Effect.gen(function* () { |
| 294 | const fs = yield* FileSystem.FileSystem; |
| 295 | const detector = yield* ProjectEnvironmentDetector; |
| 296 | const cwd = tempy.temporaryDirectory(); |
| 297 | |
| 298 | yield* writeFile( |
| 299 | fs, |
| 300 | path.join(cwd, 'package.json'), |
| 301 | JSON.stringify({ name: 'test', packageManager: 'yarn@4.0.0' }) |
| 302 | ); |
| 303 | |
| 304 | const result = yield* detector.detectJsPackageManager(cwd); |
| 305 | expect(result).toBe('yarn'); |
| 306 | }) |
| 307 | ); |
| 308 | |
| 309 | it.effect('[Given] lock file present [Then] detects from lock file', () => |
| 310 | Effect.gen(function* () { |
| 311 | const fs = yield* FileSystem.FileSystem; |
| 312 | const detector = yield* ProjectEnvironmentDetector; |
| 313 | const cwd = tempy.temporaryDirectory(); |
| 314 | |
| 315 | yield* writeFile(fs, path.join(cwd, 'package.json'), JSON.stringify({ name: 'test' })); |
| 316 | yield* writeFile(fs, path.join(cwd, 'yarn.lock'), ''); |
| 317 | |
| 318 | const result = yield* detector.detectJsPackageManager(cwd); |
| 319 | expect(result).toBe('yarn'); |
| 320 | }) |
| 321 | ); |
| 322 | |
| 323 | it.effect('[Given] no lock file, no packageManager field [Then] defaults to npm', () => |
| 324 | Effect.gen(function* () { |
| 325 | const detector = yield* ProjectEnvironmentDetector; |
| 326 | const cwd = tempy.temporaryDirectory(); |
| 327 | |
| 328 | const result = yield* detector.detectJsPackageManager(cwd); |
| 329 | expect(result).toBe('npm'); |
| 330 | }) |
| 331 | ); |
| 332 | |
| 333 | it.effect( |
| 334 | '[Given] no lock file, no packageManager field, but npm_config_user_agent=pnpm [Then] detects pnpm', |
| 335 | () => |
| 336 | Effect.gen(function* () { |
| 337 | const detector = yield* ProjectEnvironmentDetector; |
| 338 | const cwd = tempy.temporaryDirectory(); |
| 339 | |
| 340 | vi.stubEnv('npm_config_user_agent', 'pnpm/9.15.0 npm/? node/v20.11.0'); |
| 341 | |
| 342 | const result = yield* detector.detectJsPackageManager(cwd); |
| 343 | expect(result).toBe('pnpm'); |
| 344 | }) |
| 345 | ); |
| 346 | |
| 347 | it.effect( |
| 348 | '[Given] no lock file, no packageManager field, but npm_config_user_agent=bun [Then] detects bun', |
| 349 | () => |
| 350 | Effect.gen(function* () { |
| 351 | const detector = yield* ProjectEnvironmentDetector; |
| 352 | const cwd = tempy.temporaryDirectory(); |
| 353 | |
| 354 | vi.stubEnv('npm_config_user_agent', 'bun/1.1.0'); |
| 355 | |
| 356 | const result = yield* detector.detectJsPackageManager(cwd); |
| 357 | expect(result).toBe('bun'); |
| 358 | }) |
| 359 | ); |
| 360 | }); |
| 361 | }); |
| 362 | |
| 363 | // -- detectPythonPackageManager -- |
| 364 | |
| 365 | describe('detectPythonPackageManager', () => { |
| 366 | layer(testLayer)('Python package manager detection', it => { |
| 367 | it.effect('[Given] uv.lock present [Then] detects uv', () => |
| 368 | Effect.gen(function* () { |
| 369 | const fs = yield* FileSystem.FileSystem; |
| 370 | const detector = yield* ProjectEnvironmentDetector; |
| 371 | const cwd = tempy.temporaryDirectory(); |
| 372 | |
| 373 | yield* writeFile(fs, path.join(cwd, 'uv.lock'), 'version = 1'); |
| 374 | |
| 375 | const result = yield* detector.detectPythonPackageManager(cwd); |
| 376 | expect(result).toBe('uv'); |
| 377 | }) |
| 378 | ); |
| 379 | |
| 380 | it.effect('[Given] [tool.uv] in pyproject.toml [Then] detects uv', () => |
| 381 | Effect.gen(function* () { |
| 382 | const fs = yield* FileSystem.FileSystem; |
| 383 | const detector = yield* ProjectEnvironmentDetector; |
| 384 | const cwd = tempy.temporaryDirectory(); |
| 385 | |
| 386 | yield* writeFile( |
| 387 | fs, |
| 388 | path.join(cwd, 'pyproject.toml'), |
| 389 | '[project]\nname = "test"\n\n[tool.uv]\ndev-dependencies = []' |
| 390 | ); |
| 391 | |
| 392 | const result = yield* detector.detectPythonPackageManager(cwd); |
| 393 | expect(result).toBe('uv'); |
| 394 | }) |
| 395 | ); |
| 396 | |
| 397 | it.effect('[Given] no uv indicators [Then] defaults to pip', () => |
| 398 | Effect.gen(function* () { |
| 399 | const fs = yield* FileSystem.FileSystem; |
| 400 | const detector = yield* ProjectEnvironmentDetector; |
| 401 | const cwd = tempy.temporaryDirectory(); |
| 402 | |
| 403 | yield* writeFile(fs, path.join(cwd, 'requirements.txt'), 'composio'); |
| 404 | |
| 405 | const result = yield* detector.detectPythonPackageManager(cwd); |
| 406 | expect(result).toBe('pip'); |
| 407 | }) |
| 408 | ); |
| 409 | }); |
| 410 | }); |
| 411 | }); |