Repository navigation
Expand file tree
/
Copy pathtest.ps1
More file actions
231 lines (231 loc) · 15.6 KB
/
Copy pathtest.ps1
File metadata and controls
231 lines (231 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
$ErrorActionPreference = 'Stop'
foreach ($file in Get-ChildItem $PSScriptRoot -Filter '*.ps1') {
$tokens=$null; $errors=$null
[void][Management.Automation.Language.Parser]::ParseFile($file.FullName,[ref]$tokens,[ref]$errors)
if ($errors.Count) { throw ($errors | Out-String) }
}
$engine = (Get-Process -Id $PID).Path
$checks = 0
function Check([string]$File,[string]$Setup,[int]$Code,[string]$Pattern,[string]$Arguments='') {
$ErrorActionPreference = 'Continue'
$path=(Join-Path $PSScriptRoot $File).Replace("'","''")
$codeText = "`$ErrorActionPreference='Stop'; `$LASTEXITCODE=0; $Setup`n& '$path' $Arguments; exit `$LASTEXITCODE"
$encoded = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($codeText))
$output = & $engine -NoProfile -NonInteractive -EncodedCommand $encoded *>&1 | Out-String
if ($LASTEXITCODE -ne $Code -or $output -notmatch $Pattern) { throw "$File expected exit $Code / $Pattern, received $LASTEXITCODE : $output" }
$script:checks++
}
$piSetup = @'
$env:PI_TEST_INSTALLER = 'function Get-PiBinDir { "official-bin" }; $env:PI_VENDOR_ENV="ready"'
$env:PI_TEST_VERSION = '0.87.1'
function Invoke-RestMethod {
if ($args[0] -ne 'https://pi.dev/install.ps1') { throw 'wrong installer URL' }
return $env:PI_TEST_INSTALLER
}
function Join-Path {
param($Path,$ChildPath)
if ($Path -ne 'official-bin' -or $ChildPath -ne 'pi.cmd') { throw 'wrong installed path' }
return 'official-pi'
}
function official-pi {
if ($env:PI_VENDOR_ENV -ne 'ready') { throw 'official environment lost' }
$global:LASTEXITCODE=0
if ($args[0] -eq '--version') { $env:PI_TEST_VERSION }
else { Write-Output ($args -join '|') }
}
function Write-Warning { param($Message) Write-Output $Message }
function npm.cmd {
if ($env:LMM_PI_BIN -ne 'official-pi') { throw 'official Pi path was not forwarded' }
Write-Output ($args -join '|'); $global:LASTEXITCODE=0
}
function pi.cmd { throw 'must not invoke stale pi on PATH' }
'@
Check 'pi.ps1' $piSetup 0 'lmm-pi-provider\|git:github.com/TokenNotIncluded/pi-lmm-provider'
Check 'pi.ps1' ($piSetup+"`n`$env:PI_TEST_INSTALLER='Write-Output cancelled; exit 0'") 0 'cancelled'
Check 'pi.ps1' ($piSetup+"`n`$env:PI_TEST_INSTALLER='Write-Output failed; exit 9'") 9 'failed'
foreach ($version in @('0.88.0', '0.99.2', '1.2.0')) {
Check 'pi.ps1' ($piSetup+"`n`$env:PI_TEST_VERSION='$version'") 0 'lmm-pi-provider\|git:github.com/TokenNotIncluded/pi-lmm-provider'
}
Check 'pi.ps1' ($piSetup+"`nfunction official-pi { if (`$args[0] -ne '--version') { throw 'plugin must not run' }; `$global:LASTEXITCODE=8 }") 8 ''
Check 'pi.ps1' ($piSetup+"`nfunction npm.cmd { Write-Output plugin-failed; `$global:LASTEXITCODE=7 }") 7 'plugin-failed'
Check 'pi.ps1' "function Invoke-RestMethod { throw 'download-failed' }" 1 'download-failed'
$npmFailure = @'
function npm.cmd { Write-Output 'npm failed'; $global:LASTEXITCODE=9 }
function dsh.cmd { throw 'plugin must not run' }
'@
Check 'dsh.ps1' $npmFailure 9 'npm failed'
$npmSuccess = @'
function npm.cmd { Write-Output ($args -join '|'); $global:LASTEXITCODE=0 }
function dsh.cmd { Write-Output ($args -join '|'); $global:LASTEXITCODE=0 }
function Invoke-WebRequest { param($Uri,$Method,[switch]$UseBasicParsing) if ($Uri -ne 'https://github.com/TokenNotIncluded/dsh-lmm-provider/releases/latest' -or $Method -ne 'Head') { throw 'wrong release URL' }; return [pscustomobject]@{BaseResponse=[pscustomobject]@{ResponseUri=[Uri]'https://github.com/TokenNotIncluded/dsh-lmm-provider/releases/tag/v0.1.0-alpha.5'}} }
'@
Check 'dsh.ps1' $npmSuccess 0 'plugin\|--profile\|headless.*releases/download/v0.1.0-alpha.5/dsh-lmm-provider.tgz' '-Profile headless'
Check 'dsh.ps1' ($npmSuccess+"`nfunction Invoke-WebRequest { throw 'missing-release' }") 1 'missing-release'
Check 'dsh.ps1' ($npmSuccess+"`nfunction Invoke-WebRequest { [pscustomobject]@{BaseResponse=[pscustomobject]@{ResponseUri=[Uri]'https://evil.invalid/plugin.tgz'}} }") 1 'Unexpected plugin release URL'
$upstream = @'
function Invoke-RestMethod { return 'param([string]$Version) Write-Output "upstream:$Version"' }
'@
Check 'claude-code.ps1' $upstream 0 'upstream:stable' 'stable'
Check 'codex.ps1' $upstream 0 'upstream:1.2.3' '1.2.3'
Check 'codex.ps1' "function Invoke-RestMethod { throw 'download-failed' }" 1 'download-failed'
Check 'clash-verge-rev.ps1' "function winget { Write-Output (`$args -join '|'); `$global:LASTEXITCODE=9 }" 9 'ClashVergeRev.ClashVergeRev'
Check 'lmm.ps1' "function cargo { Write-Output (`$args -join '|'); `$global:LASTEXITCODE=0 }" 0 'lmm-cli\|--version\|0.1.0\|--locked' '-FromSource'
$msi = @'
$env:PROCESSOR_ARCHITEW6432=''
function Invoke-RestMethod {
[pscustomobject]@{assets=@(
[pscustomobject]@{name='CC-Switch-v1-Windows.msi';browser_download_url='https://test.invalid/x64'},
[pscustomobject]@{name='CC-Switch-v1-Windows-arm64.msi';browser_download_url='https://test.invalid/arm64'},
[pscustomobject]@{name='CC-Switch-v1-Windows.msi.sig';browser_download_url='https://test.invalid/signature'}
)}
}
function Invoke-WebRequest { param($Uri,$OutFile,[switch]$UseBasicParsing) Write-Output $Uri; Set-Content -LiteralPath $OutFile -Value 'fixture' }
function Start-Process {
param($FilePath,$ArgumentList,[switch]$Wait,[switch]$PassThru)
if ($ArgumentList[0] -ne '/i' -or $ArgumentList[1] -notmatch '^".*\.msi"$') { throw 'MSI argument boundaries lost' }
[pscustomobject]@{ExitCode=0}
}
'@
Check 'cc-switch.ps1' ($msi+"`n`$env:PROCESSOR_ARCHITECTURE='AMD64'") 0 'https://test.invalid/x64'
Check 'cc-switch.ps1' ($msi+"`n`$env:PROCESSOR_ARCHITECTURE='ARM64'") 0 'https://test.invalid/arm64'
# New AI tools: download contracts, setup separation, and native exit propagation.
$nativeUrls = @{
'grok-build'='https://x.ai/cli/install.ps1'
'kimi'='https://code.kimi.com/kimi-code/install.ps1'
'hermes'='https://hermes-agent.nousresearch.com/install.ps1'
'openclaw'='https://openclaw.ai/install.ps1'
'aider'='https://aider.chat/install.ps1'
'uv'='https://astral.sh/uv/install.ps1'
}
foreach ($name in $nativeUrls.Keys) {
$url = $nativeUrls[$name]
$fixture = "function Invoke-RestMethod { param(`$Uri) if (`$Uri -ne '$url') { throw 'wrong installer URL' }; return 'param([switch]`$SkipSetup,[switch]`$NoOnboard) Write-Output upstream; Write-Output (`$SkipSetup.IsPresent -or `$NoOnboard.IsPresent)' }"
$pattern = if ($name -in @('hermes','openclaw')) { 'True' } else { 'upstream' }
Check "$name.ps1" $fixture 0 $pattern
Check "$name.ps1" "function Invoke-RestMethod { throw 'download-failed' }" 1 'download-failed'
Check "$name.ps1" "function Invoke-RestMethod { return 'exit 9' }" 9 ''
}
$cursorWsl = @'
function wsl.exe { Write-Output ($args -join '|'); $global:LASTEXITCODE=0 }
'@
Check 'cursor-cli.ps1' $cursorWsl 0 'bash\|-c\|.*https://cursor.com/install'
Check 'cursor-cli.ps1' $cursorWsl 0 'cursor-agent\|login' 'setup'
$packages = @{'gemini'='@google/gemini-cli'; 'qwen-code'='@qwen-code/qwen-code'; 'codebuddy'='@tencent-ai/codebuddy-code'}
foreach ($name in $packages.Keys) {
$fixture = "function node { `$global:LASTEXITCODE=0 }; function npm.cmd { Write-Output (`$args -join '|'); `$global:LASTEXITCODE=0 }"
Check "$name.ps1" $fixture 0 ([regex]::Escape('install|-g|'+$packages[$name]+'@latest'))
Check "$name.ps1" ($fixture+"; function npm.cmd { Write-Output npm-failed; `$global:LASTEXITCODE=8 }") 8 'npm-failed'
Check "$name.ps1" ($fixture+"; function node { `$global:LASTEXITCODE=7 }; function npm.cmd { throw 'must not install' }") 7 ''
}
$setups = @{'grok-build'='grok'; 'kimi'='kimi'; 'hermes'='hermes'; 'openclaw'='openclaw'; 'aider'='aider'; 'gemini'='gemini.cmd'; 'qwen-code'='qwen.cmd'; 'codebuddy'='codebuddy.cmd'}
foreach ($name in $setups.Keys) {
$command = $setups[$name]
$fixture = "function Invoke-RestMethod { throw 'setup must not download' }; function $command { Write-Output configured; `$global:LASTEXITCODE=6 }"
Check "$name.ps1" $fixture 6 'configured' 'setup'
}
Check 'astrbot.ps1' "function uv { Write-Output (`$args -join '|'); `$global:LASTEXITCODE=0 }" 0 'tool\|install\|--upgrade\|astrbot\|--python\|3.12'
Check 'astrbot.ps1' '' 1 'Specify an AstrBot instance directory' 'setup'
$astrbotSetup = @'
function New-Item { }
function Push-Location { param($LiteralPath) if ($LiteralPath -ne 'instance with spaces') { throw 'directory boundaries lost' } }
function Pop-Location { }
function Test-Path { $false }
function astrbot { Write-Output ($args -join '|'); $global:LASTEXITCODE=0 }
function uv { throw 'setup must not install' }
'@
Check 'astrbot.ps1' $astrbotSetup 0 'init' "setup -Directory 'instance with spaces'"
Check 'astrbot.ps1' ($astrbotSetup+"`nfunction Test-Path { `$true }; function astrbot { throw 'must not reinitialize existing data' }") 0 'already has AstrBot data' "setup -Directory 'instance with spaces'"
$desktopFixture = @'
function Invoke-RestMethod {
param($Uri)
if ($Uri -like 'https://cursor.com/api/download*') { return [pscustomobject]@{downloadUrl='https://downloads.cursor.com/setup.exe'} }
return [pscustomobject]@{assets=@(
[pscustomobject]@{name='Cherry-Studio-1-win-x64-setup.exe';browser_download_url='https://github.com/CherryHQ/cherry-studio/releases/download/v1/x64.exe'},
[pscustomobject]@{name='Cherry-Studio-1-win-arm64-setup.exe';browser_download_url='https://github.com/CherryHQ/cherry-studio/releases/download/v1/arm64.exe'},
[pscustomobject]@{name='Cherry-Studio-CN-1-win-x64-setup.exe';browser_download_url='https://github.com/CherryHQ/cherry-studio/releases/download/v1/cn.exe'}
)}
}
function Invoke-WebRequest { param($Uri,$OutFile,[switch]$UseBasicParsing) Write-Output $Uri; Set-Content -LiteralPath $OutFile -Value fixture }
function Start-Process { param($FilePath,[switch]$Wait,[switch]$PassThru) [pscustomobject]@{ExitCode=0} }
$env:PROCESSOR_ARCHITEW6432=''
'@
foreach ($cpu in @('AMD64','ARM64')) {
$arch = if ($cpu -eq 'AMD64') { 'x64' } else { 'arm64' }
Check 'cherry-studio.ps1' ($desktopFixture+"`n`$env:PROCESSOR_ARCHITECTURE='$cpu'") 0 "$arch.exe"
Check 'cursor.ps1' ($desktopFixture+"`n`$env:PROCESSOR_ARCHITECTURE='$cpu'") 0 'downloads.cursor.com'
}
Check 'ollama.ps1' $desktopFixture 0 'https://ollama.com/download/OllamaSetup.exe'
foreach ($name in @('cursor','cherry-studio','ollama')) {
Check "$name.ps1" ($desktopFixture+"`n`$env:PROCESSOR_ARCHITECTURE='AMD64'; function Start-Process { [pscustomobject]@{ExitCode=9} }") 9 ''
Check "$name.ps1" ($desktopFixture+"`n`$env:PROCESSOR_ARCHITECTURE='AMD64'; function Invoke-WebRequest { throw 'download-failed' }") 1 'download-failed'
}
Check 'cursor.ps1' ($desktopFixture+"`n`$env:PROCESSOR_ARCHITECTURE='AMD64'; function Invoke-RestMethod { [pscustomobject]@{downloadUrl='https://evil.invalid/setup.exe'} }") 1 'Unexpected Cursor'
Check 'cherry-studio.ps1' ($desktopFixture+"`n`$env:PROCESSOR_ARCHITECTURE='AMD64'; function Invoke-RestMethod { [pscustomobject]@{assets=@()} }") 1 'No unique official'
# Exercise the actual PowerShell menu with local inert child scripts.
$menuWork = Join-Path ([IO.Path]::GetTempPath()) ('lmm-menu-contract-' + [guid]::NewGuid())
try {
New-Item -ItemType Directory -Path $menuWork | Out-Null
Copy-Item (Join-Path $PSScriptRoot 'menu.ps1') (Join-Path $menuWork 'menu.ps1')
foreach ($name in @('cursor-cli','grok-build','hermes','astrbot')) {
Set-Content -LiteralPath (Join-Path $menuWork "$name.ps1") -Value 'Write-Output ("ROUTED:" + ($args -join "|"))'
}
foreach ($entry in @(@('10','1','install'),@('11','2','setup'),@('16','0',''))) {
$global:LmmMenuAnswers = [Collections.Generic.Queue[string]]::new()
$global:LmmMenuAnswers.Enqueue($entry[0]); $global:LmmMenuAnswers.Enqueue($entry[1]); $global:LmmMenuAnswers.Enqueue('0')
function Read-Host { param($Prompt) return $global:LmmMenuAnswers.Dequeue() }
$output = & (Join-Path $menuWork 'menu.ps1') | Out-String
if ($entry[2] -and $output -notmatch ([regex]::Escape('ROUTED:'+$entry[2]))) { throw 'PowerShell menu action routing failed' }
if (-not $entry[2] -and $output -match 'ROUTED:') { throw 'Menu back unexpectedly ran an installer' }
$checks++
}
$global:LmmMenuAnswers = [Collections.Generic.Queue[string]]::new()
foreach ($answer in @('18','2','instance with spaces','0')) { $global:LmmMenuAnswers.Enqueue($answer) }
$output = & (Join-Path $menuWork 'menu.ps1') | Out-String
if ($output -notmatch 'ROUTED:setup\|-Directory\|instance with spaces') { throw 'AstrBot menu directory routing failed' }
$checks++
} finally {
Remove-Variable LmmMenuAnswers -Scope Global -ErrorAction SilentlyContinue
Remove-Item Function:Read-Host -ErrorAction SilentlyContinue
Remove-Item -LiteralPath $menuWork -Recurse -Force -ErrorAction SilentlyContinue
}
# Windows SSH setup contracts: no real service or firewall changes.
$sshFixture = @'
$env:OS = 'Windows_NT'
$global:SshInstalled = $true
$global:SshRuleExists = $true
$global:SshRestricted = $false
function whoami.exe { if ($args.Count) { 'S-1-16-12288' } else { 'PC\user' }; $global:LASTEXITCODE=0 }
function Get-WindowsCapability { [pscustomobject]@{State=$(if ($global:SshInstalled) { 'Installed' } else { 'NotPresent' })} }
function Add-WindowsCapability { $global:SshInstalled=$true; [pscustomobject]@{RestartNeeded=$false} }
function Get-NetFirewallRule { if ($global:SshRuleExists) { [pscustomobject]@{Name='OpenSSH-Server-In-TCP'} } }
function Set-NetFirewallRule {
param($Name,$Enabled,$Direction,$Action,$Profile,$RemoteAddress)
if ($Name -ne 'OpenSSH-Server-In-TCP' -or $Enabled -ne 'True' -or $Direction -ne 'Inbound' -or $Action -ne 'Allow' -or $Profile -ne 'Private' -or $RemoteAddress -ne 'LocalSubnet') { throw 'unsafe firewall scope' }
$global:SshRestricted=$true
}
function Get-NetFirewallPortFilter { [pscustomobject]@{Name='filter'} }
function Set-NetFirewallPortFilter {
param($Protocol,$LocalPort)
if ($Protocol -ne 'TCP' -or $LocalPort -ne 22) { throw 'wrong SSH port' }
}
function New-NetFirewallRule {
param($Name,$DisplayName,$Enabled,$Direction,$Action,$Protocol,$LocalPort,$Profile,$RemoteAddress)
if ($Protocol -ne 'TCP' -or $LocalPort -ne 22) { throw 'wrong SSH port' }
Set-NetFirewallRule -Name $Name -Enabled $Enabled -Direction $Direction -Action $Action -Profile $Profile -RemoteAddress $RemoteAddress
}
function Start-Service { if (-not $global:SshRestricted) { throw 'started before firewall restriction' }; Write-Output 'started-safely' }
function Get-Service { [pscustomobject]@{Status='Running'} }
function Get-NetIPAddress { [pscustomobject]@{InterfaceAlias='USB';IPAddress='192.168.42.2'} }
function Disable-NetFirewallRule { Write-Host 'rule-disabled' }
function Stop-Service { Write-Host 'service-stopped' }
'@
Check 'ssh.ps1' $sshFixture 0 '(?s)started-safely.*PC\\user.*192.168.42.2'
Check 'ssh.ps1' ($sshFixture+"`n`$global:SshInstalled=`$false; `$global:SshRuleExists=`$false") 0 '(?s)Installing Windows OpenSSH.*started-safely'
Check 'ssh.ps1' ($sshFixture+"`nfunction whoami.exe { 'S-1-16-8192'; `$global:LASTEXITCODE=0 }") 1 'Administrator'
Check 'ssh.ps1' ($sshFixture+"`n`$env:OS='Linux'") 1 'requires Windows'
Check 'ssh.ps1' ($sshFixture+"`n`$global:SshInstalled=`$false; function Add-WindowsCapability { throw 'install-failed' }; function Start-Service { throw 'must-not-start' }") 1 'install-failed'
Check 'ssh.ps1' ($sshFixture+"`n`$global:SshInstalled=`$false; function Add-WindowsCapability { [pscustomobject]@{RestartNeeded=`$true} }") 1 'Restart Windows'
Check 'ssh.ps1' ($sshFixture+"`nfunction Set-NetFirewallRule { throw 'firewall-failed' }; function Start-Service { throw 'must-not-start' }") 1 'firewall-failed'
Check 'ssh.ps1' ($sshFixture+"`nfunction Get-WindowsCapability { throw 'must-not-install' }") 0 '(?s)rule-disabled.*service-stopped' '-Action stop'
Check 'ssh.ps1' "function whoami.exe { throw 'must-not-check-admin' }" 0 'Usage:' '-Action help'
Write-Output "PowerShell syntax and $checks command-contract checks passed."