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, 03:37 UTC
| 1 | import { expect, test } from 'bun:test'; |
| 2 | import { dirname, join } from 'node:path'; |
| 3 | import * as BunFileSystem from '@effect/platform-bun/BunFileSystem'; |
| 4 | import { Effect, FileSystem, Schema } from 'effect'; |
| 5 | import { |
| 6 | ClientUpdateCommandError, |
| 7 | ClientUpdateIoError, |
| 8 | ClientUpdateRegistryError, |
| 9 | applyArtifact, |
| 10 | catalogVersion, |
| 11 | commonStableVersion, |
| 12 | isStableVersion, |
| 13 | planUpdates, |
| 14 | prepare, |
| 15 | validateEvent, |
| 16 | verifyReleases, |
| 17 | } from '../scripts/update-composio-client'; |
| 18 | |
| 19 | const wheel = { filename: 'composio_client-2.0.1-py3-none-any.whl', yanked: false }; |
| 20 | const sourceSha = 'a'.repeat(40); |
| 21 | const run = <A, E>(effect: Effect.Effect<A, E, FileSystem.FileSystem>) => |
| 22 | Effect.runPromise(effect.pipe(Effect.provide(BunFileSystem.layer))); |
| 23 | const withFs = <A, E>(action: (fs: FileSystem.FileSystem) => Effect.Effect<A, E>) => |
| 24 | Effect.gen(function* () { |
| 25 | return yield* action(yield* FileSystem.FileSystem); |
| 26 | }); |
| 27 | const fixtureIo = <A, E>(action: (fs: FileSystem.FileSystem) => Effect.Effect<A, E>) => |
| 28 | withFs(action).pipe( |
| 29 | Effect.mapError( |
| 30 | cause => |
| 31 | new ClientUpdateIoError({ |
| 32 | operation: 'fixture I/O', |
| 33 | path: 'fixture', |
| 34 | message: 'Fixture I/O failed', |
| 35 | cause, |
| 36 | }) |
| 37 | ) |
| 38 | ); |
| 39 | const tempDirectory = (prefix: string) => run(withFs(fs => fs.makeTempDirectory({ prefix }))); |
| 40 | const removeDirectory = (path: string) => |
| 41 | run(withFs(fs => fs.remove(path, { recursive: true, force: true }))); |
| 42 | const readText = (path: string) => run(withFs(fs => fs.readFileString(path))); |
| 43 | const files = { |
| 44 | 'pnpm-workspace.yaml': |
| 45 | "catalog:\n '@composio/client': 2.0.0\n effect: ^4.1.0\nminimumReleaseAgeExclude:\n - '@composio/*'\n - effect@4.0.3\n", |
| 46 | 'python/pyproject.toml': 'dependencies = [\n "composio-client==2.0.0",\n]\n', |
| 47 | 'python/setup.py': 'install_requires=["composio-client==2.0.0"],\n', |
| 48 | 'ts/packages/cli/CHANGELOG.md': |
| 49 | '# @composio/cli\n\n## Unreleased\n\n### Patch Changes\n\n- Existing note.\n', |
| 50 | }; |
| 51 | |
| 52 | test('only canonical stable versions cross the registry/event boundary', () => { |
| 53 | for (const version of [ |
| 54 | 'v2.0.1', |
| 55 | '2.0.1-rc.1', |
| 56 | '2.0.1+meta', |
| 57 | '02.0.1', |
| 58 | '2.0.1\n', |
| 59 | ' 2.0.1', |
| 60 | '2.0.1 ', |
| 61 | '9007199254740993.0.0', |
| 62 | '$(id)', |
| 63 | '2.0', |
| 64 | ]) { |
| 65 | expect(isStableVersion(version)).toBe(false); |
| 66 | } |
| 67 | const payload = { |
| 68 | version: '2.0.1', |
| 69 | source_sha: sourceSha, |
| 70 | source_repository: 'ComposioHQ/composio-client', |
| 71 | }; |
| 72 | expect(validateEvent('repository_dispatch', { client_payload: payload })?.version).toBe('2.0.1'); |
| 73 | expect(() => |
| 74 | validateEvent('repository_dispatch', { client_payload: { ...payload, ref: 'attacker' } }) |
| 75 | ).toThrow(); |
| 76 | expect(() => |
| 77 | validateEvent('repository_dispatch', { |
| 78 | client_payload: { ...payload, source_repository: 'other/repo' }, |
| 79 | }) |
| 80 | ).toThrow(); |
| 81 | expect(() => |
| 82 | validateEvent('repository_dispatch', { client_payload: { ...payload, source_sha: 'next' } }) |
| 83 | ).toThrow(); |
| 84 | expect(validateEvent('schedule', {})).toBeUndefined(); |
| 85 | }); |
| 86 | |
| 87 | test('reconciliation selects the latest common stable non-yanked release', () => { |
| 88 | expect( |
| 89 | commonStableVersion( |
| 90 | { versions: { '2.0.0': {}, '2.0.1': {}, '2.1.0': {}, '3.0.0-rc.1': {}, '2.0.2': {} } }, |
| 91 | { |
| 92 | releases: { |
| 93 | '2.0.0': [wheel], |
| 94 | '2.0.1': [wheel], |
| 95 | '2.0.2': [{ ...wheel, yanked: true }], |
| 96 | '3.0.0-rc.1': [wheel], |
| 97 | }, |
| 98 | } |
| 99 | ) |
| 100 | ).toBe('2.0.1'); |
| 101 | expect(() => commonStableVersion({ versions: { '2.0.1': {} } }, { releases: {} })).toThrow( |
| 102 | 'no common stable' |
| 103 | ); |
| 104 | expect(() => commonStableVersion({ error: 'rate limited' }, { releases: {} })).toThrow(); |
| 105 | }); |
| 106 | |
| 107 | test('exact registry metadata must confirm usable distributions from both packages', () => { |
| 108 | const npm = { |
| 109 | name: '@composio/client', |
| 110 | version: '2.0.1', |
| 111 | dist: { |
| 112 | integrity: 'sha512-value', |
| 113 | tarball: 'https://registry.npmjs.org/@composio/client/-/client-2.0.1.tgz', |
| 114 | }, |
| 115 | }; |
| 116 | const pypi = { info: { name: 'composio-client', version: '2.0.1' }, urls: [wheel] }; |
| 117 | verifyReleases('2.0.1', npm, pypi); |
| 118 | expect(() => verifyReleases('2.0.2', npm, pypi)).toThrow('Both registries'); |
| 119 | expect(() => |
| 120 | verifyReleases('2.0.1', npm, { ...pypi, urls: [{ ...wheel, yanked: true }] }) |
| 121 | ).toThrow(); |
| 122 | expect(() => verifyReleases('2.0.1', { ...npm, dist: {} }, pypi)).toThrow(); |
| 123 | }); |
| 124 | |
| 125 | test('planning updates owned pins, SDK changeset and CLI release note once', () => { |
| 126 | const plan = planUpdates(files, '2.0.1'); |
| 127 | expect(plan.typescript).toBe(true); |
| 128 | expect(plan.python).toBe(true); |
| 129 | expect(catalogVersion(plan.changed['pnpm-workspace.yaml']!)).toBe('2.0.1'); |
| 130 | expect(plan.changed['pnpm-workspace.yaml']).toBe( |
| 131 | files['pnpm-workspace.yaml'].replace("'@composio/client': 2.0.0", "'@composio/client': 2.0.1") |
| 132 | ); |
| 133 | expect(plan.changed['python/setup.py']).toContain('composio-client==2.0.1'); |
| 134 | expect(plan.changed['python/pyproject.toml']).toContain('composio-client==2.0.1'); |
| 135 | expect(plan.changed['.changeset/composio-client-update.md']).toContain("'@composio/core': patch"); |
| 136 | expect(plan.changed['.changeset/composio-client-update.md']).toContain( |
| 137 | "'@composio/python-sdk': patch" |
| 138 | ); |
| 139 | expect(plan.changed['.changeset/composio-client-update.md']).not.toContain("'@composio/cli':"); |
| 140 | expect(plan.changed['ts/packages/cli/CHANGELOG.md']).toContain('- Existing note.'); |
| 141 | expect(planUpdates({ ...files, ...plan.changed }, '2.0.1').changed).toEqual({}); |
| 142 | expect(planUpdates(files, '2.0.1').changed).toEqual(plan.changed); |
| 143 | }); |
| 144 | |
| 145 | test('the workspace exempts every first-party package from the release-age wait', async () => { |
| 146 | const workspace = Schema.decodeUnknownSync( |
| 147 | Schema.Struct({ |
| 148 | minimumReleaseAge: Schema.Number, |
| 149 | minimumReleaseAgeExclude: Schema.Array(Schema.String), |
| 150 | }) |
| 151 | )(Bun.YAML.parse(await readText(new URL('../pnpm-workspace.yaml', import.meta.url).pathname))); |
| 152 | expect(workspace.minimumReleaseAgeExclude).toContain('@composio/*'); |
| 153 | }); |
| 154 | |
| 155 | test('CLI notes create an Unreleased patch section without changing released history', () => { |
| 156 | const history = '\n## 0.3.0\n\n### Patch Changes\n\n- A published fix.\n'; |
| 157 | for (const unreleased of ['\n', '\n### Minor Changes\n\n- An upcoming feature.\n']) { |
| 158 | const header = '# @composio/cli\n\n## Unreleased\n'; |
| 159 | const updated = planUpdates( |
| 160 | { |
| 161 | ...files, |
| 162 | 'ts/packages/cli/CHANGELOG.md': `${header}${unreleased}${history}`, |
| 163 | }, |
| 164 | '2.0.1' |
| 165 | ).changed['ts/packages/cli/CHANGELOG.md']!; |
| 166 | const boundary = updated.indexOf('\n## 0.3.0'); |
| 167 | expect(updated.slice(boundary)).toBe(history); |
| 168 | expect(updated.slice(0, boundary)).toContain( |
| 169 | '### Patch Changes\n\n- Update @composio/client to 2.0.1' |
| 170 | ); |
| 171 | expect(updated.slice(0, boundary)).toContain(unreleased); |
| 172 | } |
| 173 | }); |
| 174 | |
| 175 | test('never lowers current or pending versions, and rejects inconsistent Python metadata', () => { |
| 176 | expect(() => planUpdates(files, '1.9.9')).toThrow('downgrade'); |
| 177 | expect(() => planUpdates(files, '2.0.1', '2.0.2')).toThrow('downgrade'); |
| 178 | expect(() => |
| 179 | planUpdates({ ...files, 'python/setup.py': '"composio-client==2.0.2"' }, '2.0.3') |
| 180 | ).toThrow('disagree'); |
| 181 | expect(() => |
| 182 | planUpdates( |
| 183 | { ...files, 'pnpm-workspace.yaml': files['pnpm-workspace.yaml'].repeat(2) }, |
| 184 | '2.0.1' |
| 185 | ) |
| 186 | ).toThrow('exactly one'); |
| 187 | }); |
| 188 | |
| 189 | test('Python-only catchup does not create a TypeScript release', () => { |
| 190 | const plan = planUpdates( |
| 191 | { ...files, 'pnpm-workspace.yaml': files['pnpm-workspace.yaml'].replace('2.0.0', '2.0.1') }, |
| 192 | '2.0.1' |
| 193 | ); |
| 194 | expect(plan.typescript).toBe(false); |
| 195 | expect(plan.python).toBe(true); |
| 196 | expect(Object.keys(plan.changed).sort()).toEqual([ |
| 197 | '.changeset/composio-client-update.md', |
| 198 | 'python/pyproject.toml', |
| 199 | 'python/setup.py', |
| 200 | ]); |
| 201 | expect(plan.changed['.changeset/composio-client-update.md']).toContain( |
| 202 | "'@composio/python-sdk': patch" |
| 203 | ); |
| 204 | expect(plan.changed['.changeset/composio-client-update.md']).not.toContain("'@composio/core':"); |
| 205 | }); |
| 206 | |
| 207 | test('artifact transfer rejects unexpected paths, duplicates and advancing next before writes', async () => { |
| 208 | const root = await tempDirectory('client-update-'); |
| 209 | const artifact = { |
| 210 | version: '2.0.1', |
| 211 | base_sha: sourceSha, |
| 212 | files: [{ path: 'python/setup.py', content: 'updated' }], |
| 213 | }; |
| 214 | try { |
| 215 | await expect(run(applyArtifact(root, artifact, 'b'.repeat(40)))).rejects.toThrow( |
| 216 | 'next advanced' |
| 217 | ); |
| 218 | await expect( |
| 219 | run( |
| 220 | applyArtifact( |
| 221 | root, |
| 222 | { ...artifact, files: [{ path: '../escape', content: 'bad' }] }, |
| 223 | sourceSha |
| 224 | ) |
| 225 | ) |
| 226 | ).rejects.toThrow(); |
| 227 | await expect( |
| 228 | run( |
| 229 | applyArtifact( |
| 230 | root, |
| 231 | { ...artifact, files: [...artifact.files, ...artifact.files] }, |
| 232 | sourceSha |
| 233 | ) |
| 234 | ) |
| 235 | ).rejects.toThrow('Duplicate'); |
| 236 | expect(await run(applyArtifact(root, artifact, sourceSha))).toBe('2.0.1'); |
| 237 | expect(await readText(join(root, 'python/setup.py'))).toBe('updated'); |
| 238 | } finally { |
| 239 | await removeDirectory(root); |
| 240 | } |
| 241 | }); |
| 242 | |
| 243 | test('offline preparation runs scoped lock commands and publishes no artifact on failure', async () => { |
| 244 | for (const failLock of [false, true]) { |
| 245 | const root = await tempDirectory('client-update-prepare-'); |
| 246 | const commands: string[][] = []; |
| 247 | try { |
| 248 | for (const [path, content] of Object.entries(files)) { |
| 249 | await run( |
| 250 | withFs(fs => |
| 251 | Effect.andThen(fs.makeDirectory(dirname(join(root, path)), { recursive: true }), () => |
| 252 | fs.writeFileString(join(root, path), content) |
| 253 | ) |
| 254 | ) |
| 255 | ); |
| 256 | } |
| 257 | for (const args of [ |
| 258 | ['init', '--quiet'], |
| 259 | ['add', '.'], |
| 260 | [ |
| 261 | '-c', |
| 262 | 'user.name=Fixture', |
| 263 | '-c', |
| 264 | 'user.email=fixture@example.com', |
| 265 | 'commit', |
| 266 | '--quiet', |
| 267 | '-m', |
| 268 | 'fixture', |
| 269 | ], |
| 270 | ]) { |
| 271 | expect(Bun.spawnSync(['git', ...args], { cwd: root }).exitCode).toBe(0); |
| 272 | } |
| 273 | const registry = (url: string) => |
| 274 | Effect.sync(() => { |
| 275 | if (url.endsWith('/2.0.1')) |
| 276 | return { |
| 277 | name: '@composio/client', |
| 278 | version: '2.0.1', |
| 279 | dist: { integrity: 'sha512-value', tarball: 'https://registry.npmjs.org/client.tgz' }, |
| 280 | }; |
| 281 | if (url.endsWith('/2.0.1/json')) |
| 282 | return { info: { name: 'composio-client', version: '2.0.1' }, urls: [wheel] }; |
| 283 | if (new URL(url).hostname === 'pypi.org') return { releases: { '2.0.1': [wheel] } }; |
| 284 | return { versions: { '2.0.1': {} } }; |
| 285 | }); |
| 286 | const result = run( |
| 287 | prepare(root, { |
| 288 | eventName: 'schedule', |
| 289 | event: {}, |
| 290 | registry, |
| 291 | runCommand: args => |
| 292 | Effect.gen(function* () { |
| 293 | yield* Effect.sync(() => commands.push(args)); |
| 294 | if (failLock) |
| 295 | return yield* Effect.fail( |
| 296 | new ClientUpdateCommandError({ command: args, message: 'lock resolution failed' }) |
| 297 | ); |
| 298 | yield* fixtureIo(fs => |
| 299 | fs.writeFileString( |
| 300 | join(root, args[0] === 'pnpm' ? 'pnpm-lock.yaml' : 'uv.lock'), |
| 301 | 'fixture lock' |
| 302 | ) |
| 303 | ); |
| 304 | }), |
| 305 | setOutput: () => Effect.succeed(undefined), |
| 306 | }) |
| 307 | ); |
| 308 | const artifactPath = join(root, '.artifacts/client-update/update.json'); |
| 309 | if (failLock) { |
| 310 | await expect(result).rejects.toThrow('lock resolution failed'); |
| 311 | await expect(result).rejects.toBeInstanceOf(ClientUpdateCommandError); |
| 312 | await expect(readText(artifactPath)).rejects.toThrow(); |
| 313 | } else { |
| 314 | await result; |
| 315 | expect(commands).toEqual([ |
| 316 | ['pnpm', 'install', '--lockfile-only', '--ignore-scripts', '--no-frozen-lockfile'], |
| 317 | [ |
| 318 | 'uv', |
| 319 | 'lock', |
| 320 | '--python', |
| 321 | Bun.which('python') ?? '', |
| 322 | '--upgrade-package', |
| 323 | 'composio-client', |
| 324 | ], |
| 325 | ]); |
| 326 | expect(JSON.parse(await readText(artifactPath)).version).toBe('2.0.1'); |
| 327 | } |
| 328 | expect(catalogVersion(await readText(join(root, 'pnpm-workspace.yaml')))).toBe('2.0.1'); |
| 329 | } finally { |
| 330 | await removeDirectory(root); |
| 331 | } |
| 332 | } |
| 333 | }); |
| 334 | |
| 335 | test('registry outages and dispatch/CDN races fail before mutation', async () => { |
| 336 | const root = await tempDirectory('client-update-registry-'); |
| 337 | try { |
| 338 | await expect( |
| 339 | run( |
| 340 | prepare(root, { |
| 341 | eventName: 'schedule', |
| 342 | event: {}, |
| 343 | registry: () => |
| 344 | Effect.fail( |
| 345 | new ClientUpdateRegistryError({ |
| 346 | url: 'https://pypi.org', |
| 347 | message: 'Registry request failed (503)', |
| 348 | }) |
| 349 | ), |
| 350 | }) |
| 351 | ) |
| 352 | ).rejects.toThrow('Registry request failed'); |
| 353 | await expect( |
| 354 | run( |
| 355 | prepare(root, { |
| 356 | eventName: 'repository_dispatch', |
| 357 | event: { |
| 358 | client_payload: { |
| 359 | version: '2.0.2', |
| 360 | source_sha: sourceSha, |
| 361 | source_repository: 'ComposioHQ/composio-client', |
| 362 | }, |
| 363 | }, |
| 364 | registry: url => |
| 365 | Effect.succeed( |
| 366 | new URL(url).hostname === 'pypi.org' |
| 367 | ? { releases: { '2.0.1': [wheel] } } |
| 368 | : { versions: { '2.0.1': {} } } |
| 369 | ), |
| 370 | }) |
| 371 | ) |
| 372 | ).rejects.toThrow('not yet available on both registries'); |
| 373 | await expect(readText(join(root, '.artifacts/client-update/update.json'))).rejects.toThrow(); |
| 374 | } finally { |
| 375 | await removeDirectory(root); |
| 376 | } |
| 377 | }); |
| 378 | |
| 379 | test('workflow keeps dispatched data out of code and scopes App credentials to publishing', async () => { |
| 380 | const workflow = Bun.YAML.parse( |
| 381 | await readText(new URL('../.github/workflows/client.update.yml', import.meta.url).pathname) |
| 382 | ); |
| 383 | const steps = Schema.Array(Schema.Record(Schema.String, Schema.Unknown)); |
| 384 | const parsed = Schema.decodeUnknownSync( |
| 385 | Schema.Struct({ |
| 386 | on: Schema.Struct({ |
| 387 | repository_dispatch: Schema.Struct({ types: Schema.Array(Schema.String) }), |
| 388 | schedule: Schema.Array(Schema.Struct({ cron: Schema.String })), |
| 389 | }), |
| 390 | concurrency: Schema.Struct({ 'cancel-in-progress': Schema.Literal(false) }), |
| 391 | jobs: Schema.Struct({ |
| 392 | prepare: Schema.Struct({ if: Schema.String, steps }), |
| 393 | publish: Schema.Struct({ steps }), |
| 394 | }), |
| 395 | }) |
| 396 | )(workflow); |
| 397 | expect(parsed.on.repository_dispatch.types).toContain('composio-client-published'); |
| 398 | expect(parsed.jobs.prepare.if).toContain("github.actor == 'sdkrelease[bot]'"); |
| 399 | expect(JSON.stringify(parsed.jobs.prepare)).not.toContain('RELEASE_BOT_APP_PRIVATE_KEY'); |
| 400 | const text = JSON.stringify(parsed.jobs.publish.steps); |
| 401 | expect(text).toContain('permission-pull-requests'); |
| 402 | expect(text).not.toContain('auto-merge'); |
| 403 | expect(text.indexOf('update-composio-client.ts apply')).toBeLessThan( |
| 404 | text.indexOf('RELEASE_BOT_APP_PRIVATE_KEY') |
| 405 | ); |
| 406 | }); |
| 407 | |
| 408 | test('CLI reports a failed command once without dumping structured causes', () => { |
| 409 | const result = Bun.spawnSync( |
| 410 | [ |
| 411 | process.execPath, |
| 412 | new URL('../scripts/update-composio-client.ts', import.meta.url).pathname, |
| 413 | 'invalid-command', |
| 414 | ], |
| 415 | { stdout: 'pipe', stderr: 'pipe' } |
| 416 | ); |
| 417 | expect(result.exitCode).toBe(1); |
| 418 | expect(result.stderr.toString().trim()).toBe( |
| 419 | 'Usage: bun scripts/update-composio-client.ts prepare|apply' |
| 420 | ); |
| 421 | }); |