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 { afterEach, describe, expect, test } from 'bun:test'; |
| 2 | import { Effect } from 'effect'; |
| 3 | import { CANDIDATE_MANIFEST } from '../scripts/sdk-release/artifacts'; |
| 4 | import { |
| 5 | assertCandidateVersions, |
| 6 | installCandidateDocs, |
| 7 | loadCandidatePackages, |
| 8 | serveCandidateRegistry, |
| 9 | } from '../scripts/sdk-release/candidate-registry'; |
| 10 | import { tarballName } from '../scripts/sdk-release/candidates'; |
| 11 | import { CHANNELS } from '../scripts/sdk-release/channel'; |
| 12 | import { run, withoutReleaseTokens } from '../scripts/sdk-release/runtime'; |
| 13 | import type { ReleaseBatch } from '../scripts/sdk-release/source'; |
| 14 | import { batch } from './sdk-release-controller-fixture'; |
| 15 | import { |
| 16 | json, |
| 17 | makeExecutable, |
| 18 | readText, |
| 19 | runRelease, |
| 20 | temporaryDirectories, |
| 21 | writeText, |
| 22 | type Fetch, |
| 23 | } from './sdk-release-harness'; |
| 24 | |
| 25 | const directories = temporaryDirectories('sdk-candidate-registry-'); |
| 26 | afterEach(directories.cleanup); |
| 27 | |
| 28 | const PLANNED = '@composio/release-fixture'; |
| 29 | const CANDIDATE = '1.1.0'; |
| 30 | const UPSTREAM = 'https://registry.npmjs.org'; |
| 31 | |
| 32 | const exec = (cwd: string, executable: string, args: ReadonlyArray<string>) => |
| 33 | runRelease(run(executable, args, { cwd, env: withoutReleaseTokens })); |
| 34 | |
| 35 | /** Packs `package.json` plus an index file into `<root>/<file>` and returns its bytes. */ |
| 36 | const pack = async (root: string, file: string, manifest: Record<string, unknown>) => { |
| 37 | const staging = await directories.create(); |
| 38 | await writeText(`${staging}/package/package.json`, JSON.stringify(manifest)); |
| 39 | await writeText(`${staging}/package/index.js`, `export const version = '${manifest.version}';\n`); |
| 40 | await exec(staging, 'tar', ['-czf', `${root}/${file}`, 'package']); |
| 41 | return new Uint8Array(await Bun.file(`${root}/${file}`).arrayBuffer()); |
| 42 | }; |
| 43 | |
| 44 | const sha256 = (bytes: Uint8Array) => new Bun.CryptoHasher('sha256').update(bytes).digest('hex'); |
| 45 | const sha512 = (bytes: Uint8Array) => |
| 46 | `sha512-${new Bun.CryptoHasher('sha512').update(bytes).digest('base64')}`; |
| 47 | |
| 48 | /** A downloaded TypeScript candidate holding `@composio/release-fixture@1.1.0`. */ |
| 49 | const candidateDirectory = async () => { |
| 50 | const directory = await directories.create(); |
| 51 | const file = tarballName(PLANNED, CANDIDATE); |
| 52 | const bytes = await pack(directory, file, { name: PLANNED, version: CANDIDATE }); |
| 53 | await writeText( |
| 54 | `${directory}/${CANDIDATE_MANIFEST}`, |
| 55 | JSON.stringify({ |
| 56 | sourceSha: 'b'.repeat(40), |
| 57 | producerRun: 41, |
| 58 | language: 'typescript', |
| 59 | files: [{ name: file, sha256: sha256(bytes), size: bytes.length }], |
| 60 | }) |
| 61 | ); |
| 62 | return { directory, file, bytes }; |
| 63 | }; |
| 64 | |
| 65 | const typescriptBatch: ReleaseBatch = { |
| 66 | ...batch, |
| 67 | plan: { |
| 68 | ...batch.plan, |
| 69 | releases: [{ ...batch.plan.releases[0], name: PLANNED, newVersion: CANDIDATE }], |
| 70 | }, |
| 71 | }; |
| 72 | |
| 73 | /** |
| 74 | * A fake npm registry. Packuments name tarballs under `tarballOrigin()`, which a test points at |
| 75 | * the candidate registry, so every download goes through it and nothing reaches the network. |
| 76 | */ |
| 77 | const fakeUpstream = ( |
| 78 | tarballs: Record<string, { manifest: Record<string, unknown>; bytes: Uint8Array<ArrayBuffer> }>, |
| 79 | tarballOrigin: () => string |
| 80 | ) => { |
| 81 | const requests: string[] = []; |
| 82 | const fetcher: Fetch = async input => { |
| 83 | const url = new URL(String(input)); |
| 84 | requests.push(url.pathname); |
| 85 | const name = decodeURIComponent(url.pathname).slice(1); |
| 86 | const versions = Object.values(tarballs).filter(({ manifest }) => manifest.name === name); |
| 87 | if (url.origin === UPSTREAM && versions.length) |
| 88 | return json({ |
| 89 | name, |
| 90 | 'dist-tags': { latest: versions.at(-1)?.manifest.version }, |
| 91 | versions: Object.fromEntries( |
| 92 | versions.map(({ manifest, bytes }) => [ |
| 93 | manifest.version, |
| 94 | { |
| 95 | ...manifest, |
| 96 | dist: { |
| 97 | tarball: `${tarballOrigin()}/${name}/-/${name.split('/').at(-1)}-${manifest.version}.tgz`, |
| 98 | integrity: sha512(bytes), |
| 99 | }, |
| 100 | }, |
| 101 | ]) |
| 102 | ), |
| 103 | }); |
| 104 | const tarball = tarballs[decodeURIComponent(url.pathname)]; |
| 105 | if (url.origin === UPSTREAM && tarball) return new Response(tarball.bytes); |
| 106 | return json({ error: 'Not found' }, 404); |
| 107 | }; |
| 108 | return { fetcher, requests }; |
| 109 | }; |
| 110 | |
| 111 | describe('candidate registry', () => { |
| 112 | test('serves a planned package from its candidate file and proxies every other package', async () => { |
| 113 | const candidate = await candidateDirectory(); |
| 114 | const upstream = fakeUpstream({}, () => UPSTREAM); |
| 115 | const leftPad = { name: 'left-pad', 'dist-tags': { latest: '1.3.0' }, versions: {} }; |
| 116 | const fetcher: Fetch = async (input, init) => |
| 117 | new URL(String(input)).pathname === '/left-pad' |
| 118 | ? json(leftPad) |
| 119 | : upstream.fetcher(input, init); |
| 120 | const responses = await runRelease( |
| 121 | Effect.scoped( |
| 122 | Effect.gen(function* () { |
| 123 | const packages = yield* loadCandidatePackages(candidate.directory, [ |
| 124 | { name: PLANNED, newVersion: CANDIDATE }, |
| 125 | ]); |
| 126 | const origin = yield* serveCandidateRegistry(packages); |
| 127 | return yield* Effect.promise(async () => { |
| 128 | const packument = await (await fetch(`${origin}/@composio%2frelease-fixture`)).json(); |
| 129 | const dist = packument.versions[CANDIDATE].dist; |
| 130 | return { |
| 131 | packument, |
| 132 | tarballUrl: dist.tarball as string, |
| 133 | tarball: new Uint8Array(await (await fetch(dist.tarball)).arrayBuffer()), |
| 134 | proxied: await (await fetch(`${origin}/left-pad`)).json(), |
| 135 | published: ( |
| 136 | await fetch(`${origin}/@composio/release-fixture/-/release-fixture-1.0.0.tgz`) |
| 137 | ).status, |
| 138 | origin, |
| 139 | }; |
| 140 | }); |
| 141 | }) |
| 142 | ), |
| 143 | fetcher |
| 144 | ); |
| 145 | expect(responses.packument['dist-tags']).toEqual({ latest: CANDIDATE }); |
| 146 | expect(Object.keys(responses.packument.versions)).toEqual([CANDIDATE]); |
| 147 | expect(responses.packument.versions[CANDIDATE]).toMatchObject({ |
| 148 | name: PLANNED, |
| 149 | version: CANDIDATE, |
| 150 | dist: { integrity: sha512(candidate.bytes) }, |
| 151 | }); |
| 152 | expect(responses.tarballUrl.startsWith(responses.origin)).toBe(true); |
| 153 | expect(responses.tarball).toEqual(candidate.bytes); |
| 154 | expect(responses.proxied).toEqual(leftPad); |
| 155 | // A planned package never falls through to the public registry, not even an older version. |
| 156 | expect(responses.published).toBe(404); |
| 157 | expect(upstream.requests).toEqual([]); |
| 158 | }); |
| 159 | |
| 160 | test('a candidate whose bytes differ from its manifest is refused before anything is installed', async () => { |
| 161 | const candidate = await candidateDirectory(); |
| 162 | await Bun.write(`${candidate.directory}/${candidate.file}`, 'tampered'); |
| 163 | const docs = await directories.create(); |
| 164 | const manifest = JSON.stringify({ dependencies: { [PLANNED]: '1.0.0' } }); |
| 165 | await writeText(`${docs}/package.json`, manifest); |
| 166 | const bin = await directories.create(); |
| 167 | await writeText(`${bin}/bun`, `#!/bin/sh\necho "$@" >> ${bin}/bun.log\n`); |
| 168 | await makeExecutable(`${bin}/bun`); |
| 169 | const previous = process.env.PATH; |
| 170 | try { |
| 171 | process.env.PATH = `${bin}:${previous}`; |
| 172 | await expect( |
| 173 | runRelease(installCandidateDocs(docs, typescriptBatch, candidate.directory)) |
| 174 | ).rejects.toThrow(`Candidate checksum mismatch: ${candidate.file}`); |
| 175 | } finally { |
| 176 | process.env.PATH = previous; |
| 177 | } |
| 178 | expect(await Bun.file(`${bin}/bun.log`).exists()).toBe(false); |
| 179 | expect(await readText(`${docs}/package.json`)).toBe(manifest); |
| 180 | }); |
| 181 | }); |
| 182 | |
| 183 | describe('docs install against the candidate', () => { |
| 184 | test('every copy of a planned package, nested ones included, resolves to the candidate version', async () => { |
| 185 | const candidate = await candidateDirectory(); |
| 186 | const published = await directories.create(); |
| 187 | const tarball = async (name: string, version: string, extra: Record<string, unknown> = {}) => { |
| 188 | const manifest = { name, version, ...extra }; |
| 189 | const file = `${name.split('/').at(-1)}-${version}.tgz`; |
| 190 | return [ |
| 191 | `/${name}/-/${file}`, |
| 192 | { manifest, bytes: await pack(published, file.replace('/', '-'), manifest) }, |
| 193 | ] as const; |
| 194 | }; |
| 195 | const tarballs = Object.fromEntries([ |
| 196 | await tarball(PLANNED, '1.0.0'), |
| 197 | await tarball(PLANNED, '0.9.0'), |
| 198 | // Pins an older planned version, so the hoisted install nests its own copy. |
| 199 | await tarball('consumer-fixture', '1.0.0', { dependencies: { [PLANNED]: '0.9.0' } }), |
| 200 | ]); |
| 201 | let origin = ''; |
| 202 | const upstream = fakeUpstream(tarballs, () => origin); |
| 203 | const docs = await directories.create(); |
| 204 | await writeText( |
| 205 | `${docs}/package.json`, |
| 206 | JSON.stringify({ |
| 207 | name: 'docs-fixture', |
| 208 | private: true, |
| 209 | dependencies: { [PLANNED]: '1.0.0', 'consumer-fixture': '1.0.0' }, |
| 210 | }) |
| 211 | ); |
| 212 | |
| 213 | // The committed lock pins the published versions, as the docs lock does before release. |
| 214 | const cache = await directories.create(); |
| 215 | await runRelease( |
| 216 | Effect.scoped( |
| 217 | Effect.gen(function* () { |
| 218 | origin = yield* serveCandidateRegistry([]); |
| 219 | yield* run('bun', ['install'], { |
| 220 | cwd: docs, |
| 221 | env: { |
| 222 | ...withoutReleaseTokens, |
| 223 | NPM_CONFIG_REGISTRY: origin, |
| 224 | BUN_INSTALL_CACHE_DIR: cache, |
| 225 | }, |
| 226 | }); |
| 227 | }) |
| 228 | ), |
| 229 | upstream.fetcher |
| 230 | ); |
| 231 | const nested = `${docs}/node_modules/consumer-fixture/node_modules/${PLANNED}/package.json`; |
| 232 | expect((await Bun.file(nested).json()).version).toBe('0.9.0'); |
| 233 | const committedLock = await readText(`${docs}/bun.lock`); |
| 234 | |
| 235 | upstream.requests.length = 0; |
| 236 | await runRelease( |
| 237 | installCandidateDocs(docs, typescriptBatch, candidate.directory), |
| 238 | upstream.fetcher |
| 239 | ); |
| 240 | |
| 241 | expect((await Bun.file(`${docs}/node_modules/${PLANNED}/package.json`).json()).version).toBe( |
| 242 | CANDIDATE |
| 243 | ); |
| 244 | expect(await Bun.file(nested).exists()).toBe(false); |
| 245 | expect(await readText(`${docs}/bun.lock`)).not.toBe(committedLock); |
| 246 | expect(upstream.requests.filter(path => decodeURIComponent(path).includes(PLANNED))).toEqual( |
| 247 | [] |
| 248 | ); |
| 249 | await runRelease( |
| 250 | assertCandidateVersions( |
| 251 | `${docs}/node_modules`, |
| 252 | [{ name: PLANNED, newVersion: CANDIDATE }], |
| 253 | [PLANNED] |
| 254 | ) |
| 255 | ); |
| 256 | }); |
| 257 | |
| 258 | test('the version check names every installed copy that kept another version', async () => { |
| 259 | const nodeModules = `${await directories.create()}/node_modules`; |
| 260 | const at = (directory: string, version: string) => |
| 261 | writeText(`${directory}/package.json`, JSON.stringify({ name: PLANNED, version })); |
| 262 | await at(`${nodeModules}/${PLANNED}`, CANDIDATE); |
| 263 | await at(`${nodeModules}/consumer-fixture/node_modules/${PLANNED}`, '1.0.0'); |
| 264 | await writeText(`${nodeModules}/consumer-fixture/package.json`, '{"version":"1.0.0"}'); |
| 265 | const releases = [{ name: PLANNED, newVersion: CANDIDATE }]; |
| 266 | |
| 267 | await expect( |
| 268 | runRelease(assertCandidateVersions(nodeModules, releases, [PLANNED])) |
| 269 | ).rejects.toThrow(`consumer-fixture/node_modules/${PLANNED} is 1.0.0`); |
| 270 | |
| 271 | await at(`${nodeModules}/consumer-fixture/node_modules/${PLANNED}`, CANDIDATE); |
| 272 | await runRelease(assertCandidateVersions(nodeModules, releases, [PLANNED])); |
| 273 | await expect( |
| 274 | runRelease(assertCandidateVersions(await directories.create(), releases, [PLANNED])) |
| 275 | ).rejects.toThrow(`${PLANNED} is not installed`); |
| 276 | }); |
| 277 | |
| 278 | test('a Python-only stable plan installs the committed docs lock from npm unchanged', async () => { |
| 279 | const docs = await directories.create(); |
| 280 | const manifest = JSON.stringify({ dependencies: { '@composio/core': '0.29.0' } }); |
| 281 | await writeText(`${docs}/package.json`, manifest); |
| 282 | const bin = await directories.create(); |
| 283 | await writeText( |
| 284 | `${bin}/bun`, |
| 285 | `#!/bin/sh\necho "$PWD|$*|registry=$NPM_CONFIG_REGISTRY" >> ${bin}/bun.log\n` |
| 286 | ); |
| 287 | await makeExecutable(`${bin}/bun`); |
| 288 | const python: ReleaseBatch = { |
| 289 | ...batch, |
| 290 | plan: { |
| 291 | ...batch.plan, |
| 292 | choices: { ...batch.plan.choices, typescript: 'skip', python: 'auto' }, |
| 293 | releases: [ |
| 294 | { |
| 295 | name: '@composio/python-sdk', |
| 296 | type: 'minor', |
| 297 | oldVersion: '0.31.0', |
| 298 | newVersion: '0.32.0', |
| 299 | changesets: [], |
| 300 | language: 'python', |
| 301 | }, |
| 302 | ], |
| 303 | }, |
| 304 | }; |
| 305 | const previous = process.env.PATH; |
| 306 | try { |
| 307 | process.env.PATH = `${bin}:${previous}`; |
| 308 | await runRelease(installCandidateDocs(docs, python, `${docs}/no-candidate`)); |
| 309 | } finally { |
| 310 | process.env.PATH = previous; |
| 311 | } |
| 312 | const realDocs = (await exec(docs, 'pwd', ['-P'])).trim(); |
| 313 | expect((await readText(`${bin}/bun.log`)).trim()).toBe( |
| 314 | `${realDocs}|install --frozen-lockfile|registry=` |
| 315 | ); |
| 316 | expect(await readText(`${docs}/package.json`)).toBe(manifest); |
| 317 | }); |
| 318 | }); |
| 319 | |
| 320 | describe('docs workflows', () => { |
| 321 | const workflow = async (name: string) => |
| 322 | Bun.YAML.parse(await readText(`.github/workflows/${name}`)) as { |
| 323 | jobs: Record<string, Record<string, unknown>>; |
| 324 | }; |
| 325 | /** Reads the only condition shape these jobs use: a negated head-branch prefix. */ |
| 326 | const headCondition = (condition: unknown) => { |
| 327 | const match = /^\$\{\{ !startsWith\(github\.head_ref, '([^']+)'\) \}\}$/.exec( |
| 328 | String(condition) |
| 329 | ); |
| 330 | expect(match).not.toBeNull(); |
| 331 | return (head: string) => !head.startsWith(match?.[1] ?? ''); |
| 332 | }; |
| 333 | |
| 334 | test.each([ |
| 335 | ['docs-typescript-check.yml', 'typescript-check'], |
| 336 | ['docs-tests.yml', 'test'], |
| 337 | ])('%s skips release PR heads, which pin versions npm does not serve yet', async (file, job) => { |
| 338 | const runsOn = headCondition((await workflow(file)).jobs[job].if); |
| 339 | expect(runsOn('sdk-release/batch-42589989280')).toBe(false); |
| 340 | expect(runsOn('feat/docs-change')).toBe(true); |
| 341 | expect(runsOn('sdk-release-notes')).toBe(true); |
| 342 | }); |
| 343 | |
| 344 | test('the candidate docs job runs on stable release PRs only, with no credentials', async () => { |
| 345 | const { jobs } = await workflow('sdk.release.candidates.yml'); |
| 346 | const docs = jobs.docs; |
| 347 | expect(docs.name).toBe('SDK candidate docs'); |
| 348 | // The stable gate requires the check this job reports, under the same name. |
| 349 | expect(CHANNELS.stable.extraChecks.join()).toBe(String(docs.name)); |
| 350 | expect(docs.needs).toEqual(['select-candidates', 'candidate']); |
| 351 | expect(docs.if).toBe( |
| 352 | "${{ !cancelled() && needs.select-candidates.outputs.channel == 'stable' }}" |
| 353 | ); |
| 354 | expect(docs.permissions).toEqual({ contents: 'read' }); |
| 355 | expect(docs['timeout-minutes']).toBeGreaterThan(0); |
| 356 | expect(JSON.stringify(docs)).not.toContain('secrets.'); |
| 357 | }); |
| 358 | }); |