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 { dirname, isAbsolute, resolve } from 'node:path'; |
| 2 | import { fileURLToPath } from 'node:url'; |
| 3 | import { readFileSync } from 'node:fs'; |
| 4 | import { execFileSync } from 'node:child_process'; |
| 5 | import type { |
| 6 | NodeVersionMeta, |
| 7 | NodeVersionFromUser, |
| 8 | CliVersionMeta, |
| 9 | CliVersionFromUser, |
| 10 | SkipInCI, |
| 11 | NonEmptyArray, |
| 12 | } from './types'; |
| 13 | import { INSTALL_E2E_MODES, INSTALL_E2E_SHELLS } from './const'; |
| 14 | |
| 15 | export type InstallE2EMode = (typeof INSTALL_E2E_MODES)[number]; |
| 16 | export type InstallE2EShell = (typeof INSTALL_E2E_SHELLS)[number]; |
| 17 | |
| 18 | export interface InstallE2EConfig { |
| 19 | mode: InstallE2EMode; |
| 20 | shell: InstallE2EShell; |
| 21 | version: string; |
| 22 | releaseDir?: string; |
| 23 | } |
| 24 | |
| 25 | declare module 'bun' { |
| 26 | interface Env { |
| 27 | CI?: string; |
| 28 | COMPOSIO_E2E_NODE_VERSION?: string; |
| 29 | COMPOSIO_E2E_CLI_VERSION?: string; |
| 30 | COMPOSIO_INSTALL_E2E_MODE?: string; |
| 31 | COMPOSIO_INSTALL_E2E_SHELL?: string; |
| 32 | COMPOSIO_INSTALL_E2E_VERSION?: string; |
| 33 | COMPOSIO_INSTALL_E2E_RELEASE_DIR?: string; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | function resolveEnumValue<T extends string>( |
| 38 | name: string, |
| 39 | value: string | undefined, |
| 40 | allowed: readonly T[], |
| 41 | fallback: T |
| 42 | ): T { |
| 43 | const resolved = value || fallback; |
| 44 | if (!allowed.includes(resolved as T)) { |
| 45 | throw new Error(`${name} must be one of: ${allowed.join(', ')} (got ${resolved})`); |
| 46 | } |
| 47 | return resolved as T; |
| 48 | } |
| 49 | |
| 50 | export function resolveInstallE2EConfig(): InstallE2EConfig { |
| 51 | const mode = resolveEnumValue( |
| 52 | 'COMPOSIO_INSTALL_E2E_MODE', |
| 53 | Bun.env.COMPOSIO_INSTALL_E2E_MODE, |
| 54 | INSTALL_E2E_MODES, |
| 55 | 'local' |
| 56 | ); |
| 57 | const shell = resolveEnumValue( |
| 58 | 'COMPOSIO_INSTALL_E2E_SHELL', |
| 59 | Bun.env.COMPOSIO_INSTALL_E2E_SHELL, |
| 60 | INSTALL_E2E_SHELLS, |
| 61 | 'bash' |
| 62 | ); |
| 63 | const version = Bun.env.COMPOSIO_INSTALL_E2E_VERSION || 'latest'; |
| 64 | const releaseDir = Bun.env.COMPOSIO_INSTALL_E2E_RELEASE_DIR; |
| 65 | |
| 66 | if ( |
| 67 | version !== 'latest' && |
| 68 | !/^@composio\/cli@[0-9]+\.[0-9]+\.[0-9]+(?:-beta\.[0-9]+)?$/.test(version) |
| 69 | ) { |
| 70 | throw new Error( |
| 71 | 'COMPOSIO_INSTALL_E2E_VERSION must be latest or a full @composio/cli release tag' |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | if (mode === 'local') { |
| 76 | if (!releaseDir) { |
| 77 | throw new Error('COMPOSIO_INSTALL_E2E_RELEASE_DIR is required in local mode'); |
| 78 | } |
| 79 | if (!isAbsolute(releaseDir)) { |
| 80 | throw new Error('COMPOSIO_INSTALL_E2E_RELEASE_DIR must be an absolute path'); |
| 81 | } |
| 82 | if (version !== 'latest') { |
| 83 | throw new Error('COMPOSIO_INSTALL_E2E_VERSION is only supported in prod mode'); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return { |
| 88 | mode, |
| 89 | shell, |
| 90 | version, |
| 91 | releaseDir, |
| 92 | }; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Determine if we're running in a Continuous Integration suite. |
| 97 | */ |
| 98 | export function isCI(): boolean { |
| 99 | return Boolean(Bun.env.CI); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get the repository root path. |
| 104 | * Computed from the location of this module file. |
| 105 | */ |
| 106 | export function getRepoRoot(): string { |
| 107 | const here = dirname(fileURLToPath(import.meta.url)); |
| 108 | // From _utils/src/ to repo root: src -> _utils -> e2e-tests -> ts -> composio |
| 109 | return resolve(here, '../../../..'); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Resolve the Node.js versions to test against, with CI skip state. |
| 114 | * |
| 115 | * The `'current'` version always resolves to the value in mise.toml. |
| 116 | * |
| 117 | * In CI mode (CI env var set + COMPOSIO_E2E_NODE_VERSION set): |
| 118 | * - Versions not matching COMPOSIO_E2E_NODE_VERSION are marked to skip |
| 119 | * |
| 120 | * In local mode: |
| 121 | * - COMPOSIO_E2E_NODE_VERSION overrides everything (single version, no skip) |
| 122 | * - Otherwise returns all configured versions (no skip) |
| 123 | */ |
| 124 | export function resolveNodeVersionMetaList( |
| 125 | configNodeVersions?: readonly NodeVersionFromUser[] |
| 126 | ): NonEmptyArray<NodeVersionMeta> { |
| 127 | const envVersion = Bun.env.COMPOSIO_E2E_NODE_VERSION; |
| 128 | |
| 129 | // Local mode with env override: single version, no skip |
| 130 | if (!isCI() && envVersion) { |
| 131 | return [{ kind: 'overridden', value: envVersion, skip: { value: false } }]; |
| 132 | } |
| 133 | |
| 134 | // Only read mise.toml after env override check. |
| 135 | const miseNodeVersion = getMiseVersion('node'); |
| 136 | |
| 137 | // No config provided: use mise.toml version |
| 138 | if (configNodeVersions === undefined || configNodeVersions.length === 0) { |
| 139 | return [{ kind: 'current', value: miseNodeVersion, skip: { value: false } }]; |
| 140 | } |
| 141 | |
| 142 | const resolvedVersions = configNodeVersions.map((v): NodeVersionMeta => { |
| 143 | if (v === 'current') { |
| 144 | return { |
| 145 | kind: 'current', |
| 146 | value: miseNodeVersion, |
| 147 | skip: computeSkipForVersion(envVersion, miseNodeVersion), |
| 148 | }; |
| 149 | } |
| 150 | return { kind: 'static', value: v, skip: computeSkipForVersion(envVersion, v) }; |
| 151 | }); |
| 152 | |
| 153 | return resolvedVersions as NonEmptyArray<NodeVersionMeta>; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Compute skip state for a single version in CI mode. |
| 158 | */ |
| 159 | function computeSkipForVersion(envVersion: string | undefined, versionValue: string): SkipInCI { |
| 160 | if (!isCI() || !envVersion) { |
| 161 | return { value: false }; |
| 162 | } |
| 163 | |
| 164 | if (versionValue !== envVersion) { |
| 165 | return { |
| 166 | value: true, |
| 167 | reason: `Node ${versionValue} not selected (running ${envVersion})`, |
| 168 | }; |
| 169 | } |
| 170 | |
| 171 | return { value: false }; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Read a tool version from mise.toml. |
| 176 | * Used to determine the version for 'current' tests. |
| 177 | */ |
| 178 | export function getMiseVersion(tool: 'node'): string { |
| 179 | try { |
| 180 | return execFileSync('mise', ['current', tool], { |
| 181 | cwd: getRepoRoot(), |
| 182 | encoding: 'utf-8', |
| 183 | }).trim(); |
| 184 | } catch (err) { |
| 185 | throw new Error(`Failed to resolve ${tool} version from mise.toml: ${(err as Error).message}`); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Read the CLI version from ts/packages/cli/package.json. |
| 191 | * Used to determine the version for 'current' tests. |
| 192 | */ |
| 193 | export function getCliPackageVersion(): string { |
| 194 | try { |
| 195 | const pkg = readFileSync(resolve(getRepoRoot(), 'ts/packages/cli/package.json'), 'utf-8'); |
| 196 | const parsed = JSON.parse(pkg) as { version?: string }; |
| 197 | if (!parsed.version) { |
| 198 | throw new Error('Missing version field'); |
| 199 | } |
| 200 | return String(parsed.version).trim(); |
| 201 | } catch (err) { |
| 202 | throw new Error( |
| 203 | `Failed to read CLI version from ts/packages/cli/package.json: ${(err as Error).message}` |
| 204 | ); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Compute skip state for a CLI version in CI mode. |
| 210 | */ |
| 211 | function computeSkipForCliVersion(envVersion: string | undefined, versionValue: string): SkipInCI { |
| 212 | if (!isCI() || !envVersion) { |
| 213 | return { value: false }; |
| 214 | } |
| 215 | |
| 216 | if (versionValue !== envVersion) { |
| 217 | return { |
| 218 | value: true, |
| 219 | reason: `CLI ${versionValue} not selected (running ${envVersion})`, |
| 220 | }; |
| 221 | } |
| 222 | |
| 223 | return { value: false }; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Resolve the CLI versions to test against, with CI skip state. |
| 228 | * |
| 229 | * The `'current'` version always resolves to the value in ts/packages/cli/package.json. |
| 230 | * |
| 231 | * In CI mode (CI env var set + COMPOSIO_E2E_CLI_VERSION set): |
| 232 | * - Versions not matching COMPOSIO_E2E_CLI_VERSION are marked to skip |
| 233 | * |
| 234 | * In local mode: |
| 235 | * - COMPOSIO_E2E_CLI_VERSION overrides everything (single version, no skip) |
| 236 | * - Otherwise returns all configured versions (no skip) |
| 237 | */ |
| 238 | export function resolveCliVersionMetaList( |
| 239 | configCliVersions?: readonly CliVersionFromUser[] |
| 240 | ): NonEmptyArray<CliVersionMeta> { |
| 241 | const envVersion = Bun.env.COMPOSIO_E2E_CLI_VERSION; |
| 242 | const currentVersion = getCliPackageVersion(); |
| 243 | |
| 244 | // Local mode with env override: single version, no skip |
| 245 | if (!isCI() && envVersion) { |
| 246 | return [{ kind: 'overridden', value: envVersion, skip: { value: false } }]; |
| 247 | } |
| 248 | |
| 249 | // No config provided: use package.json version |
| 250 | if (configCliVersions === undefined || configCliVersions.length === 0) { |
| 251 | return [{ kind: 'current', value: currentVersion, skip: { value: false } }]; |
| 252 | } |
| 253 | |
| 254 | const resolvedVersions = configCliVersions.map((v): CliVersionMeta => { |
| 255 | if (v === 'current') { |
| 256 | return { |
| 257 | kind: 'current', |
| 258 | value: currentVersion, |
| 259 | skip: computeSkipForCliVersion(envVersion, currentVersion), |
| 260 | }; |
| 261 | } |
| 262 | return { kind: 'static', value: v, skip: computeSkipForCliVersion(envVersion, v) }; |
| 263 | }); |
| 264 | |
| 265 | return resolvedVersions as NonEmptyArray<CliVersionMeta>; |
| 266 | } |