@devries-cameron20 / composio Mirror · read-only
Fork 0

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

next
ps1 266 lines 15.7 KB
Raw
1 # Checks a Windows x64 CLI archive on a real Windows machine: the documented installer
2 # one-liner, the native binary, companion modules, self-upgrade, and repair.
3 # A local server stands in for GitHub and serves install.ps1 plus the archive under test.
4 param(
5 [Parameter(Mandatory)][string]$ArchivePath,
6 [Parameter(Mandatory)][string]$ExpectedVersion
7 )
8 $ErrorActionPreference = 'Stop'
9 $ArchivePath = (Resolve-Path -LiteralPath $ArchivePath).Path
10 $testRoot = Join-Path ([IO.Path]::GetTempPath()) "composio windows $([Guid]::NewGuid())"
11 $envNames = @(
12 'BUN_BE_BUN', 'COMPOSIO_CACHE_DIR', 'COMPOSIO_DISABLE_TELEMETRY', 'COMPOSIO_GITHUB_API_BASE_URL', 'COMPOSIO_INSTALL_DIR',
13 'COMPOSIO_INSTALL_SHELL', 'COMPOSIO_INSTALL_VERSION', 'COMPOSIO_USER_API_KEY', 'COMPOSIO_WINDOWS_SMOKE_ARCHIVE',
14 'COMPOSIO_WINDOWS_SMOKE_INSTALLER', 'COMPOSIO_WINDOWS_SMOKE_PORT', 'COMPOSIO_WINDOWS_SMOKE_SKILL', 'COMPOSIO_WINDOWS_SMOKE_TAG',
15 'USERPROFILE'
16 )
17 $savedEnv = @{}
18 foreach ($name in $envNames) { $savedEnv[$name] = [Environment]::GetEnvironmentVariable($name) }
19 $savedUserPath = [Environment]::GetEnvironmentVariable('Path', 'User')
20 $server = $null
21
22 function Assert([bool]$Condition, [string]$Message) {
23 if (-not $Condition) { throw $Message }
24 }
25
26 # Runs the documented one-liner in a fresh shell. COMPOSIO_INSTALL_* variables configure it.
27 function Invoke-Installer([string]$Shell = 'powershell', [string]$Then = '') {
28 & $Shell -NoProfile -Command "irm $baseUrl/install.ps1 | iex; $Then"
29 }
30
31 function Invoke-Cli([string[]]$Arguments) {
32 $output = & $binary @Arguments
33 Assert ($LASTEXITCODE -eq 0) "CLI failed ($LASTEXITCODE): $Arguments"
34 return ($output -join "`n").Trim()
35 }
36
37 function Get-Sha256([string]$Path) {
38 return (Get-FileHash -LiteralPath $Path -Algorithm SHA256).Hash
39 }
40
41 New-Item -ItemType Directory -Path $testRoot | Out-Null
42 try {
43 # The compiled CLI doubles as a Bun runtime (BUN_BE_BUN), so it can serve its own release.
44 # The first path segment of the API base URL selects a scenario:
45 # /stable lists a stable release behind a newer beta, for the default (latest) install
46 # /corrupt publishes a checksum that does not match the archive
47 # /skill adds the CLI skill bundle to the release
48 $source = Join-Path $testRoot 'source'
49 Expand-Archive -LiteralPath $ArchivePath -DestinationPath $source
50 $serverFile = Join-Path $testRoot 'release-server.mjs'
51 $portFile = Join-Path $testRoot 'port.txt'
52 @'
53 const archive = Bun.file(process.env.COMPOSIO_WINDOWS_SMOKE_ARCHIVE);
54 const hash = new Bun.CryptoHasher('sha256').update(await archive.arrayBuffer()).digest('hex');
55 const tag = process.env.COMPOSIO_WINDOWS_SMOKE_TAG;
56 const text = body => new Response(body, {headers: {'content-type': 'text/plain; charset=utf-8'}});
57 const server = Bun.serve({
58 hostname: '127.0.0.1', port: 0,
59 async fetch(request) {
60 const {origin, pathname} = new URL(request.url);
61 if (pathname === '/install.ps1') return text(await Bun.file(process.env.COMPOSIO_WINDOWS_SMOKE_INSTALLER).text());
62 if (pathname === '/cli.zip') return new Response(archive);
63 if (pathname === '/skill.zip') return new Response(Bun.file(process.env.COMPOSIO_WINDOWS_SMOKE_SKILL));
64 if (pathname === '/checksums.txt') return text(`${hash} composio-windows-x64.zip\n`);
65 if (pathname === '/corrupt-checksums.txt') return text(`${'0'.repeat(64)} composio-windows-x64.zip\n`);
66 const release = (tagName, checksums = 'checksums.txt') => ({
67 tag_name: tagName, name: tagName, draft: false, prerelease: tagName.includes('-beta.'),
68 assets: [
69 {name: 'composio-windows-x64.zip', size: archive.size, browser_download_url: `${origin}/cli.zip`},
70 {name: 'checksums.txt', browser_download_url: `${origin}/${checksums}`}
71 ]});
72 if (pathname.startsWith('/corrupt/')) return Response.json(release(tag, 'corrupt-checksums.txt'));
73 if (pathname.startsWith('/skill/')) {
74 const withSkill = release(tag);
75 withSkill.assets.push({name: 'composio-skill.zip', browser_download_url: `${origin}/skill.zip`});
76 return Response.json(withSkill);
77 }
78 if (pathname.includes('/releases/tags/')) return Response.json(release(tag));
79 if (pathname.startsWith('/stable/')) return Response.json([release('@composio/cli@9.9.9-beta.1'), release('@composio/cli@1.2.3')]);
80 if (pathname.endsWith('/releases')) return Response.json([release(tag)]);
81 return new Response('not found', {status: 404});
82 }
83 });
84 await Bun.write(process.env.COMPOSIO_WINDOWS_SMOKE_PORT, String(server.port));
85 '@ | Set-Content -LiteralPath $serverFile
86 $env:COMPOSIO_WINDOWS_SMOKE_ARCHIVE = $ArchivePath
87 $env:COMPOSIO_WINDOWS_SMOKE_INSTALLER = (Resolve-Path -LiteralPath "$PSScriptRoot/../../../install.ps1").Path
88 $env:COMPOSIO_WINDOWS_SMOKE_PORT = $portFile
89 $skillSource = Join-Path $testRoot 'skill/composio-cli'
90 New-Item -ItemType Directory -Path $skillSource -Force | Out-Null
91 Set-Content -LiteralPath (Join-Path $skillSource 'SKILL.md') -Value '# fixture skill'
92 $env:COMPOSIO_WINDOWS_SMOKE_SKILL = Join-Path $testRoot 'composio-skill.zip'
93 Compress-Archive -Path $skillSource -DestinationPath $env:COMPOSIO_WINDOWS_SMOKE_SKILL
94 $env:COMPOSIO_WINDOWS_SMOKE_TAG = "@composio/cli@$ExpectedVersion"
95 $env:BUN_BE_BUN = '1'
96 $server = Start-Process -FilePath (Join-Path $source 'composio-windows-x64/composio.exe') -ArgumentList @('"' + $serverFile + '"') -PassThru -RedirectStandardError (Join-Path $testRoot 'server.err')
97 Remove-Item Env:BUN_BE_BUN
98 for ($attempt = 0; $attempt -lt 100 -and -not (Test-Path -LiteralPath $portFile); $attempt++) {
99 if ($server.HasExited) { throw "Release fixture server failed: $(Get-Content -LiteralPath (Join-Path $testRoot 'server.err') -Raw)" }
100 Start-Sleep -Milliseconds 100
101 }
102 Assert (Test-Path -LiteralPath $portFile) 'Release fixture server did not start.'
103 $baseUrl = "http://127.0.0.1:$(Get-Content -LiteralPath $portFile)"
104
105 $installDir = Join-Path $testRoot 'installed CLI'
106 $binary = Join-Path $installDir 'composio.exe'
107 $env:COMPOSIO_GITHUB_API_BASE_URL = $baseUrl
108 $env:COMPOSIO_CACHE_DIR = Join-Path $testRoot 'isolated config'
109 $env:COMPOSIO_DISABLE_TELEMETRY = '1'
110 $env:COMPOSIO_USER_API_KEY = 'test-dummy-api-key-for-ci'
111 $env:COMPOSIO_INSTALL_DIR = $installDir
112 $env:COMPOSIO_INSTALL_VERSION = $ExpectedVersion
113 $env:COMPOSIO_INSTALL_SHELL = 'none'
114
115 # Install a pinned version from Windows PowerShell 5.1, into a path with spaces.
116 Invoke-Installer | Out-Host
117 Assert ($LASTEXITCODE -eq 0) 'Windows PowerShell installation failed.'
118 Assert ((Invoke-Cli @('--version')) -eq $ExpectedVersion) 'Unexpected baked CLI version.'
119 Invoke-Cli @('--help') | Out-Host
120 Assert ((Invoke-Cli @('run', 'console.log("windows-inline-ok")')) -eq 'windows-inline-ok') 'Inline companion execution failed.'
121 $scriptFile = Join-Path $testRoot 'script with spaces.ts'
122 Set-Content -LiteralPath $scriptFile -Value 'console.log("windows-file-ok");'
123 Assert ((Invoke-Cli @('run', '--file', $scriptFile)) -eq 'windows-file-ok') 'File companion execution failed.'
124 $expectedBinaryHash = Get-Sha256 $binary
125
126 # Reinstalling keeps user data. 32-bit PowerShell on x64 Windows is a supported host.
127 $userData = Join-Path $installDir 'user_data.json'
128 Set-Content -LiteralPath $userData -Value 'preserve credentials'
129 Invoke-Installer -Shell (Join-Path $env:WINDIR 'SysWOW64/WindowsPowerShell/v1.0/powershell.exe') | Out-Host
130 Assert ($LASTEXITCODE -eq 0) '32-bit PowerShell reinstallation failed.'
131 Assert ((Get-Content -LiteralPath $userData) -eq 'preserve credentials') 'Reinstallation did not preserve user data.'
132 Assert ((Get-Sha256 $binary) -eq $expectedBinaryHash) 'Reinstallation did not restore the executable.'
133
134 # A checksum mismatch aborts before the installation is touched.
135 Set-Content -LiteralPath $binary -Value 'installed before the corrupt download'
136 $env:COMPOSIO_GITHUB_API_BASE_URL = "$baseUrl/corrupt"
137 $output = Invoke-Installer 2>&1 | Out-String
138 Assert ($LASTEXITCODE -ne 0 -and $output -like '*Checksum mismatch*') "Expected a checksum mismatch, got: $output"
139 Assert ((Get-Content -LiteralPath $binary) -eq 'installed before the corrupt download') 'A corrupt download modified the executable.'
140
141 # A remote server must use HTTPS: it supplies both the archive and its checksum.
142 foreach ($untrusted in 'http://example.com', 'https://user:secret@example.com') {
143 $env:COMPOSIO_GITHUB_API_BASE_URL = $untrusted
144 $output = Invoke-Installer 2>&1 | Out-String
145 Assert ($LASTEXITCODE -ne 0 -and $output -like '*Refusing to download*') "Expected $untrusted to be refused, got: $output"
146 }
147
148 # With no version, the installer picks the latest stable release and puts it on PATH,
149 # both for the current session and, exactly once, for the user.
150 $latestDir = Join-Path $testRoot 'latest install'
151 $env:COMPOSIO_GITHUB_API_BASE_URL = "$baseUrl/stable"
152 $env:COMPOSIO_INSTALL_DIR = $latestDir
153 Remove-Item Env:COMPOSIO_INSTALL_VERSION, Env:COMPOSIO_INSTALL_SHELL
154 foreach ($attempt in 1..2) {
155 $output = Invoke-Installer -Then 'composio --version'
156 Assert ($LASTEXITCODE -eq 0) "Default installation failed: $output"
157 Assert (($output -join "`n") -like '*Installed @composio/cli@1.2.3*') 'The installer did not pick the latest stable release.'
158 Assert ($output[-1].Trim() -eq $ExpectedVersion) 'composio was not on PATH after installation.'
159 }
160 $userPathEntries = [Environment]::GetEnvironmentVariable('Path', 'User') -split ';'
161 Assert (@($userPathEntries | Where-Object { $_ -eq $latestDir }).Count -eq 1) 'The User PATH entry is missing or duplicated.'
162
163 $env:COMPOSIO_GITHUB_API_BASE_URL = $baseUrl
164 $env:COMPOSIO_INSTALL_DIR = $installDir
165 $env:COMPOSIO_INSTALL_VERSION = $ExpectedVersion
166 $env:COMPOSIO_INSTALL_SHELL = 'none'
167 Invoke-Installer | Out-Host
168 Assert ($LASTEXITCODE -eq 0) 'Reinstallation after a corrupt download failed.'
169
170 # `composio upgrade` resolves the Windows asset, verifies it, and replaces the executable
171 # that is currently running, keeping the previous one as composio.exe.old.
172 foreach ($upgrade in 1..2) {
173 Invoke-Cli @('upgrade', $ExpectedVersion) | Out-Host
174 Assert ((Invoke-Cli @('--version')) -eq $ExpectedVersion) "CLI failed after upgrade $upgrade."
175 Assert ((Get-Sha256 "$binary.old") -eq $expectedBinaryHash) "Upgrade $upgrade did not replace the running executable."
176 if ($upgrade -eq 1) {
177 # The replaced executable stays locked until its process has fully exited.
178 for ($attempt = 0; $attempt -lt 50; $attempt++) {
179 try { Set-Content -LiteralPath "$binary.old" -Value 'stale backup'; break } catch {
180 if ($attempt -eq 0) {
181 Write-Host 'composio.exe.old is locked. Processes running from the install directory:'
182 Get-Process | Where-Object { $_.Path -like "$installDir*" } | Format-Table Id, ProcessName, StartTime, Path | Out-Host
183 }
184 Start-Sleep -Milliseconds 200
185 }
186 }
187 Write-Host "composio.exe.old became writable after $attempt retries."
188 }
189 }
190
191 # While another process holds composio.exe.old, neither upgrade path may modify the installation.
192 $companionPath = Join-Path $installDir 'run-helpers-runtime.mjs'
193 $tagPath = Join-Path $installDir 'release-tag.txt'
194 $originalCompanion = [IO.File]::ReadAllText($companionPath)
195 $originalTag = [IO.File]::ReadAllText($tagPath)
196 $markerCompanion = $originalCompanion + "`n// retained while backup is locked`n"
197 $markerTag = '@composio/cli@0.0.0-beta.999999'
198 [IO.File]::WriteAllText($companionPath, $markerCompanion)
199 [IO.File]::WriteAllText($tagPath, $markerTag)
200 $lockedBackup = [IO.File]::Open("$binary.old", 'Open', 'Read', 'Read')
201 try {
202 foreach ($action in @({ & $binary upgrade $ExpectedVersion }, { Invoke-Installer })) {
203 & $action 2>&1 | Out-Host
204 Assert ($LASTEXITCODE -ne 0) 'Expected a locked backup to block replacement.'
205 Assert ([IO.File]::ReadAllText($companionPath) -eq $markerCompanion) 'A blocked upgrade modified the support files.'
206 Assert ([IO.File]::ReadAllText($tagPath) -eq $markerTag) 'A blocked upgrade modified the release metadata.'
207 Assert ((Get-Sha256 $binary) -eq $expectedBinaryHash) 'A blocked upgrade modified the executable.'
208 }
209 } finally {
210 $lockedBackup.Dispose()
211 [IO.File]::WriteAllText($companionPath, $originalCompanion)
212 [IO.File]::WriteAllText($tagPath, $originalTag)
213 }
214
215 # A missing companion module is downloaded again on demand.
216 $companion = Join-Path $installDir 'services/generation-runtime.mjs'
217 Remove-Item -LiteralPath $companion
218 Assert ((Invoke-Cli @('run', 'console.log("windows-repair-ok")')) -eq 'windows-repair-ok') 'Companion repair failed.'
219 Assert (Test-Path -LiteralPath $companion) 'Companion was not restored.'
220
221 # The CLI skill is linked into the agent's directory with a junction, which needs no
222 # elevated rights. Installing twice must replace the link without emptying its target.
223 $env:USERPROFILE = Join-Path $testRoot 'home'
224 New-Item -ItemType Directory -Path $env:USERPROFILE | Out-Null
225 $env:COMPOSIO_GITHUB_API_BASE_URL = "$baseUrl/skill"
226 $linkedSkill = Join-Path $env:USERPROFILE '.claude/skills/composio-cli'
227 foreach ($attempt in 1..2) {
228 Invoke-Cli @('setup', 'skill', 'claude') | Out-Host
229 Assert ((Get-Item -LiteralPath $linkedSkill).LinkType -eq 'Junction') 'The skill was not linked with a junction.'
230 Assert (Test-Path -LiteralPath (Join-Path $linkedSkill 'SKILL.md')) 'The linked skill is empty.'
231 }
232
233 # An npm-installed agent CLI is a .cmd shim. Setup must find and run it, here from a
234 # directory with spaces, and get as far as asking for approval.
235 $shimDir = Join-Path $testRoot 'npm global bin'
236 New-Item -ItemType Directory -Path $shimDir | Out-Null
237 @'
238 @echo off
239 if "%~1"=="--version" (echo 9.9.9& exit /b 0)
240 echo %* | findstr /c:"--help" >nul && (echo --json Print JSON& exit /b 0)
241 echo []
242 '@ | Set-Content -LiteralPath (Join-Path $shimDir 'claude.cmd')
243 $pathBeforeShim = $env:Path
244 $env:Path = "$shimDir;$env:Path"
245 try { $output = & $binary setup --target claude 2>&1 | Out-String } finally { $env:Path = $pathBeforeShim }
246 Assert ($output -notlike '*not available on PATH*') "Setup did not find the claude.cmd shim: $output"
247 Assert ($output -like '*--yes*') "Setup did not reach the approval step: $output"
248
249 # Credentials can be kept in Windows Credential Manager. The compiled CLI doubles as the
250 # Bun runtime for the store's roundtrip check.
251 $env:BUN_BE_BUN = '1'
252 try { & $binary (Join-Path $PSScriptRoot '../../../ts/packages/cli-keyring/test/windows-credential.smoke.ts') | Out-Host } finally { Remove-Item Env:BUN_BE_BUN }
253 Assert ($LASTEXITCODE -eq 0) 'The Windows Credential Manager roundtrip failed.'
254
255 # Shell setup has nothing to do on Windows and says so.
256 $output = & $binary install 2>&1 | Out-String
257 Assert ($LASTEXITCODE -ne 0 -and $output -like '*Windows*') "Expected composio install to explain itself, got: $output"
258 Write-Host 'Windows installation, run, repeated upgrade, repair, agent setup, and credential storage passed.'
259 } finally {
260 if ($server -and -not $server.HasExited) { Stop-Process -Id $server.Id -Force; $server.WaitForExit() }
261 foreach ($name in $envNames) { [Environment]::SetEnvironmentVariable($name, $savedEnv[$name]) }
262 [Environment]::SetEnvironmentVariable('Path', $savedUserPath, 'User')
263 Remove-Item -LiteralPath $testRoot -Recurse -Force
264 }
265 # The last native command above fails on purpose; do not let its exit code become the script's.
266 exit 0