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 { afterAll, afterEach, describe, expect, it, vi } from '@effect/vitest'; |
| 2 | import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; |
| 3 | import { tmpdir } from 'node:os'; |
| 4 | import { join } from 'node:path'; |
| 5 | import { ConfigProvider, Effect, Exit, Layer, Option } from 'effect'; |
| 6 | import { FetchHttpClient } from 'effect/http'; |
| 7 | import * as FileSystem from 'effect/FileSystem'; |
| 8 | import * as Path from 'effect/Path'; |
| 9 | import * as PlatformError from 'effect/PlatformError'; |
| 10 | import * as BunFileSystem from '@effect/platform-bun/BunFileSystem'; |
| 11 | import * as BunPath from '@effect/platform-bun/BunPath'; |
| 12 | import { withHttpServer } from 'test/__utils__/http-server'; |
| 13 | import { getTerminalCapabilities, TerminalUI } from 'src/services/terminal-ui'; |
| 14 | import { |
| 15 | formatDownloadProgress, |
| 16 | formatMegabytes, |
| 17 | UpgradeBelowMinimumError, |
| 18 | UpgradeBinary, |
| 19 | UpgradeBinaryError, |
| 20 | } from 'src/services/upgrade-binary'; |
| 21 | import { NodeOs } from 'src/services/node-os'; |
| 22 | import { |
| 23 | collectExpectedRunCompanionAssetRelativePaths, |
| 24 | RELEASE_MANIFEST_FILENAME, |
| 25 | RUN_COMPANION_RELEASE_TAG_FILENAME, |
| 26 | } from 'src/services/run-companion-modules'; |
| 27 | |
| 28 | const TerminalUINoop = Layer.succeed( |
| 29 | TerminalUI, |
| 30 | TerminalUI.of({ |
| 31 | capabilities: Effect.succeed( |
| 32 | getTerminalCapabilities({ |
| 33 | stdin: { isTTY: false }, |
| 34 | stdout: { isTTY: false }, |
| 35 | stderr: { isTTY: false }, |
| 36 | }) |
| 37 | ), |
| 38 | output: () => Effect.void, |
| 39 | stdoutClosed: Effect.never, |
| 40 | error: () => Effect.void, |
| 41 | intro: () => Effect.void, |
| 42 | outro: () => Effect.void, |
| 43 | log: { |
| 44 | info: () => Effect.void, |
| 45 | success: () => Effect.void, |
| 46 | warn: () => Effect.void, |
| 47 | error: () => Effect.void, |
| 48 | step: () => Effect.void, |
| 49 | message: () => Effect.void, |
| 50 | }, |
| 51 | note: () => Effect.void, |
| 52 | select: (_message, options) => Effect.succeed(options[0].value), |
| 53 | confirm: () => Effect.succeed(true), |
| 54 | withSpinner: (_message, effect) => effect, |
| 55 | useMakeSpinner: (_message, use) => |
| 56 | use({ |
| 57 | message: () => Effect.void, |
| 58 | stop: () => Effect.void, |
| 59 | error: () => Effect.void, |
| 60 | }), |
| 61 | }) |
| 62 | ); |
| 63 | |
| 64 | // A private home directory: stable-channel lookups record the update-check cache under it. |
| 65 | const testHomedir = mkdtempSync(join(tmpdir(), 'composio-upgrade-home-')); |
| 66 | const updateCheckDir = join(testHomedir, '.composio'); |
| 67 | const updateCheckFile = join(updateCheckDir, 'update-check.json'); |
| 68 | afterEach(() => rmSync(updateCheckDir, { recursive: true, force: true })); |
| 69 | afterAll(() => rmSync(testHomedir, { recursive: true, force: true })); |
| 70 | |
| 71 | const NodeOsTest = Layer.succeed( |
| 72 | NodeOs, |
| 73 | NodeOs.of({ |
| 74 | homedir: testHomedir, |
| 75 | tmpdir: '/tmp', |
| 76 | platform: 'darwin', |
| 77 | arch: 'arm64', |
| 78 | }) |
| 79 | ); |
| 80 | |
| 81 | const TestPlatform = Layer.mergeAll(BunFileSystem.layer, BunPath.layer); |
| 82 | |
| 83 | type UpgradeOptions = { |
| 84 | readonly prerelease?: boolean; |
| 85 | readonly tag?: string; |
| 86 | }; |
| 87 | |
| 88 | const makeUpgradeEffect = ( |
| 89 | configEntries: ReadonlyArray<[string, string]>, |
| 90 | options?: UpgradeOptions, |
| 91 | fileSystemLayer: Layer.Layer<FileSystem.FileSystem> = BunFileSystem.layer, |
| 92 | nodeOsLayer: Layer.Layer<NodeOs> = NodeOsTest |
| 93 | ) => |
| 94 | Effect.gen(function* () { |
| 95 | const service = yield* UpgradeBinary; |
| 96 | return yield* service.upgrade(options); |
| 97 | }).pipe( |
| 98 | Effect.provide(UpgradeBinary.Default), |
| 99 | Effect.provide(FetchHttpClient.layer), |
| 100 | Effect.provide(fileSystemLayer), |
| 101 | Effect.provide(TerminalUINoop), |
| 102 | Effect.provide(nodeOsLayer), |
| 103 | Effect.provideService( |
| 104 | ConfigProvider.ConfigProvider, |
| 105 | ConfigProvider.fromEnv({ env: Object.fromEntries(configEntries) }) |
| 106 | ), |
| 107 | Effect.scoped |
| 108 | ); |
| 109 | |
| 110 | const runUpgrade = ( |
| 111 | configEntries: ReadonlyArray<[string, string]>, |
| 112 | options?: UpgradeOptions, |
| 113 | fileSystemLayer?: Layer.Layer<FileSystem.FileSystem> |
| 114 | ) => makeUpgradeEffect(configEntries, options, fileSystemLayer).pipe(Effect.flip); |
| 115 | |
| 116 | const runUpgradeSuccess = ( |
| 117 | configEntries: ReadonlyArray<[string, string]>, |
| 118 | options?: UpgradeOptions, |
| 119 | fileSystemLayer?: Layer.Layer<FileSystem.FileSystem> |
| 120 | ) => makeUpgradeEffect(configEntries, options, fileSystemLayer); |
| 121 | |
| 122 | const failRenameTo = (failedTargetPath: string) => |
| 123 | Layer.effect( |
| 124 | FileSystem.FileSystem, |
| 125 | Effect.map( |
| 126 | FileSystem.FileSystem, |
| 127 | fs => |
| 128 | new Proxy(fs, { |
| 129 | get(target, property, receiver) { |
| 130 | if (property !== 'rename') { |
| 131 | return Reflect.get(target, property, receiver); |
| 132 | } |
| 133 | |
| 134 | return (oldPath: string, newPath: string) => |
| 135 | newPath === failedTargetPath |
| 136 | ? Effect.fail( |
| 137 | PlatformError.systemError({ |
| 138 | _tag: 'Busy', |
| 139 | module: 'FileSystem', |
| 140 | method: 'rename', |
| 141 | pathOrDescriptor: newPath, |
| 142 | }) |
| 143 | ) |
| 144 | : target.rename(oldPath, newPath); |
| 145 | }, |
| 146 | }) |
| 147 | ) |
| 148 | ).pipe(Layer.provide(BunFileSystem.layer)); |
| 149 | |
| 150 | /** |
| 151 | * Bridge the promise-based `withHttpServer` harness into a scoped Effect: |
| 152 | * acquisition starts the server and yields its base URL, and closing the |
| 153 | * test scope lets `withHttpServer` run its teardown path. |
| 154 | */ |
| 155 | const scopedHttpServer = (handler: Parameters<typeof withHttpServer>[0]) => |
| 156 | Effect.acquireRelease( |
| 157 | Effect.promise( |
| 158 | () => |
| 159 | new Promise<{ baseUrl: string; release: () => void; closed: Promise<void> }>( |
| 160 | (resolveAcquire, rejectAcquire) => { |
| 161 | let release!: () => void; |
| 162 | const released = new Promise<void>(resolve => { |
| 163 | release = resolve; |
| 164 | }); |
| 165 | const closed = withHttpServer(handler, baseUrl => { |
| 166 | resolveAcquire({ baseUrl, release, closed }); |
| 167 | return released; |
| 168 | }); |
| 169 | closed.catch(rejectAcquire); |
| 170 | } |
| 171 | ) |
| 172 | ), |
| 173 | ({ release, closed }) => |
| 174 | Effect.promise(() => { |
| 175 | release(); |
| 176 | return closed; |
| 177 | }) |
| 178 | ).pipe(Effect.map(({ baseUrl }) => baseUrl)); |
| 179 | |
| 180 | const restoreStubsAndMocks = Effect.sync(() => { |
| 181 | vi.unstubAllGlobals(); |
| 182 | vi.restoreAllMocks(); |
| 183 | }); |
| 184 | |
| 185 | /** Write each companion file of a binary package under `sourceDir` with `contents`. */ |
| 186 | const writeCompanionFiles = ( |
| 187 | sourceDir: string, |
| 188 | relativePaths: ReadonlyArray<string>, |
| 189 | contents: string |
| 190 | ) => |
| 191 | Effect.gen(function* () { |
| 192 | const fs = yield* FileSystem.FileSystem; |
| 193 | const path = yield* Path.Path; |
| 194 | yield* Effect.forEach(relativePaths, relativePath => |
| 195 | Effect.gen(function* () { |
| 196 | const companionPath = path.join(sourceDir, relativePath); |
| 197 | yield* fs.makeDirectory(path.dirname(companionPath), { recursive: true }); |
| 198 | yield* fs.writeFileString(companionPath, contents); |
| 199 | }) |
| 200 | ); |
| 201 | }); |
| 202 | |
| 203 | describe('UpgradeBinary', () => { |
| 204 | it.effect('wraps non-2xx releases fetch failures with fetch context (no tag branch)', () => { |
| 205 | vi.stubGlobal('Bun', { which: vi.fn(() => null) }); |
| 206 | |
| 207 | return Effect.gen(function* () { |
| 208 | const apiBaseUrl = yield* scopedHttpServer((_req, res) => { |
| 209 | res.writeHead(500, { 'content-type': 'application/json' }); |
| 210 | res.end(JSON.stringify({ message: 'rate limited' })); |
| 211 | }); |
| 212 | |
| 213 | const error = yield* runUpgrade([ |
| 214 | ['COMPOSIO_GITHUB_API_BASE_URL', apiBaseUrl], |
| 215 | ['COMPOSIO_GITHUB_OWNER', 'test-owner'], |
| 216 | ['COMPOSIO_GITHUB_REPO', 'test-repo'], |
| 217 | ]); |
| 218 | |
| 219 | expect(error).toBeInstanceOf(UpgradeBinaryError); |
| 220 | expect(error.message).toBe('Failed to fetch releases from GitHub'); |
| 221 | expect(String(error.cause)).toContain('HTTP 500'); |
| 222 | }).pipe(Effect.ensuring(restoreStubsAndMocks)); |
| 223 | }); |
| 224 | |
| 225 | it.effect('wraps tagged release JSON parse failures with parse context (tag branch)', () => { |
| 226 | vi.stubGlobal('Bun', { which: vi.fn(() => null) }); |
| 227 | |
| 228 | return Effect.gen(function* () { |
| 229 | const apiBaseUrl = yield* scopedHttpServer((_req, res) => { |
| 230 | res.writeHead(200, { 'content-type': 'application/json' }); |
| 231 | res.end('not-json'); |
| 232 | }); |
| 233 | |
| 234 | const error = yield* runUpgrade([ |
| 235 | ['COMPOSIO_GITHUB_API_BASE_URL', apiBaseUrl], |
| 236 | ['COMPOSIO_GITHUB_OWNER', 'test-owner'], |
| 237 | ['COMPOSIO_GITHUB_REPO', 'test-repo'], |
| 238 | ['COMPOSIO_GITHUB_TAG', 'v9.9.9'], |
| 239 | ]); |
| 240 | |
| 241 | expect(error).toBeInstanceOf(UpgradeBinaryError); |
| 242 | expect(error.message).toBe('Failed to parse GitHub release JSON response'); |
| 243 | }).pipe(Effect.ensuring(restoreStubsAndMocks)); |
| 244 | }); |
| 245 | |
| 246 | it.effect('rejects structurally invalid tagged release JSON', () => { |
| 247 | vi.stubGlobal('Bun', { which: vi.fn(() => null) }); |
| 248 | |
| 249 | return Effect.gen(function* () { |
| 250 | const apiBaseUrl = yield* scopedHttpServer((_req, res) => { |
| 251 | res.writeHead(200, { 'content-type': 'application/json' }); |
| 252 | res.end(JSON.stringify({ tag_name: 42, assets: 'not-an-array' })); |
| 253 | }); |
| 254 | |
| 255 | const error = yield* runUpgrade([ |
| 256 | ['COMPOSIO_GITHUB_API_BASE_URL', apiBaseUrl], |
| 257 | ['COMPOSIO_GITHUB_OWNER', 'test-owner'], |
| 258 | ['COMPOSIO_GITHUB_REPO', 'test-repo'], |
| 259 | ['COMPOSIO_GITHUB_TAG', 'v9.9.9'], |
| 260 | ]); |
| 261 | |
| 262 | expect(error).toBeInstanceOf(UpgradeBinaryError); |
| 263 | expect(error.message).toBe('Failed to parse GitHub release JSON response'); |
| 264 | }).pipe(Effect.ensuring(restoreStubsAndMocks)); |
| 265 | }); |
| 266 | |
| 267 | it.effect('URL-encodes slash-containing tags in tagged release request path', () => { |
| 268 | vi.stubGlobal('Bun', { which: vi.fn(() => null) }); |
| 269 | let receivedPath: string | undefined; |
| 270 | |
| 271 | return Effect.gen(function* () { |
| 272 | const apiBaseUrl = yield* scopedHttpServer((req, res) => { |
| 273 | receivedPath = req.url; |
| 274 | res.writeHead(500, { 'content-type': 'application/json' }); |
| 275 | res.end(JSON.stringify({ message: 'forced failure' })); |
| 276 | }); |
| 277 | |
| 278 | const tag = '@composio/cli@0.1.24'; |
| 279 | const error = yield* runUpgrade([ |
| 280 | ['COMPOSIO_GITHUB_API_BASE_URL', apiBaseUrl], |
| 281 | ['COMPOSIO_GITHUB_OWNER', 'test-owner'], |
| 282 | ['COMPOSIO_GITHUB_REPO', 'test-repo'], |
| 283 | ['COMPOSIO_GITHUB_TAG', tag], |
| 284 | ]); |
| 285 | |
| 286 | expect(error).toBeInstanceOf(UpgradeBinaryError); |
| 287 | expect(receivedPath).toBe( |
| 288 | `/repos/test-owner/test-repo/releases/tags/${encodeURIComponent(tag)}` |
| 289 | ); |
| 290 | }).pipe(Effect.ensuring(restoreStubsAndMocks)); |
| 291 | }); |
| 292 | |
| 293 | it.effect('skips newer releases that do not contain a binary for the current platform', () => { |
| 294 | vi.stubGlobal('Bun', { which: vi.fn(() => null) }); |
| 295 | |
| 296 | return Effect.gen(function* () { |
| 297 | const fs = yield* FileSystem.FileSystem; |
| 298 | const path = yield* Path.Path; |
| 299 | const installDir = yield* fs.makeTempDirectoryScoped({ |
| 300 | prefix: 'composio-upgrade-current-', |
| 301 | }); |
| 302 | const fakeExecPath = path.join(installDir, 'composio'); |
| 303 | yield* fs.writeFileString(path.join(installDir, 'release-tag.txt'), '@composio/cli@0.2.14\n'); |
| 304 | vi.spyOn(process, 'execPath', 'get').mockReturnValue(fakeExecPath); |
| 305 | |
| 306 | const apiBaseUrl = yield* scopedHttpServer((_req, res) => { |
| 307 | res.writeHead(200, { 'content-type': 'application/json' }); |
| 308 | res.end( |
| 309 | JSON.stringify([ |
| 310 | { |
| 311 | tag_name: '@composio/cli@0.2.15', |
| 312 | draft: false, |
| 313 | prerelease: false, |
| 314 | assets: [ |
| 315 | { |
| 316 | name: 'composio-linux-x64.zip', |
| 317 | browser_download_url: 'http://127.0.0.1/unused-linux.zip', |
| 318 | }, |
| 319 | ], |
| 320 | }, |
| 321 | { |
| 322 | tag_name: '@composio/cli@0.2.14', |
| 323 | draft: false, |
| 324 | prerelease: false, |
| 325 | assets: [ |
| 326 | { |
| 327 | name: 'composio-darwin-aarch64.zip', |
| 328 | browser_download_url: 'http://127.0.0.1/current-darwin.zip', |
| 329 | }, |
| 330 | ], |
| 331 | }, |
| 332 | ]) |
| 333 | ); |
| 334 | }); |
| 335 | |
| 336 | const result = yield* runUpgradeSuccess([ |
| 337 | ['COMPOSIO_GITHUB_API_BASE_URL', apiBaseUrl], |
| 338 | ['COMPOSIO_GITHUB_OWNER', 'test-owner'], |
| 339 | ['COMPOSIO_GITHUB_REPO', 'test-repo'], |
| 340 | ]); |
| 341 | |
| 342 | expect(result).toBeUndefined(); |
| 343 | }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks)); |
| 344 | }); |
| 345 | |
| 346 | it.effect('ignores prereleases when checking the stable channel', () => { |
| 347 | vi.stubGlobal('Bun', { which: vi.fn(() => null) }); |
| 348 | |
| 349 | return Effect.gen(function* () { |
| 350 | const fs = yield* FileSystem.FileSystem; |
| 351 | const path = yield* Path.Path; |
| 352 | const installDir = yield* fs.makeTempDirectoryScoped({ |
| 353 | prefix: 'composio-upgrade-current-', |
| 354 | }); |
| 355 | const fakeExecPath = path.join(installDir, 'composio'); |
| 356 | yield* fs.writeFileString(path.join(installDir, 'release-tag.txt'), '@composio/cli@0.2.17\n'); |
| 357 | vi.spyOn(process, 'execPath', 'get').mockReturnValue(fakeExecPath); |
| 358 | |
| 359 | const apiBaseUrl = yield* scopedHttpServer((_req, res) => { |
| 360 | res.writeHead(200, { 'content-type': 'application/json' }); |
| 361 | res.end( |
| 362 | JSON.stringify([ |
| 363 | { |
| 364 | tag_name: '@composio/cli@0.2.18-beta.9', |
| 365 | draft: false, |
| 366 | prerelease: true, |
| 367 | assets: [ |
| 368 | { |
| 369 | name: 'composio-darwin-aarch64.zip', |
| 370 | browser_download_url: 'http://127.0.0.1/beta.zip', |
| 371 | }, |
| 372 | ], |
| 373 | }, |
| 374 | { |
| 375 | tag_name: '@composio/cli@0.2.17', |
| 376 | draft: false, |
| 377 | prerelease: false, |
| 378 | assets: [ |
| 379 | { |
| 380 | name: 'composio-darwin-aarch64.zip', |
| 381 | browser_download_url: 'http://127.0.0.1/stable.zip', |
| 382 | }, |
| 383 | ], |
| 384 | }, |
| 385 | ]) |
| 386 | ); |
| 387 | }); |
| 388 | |
| 389 | const result = yield* runUpgradeSuccess([ |
| 390 | ['COMPOSIO_GITHUB_API_BASE_URL', apiBaseUrl], |
| 391 | ['COMPOSIO_GITHUB_OWNER', 'test-owner'], |
| 392 | ['COMPOSIO_GITHUB_REPO', 'test-repo'], |
| 393 | ]); |
| 394 | |
| 395 | expect(result).toBeUndefined(); |
| 396 | }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks)); |
| 397 | }); |
| 398 | |
| 399 | it.effect('selects the latest prerelease when beta upgrades are requested', () => { |
| 400 | vi.stubGlobal('Bun', { which: vi.fn(() => null) }); |
| 401 | |
| 402 | return Effect.gen(function* () { |
| 403 | const fs = yield* FileSystem.FileSystem; |
| 404 | const path = yield* Path.Path; |
| 405 | const installDir = yield* fs.makeTempDirectoryScoped({ |
| 406 | prefix: 'composio-beta-select-', |
| 407 | }); |
| 408 | const fakeExecPath = path.join(installDir, 'composio'); |
| 409 | yield* fs.writeFileString( |
| 410 | path.join(installDir, 'release-tag.txt'), |
| 411 | '@composio/cli@0.1.0-beta.0\n' |
| 412 | ); |
| 413 | vi.spyOn(process, 'execPath', 'get').mockReturnValue(fakeExecPath); |
| 414 | |
| 415 | const apiBaseUrl = yield* scopedHttpServer((_req, res) => { |
| 416 | res.writeHead(200, { 'content-type': 'application/json' }); |
| 417 | res.end( |
| 418 | JSON.stringify([ |
| 419 | { |
| 420 | tag_name: '@composio/cli@0.2.19-beta.1', |
| 421 | draft: false, |
| 422 | prerelease: true, |
| 423 | assets: [ |
| 424 | { |
| 425 | name: 'composio-darwin-aarch64.zip', |
| 426 | browser_download_url: 'http://127.0.0.1/beta-1.zip', |
| 427 | }, |
| 428 | ], |
| 429 | }, |
| 430 | { |
| 431 | tag_name: '@composio/cli@0.2.19-beta.3', |
| 432 | draft: false, |
| 433 | prerelease: true, |
| 434 | assets: [ |
| 435 | { |
| 436 | name: 'composio-darwin-aarch64.zip', |
| 437 | browser_download_url: 'http://127.0.0.1/beta-3.zip', |
| 438 | }, |
| 439 | ], |
| 440 | }, |
| 441 | { |
| 442 | tag_name: '@composio/cli@0.2.17', |
| 443 | draft: false, |
| 444 | prerelease: false, |
| 445 | assets: [ |
| 446 | { |
| 447 | name: 'composio-darwin-aarch64.zip', |
| 448 | browser_download_url: 'http://127.0.0.1/stable.zip', |
| 449 | }, |
| 450 | ], |
| 451 | }, |
| 452 | ]) |
| 453 | ); |
| 454 | }); |
| 455 | |
| 456 | const error = yield* runUpgrade( |
| 457 | [ |
| 458 | ['COMPOSIO_GITHUB_API_BASE_URL', apiBaseUrl], |
| 459 | ['COMPOSIO_GITHUB_OWNER', 'test-owner'], |
| 460 | ['COMPOSIO_GITHUB_REPO', 'test-repo'], |
| 461 | ], |
| 462 | { prerelease: true } |
| 463 | ); |
| 464 | |
| 465 | expect(error).toBeInstanceOf(UpgradeBinaryError); |
| 466 | expect(error.message).toBe('Failed to download binary: composio-darwin-aarch64.zip'); |
| 467 | expect(String(error.cause)).toContain('beta-3.zip'); |
| 468 | }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks)); |
| 469 | }); |
| 470 | |
| 471 | it.effect('replaces the binary during local-target upgrades', () => { |
| 472 | vi.stubGlobal('Bun', { which: vi.fn(() => null) }); |
| 473 | |
| 474 | return Effect.gen(function* () { |
| 475 | const fs = yield* FileSystem.FileSystem; |
| 476 | const path = yield* Path.Path; |
| 477 | const installDir = yield* fs.makeTempDirectoryScoped({ |
| 478 | prefix: 'composio-local-upgrade-target-', |
| 479 | }); |
| 480 | const sourceDir = yield* fs.makeTempDirectoryScoped({ |
| 481 | prefix: 'composio-local-upgrade-source-', |
| 482 | }); |
| 483 | const fakeExecPath = path.join(installDir, 'composio'); |
| 484 | const sourceBinaryPath = path.join(sourceDir, 'composio'); |
| 485 | const legacyLocalToolsPath = path.join(installDir, 'local-tools-binaries'); |
| 486 | |
| 487 | yield* fs.writeFileString(fakeExecPath, 'old-binary'); |
| 488 | yield* fs.writeFileString(sourceBinaryPath, 'new-binary'); |
| 489 | yield* fs.makeDirectory(path.join(legacyLocalToolsPath, 'peekaboo'), { recursive: true }); |
| 490 | yield* fs.writeFileString(path.join(legacyLocalToolsPath, 'peekaboo', 'peekaboo'), 'old'); |
| 491 | const originalBinaryInfo = yield* fs.stat(fakeExecPath); |
| 492 | |
| 493 | vi.spyOn(process, 'execPath', 'get').mockReturnValue(fakeExecPath); |
| 494 | |
| 495 | const companionRelativePaths = |
| 496 | yield* collectExpectedRunCompanionAssetRelativePaths(sourceDir); |
| 497 | yield* writeCompanionFiles(sourceDir, companionRelativePaths, 'support-file'); |
| 498 | |
| 499 | const result = yield* runUpgradeSuccess([ |
| 500 | ['DEBUG_OVERRIDE_UPGRADE_TARGET', sourceBinaryPath], |
| 501 | ]); |
| 502 | |
| 503 | expect(result).toBeUndefined(); |
| 504 | expect(yield* fs.readFileString(fakeExecPath)).toBe('new-binary'); |
| 505 | const replacedBinaryInfo = yield* fs.stat(fakeExecPath); |
| 506 | expect(replacedBinaryInfo.dev).toBe(originalBinaryInfo.dev); |
| 507 | expect(Option.getOrThrow(replacedBinaryInfo.ino)).not.toBe( |
| 508 | Option.getOrThrow(originalBinaryInfo.ino) |
| 509 | ); |
| 510 | expect(replacedBinaryInfo.mode & 0o777).toBe(0o755); |
| 511 | expect(yield* fs.exists(legacyLocalToolsPath)).toBe(false); |
| 512 | }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks)); |
| 513 | }); |
| 514 | |
| 515 | // Live clock: the upgrade waits out a briefly locked backup before giving up. |
| 516 | it.live('preserves the Windows bundle when a previous executable backup is locked', () => { |
| 517 | vi.stubGlobal('Bun', { which: vi.fn(() => null) }); |
| 518 | |
| 519 | return Effect.gen(function* () { |
| 520 | const fs = yield* FileSystem.FileSystem; |
| 521 | const path = yield* Path.Path; |
| 522 | const installDir = yield* fs.makeTempDirectoryScoped(); |
| 523 | const sourceDir = yield* fs.makeTempDirectoryScoped(); |
| 524 | const fakeExecPath = path.join(installDir, 'composio.exe'); |
| 525 | const sourceBinaryPath = path.join(sourceDir, 'composio.exe'); |
| 526 | const backupPath = `${fakeExecPath}.old`; |
| 527 | const releaseTagPath = path.join(installDir, RUN_COMPANION_RELEASE_TAG_FILENAME); |
| 528 | yield* fs.writeFileString(fakeExecPath, 'old-binary'); |
| 529 | yield* fs.writeFileString(sourceBinaryPath, 'new-binary'); |
| 530 | yield* fs.writeFileString(backupPath, 'locked-previous-binary'); |
| 531 | yield* fs.writeFileString(releaseTagPath, '@composio/cli@0.2.31\n'); |
| 532 | const companions = yield* collectExpectedRunCompanionAssetRelativePaths(sourceDir); |
| 533 | yield* writeCompanionFiles(sourceDir, companions, 'new-support'); |
| 534 | yield* writeCompanionFiles(installDir, companions, 'old-support'); |
| 535 | vi.spyOn(process, 'execPath', 'get').mockReturnValue(fakeExecPath); |
| 536 | const lockedFileSystem = Layer.succeed( |
| 537 | FileSystem.FileSystem, |
| 538 | FileSystem.make({ |
| 539 | ...fs, |
| 540 | remove: (target, options) => |
| 541 | target === backupPath |
| 542 | ? Effect.fail( |
| 543 | PlatformError.systemError({ |
| 544 | _tag: 'Busy', |
| 545 | module: 'FileSystem', |
| 546 | method: 'remove', |
| 547 | pathOrDescriptor: target, |
| 548 | }) |
| 549 | ) |
| 550 | : fs.remove(target, options), |
| 551 | }) |
| 552 | ); |
| 553 | const windowsOs = Layer.succeed( |
| 554 | NodeOs, |
| 555 | NodeOs.of({ |
| 556 | homedir: testHomedir, |
| 557 | tmpdir: '/tmp', |
| 558 | platform: 'win32', |
| 559 | arch: 'x64', |
| 560 | }) |
| 561 | ); |
| 562 | const error = yield* makeUpgradeEffect( |
| 563 | [['DEBUG_OVERRIDE_UPGRADE_TARGET', sourceBinaryPath]], |
| 564 | { tag: '@composio/cli@0.3.0' }, |
| 565 | lockedFileSystem, |
| 566 | windowsOs |
| 567 | ).pipe(Effect.flip); |
| 568 | |
| 569 | expect(yield* fs.readFileString(fakeExecPath)).toBe('old-binary'); |
| 570 | expect(yield* fs.readFileString(releaseTagPath)).toBe('@composio/cli@0.2.31\n'); |
| 571 | expect(yield* fs.readFileString(path.join(installDir, companions[0]!))).toBe('old-support'); |
| 572 | expect(error).toBeInstanceOf(UpgradeBinaryError); |
| 573 | expect(error.message).toContain('Close other Composio processes'); |
| 574 | }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks)); |
| 575 | }); |
| 576 | |
| 577 | it.effect('commits companions and release metadata before replacing the binary', () => { |
| 578 | vi.stubGlobal('Bun', { which: vi.fn(() => null) }); |
| 579 | |
| 580 | return Effect.gen(function* () { |
| 581 | const fs = yield* FileSystem.FileSystem; |
| 582 | const path = yield* Path.Path; |
| 583 | const installDir = yield* fs.makeTempDirectoryScoped({ |
| 584 | prefix: 'composio-ordered-upgrade-target-', |
| 585 | }); |
| 586 | const sourceDir = yield* fs.makeTempDirectoryScoped({ |
| 587 | prefix: 'composio-ordered-upgrade-source-', |
| 588 | }); |
| 589 | const fakeExecPath = path.join(installDir, 'composio'); |
| 590 | const sourceBinaryPath = path.join(sourceDir, 'composio'); |
| 591 | const releaseTagPath = path.join(installDir, RUN_COMPANION_RELEASE_TAG_FILENAME); |
| 592 | const newReleaseTag = '@composio/cli@0.3.0'; |
| 593 | yield* fs.writeFileString(fakeExecPath, 'old-binary'); |
| 594 | yield* fs.writeFileString(sourceBinaryPath, 'new-binary'); |
| 595 | yield* fs.writeFileString(releaseTagPath, '@composio/cli@0.2.31\n'); |
| 596 | const originalReleaseTagInfo = yield* fs.stat(releaseTagPath); |
| 597 | |
| 598 | const companionRelativePaths = |
| 599 | yield* collectExpectedRunCompanionAssetRelativePaths(sourceDir); |
| 600 | yield* writeCompanionFiles(sourceDir, companionRelativePaths, 'new-support-file'); |
| 601 | const observedCompanionPath = path.join(installDir, companionRelativePaths[0]!); |
| 602 | yield* fs.makeDirectory(path.dirname(observedCompanionPath), { recursive: true }); |
| 603 | yield* fs.writeFileString(observedCompanionPath, 'old-support-file'); |
| 604 | vi.spyOn(process, 'execPath', 'get').mockReturnValue(fakeExecPath); |
| 605 | |
| 606 | const error = yield* runUpgrade( |
| 607 | [['DEBUG_OVERRIDE_UPGRADE_TARGET', sourceBinaryPath]], |
| 608 | { tag: newReleaseTag }, |
| 609 | failRenameTo(fakeExecPath) |
| 610 | ); |
| 611 | |
| 612 | expect(error).toBeInstanceOf(UpgradeBinaryError); |
| 613 | expect(error.message).toBe( |
| 614 | `Failed to replace binary: Failed to replace file at ${fakeExecPath}` |
| 615 | ); |
| 616 | expect(error.cause).toBeInstanceOf(PlatformError.PlatformError); |
| 617 | expect((error.cause as PlatformError.PlatformError).reason).toBeInstanceOf( |
| 618 | PlatformError.SystemError |
| 619 | ); |
| 620 | expect(yield* fs.readFileString(fakeExecPath)).toBe('old-binary'); |
| 621 | expect(yield* fs.readFileString(observedCompanionPath)).toBe('new-support-file'); |
| 622 | expect(yield* fs.readFileString(releaseTagPath)).toBe(`${newReleaseTag}\n`); |
| 623 | const replacedReleaseTagInfo = yield* fs.stat(releaseTagPath); |
| 624 | expect(Option.getOrThrow(replacedReleaseTagInfo.ino)).not.toBe( |
| 625 | Option.getOrThrow(originalReleaseTagInfo.ino) |
| 626 | ); |
| 627 | }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks)); |
| 628 | }); |
| 629 | |
| 630 | it.effect('preflights every companion before changing installed files', () => { |
| 631 | vi.stubGlobal('Bun', { which: vi.fn(() => null) }); |
| 632 | |
| 633 | return Effect.gen(function* () { |
| 634 | const fs = yield* FileSystem.FileSystem; |
| 635 | const path = yield* Path.Path; |
| 636 | const installDir = yield* fs.makeTempDirectoryScoped({ |
| 637 | prefix: 'composio-incomplete-upgrade-target-', |
| 638 | }); |
| 639 | const sourceDir = yield* fs.makeTempDirectoryScoped({ |
| 640 | prefix: 'composio-incomplete-upgrade-source-', |
| 641 | }); |
| 642 | const fakeExecPath = path.join(installDir, 'composio'); |
| 643 | const sourceBinaryPath = path.join(sourceDir, 'composio'); |
| 644 | const releaseTagPath = path.join(installDir, RUN_COMPANION_RELEASE_TAG_FILENAME); |
| 645 | yield* fs.writeFileString(fakeExecPath, 'old-binary'); |
| 646 | yield* fs.writeFileString(sourceBinaryPath, 'new-binary'); |
| 647 | yield* fs.writeFileString(releaseTagPath, '@composio/cli@0.2.31\n'); |
| 648 | |
| 649 | const companionRelativePaths = |
| 650 | yield* collectExpectedRunCompanionAssetRelativePaths(sourceDir); |
| 651 | const missingRelativePath = companionRelativePaths.at(-1)!; |
| 652 | yield* writeCompanionFiles( |
| 653 | sourceDir, |
| 654 | companionRelativePaths.slice(0, -1), |
| 655 | 'new-support-file' |
| 656 | ); |
| 657 | const observedCompanionPath = path.join(installDir, companionRelativePaths[0]!); |
| 658 | yield* fs.makeDirectory(path.dirname(observedCompanionPath), { recursive: true }); |
| 659 | yield* fs.writeFileString(observedCompanionPath, 'old-support-file'); |
| 660 | |
| 661 | vi.spyOn(process, 'execPath', 'get').mockReturnValue(fakeExecPath); |
| 662 | |
| 663 | const error = yield* runUpgrade([['DEBUG_OVERRIDE_UPGRADE_TARGET', sourceBinaryPath]], { |
| 664 | tag: '@composio/cli@0.3.0', |
| 665 | }); |
| 666 | |
| 667 | expect(error).toBeInstanceOf(UpgradeBinaryError); |
| 668 | expect(error.message).toBe('Downloaded binary package is incomplete'); |
| 669 | expect(String(error.cause)).toContain(missingRelativePath); |
| 670 | expect(yield* fs.readFileString(fakeExecPath)).toBe('old-binary'); |
| 671 | expect(yield* fs.readFileString(observedCompanionPath)).toBe('old-support-file'); |
| 672 | expect(yield* fs.readFileString(releaseTagPath)).toBe('@composio/cli@0.2.31\n'); |
| 673 | }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks)); |
| 674 | }); |
| 675 | |
| 676 | it.effect('replaces the bundle target without replacing its entry-point symlink', () => { |
| 677 | vi.stubGlobal('Bun', { which: vi.fn(() => null) }); |
| 678 | |
| 679 | return Effect.gen(function* () { |
| 680 | const fs = yield* FileSystem.FileSystem; |
| 681 | const path = yield* Path.Path; |
| 682 | const installDir = yield* fs.makeTempDirectoryScoped({ |
| 683 | prefix: 'composio-symlink-upgrade-target-', |
| 684 | }); |
| 685 | const binDir = yield* fs.makeTempDirectoryScoped({ |
| 686 | prefix: 'composio-symlink-upgrade-entry-', |
| 687 | }); |
| 688 | const sourceDir = yield* fs.makeTempDirectoryScoped({ |
| 689 | prefix: 'composio-symlink-upgrade-source-', |
| 690 | }); |
| 691 | const installedBinaryPath = path.join(installDir, 'composio'); |
| 692 | const entryPointPath = path.join(binDir, 'composio'); |
| 693 | const sourceBinaryPath = path.join(sourceDir, 'composio'); |
| 694 | |
| 695 | yield* fs.writeFileString(installedBinaryPath, 'old-binary'); |
| 696 | yield* fs.writeFileString(sourceBinaryPath, 'new-binary'); |
| 697 | yield* fs.symlink(installedBinaryPath, entryPointPath); |
| 698 | const originalLinkTarget = yield* fs.readLink(entryPointPath); |
| 699 | vi.spyOn(process, 'execPath', 'get').mockReturnValue(installedBinaryPath); |
| 700 | |
| 701 | const companionRelativePaths = |
| 702 | yield* collectExpectedRunCompanionAssetRelativePaths(sourceDir); |
| 703 | yield* writeCompanionFiles(sourceDir, companionRelativePaths, 'support-file'); |
| 704 | |
| 705 | const result = yield* runUpgradeSuccess([ |
| 706 | ['DEBUG_OVERRIDE_UPGRADE_TARGET', sourceBinaryPath], |
| 707 | ]); |
| 708 | |
| 709 | expect(result).toBeUndefined(); |
| 710 | expect(yield* fs.readFileString(installedBinaryPath)).toBe('new-binary'); |
| 711 | expect(yield* fs.readLink(entryPointPath)).toBe(originalLinkTarget); |
| 712 | expect(yield* fs.exists(entryPointPath)).toBe(true); |
| 713 | }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks)); |
| 714 | }); |
| 715 | |
| 716 | it.effect('uses the installed beta release tag when comparing beta updates', () => { |
| 717 | vi.stubGlobal('Bun', { which: vi.fn(() => null) }); |
| 718 | |
| 719 | return Effect.gen(function* () { |
| 720 | const fs = yield* FileSystem.FileSystem; |
| 721 | const path = yield* Path.Path; |
| 722 | const installDir = yield* fs.makeTempDirectoryScoped({ |
| 723 | prefix: 'composio-beta-upgrade-', |
| 724 | }); |
| 725 | const fakeExecPath = path.join(installDir, 'composio'); |
| 726 | yield* fs.writeFileString( |
| 727 | path.join(installDir, 'release-tag.txt'), |
| 728 | '@composio/cli@0.2.17-beta.1\n' |
| 729 | ); |
| 730 | vi.spyOn(process, 'execPath', 'get').mockReturnValue(fakeExecPath); |
| 731 | |
| 732 | const apiBaseUrl = yield* scopedHttpServer((_req, res) => { |
| 733 | res.writeHead(200, { 'content-type': 'application/json' }); |
| 734 | res.end( |
| 735 | JSON.stringify([ |
| 736 | { |
| 737 | tag_name: '@composio/cli@0.2.17-beta.3', |
| 738 | draft: false, |
| 739 | prerelease: true, |
| 740 | assets: [ |
| 741 | { |
| 742 | name: 'composio-darwin-aarch64.zip', |
| 743 | browser_download_url: 'http://127.0.0.1/beta-3.zip', |
| 744 | }, |
| 745 | ], |
| 746 | }, |
| 747 | ]) |
| 748 | ); |
| 749 | }); |
| 750 | |
| 751 | const error = yield* runUpgrade( |
| 752 | [ |
| 753 | ['COMPOSIO_GITHUB_API_BASE_URL', apiBaseUrl], |
| 754 | ['COMPOSIO_GITHUB_OWNER', 'test-owner'], |
| 755 | ['COMPOSIO_GITHUB_REPO', 'test-repo'], |
| 756 | ], |
| 757 | { prerelease: true } |
| 758 | ); |
| 759 | |
| 760 | expect(error).toBeInstanceOf(UpgradeBinaryError); |
| 761 | expect(error.message).toBe('Failed to download binary: composio-darwin-aarch64.zip'); |
| 762 | expect(String(error.cause)).toContain('beta-3.zip'); |
| 763 | }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks)); |
| 764 | }); |
| 765 | }); |
| 766 | |
| 767 | describe('UpgradeBinary with a release manifest', () => { |
| 768 | const LIVE_COMPANIONS = [ |
| 769 | 'generation-runtime.mjs', |
| 770 | 'run-helpers-runtime.mjs', |
| 771 | 'services/generation-runtime.mjs', |
| 772 | 'services/run-helpers-runtime.mjs', |
| 773 | ]; |
| 774 | // An archive from a release that dropped the generation runtime. |
| 775 | const RUN_HELPERS_ONLY = ['run-helpers-runtime.mjs', 'services/run-helpers-runtime.mjs']; |
| 776 | |
| 777 | const manifestJson = (fields: Record<string, unknown> = {}) => |
| 778 | `${JSON.stringify( |
| 779 | { schemaVersion: 1, minimumUpgradeFrom: '0.1.26', companions: RUN_HELPERS_ONLY, ...fields }, |
| 780 | null, |
| 781 | 2 |
| 782 | )}\n`; |
| 783 | |
| 784 | const setUpLocalUpgrade = (installedTag: string, archiveFiles: ReadonlyArray<string>) => |
| 785 | Effect.gen(function* () { |
| 786 | const fs = yield* FileSystem.FileSystem; |
| 787 | const path = yield* Path.Path; |
| 788 | const installDir = yield* fs.makeTempDirectoryScoped({ |
| 789 | prefix: 'composio-manifest-install-', |
| 790 | }); |
| 791 | const sourceDir = yield* fs.makeTempDirectoryScoped({ prefix: 'composio-manifest-source-' }); |
| 792 | const execPath = path.join(installDir, 'composio'); |
| 793 | const sourceBinaryPath = path.join(sourceDir, 'composio'); |
| 794 | yield* fs.writeFileString(execPath, 'old-binary'); |
| 795 | yield* fs.writeFileString(sourceBinaryPath, 'new-binary'); |
| 796 | yield* fs.writeFileString( |
| 797 | path.join(installDir, RUN_COMPANION_RELEASE_TAG_FILENAME), |
| 798 | `${installedTag}\n` |
| 799 | ); |
| 800 | yield* writeCompanionFiles(installDir, LIVE_COMPANIONS, 'old-support'); |
| 801 | yield* writeCompanionFiles(sourceDir, archiveFiles, 'new-support'); |
| 802 | vi.spyOn(process, 'execPath', 'get').mockReturnValue(execPath); |
| 803 | |
| 804 | const installedFiles = Effect.gen(function* () { |
| 805 | const entries = yield* fs.readDirectory(installDir, { recursive: true }); |
| 806 | const files = yield* Effect.filter(entries, entry => |
| 807 | Effect.map(fs.stat(path.join(installDir, entry)), info => info.type === 'File') |
| 808 | ); |
| 809 | return files.map(entry => entry.replaceAll(path.sep, '/')).sort(); |
| 810 | }); |
| 811 | const readInstalled = (relativePath: string) => |
| 812 | fs.readFileString(path.join(installDir, relativePath)); |
| 813 | const writeSourceManifest = (contents: string) => |
| 814 | fs.writeFileString(path.join(sourceDir, RELEASE_MANIFEST_FILENAME), contents); |
| 815 | const upgradeConfig: Array<[string, string]> = [ |
| 816 | ['DEBUG_OVERRIDE_UPGRADE_TARGET', sourceBinaryPath], |
| 817 | ]; |
| 818 | |
| 819 | return { |
| 820 | installDir, |
| 821 | upgradeConfig, |
| 822 | installedFiles, |
| 823 | readInstalled, |
| 824 | writeSourceManifest, |
| 825 | }; |
| 826 | }); |
| 827 | |
| 828 | const UNCHANGED_INSTALL = [ |
| 829 | RUN_COMPANION_RELEASE_TAG_FILENAME, |
| 830 | 'composio', |
| 831 | ...LIVE_COMPANIONS, |
| 832 | ].sort(); |
| 833 | |
| 834 | it.effect('leaves companions the archive does not list in place when it downgrades', () => { |
| 835 | vi.stubGlobal('Bun', { which: vi.fn(() => null) }); |
| 836 | |
| 837 | return Effect.gen(function* () { |
| 838 | const install = yield* setUpLocalUpgrade('@composio/cli@0.6.0', RUN_HELPERS_ONLY); |
| 839 | yield* install.writeSourceManifest(manifestJson()); |
| 840 | |
| 841 | yield* runUpgradeSuccess(install.upgradeConfig, { tag: '@composio/cli@0.5.0' }); |
| 842 | |
| 843 | expect(yield* install.installedFiles).toEqual( |
| 844 | [...UNCHANGED_INSTALL, RELEASE_MANIFEST_FILENAME].sort() |
| 845 | ); |
| 846 | expect(yield* install.readInstalled('composio')).toBe('new-binary'); |
| 847 | expect(yield* install.readInstalled('services/run-helpers-runtime.mjs')).toBe('new-support'); |
| 848 | expect(yield* install.readInstalled('services/generation-runtime.mjs')).toBe('old-support'); |
| 849 | expect(yield* install.readInstalled(RELEASE_MANIFEST_FILENAME)).toBe(manifestJson()); |
| 850 | expect(yield* install.readInstalled(RUN_COMPANION_RELEASE_TAG_FILENAME)).toBe( |
| 851 | '@composio/cli@0.5.0\n' |
| 852 | ); |
| 853 | }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks)); |
| 854 | }); |
| 855 | |
| 856 | it.effect('refuses an archive whose minimum version is above the running one', () => { |
| 857 | vi.stubGlobal('Bun', { which: vi.fn(() => null) }); |
| 858 | |
| 859 | return Effect.gen(function* () { |
| 860 | const install = yield* setUpLocalUpgrade('@composio/cli@0.4.0', LIVE_COMPANIONS); |
| 861 | yield* install.writeSourceManifest( |
| 862 | manifestJson({ minimumUpgradeFrom: '0.5.0', companions: LIVE_COMPANIONS }) |
| 863 | ); |
| 864 | |
| 865 | const error = yield* runUpgrade(install.upgradeConfig, { tag: '@composio/cli@0.9.0' }); |
| 866 | |
| 867 | expect(error).toBeInstanceOf(UpgradeBelowMinimumError); |
| 868 | expect(error).toMatchObject({ currentVersion: '0.4.0', minimumUpgradeFrom: '0.5.0' }); |
| 869 | expect(error.message).toContain('curl -fsSL https://composio.dev/install | sh'); |
| 870 | expect(yield* install.installedFiles).toEqual(UNCHANGED_INSTALL); |
| 871 | expect(yield* install.readInstalled('composio')).toBe('old-binary'); |
| 872 | expect(yield* install.readInstalled('run-helpers-runtime.mjs')).toBe('old-support'); |
| 873 | expect(yield* install.readInstalled(RUN_COMPANION_RELEASE_TAG_FILENAME)).toBe( |
| 874 | '@composio/cli@0.4.0\n' |
| 875 | ); |
| 876 | }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks)); |
| 877 | }); |
| 878 | |
| 879 | it.effect('does not check the minimum version from a source build with no release tag', () => { |
| 880 | vi.stubGlobal('Bun', { which: vi.fn(() => null) }); |
| 881 | |
| 882 | return Effect.gen(function* () { |
| 883 | const fs = yield* FileSystem.FileSystem; |
| 884 | const path = yield* Path.Path; |
| 885 | const install = yield* setUpLocalUpgrade('@composio/cli@0.4.0', RUN_HELPERS_ONLY); |
| 886 | yield* fs.remove(path.join(install.installDir, RUN_COMPANION_RELEASE_TAG_FILENAME)); |
| 887 | yield* install.writeSourceManifest(manifestJson({ minimumUpgradeFrom: '99.0.0' })); |
| 888 | |
| 889 | yield* runUpgradeSuccess(install.upgradeConfig); |
| 890 | |
| 891 | expect(yield* install.readInstalled('composio')).toBe('new-binary'); |
| 892 | }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks)); |
| 893 | }); |
| 894 | |
| 895 | it.effect('fails without touching the install when a listed companion is missing', () => { |
| 896 | vi.stubGlobal('Bun', { which: vi.fn(() => null) }); |
| 897 | |
| 898 | return Effect.gen(function* () { |
| 899 | const install = yield* setUpLocalUpgrade('@composio/cli@0.5.0', ['run-helpers-runtime.mjs']); |
| 900 | yield* install.writeSourceManifest(manifestJson()); |
| 901 | |
| 902 | const error = yield* runUpgrade(install.upgradeConfig, { tag: '@composio/cli@0.6.0' }); |
| 903 | |
| 904 | expect(error).toBeInstanceOf(UpgradeBinaryError); |
| 905 | expect(error.message).toBe('Downloaded binary package is incomplete'); |
| 906 | expect(String((error as UpgradeBinaryError).cause)).toContain( |
| 907 | 'services/run-helpers-runtime.mjs' |
| 908 | ); |
| 909 | expect(yield* install.installedFiles).toEqual(UNCHANGED_INSTALL); |
| 910 | expect(yield* install.readInstalled('composio')).toBe('old-binary'); |
| 911 | expect(yield* install.readInstalled('run-helpers-runtime.mjs')).toBe('old-support'); |
| 912 | }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks)); |
| 913 | }); |
| 914 | |
| 915 | it.effect.each([ |
| 916 | { problem: 'a path that leaves the package', fields: { companions: ['../x'] } }, |
| 917 | { problem: 'an absolute path', fields: { companions: ['/etc/passwd'] } }, |
| 918 | { problem: 'an unknown schema version', fields: { schemaVersion: 2 } }, |
| 919 | { problem: 'a minimum that is not a version', fields: { minimumUpgradeFrom: 'latest' } }, |
| 920 | { problem: 'no companions', fields: { companions: [] } }, |
| 921 | ])('fails without touching the install when the manifest has $problem', ({ fields }) => { |
| 922 | vi.stubGlobal('Bun', { which: vi.fn(() => null) }); |
| 923 | |
| 924 | return Effect.gen(function* () { |
| 925 | const install = yield* setUpLocalUpgrade('@composio/cli@0.5.0', LIVE_COMPANIONS); |
| 926 | yield* install.writeSourceManifest(manifestJson(fields)); |
| 927 | |
| 928 | const error = yield* runUpgrade(install.upgradeConfig, { tag: '@composio/cli@0.6.0' }); |
| 929 | |
| 930 | expect(error).toBeInstanceOf(UpgradeBinaryError); |
| 931 | expect(error.message).toContain('Downloaded binary package is invalid'); |
| 932 | expect(yield* install.installedFiles).toEqual(UNCHANGED_INSTALL); |
| 933 | expect(yield* install.readInstalled('composio')).toBe('old-binary'); |
| 934 | }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks)); |
| 935 | }); |
| 936 | |
| 937 | it.effect('removes the installed manifest when the archive has none', () => { |
| 938 | vi.stubGlobal('Bun', { which: vi.fn(() => null) }); |
| 939 | |
| 940 | return Effect.gen(function* () { |
| 941 | const fs = yield* FileSystem.FileSystem; |
| 942 | const path = yield* Path.Path; |
| 943 | const install = yield* setUpLocalUpgrade('@composio/cli@0.6.0', LIVE_COMPANIONS); |
| 944 | yield* fs.writeFileString( |
| 945 | path.join(install.installDir, RELEASE_MANIFEST_FILENAME), |
| 946 | manifestJson() |
| 947 | ); |
| 948 | yield* fs.writeFileString(path.join(install.installDir, 'run-subagent-shared.mjs'), ''); |
| 949 | |
| 950 | yield* runUpgradeSuccess(install.upgradeConfig, { tag: '@composio/cli@0.4.2' }); |
| 951 | |
| 952 | expect(yield* install.installedFiles).toEqual( |
| 953 | [...UNCHANGED_INSTALL, 'run-subagent-shared.mjs'].sort() |
| 954 | ); |
| 955 | expect(yield* install.readInstalled('composio')).toBe('new-binary'); |
| 956 | expect(yield* install.readInstalled('generation-runtime.mjs')).toBe('new-support'); |
| 957 | }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks)); |
| 958 | }); |
| 959 | }); |
| 960 | |
| 961 | describe('UpgradeBinary update-check cache', () => { |
| 962 | // The cache that misled the user: written before 0.5.0 was released. |
| 963 | const staleState = { lastChecked: '2026-10-21T14:31:56.646Z', latestVersion: '0.4.5' }; |
| 964 | |
| 965 | const writeStaleState = () => { |
| 966 | mkdirSync(updateCheckDir, { recursive: true }); |
| 967 | writeFileSync(updateCheckFile, JSON.stringify(staleState)); |
| 968 | }; |
| 969 | |
| 970 | const readCachedState = (): unknown => JSON.parse(readFileSync(updateCheckFile, 'utf8')); |
| 971 | |
| 972 | const release = (version: string) => ({ |
| 973 | tag_name: `@composio/cli@${version}`, |
| 974 | draft: false, |
| 975 | prerelease: version.includes('-'), |
| 976 | assets: [ |
| 977 | { |
| 978 | name: 'composio-darwin-aarch64.zip', |
| 979 | browser_download_url: `http://127.0.0.1/${version}.zip`, |
| 980 | }, |
| 981 | ], |
| 982 | }); |
| 983 | |
| 984 | /** Serves the release list, plus any single release by tag, and installs `currentTag`. */ |
| 985 | const setUpReleases = (currentTag: string) => |
| 986 | Effect.gen(function* () { |
| 987 | const fs = yield* FileSystem.FileSystem; |
| 988 | const path = yield* Path.Path; |
| 989 | const installDir = yield* fs.makeTempDirectoryScoped({ prefix: 'composio-upgrade-cache-' }); |
| 990 | yield* fs.writeFileString(path.join(installDir, 'release-tag.txt'), `${currentTag}\n`); |
| 991 | vi.spyOn(process, 'execPath', 'get').mockReturnValue(path.join(installDir, 'composio')); |
| 992 | |
| 993 | const apiBaseUrl = yield* scopedHttpServer((req, res) => { |
| 994 | res.writeHead(200, { 'content-type': 'application/json' }); |
| 995 | const taggedPrefix = '/repos/test-owner/test-repo/releases/tags/'; |
| 996 | res.end( |
| 997 | JSON.stringify( |
| 998 | req.url?.startsWith(taggedPrefix) |
| 999 | ? release(decodeURIComponent(req.url.slice(taggedPrefix.length)).split('@').at(-1)!) |
| 1000 | : [release('0.5.1-beta.1'), release('0.5.0'), release('0.4.5'), release('0.4.2')] |
| 1001 | ) |
| 1002 | ); |
| 1003 | }); |
| 1004 | |
| 1005 | return [ |
| 1006 | ['COMPOSIO_GITHUB_API_BASE_URL', apiBaseUrl], |
| 1007 | ['COMPOSIO_GITHUB_OWNER', 'test-owner'], |
| 1008 | ['COMPOSIO_GITHUB_REPO', 'test-repo'], |
| 1009 | ] as Array<[string, string]>; |
| 1010 | }); |
| 1011 | |
| 1012 | it.effect('records the live-resolved latest stable release', () => { |
| 1013 | vi.stubGlobal('Bun', { which: vi.fn(() => null) }); |
| 1014 | // Only Date is faked: the upgrade awaits a real local HTTP server. |
| 1015 | vi.useFakeTimers({ toFake: ['Date'] }); |
| 1016 | vi.setSystemTime(new Date('2026-10-21T16:20:00.000Z')); |
| 1017 | |
| 1018 | return Effect.gen(function* () { |
| 1019 | writeStaleState(); |
| 1020 | const config = yield* setUpReleases('@composio/cli@0.5.0'); |
| 1021 | |
| 1022 | const result = yield* runUpgradeSuccess(config); |
| 1023 | |
| 1024 | expect(result).toBeUndefined(); |
| 1025 | expect(readCachedState()).toEqual({ |
| 1026 | lastChecked: '2026-10-21T16:20:00.000Z', |
| 1027 | latestVersion: '0.5.0', |
| 1028 | }); |
| 1029 | }).pipe( |
| 1030 | Effect.provide(TestPlatform), |
| 1031 | Effect.ensuring(Effect.sync(() => vi.useRealTimers())), |
| 1032 | Effect.ensuring(restoreStubsAndMocks) |
| 1033 | ); |
| 1034 | }); |
| 1035 | |
| 1036 | it.effect.each([ |
| 1037 | { target: 'a version argument', options: { tag: '@composio/cli@0.4.2' }, env: [] }, |
| 1038 | { |
| 1039 | target: 'COMPOSIO_GITHUB_TAG', |
| 1040 | options: {}, |
| 1041 | env: [['COMPOSIO_GITHUB_TAG', '@composio/cli@0.4.2']] as Array<[string, string]>, |
| 1042 | }, |
| 1043 | { target: '--beta', options: { prerelease: true }, env: [] }, |
| 1044 | ])('leaves the cache alone when $target picks the target', ({ options, env }) => { |
| 1045 | vi.stubGlobal('Bun', { which: vi.fn(() => null) }); |
| 1046 | |
| 1047 | return Effect.gen(function* () { |
| 1048 | writeStaleState(); |
| 1049 | const config = yield* setUpReleases('@composio/cli@0.4.0'); |
| 1050 | |
| 1051 | // The download is unreachable, so only the release lookup could have touched the cache. |
| 1052 | const exit = yield* Effect.exit(makeUpgradeEffect([...config, ...env], options)); |
| 1053 | |
| 1054 | expect(Exit.isFailure(exit)).toBe(true); |
| 1055 | expect(readCachedState()).toEqual(staleState); |
| 1056 | }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks)); |
| 1057 | }); |
| 1058 | |
| 1059 | it.effect('still upgrades when the cache cannot be written', () => { |
| 1060 | vi.stubGlobal('Bun', { which: vi.fn(() => null) }); |
| 1061 | |
| 1062 | return Effect.gen(function* () { |
| 1063 | // A file where the cache directory should be makes every cache write fail. |
| 1064 | writeFileSync(updateCheckDir, 'not a directory'); |
| 1065 | const config = yield* setUpReleases('@composio/cli@0.5.0'); |
| 1066 | |
| 1067 | const result = yield* runUpgradeSuccess(config); |
| 1068 | |
| 1069 | expect(result).toBeUndefined(); |
| 1070 | expect(readFileSync(updateCheckDir, 'utf8')).toBe('not a directory'); |
| 1071 | }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks)); |
| 1072 | }); |
| 1073 | }); |
| 1074 | |
| 1075 | describe('formatDownloadProgress', () => { |
| 1076 | it('reports percent and both sizes when the total is known', () => { |
| 1077 | expect(formatDownloadProgress({ receivedBytes: 142_000_000, totalBytes: 338_000_000 })).toBe( |
| 1078 | 'Downloading... 42% (142.0 MB / 338.0 MB)' |
| 1079 | ); |
| 1080 | }); |
| 1081 | |
| 1082 | it('reports bytes alone when the server never sent a size', () => { |
| 1083 | expect(formatDownloadProgress({ receivedBytes: 12_500_000, totalBytes: undefined })).toBe( |
| 1084 | 'Downloading... 12.5 MB' |
| 1085 | ); |
| 1086 | }); |
| 1087 | |
| 1088 | it('treats a zero total as unknown rather than dividing by it', () => { |
| 1089 | expect(formatDownloadProgress({ receivedBytes: 1_000_000, totalBytes: 0 })).toBe( |
| 1090 | 'Downloading... 1.0 MB' |
| 1091 | ); |
| 1092 | }); |
| 1093 | |
| 1094 | it('never exceeds 100% when more bytes arrive than announced', () => { |
| 1095 | expect(formatDownloadProgress({ receivedBytes: 400_000_000, totalBytes: 338_000_000 })).toBe( |
| 1096 | 'Downloading... 100% (400.0 MB / 338.0 MB)' |
| 1097 | ); |
| 1098 | }); |
| 1099 | |
| 1100 | it('formats megabytes to one decimal place', () => { |
| 1101 | expect(formatMegabytes(0)).toBe('0.0 MB'); |
| 1102 | expect(formatMegabytes(338_027_339)).toBe('338.0 MB'); |
| 1103 | }); |
| 1104 | }); |