char-slop/berry
multipurpose self-modifiable agentic harness
git clone https://git.t4t.associates/char-slop/berry
3ea88e4
main
1import { assert , assertEquals , assertRejects , assertStringIncludes } from "@std/assert" ; 2import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context" ; 3import { mkdir , writeFile } from "node:fs/promises" ; 4import { join } from "node:path" ; 5import { defineExtension } from "@berry/api" ; 6import { localEnvironment } from "@berry/api/environment.ts" ; 7import { rootedEnvironment , testExtension } from "@berry/api/testing.ts" ; 8import { tempDir } from "@berry/util" ; 9import code from "./mod.ts" ; 10import { powershellCommand } from "./commands.ts" ; 11 12Deno . test ( "PowerShell registers only in Windows conversations, never globally" , async () => { 13await usingroot = await tempDir (); 14const registered :string [] = []; 15const ext = testExtension ( 16defineExtension ({ 17 ...code , 18setup ( api ) { 19const contribute = code . setup ({ 20 ...api , 21tool ( tool ) { 22assert ( tool . name !== "powershell" ); 23api . tool ( tool ); 24}, 25}); 26return ( conv ) => 27contribute ?.({ 28 ...conv , 29tool ( tool ) { 30registered . push ( tool . name ); 31conv . tool ( tool ); 32}, 33}); 34}, 35}), 36{ env :rootedEnvironment ( root . path ) }, 37); 38const probe = async ( platform :string ) => { 39registered . length = 0 ; 40await assertRejects ( 41() => ext . call ( "absent" , {}, { env :{ ...ext . env , platform} }), 42Error , 43"No tool named absent" , 44); 45return registered . includes ( "powershell" ); 46}; 47assertEquals ( await probe ( "linux" ), false ); 48assertEquals ( await probe ( "win32" ), true ); 49assertEquals ( await probe ( "linux" ), false ); 50}); 51 52Deno . test ( "PowerShell follows the target environment and prefers PS7 before falling back to PS5" , async () => { 53await usingroot = await tempDir (); 54for ( 55const dir of [ 56"bin dir" , 57"legacy" , 58"program files/PowerShell/7" , 59"windows/System32/WindowsPowerShell/v1.0" , 60] 61) { 62await mkdir ( join ( root . path , dir ), { recursive :true }); 63} 64const binaries = [ 65"bin dir/pwsh.EXE" , 66"program files/PowerShell/7/pwsh.exe" , 67"legacy/powershell.EXE" , 68"windows/System32/WindowsPowerShell/v1.0/powershell.exe" , 69]; 70for ( const file of binaries ) await writeFile ( join ( root . path , file ), "" ); 71 72let invocation :string | readonly string [] = "" ; 73const env = { 74 ...rootedEnvironment ( root . path , { 75variables :{ 76pAtH :'"/bin dir";/legacy' , 77PaThExT :".EXE" , 78ProgramFiles :"/program files" , 79SystemRoot :"/windows" , 80}, 81}), 82platform :"win32" , 83exec :( command :string | readonly string []) => { 84invocation = command ; 85return Promise . resolve ({ ok :true as const , value :{ exitCode :0 } }); 86}, 87}; 88const ext = testExtension ( code , { env}); 89await ext . call ( "powershell" , { command :"Get-Location" , timeout :10 }); 90assert ( Array . isArray ( invocation )); 91assertEquals ( invocation [ 0 ], "/bin dir/pwsh.EXE" ); 92 93await Deno . remove ( join ( root . path , binaries [ 0 ])); 94await ext . call ( "powershell" , { command :"Get-Location" , timeout :10 }); 95assertEquals ( invocation [ 0 ], "/program files/PowerShell/7/pwsh.exe" ); 96 97await Deno . remove ( join ( root . path , binaries [ 1 ])); 98await ext . call ( "powershell" , { command :"Get-Location" , timeout :10 }); 99assertEquals ( invocation [ 0 ], "/legacy/powershell.EXE" ); 100 101await Deno . remove ( join ( root . path , binaries [ 2 ])); 102await ext . call ( "powershell" , { command :"Get-Location" , timeout :10 }); 103assertEquals ( invocation [ 0 ], "/windows/System32/WindowsPowerShell/v1.0/powershell.exe" ); 104 105await Deno . remove ( join ( root . path , binaries [ 3 ])); 106await assertRejects ( 107() => ext . call ( "powershell" , { command :"Get-Location" , timeout :10 }), 108Error , 109"PowerShell not found" , 110); 111await assertRejects ( 112() => 113ext . call ( "powershell" , { command :"Get-Location" , timeout :10 }, { 114env :{ ...env , platform :"linux" }, 115}), 116Error , 117"No tool named powershell" , 118); 119}); 120 121let hasPowerShell = true ; 122try { 123await powershellCommand ( localEnvironment ( Deno . cwd ()), "" ); 124} catch { 125hasPowerShell = false ; 126} 127 128Deno . test ({ 129name : 130"PowerShell runs literal Unicode scripts, preserves native failures, and spills UTF-8 output" , 131ignore :! hasPowerShell , 132async fn () { 133await usingroot = await tempDir (); 134const env = localEnvironment ( root . path ); 135const run = async ( command :string , timeout = 10 ) => { 136let output = "" ; 137const result = await env . exec ( await powershellCommand ( env , command ), { 138 timeout, 139onOutput :( chunk ) => output += chunk , 140spill :{ afterBytes :50 * 1024 , afterLines :2000 }, 141}, BACKGROUND_CONTEXT ); 142assert ( result . ok , result . ok ?"" :result . error . message ); 143return { ...result . value , output}; 144}; 145const success = await run ( `'héllo 🐈 "quotes" \\ backslash'; (Get-Location).Path` ); 146assertEquals ( success . exitCode , 0 ); 147assertStringIncludes ( success . output , 'héllo 🐈 "quotes" \\ backslash' ); 148assertStringIncludes ( success . output , root . path ); 149const deno = Deno . execPath (). replaceAll ( "'" , "''" ); 150assertEquals (( await run ( `& ' ${ deno } ' eval 'Deno.exit(23)'` )). exitCode , 23 ); 151assertEquals (( await run ( "Write-Error 'failure'" )). exitCode , 1 ); 152const long = await run ( "1..2500 | ForEach-Object { '🐈' + $_ }" ); 153assert ( long . spillPath ); 154assertStringIncludes ( await Deno . readTextFile ( long . spillPath ), "🐈2500" ); 155const timedOut = await env . exec ( await powershellCommand ( env , "Start-Sleep -Seconds 5" ), { 156timeout :0.2 , 157}, BACKGROUND_CONTEXT ); 158assert ( ! timedOut . ok ); 159assertEquals ( timedOut . error . code , "timeout" ); 160}, 161});