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 * as fs from 'node:fs'; |
| 2 | import * as os from 'node:os'; |
| 3 | import * as path from 'node:path'; |
| 4 | import * as BunServices from '@effect/platform-bun/BunServices'; |
| 5 | import * as BunPath from '@effect/platform-bun/BunPath'; |
| 6 | import { afterEach, describe, expect, it, layer } from '@effect/vitest'; |
| 7 | import { ConfigProvider, Effect } from 'effect'; |
| 8 | import { vi } from 'vitest'; |
| 9 | import { |
| 10 | listMissingInstalledRunCompanionModules, |
| 11 | loadInstalledCompanionModule, |
| 12 | repairMissingInstalledRunCompanionModules, |
| 13 | RUN_COMPANION_LEGACY_PLACEHOLDER_RELATIVE_PATHS, |
| 14 | RUN_COMPANION_MODULE_BASENAMES, |
| 15 | RUN_COMPANION_MODULE_FILENAMES, |
| 16 | RELEASE_MANIFEST_FILENAME, |
| 17 | RUN_COMPANION_RELEASE_TAG_FILENAME, |
| 18 | resolveRunCompanionModulePath, |
| 19 | } from 'src/services/run-companion-modules'; |
| 20 | |
| 21 | const extractZipMock = vi.hoisted(() => vi.fn()); |
| 22 | vi.mock('extract-zip', () => ({ default: extractZipMock })); |
| 23 | |
| 24 | const TEST_RELEASE_TAG = '@composio/cli@0.3.0-test'; |
| 25 | const TEST_BINARY_ASSET_NAMES = [ |
| 26 | 'composio-darwin-aarch64.zip', |
| 27 | 'composio-darwin-x64.zip', |
| 28 | 'composio-linux-aarch64.zip', |
| 29 | 'composio-linux-x64.zip', |
| 30 | ]; |
| 31 | // A release archive ships the live companions plus the legacy placeholders. |
| 32 | const TEST_ARCHIVE_RELATIVE_PATHS = [ |
| 33 | ...RUN_COMPANION_MODULE_FILENAMES, |
| 34 | ...RUN_COMPANION_LEGACY_PLACEHOLDER_RELATIVE_PATHS, |
| 35 | ]; |
| 36 | |
| 37 | // What a complete install holds next to the executable. |
| 38 | const writeLiveCompanions = (installDirectory: string) => { |
| 39 | fs.mkdirSync(path.join(installDirectory, 'services')); |
| 40 | RUN_COMPANION_MODULE_FILENAMES.forEach(fileName => { |
| 41 | fs.writeFileSync( |
| 42 | path.join(installDirectory, fileName), |
| 43 | `export * from "./services/${fileName}";\n` |
| 44 | ); |
| 45 | fs.writeFileSync(path.join(installDirectory, 'services', fileName), 'export {};\n'); |
| 46 | }); |
| 47 | }; |
| 48 | |
| 49 | // What an install upgraded from an older release still holds: every legacy |
| 50 | // placeholder, empty. |
| 51 | const writeLegacyPlaceholders = (installDirectory: string) => { |
| 52 | RUN_COMPANION_LEGACY_PLACEHOLDER_RELATIVE_PATHS.forEach(relativePath => { |
| 53 | const filePath = path.join(installDirectory, relativePath); |
| 54 | fs.mkdirSync(path.dirname(filePath), { recursive: true }); |
| 55 | fs.writeFileSync(filePath, ''); |
| 56 | }); |
| 57 | }; |
| 58 | |
| 59 | const stubRepairFetch = () => { |
| 60 | const fetchMock = vi.fn((url: string) => |
| 61 | Promise.resolve( |
| 62 | url.includes('/releases/tags/') |
| 63 | ? new Response( |
| 64 | JSON.stringify({ |
| 65 | tag_name: TEST_RELEASE_TAG, |
| 66 | assets: TEST_BINARY_ASSET_NAMES.map(name => ({ |
| 67 | name, |
| 68 | browser_download_url: `https://download.test/${name}`, |
| 69 | })), |
| 70 | }), |
| 71 | { status: 200, headers: { 'content-type': 'application/json' } } |
| 72 | ) |
| 73 | : new Response(new Uint8Array([1]), { status: 200 }) |
| 74 | ) |
| 75 | ); |
| 76 | vi.stubGlobal('fetch', fetchMock); |
| 77 | vi.stubEnv('GITHUB_TAG', TEST_RELEASE_TAG); |
| 78 | return fetchMock; |
| 79 | }; |
| 80 | |
| 81 | const mockArchiveContents = (missingRelativePath?: string) => { |
| 82 | extractZipMock.mockImplementation( |
| 83 | async (archivePath: string, options: { readonly dir: string }) => { |
| 84 | const packageDirectory = path.join(options.dir, path.parse(archivePath).name); |
| 85 | TEST_ARCHIVE_RELATIVE_PATHS.filter( |
| 86 | relativePath => relativePath !== missingRelativePath |
| 87 | ).forEach(relativePath => { |
| 88 | const filePath = path.join(packageDirectory, relativePath); |
| 89 | fs.mkdirSync(path.dirname(filePath), { recursive: true }); |
| 90 | fs.writeFileSync( |
| 91 | filePath, |
| 92 | RUN_COMPANION_LEGACY_PLACEHOLDER_RELATIVE_PATHS.includes(relativePath) |
| 93 | ? '' |
| 94 | : `new:${relativePath}` |
| 95 | ); |
| 96 | }); |
| 97 | } |
| 98 | ); |
| 99 | }; |
| 100 | |
| 101 | const mockManifestArchive = (relativePaths: ReadonlyArray<string>, manifest: unknown) => { |
| 102 | extractZipMock.mockImplementation( |
| 103 | async (archivePath: string, options: { readonly dir: string }) => { |
| 104 | const packageDirectory = path.join(options.dir, path.parse(archivePath).name); |
| 105 | relativePaths.forEach(relativePath => { |
| 106 | const filePath = path.join(packageDirectory, relativePath); |
| 107 | fs.mkdirSync(path.dirname(filePath), { recursive: true }); |
| 108 | fs.writeFileSync(filePath, `new:${relativePath}`); |
| 109 | }); |
| 110 | fs.writeFileSync( |
| 111 | path.join(packageDirectory, RELEASE_MANIFEST_FILENAME), |
| 112 | JSON.stringify(manifest) |
| 113 | ); |
| 114 | } |
| 115 | ); |
| 116 | }; |
| 117 | |
| 118 | const LIVE_COMPANIONS_WITH_BUNDLES = [ |
| 119 | 'generation-runtime.mjs', |
| 120 | 'run-helpers-runtime.mjs', |
| 121 | 'services/generation-runtime.mjs', |
| 122 | 'services/run-helpers-runtime.mjs', |
| 123 | ]; |
| 124 | |
| 125 | const listFilesRecursively = (directory: string): ReadonlyArray<string> => |
| 126 | fs |
| 127 | .readdirSync(directory, { recursive: true, withFileTypes: true }) |
| 128 | .filter(entry => entry.isFile()) |
| 129 | .map(entry => |
| 130 | path.relative(directory, path.join(entry.parentPath, entry.name)).replaceAll(path.sep, '/') |
| 131 | ) |
| 132 | .sort(); |
| 133 | |
| 134 | describe('run-companion-modules', () => { |
| 135 | afterEach(() => { |
| 136 | extractZipMock.mockReset(); |
| 137 | vi.unstubAllGlobals(); |
| 138 | vi.unstubAllEnvs(); |
| 139 | }); |
| 140 | |
| 141 | it('pins the legacy placeholder list to the ten paths released clients require', () => { |
| 142 | expect(RUN_COMPANION_LEGACY_PLACEHOLDER_RELATIVE_PATHS).toEqual([ |
| 143 | 'run-subagent-shared.mjs', |
| 144 | 'run-subagent-acp.mjs', |
| 145 | 'run-subagent-legacy.mjs', |
| 146 | 'run-subagent-output-mcp.mjs', |
| 147 | 'acp-adapters/claude-code-acp.mjs', |
| 148 | 'acp-adapters/cli.js', |
| 149 | 'acp-adapters/codex/darwin-arm64/codex-acp', |
| 150 | 'acp-adapters/codex/darwin-x64/codex-acp', |
| 151 | 'acp-adapters/codex/linux-arm64/codex-acp', |
| 152 | 'acp-adapters/codex/linux-x64/codex-acp', |
| 153 | ]); |
| 154 | }); |
| 155 | |
| 156 | it('keeps the legacy placeholders out of the live companion list', () => { |
| 157 | expect( |
| 158 | RUN_COMPANION_MODULE_FILENAMES.filter(fileName => |
| 159 | RUN_COMPANION_LEGACY_PLACEHOLDER_RELATIVE_PATHS.includes(fileName) |
| 160 | ) |
| 161 | ).toEqual([]); |
| 162 | }); |
| 163 | |
| 164 | layer(BunServices.layer)(it => { |
| 165 | it.effect( |
| 166 | '[Given] an install with only the live companions and their bundles [Then] nothing needs repair', |
| 167 | () => |
| 168 | Effect.gen(function* () { |
| 169 | const installDirectory = fs.mkdtempSync(path.join(os.tmpdir(), 'composio-run-live-')); |
| 170 | const execPath = path.join(installDirectory, 'composio'); |
| 171 | writeLiveCompanions(installDirectory); |
| 172 | const fetchMock = stubRepairFetch(); |
| 173 | |
| 174 | return yield* Effect.gen(function* () { |
| 175 | expect(yield* listMissingInstalledRunCompanionModules(execPath)).toEqual([]); |
| 176 | expect( |
| 177 | yield* repairMissingInstalledRunCompanionModules({ |
| 178 | callerImportMetaUrl: 'file:///$bunfs/root/commands.mjs', |
| 179 | execPath, |
| 180 | appVersion: '0.0.0-test', |
| 181 | }) |
| 182 | ).toEqual({ repaired: false }); |
| 183 | expect(fetchMock).not.toHaveBeenCalled(); |
| 184 | }).pipe( |
| 185 | Effect.ensuring( |
| 186 | Effect.sync(() => fs.rmSync(installDirectory, { recursive: true, force: true })) |
| 187 | ) |
| 188 | ); |
| 189 | }) |
| 190 | ); |
| 191 | |
| 192 | it.effect( |
| 193 | '[Given] an install holding empty legacy placeholders [Then] it is not damaged and nothing is repaired', |
| 194 | () => |
| 195 | Effect.gen(function* () { |
| 196 | const installDirectory = fs.mkdtempSync(path.join(os.tmpdir(), 'composio-run-legacy-')); |
| 197 | const execPath = path.join(installDirectory, 'composio'); |
| 198 | writeLiveCompanions(installDirectory); |
| 199 | writeLegacyPlaceholders(installDirectory); |
| 200 | const fetchMock = stubRepairFetch(); |
| 201 | |
| 202 | return yield* Effect.gen(function* () { |
| 203 | expect(yield* listMissingInstalledRunCompanionModules(execPath)).toEqual([]); |
| 204 | expect( |
| 205 | yield* repairMissingInstalledRunCompanionModules({ |
| 206 | callerImportMetaUrl: 'file:///$bunfs/root/commands.mjs', |
| 207 | execPath, |
| 208 | appVersion: '0.0.0-test', |
| 209 | }) |
| 210 | ).toEqual({ repaired: false }); |
| 211 | expect(fetchMock).not.toHaveBeenCalled(); |
| 212 | }).pipe( |
| 213 | Effect.ensuring( |
| 214 | Effect.sync(() => fs.rmSync(installDirectory, { recursive: true, force: true })) |
| 215 | ) |
| 216 | ); |
| 217 | }) |
| 218 | ); |
| 219 | |
| 220 | it.effect('[Given] a missing run-helpers-runtime.mjs [Then] it is reported as missing', () => |
| 221 | Effect.gen(function* () { |
| 222 | const installDirectory = fs.mkdtempSync(path.join(os.tmpdir(), 'composio-run-missing-')); |
| 223 | const execPath = path.join(installDirectory, 'composio'); |
| 224 | writeLiveCompanions(installDirectory); |
| 225 | fs.rmSync(path.join(installDirectory, 'run-helpers-runtime.mjs')); |
| 226 | |
| 227 | return yield* Effect.gen(function* () { |
| 228 | expect(yield* listMissingInstalledRunCompanionModules(execPath)).toEqual([ |
| 229 | 'run-helpers-runtime.mjs', |
| 230 | ]); |
| 231 | }).pipe( |
| 232 | Effect.ensuring( |
| 233 | Effect.sync(() => fs.rmSync(installDirectory, { recursive: true, force: true })) |
| 234 | ) |
| 235 | ); |
| 236 | }) |
| 237 | ); |
| 238 | |
| 239 | it.effect( |
| 240 | '[Given] a missing companion wrapper [Then] repair restores the companions and leaves the archive placeholders behind', |
| 241 | () => { |
| 242 | const installDirectory = fs.mkdtempSync( |
| 243 | path.join(os.tmpdir(), 'composio-run-repair-scope-') |
| 244 | ); |
| 245 | const execPath = path.join(installDirectory, 'composio'); |
| 246 | stubRepairFetch(); |
| 247 | mockArchiveContents(); |
| 248 | |
| 249 | return Effect.gen(function* () { |
| 250 | const result = yield* repairMissingInstalledRunCompanionModules({ |
| 251 | callerImportMetaUrl: 'file:///$bunfs/root/commands.mjs', |
| 252 | execPath, |
| 253 | appVersion: '0.0.0-test', |
| 254 | }); |
| 255 | |
| 256 | expect(result).toEqual({ repaired: true, releaseTag: TEST_RELEASE_TAG }); |
| 257 | expect(fs.readdirSync(installDirectory).sort()).toEqual( |
| 258 | [...RUN_COMPANION_MODULE_FILENAMES, RUN_COMPANION_RELEASE_TAG_FILENAME].sort() |
| 259 | ); |
| 260 | }).pipe( |
| 261 | Effect.ensuring( |
| 262 | Effect.sync(() => fs.rmSync(installDirectory, { recursive: true, force: true })) |
| 263 | ) |
| 264 | ); |
| 265 | } |
| 266 | ); |
| 267 | |
| 268 | it.effect( |
| 269 | '[Given] only an unrelated companion is missing [Then] a scoped repair does nothing', |
| 270 | () => { |
| 271 | const installDirectory = fs.mkdtempSync( |
| 272 | path.join(os.tmpdir(), 'composio-run-scoped-repair-') |
| 273 | ); |
| 274 | const execPath = path.join(installDirectory, 'composio'); |
| 275 | fs.mkdirSync(path.join(installDirectory, 'services')); |
| 276 | fs.writeFileSync( |
| 277 | path.join(installDirectory, 'generation-runtime.mjs'), |
| 278 | 'export * from "./services/generation-runtime.mjs";\n' |
| 279 | ); |
| 280 | fs.writeFileSync(path.join(installDirectory, 'services', 'generation-runtime.mjs'), ''); |
| 281 | const fetchMock = stubRepairFetch(); |
| 282 | |
| 283 | return Effect.gen(function* () { |
| 284 | expect(yield* listMissingInstalledRunCompanionModules(execPath)).not.toEqual([]); |
| 285 | expect( |
| 286 | yield* repairMissingInstalledRunCompanionModules({ |
| 287 | callerImportMetaUrl: 'file:///$bunfs/root/commands.mjs', |
| 288 | execPath, |
| 289 | appVersion: '0.0.0-test', |
| 290 | companionBaseName: 'generation-runtime', |
| 291 | }) |
| 292 | ).toEqual({ repaired: false }); |
| 293 | expect(fetchMock).not.toHaveBeenCalled(); |
| 294 | }).pipe( |
| 295 | Effect.ensuring( |
| 296 | Effect.sync(() => fs.rmSync(installDirectory, { recursive: true, force: true })) |
| 297 | ) |
| 298 | ); |
| 299 | } |
| 300 | ); |
| 301 | |
| 302 | it.effect( |
| 303 | '[Given] a companion wrapper whose bundle is missing [Then] a scoped repair restores it', |
| 304 | () => { |
| 305 | const installDirectory = fs.mkdtempSync( |
| 306 | path.join(os.tmpdir(), 'composio-run-scoped-repair-') |
| 307 | ); |
| 308 | const execPath = path.join(installDirectory, 'composio'); |
| 309 | fs.writeFileSync( |
| 310 | path.join(installDirectory, 'generation-runtime.mjs'), |
| 311 | 'export * from "./services/generation-runtime.mjs";\n' |
| 312 | ); |
| 313 | stubRepairFetch(); |
| 314 | mockArchiveContents(); |
| 315 | |
| 316 | return Effect.gen(function* () { |
| 317 | const result = yield* repairMissingInstalledRunCompanionModules({ |
| 318 | callerImportMetaUrl: 'file:///$bunfs/root/commands.mjs', |
| 319 | execPath, |
| 320 | appVersion: '0.0.0-test', |
| 321 | companionBaseName: 'generation-runtime', |
| 322 | }); |
| 323 | |
| 324 | expect(result).toEqual({ repaired: true, releaseTag: TEST_RELEASE_TAG }); |
| 325 | expect(fs.existsSync(path.join(installDirectory, 'generation-runtime.mjs'))).toBe(true); |
| 326 | }).pipe( |
| 327 | Effect.ensuring( |
| 328 | Effect.sync(() => fs.rmSync(installDirectory, { recursive: true, force: true })) |
| 329 | ) |
| 330 | ); |
| 331 | } |
| 332 | ); |
| 333 | |
| 334 | it.effect('[Given] a complete archive [Then] repair atomically replaces companions', () => { |
| 335 | const installDirectory = fs.mkdtempSync(path.join(os.tmpdir(), 'composio-run-repair-test-')); |
| 336 | const execPath = path.join(installDirectory, 'composio'); |
| 337 | const companionRelativePath = RUN_COMPANION_MODULE_FILENAMES[0]!; |
| 338 | const companionPath = path.join(installDirectory, companionRelativePath); |
| 339 | const releaseTagPath = path.join(installDirectory, RUN_COMPANION_RELEASE_TAG_FILENAME); |
| 340 | fs.mkdirSync(path.dirname(companionPath), { recursive: true }); |
| 341 | fs.writeFileSync(companionPath, 'old-companion'); |
| 342 | fs.writeFileSync(releaseTagPath, '@composio/cli@0.2.31\n'); |
| 343 | const originalCompanion = fs.statSync(companionPath); |
| 344 | const fetchMock = stubRepairFetch(); |
| 345 | mockArchiveContents(); |
| 346 | |
| 347 | return Effect.gen(function* () { |
| 348 | const result = yield* repairMissingInstalledRunCompanionModules({ |
| 349 | callerImportMetaUrl: 'file:///$bunfs/root/commands.mjs', |
| 350 | execPath, |
| 351 | appVersion: '0.0.0-test', |
| 352 | }); |
| 353 | |
| 354 | expect(result).toEqual({ repaired: true, releaseTag: TEST_RELEASE_TAG }); |
| 355 | expect(fs.readFileSync(companionPath, 'utf8')).toBe(`new:${companionRelativePath}`); |
| 356 | const replacedCompanion = fs.statSync(companionPath); |
| 357 | expect(replacedCompanion.dev).toBe(originalCompanion.dev); |
| 358 | expect(replacedCompanion.ino).not.toBe(originalCompanion.ino); |
| 359 | expect(fs.readFileSync(releaseTagPath, 'utf8')).toBe(`${TEST_RELEASE_TAG}\n`); |
| 360 | expect(fetchMock).toHaveBeenCalledTimes(2); |
| 361 | }).pipe( |
| 362 | Effect.ensuring( |
| 363 | Effect.sync(() => fs.rmSync(installDirectory, { recursive: true, force: true })) |
| 364 | ) |
| 365 | ); |
| 366 | }); |
| 367 | |
| 368 | it.effect( |
| 369 | '[Given] an archive with a manifest [Then] repair restores the files it lists and the manifest', |
| 370 | () => { |
| 371 | const installDirectory = fs.mkdtempSync( |
| 372 | path.join(os.tmpdir(), 'composio-run-repair-manifest-') |
| 373 | ); |
| 374 | const execPath = path.join(installDirectory, 'composio'); |
| 375 | const manifest = { |
| 376 | schemaVersion: 1, |
| 377 | minimumUpgradeFrom: '0.1.26', |
| 378 | companions: LIVE_COMPANIONS_WITH_BUNDLES, |
| 379 | }; |
| 380 | stubRepairFetch(); |
| 381 | mockManifestArchive( |
| 382 | [...LIVE_COMPANIONS_WITH_BUNDLES, ...RUN_COMPANION_LEGACY_PLACEHOLDER_RELATIVE_PATHS], |
| 383 | manifest |
| 384 | ); |
| 385 | |
| 386 | return Effect.gen(function* () { |
| 387 | const result = yield* repairMissingInstalledRunCompanionModules({ |
| 388 | callerImportMetaUrl: 'file:///$bunfs/root/commands.mjs', |
| 389 | execPath, |
| 390 | appVersion: '0.0.0-test', |
| 391 | }); |
| 392 | |
| 393 | expect(result).toEqual({ repaired: true, releaseTag: TEST_RELEASE_TAG }); |
| 394 | expect(listFilesRecursively(installDirectory)).toEqual( |
| 395 | [ |
| 396 | ...LIVE_COMPANIONS_WITH_BUNDLES, |
| 397 | RELEASE_MANIFEST_FILENAME, |
| 398 | RUN_COMPANION_RELEASE_TAG_FILENAME, |
| 399 | ].sort() |
| 400 | ); |
| 401 | expect( |
| 402 | JSON.parse( |
| 403 | fs.readFileSync(path.join(installDirectory, RELEASE_MANIFEST_FILENAME), 'utf8') |
| 404 | ) |
| 405 | ).toEqual(manifest); |
| 406 | }).pipe( |
| 407 | Effect.ensuring( |
| 408 | Effect.sync(() => fs.rmSync(installDirectory, { recursive: true, force: true })) |
| 409 | ) |
| 410 | ); |
| 411 | } |
| 412 | ); |
| 413 | |
| 414 | it.effect('[Given] an archive with an invalid manifest [Then] repair restores nothing', () => { |
| 415 | const installDirectory = fs.mkdtempSync( |
| 416 | path.join(os.tmpdir(), 'composio-run-repair-manifest-') |
| 417 | ); |
| 418 | const execPath = path.join(installDirectory, 'composio'); |
| 419 | stubRepairFetch(); |
| 420 | mockManifestArchive(LIVE_COMPANIONS_WITH_BUNDLES, { |
| 421 | schemaVersion: 1, |
| 422 | minimumUpgradeFrom: '0.1.26', |
| 423 | companions: ['../outside.mjs'], |
| 424 | }); |
| 425 | |
| 426 | return Effect.gen(function* () { |
| 427 | const error = yield* repairMissingInstalledRunCompanionModules({ |
| 428 | callerImportMetaUrl: 'file:///$bunfs/root/commands.mjs', |
| 429 | execPath, |
| 430 | appVersion: '0.0.0-test', |
| 431 | }).pipe(Effect.flip); |
| 432 | |
| 433 | expect(error._tag).toBe('services/RunCompanionRepairError'); |
| 434 | expect(error.message).toContain(`broken ${RELEASE_MANIFEST_FILENAME}`); |
| 435 | expect(fs.readdirSync(installDirectory)).toEqual([]); |
| 436 | }).pipe( |
| 437 | Effect.ensuring( |
| 438 | Effect.sync(() => fs.rmSync(installDirectory, { recursive: true, force: true })) |
| 439 | ) |
| 440 | ); |
| 441 | }); |
| 442 | |
| 443 | it.effect('[Given] an incomplete archive [Then] repair preserves release metadata', () => { |
| 444 | const installDirectory = fs.mkdtempSync(path.join(os.tmpdir(), 'composio-run-repair-test-')); |
| 445 | const execPath = path.join(installDirectory, 'composio'); |
| 446 | const releaseTagPath = path.join(installDirectory, RUN_COMPANION_RELEASE_TAG_FILENAME); |
| 447 | const previousReleaseTag = '@composio/cli@0.2.31\n'; |
| 448 | const missingRelativePath = RUN_COMPANION_MODULE_FILENAMES.at(-1)!; |
| 449 | fs.writeFileSync(releaseTagPath, previousReleaseTag); |
| 450 | stubRepairFetch(); |
| 451 | mockArchiveContents(missingRelativePath); |
| 452 | |
| 453 | return Effect.gen(function* () { |
| 454 | const error = yield* repairMissingInstalledRunCompanionModules({ |
| 455 | callerImportMetaUrl: 'file:///$bunfs/root/commands.mjs', |
| 456 | execPath, |
| 457 | appVersion: '0.0.0-test', |
| 458 | }).pipe(Effect.flip); |
| 459 | |
| 460 | expect(error.message).toContain( |
| 461 | `missing ${missingRelativePath}; cannot restore the CLI's bundled support files` |
| 462 | ); |
| 463 | expect(fs.readFileSync(releaseTagPath, 'utf8')).toBe(previousReleaseTag); |
| 464 | }).pipe( |
| 465 | Effect.ensuring( |
| 466 | Effect.sync(() => fs.rmSync(installDirectory, { recursive: true, force: true })) |
| 467 | ) |
| 468 | ); |
| 469 | }); |
| 470 | |
| 471 | it.effect( |
| 472 | '[Given] malformed release metadata [Then] repair rejects it before using release assets', |
| 473 | () => { |
| 474 | const installDirectory = fs.mkdtempSync( |
| 475 | path.join(os.tmpdir(), 'composio-run-repair-test-') |
| 476 | ); |
| 477 | const fetchMock = vi.fn().mockResolvedValue( |
| 478 | new Response(JSON.stringify({ tag_name: 123, assets: 'invalid' }), { |
| 479 | status: 200, |
| 480 | headers: { 'content-type': 'application/json' }, |
| 481 | }) |
| 482 | ); |
| 483 | vi.stubGlobal('fetch', fetchMock); |
| 484 | |
| 485 | return Effect.gen(function* () { |
| 486 | const error = yield* repairMissingInstalledRunCompanionModules({ |
| 487 | callerImportMetaUrl: 'file:///$bunfs/root/commands.mjs', |
| 488 | execPath: path.join(installDirectory, 'composio'), |
| 489 | appVersion: '0.0.0-test', |
| 490 | }).pipe(Effect.flip); |
| 491 | |
| 492 | expect(error.message).toContain("Unable to restore the CLI's bundled support files"); |
| 493 | expect(fetchMock).toHaveBeenCalledOnce(); |
| 494 | }).pipe( |
| 495 | Effect.ensuring( |
| 496 | Effect.sync(() => fs.rmSync(installDirectory, { recursive: true, force: true })) |
| 497 | ) |
| 498 | ); |
| 499 | } |
| 500 | ); |
| 501 | |
| 502 | it.effect( |
| 503 | '[Given] unprefixed GITHUB_* env under the CLI-wide prefixed provider [Then] repair honors the unprefixed contract', |
| 504 | () => { |
| 505 | const installDirectory = fs.mkdtempSync( |
| 506 | path.join(os.tmpdir(), 'composio-run-repair-test-') |
| 507 | ); |
| 508 | const fetchMock = vi.fn().mockResolvedValue( |
| 509 | new Response(JSON.stringify({ tag_name: 123, assets: 'invalid' }), { |
| 510 | status: 200, |
| 511 | headers: { 'content-type': 'application/json' }, |
| 512 | }) |
| 513 | ); |
| 514 | vi.stubGlobal('fetch', fetchMock); |
| 515 | vi.stubEnv('GITHUB_TAG', '@composio/cli@9.9.9-test'); |
| 516 | vi.stubEnv('GITHUB_API_BASE_URL', 'https://github-proxy.test'); |
| 517 | vi.stubEnv('GITHUB_OWNER', 'fork-owner'); |
| 518 | vi.stubEnv('GITHUB_REPO', 'fork-repo'); |
| 519 | |
| 520 | return Effect.gen(function* () { |
| 521 | yield* repairMissingInstalledRunCompanionModules({ |
| 522 | callerImportMetaUrl: 'file:///$bunfs/root/commands.mjs', |
| 523 | execPath: path.join(installDirectory, 'composio'), |
| 524 | appVersion: '0.0.0-test', |
| 525 | }).pipe(Effect.flip); |
| 526 | |
| 527 | expect(fetchMock).toHaveBeenCalledOnce(); |
| 528 | const [[requestUrl]] = fetchMock.mock.calls; |
| 529 | expect(String(requestUrl)).toBe( |
| 530 | 'https://github-proxy.test/repos/fork-owner/fork-repo/releases/tags/%40composio%2Fcli%409.9.9-test' |
| 531 | ); |
| 532 | }).pipe( |
| 533 | // Simulate the cli-main runtime, whose provider rewrites config keys |
| 534 | // to their COMPOSIO_-prefixed spelling. |
| 535 | Effect.provideService(ConfigProvider.ConfigProvider, ConfigProvider.fromEnv()), |
| 536 | Effect.ensuring( |
| 537 | Effect.sync(() => fs.rmSync(installDirectory, { recursive: true, force: true })) |
| 538 | ) |
| 539 | ); |
| 540 | } |
| 541 | ); |
| 542 | |
| 543 | it.effect( |
| 544 | '[Given] only COMPOSIO_-prefixed GITHUB_* env [Then] repair falls back to the prefixed spelling', |
| 545 | () => { |
| 546 | const installDirectory = fs.mkdtempSync( |
| 547 | path.join(os.tmpdir(), 'composio-run-repair-test-') |
| 548 | ); |
| 549 | const fetchMock = vi.fn().mockResolvedValue( |
| 550 | new Response(JSON.stringify({ tag_name: 123, assets: 'invalid' }), { |
| 551 | status: 200, |
| 552 | headers: { 'content-type': 'application/json' }, |
| 553 | }) |
| 554 | ); |
| 555 | vi.stubGlobal('fetch', fetchMock); |
| 556 | vi.stubEnv('GITHUB_TAG', undefined); |
| 557 | vi.stubEnv('GITHUB_API_BASE_URL', undefined); |
| 558 | vi.stubEnv('GITHUB_OWNER', undefined); |
| 559 | vi.stubEnv('GITHUB_REPO', undefined); |
| 560 | vi.stubEnv('COMPOSIO_GITHUB_TAG', '@composio/cli@8.8.8-test'); |
| 561 | vi.stubEnv('COMPOSIO_GITHUB_API_BASE_URL', 'https://prefixed-proxy.test'); |
| 562 | |
| 563 | return Effect.gen(function* () { |
| 564 | yield* repairMissingInstalledRunCompanionModules({ |
| 565 | callerImportMetaUrl: 'file:///$bunfs/root/commands.mjs', |
| 566 | execPath: path.join(installDirectory, 'composio'), |
| 567 | appVersion: '0.0.0-test', |
| 568 | }).pipe(Effect.flip); |
| 569 | |
| 570 | expect(fetchMock).toHaveBeenCalledOnce(); |
| 571 | const [[requestUrl]] = fetchMock.mock.calls; |
| 572 | expect(String(requestUrl)).toBe( |
| 573 | 'https://prefixed-proxy.test/repos/ComposioHQ/composio/releases/tags/%40composio%2Fcli%408.8.8-test' |
| 574 | ); |
| 575 | }).pipe( |
| 576 | Effect.provideService(ConfigProvider.ConfigProvider, ConfigProvider.fromEnv()), |
| 577 | Effect.ensuring( |
| 578 | Effect.sync(() => fs.rmSync(installDirectory, { recursive: true, force: true })) |
| 579 | ) |
| 580 | ); |
| 581 | } |
| 582 | ); |
| 583 | }); |
| 584 | }); |
| 585 | |
| 586 | describe('loadInstalledCompanionModule', () => { |
| 587 | layer(BunServices.layer)(it => { |
| 588 | it('registers the in-process companion alongside the run preload set', () => { |
| 589 | expect(RUN_COMPANION_MODULE_BASENAMES).toEqual( |
| 590 | expect.arrayContaining(['generation-runtime']) |
| 591 | ); |
| 592 | }); |
| 593 | |
| 594 | // From a checkout there is no `.mjs` next to an executable, so the loader |
| 595 | // has to fall through to the `.ts` source next to `run-companion-modules.ts`. |
| 596 | // The compiled-binary path (`dist/<name>.mjs` next to `process.execPath`) |
| 597 | // is covered by the Docker E2E suite, which runs the real binary. |
| 598 | it.effect('[Given] a source checkout [Then] it loads the module from its .ts source', () => |
| 599 | Effect.gen(function* () { |
| 600 | const generation = yield* loadInstalledCompanionModule< |
| 601 | typeof import('src/services/generation-runtime') |
| 602 | >('generation-runtime', ['wrapInlineCodeForRun']); |
| 603 | |
| 604 | expect(generation.wrapInlineCodeForRun('1 + 1')).toBe('return (1 + 1);'); |
| 605 | }) |
| 606 | ); |
| 607 | |
| 608 | it.effect('[Given] a module that cannot be loaded [Then] it fails with a typed error', () => |
| 609 | Effect.gen(function* () { |
| 610 | const error = yield* loadInstalledCompanionModule('missing-companion-module', []).pipe( |
| 611 | Effect.flip |
| 612 | ); |
| 613 | |
| 614 | expect(error._tag).toBe('services/RunCompanionRepairError'); |
| 615 | expect(error.message).toContain('missing-companion-module.mjs'); |
| 616 | }) |
| 617 | ); |
| 618 | |
| 619 | it.effect( |
| 620 | '[Given] a module from another release [Then] it fails with a typed error naming the missing export', |
| 621 | () => |
| 622 | Effect.gen(function* () { |
| 623 | const error = yield* loadInstalledCompanionModule<{ |
| 624 | readonly wrapInlineCodeForRun: unknown; |
| 625 | readonly retiredExport: unknown; |
| 626 | }>('generation-runtime', ['wrapInlineCodeForRun', 'retiredExport']).pipe(Effect.flip); |
| 627 | |
| 628 | expect(error._tag).toBe('services/RunCompanionRepairError'); |
| 629 | expect(error.message).toContain('generation-runtime'); |
| 630 | expect(error.message).toContain('missing retiredExport'); |
| 631 | }) |
| 632 | ); |
| 633 | |
| 634 | it.effect('[Given] the generation companion [Then] its API is promise-shaped', () => |
| 635 | Effect.gen(function* () { |
| 636 | const generation = yield* loadInstalledCompanionModule< |
| 637 | typeof import('src/services/generation-runtime') |
| 638 | >('generation-runtime', ['wrapInlineCodeForRun', 'generatePythonSourceFiles']); |
| 639 | |
| 640 | expect(generation.wrapInlineCodeForRun('1 + 1')).toBe('return (1 + 1);'); |
| 641 | const outcome = yield* Effect.promise(() => |
| 642 | generation.generatePythonSourceFiles({ banner: 'b', outputDir: '/tmp/out' }, {}) |
| 643 | ); |
| 644 | expect(outcome).toEqual({ _tag: 'Success', value: [] }); |
| 645 | }) |
| 646 | ); |
| 647 | }); |
| 648 | }); |
| 649 | |
| 650 | describe('Windows compiled companion resolution', () => { |
| 651 | it.effect('falls back beside the executable for a Windows Bun virtual module', () => |
| 652 | Effect.gen(function* () { |
| 653 | const result = yield* resolveRunCompanionModulePath({ |
| 654 | callerImportMetaUrl: 'file:///B:/~BUN/root/commands.mjs', |
| 655 | execPath: 'C:\\Users\\Example User\\.composio\\composio.exe', |
| 656 | relativeNoExtensionFromCaller: '../../services/run-helpers-runtime', |
| 657 | }); |
| 658 | expect(result).toBe('C:\\Users\\Example User\\.composio\\run-helpers-runtime.mjs'); |
| 659 | }).pipe(Effect.provide(BunPath.layerWin32), Effect.provide(BunServices.layer)) |
| 660 | ); |
| 661 | }); |