char-slop/berry
multipurpose self-modifiable agentic harness
git clone https://git.t4t.associates/char-slop/berry
cb11725
main
1import { assert , assertEquals , assertRejects , assertThrows } from "@std/assert" ; 2import { writeFile } from "node:fs/promises" ; 3import { dirname , join } from "node:path" ; 4import { piAuthStore , resolveConfigValue } from "./auth.ts" ; 5import { tempDir } from "@berry/util" ; 6 7async function store ( run :( path :string ) => Promise < void >) { 8await usingtemporary = await tempDir ({ prefix :"berry-auth-" }); 9const dir = temporary . path ; 10await run ( join ( dir , "auth.json" )); 11} 12 13Deno . test ( "reads see what another process wrote, and what this one modified" , () => 14store ( async ( path ) => { 15const auth = piAuthStore ( path ); 16assertEquals ( await auth . list (), []); 17// pi, writing the file itself 18await writeFile ( path , JSON . stringify ({ a :{ type :"api_key" , key :"from pi" } }, null , 2 )); 19assertEquals ( await auth . read ( "a" ), { type :"api_key" , key :"from pi" }); 20 21await auth . modify ( "b" , () => Promise . resolve ({ type :"api_key" , key :"from berry" })); 22assertEquals ( await auth . list (), [{ providerId :"a" , type :"api_key" }, { 23providerId :"b" , 24type :"api_key" , 25}]); 26await auth . delete ( "a" ); 27assertEquals ( await auth . read ( "a" ), undefined ); 28assertEquals ( await piAuthStore ( path ). read ( "b" ), { type :"api_key" , key :"from berry" }); 29})); 30 31Deno . test ( "command-based API keys fail without executing their command" , () => 32store ( async ( path ) => { 33const marker = join ( dirname ( path ), "command-ran" ); 34const command = `!touch ${ JSON . stringify ( marker )} ` ; 35await writeFile ( path , JSON . stringify ({ a :{ type :"api_key" , key :command } })); 36const auth = piAuthStore ( path ); 37const error = await assertRejects ( 38() => auth . read ( "a" ), 39Error , 40"Command-based API keys are disabled" , 41); 42assert ( ! error . message . includes ( command )); 43await assertRejects (() => Deno . stat ( marker ), Deno . errors . NotFound ); 44await auth . modify ( "a" , () => Promise . resolve ({ type :"api_key" , key :"literal" })); 45assertEquals ( await auth . read ( "a" ), { type :"api_key" , key :"literal" }); 46})); 47 48Deno . test ( "config values reject command syntax and keep literal and environment forms" , () => { 49for ( const config of [ "!" , "!echo secret" , "!false" ]) { 50assertThrows (() => resolveConfigValue ( config ), Error , "Command-based API keys are disabled" ); 51} 52assertEquals ( resolveConfigValue ( "literal" ), "literal" ); 53assertEquals ( resolveConfigValue ( "$TOKEN" , { TOKEN :"secret" }), "secret" ); 54assertEquals ( resolveConfigValue ( "prefix-${TOKEN}" , { TOKEN :"secret" }), "prefix-secret" ); 55assertEquals ( resolveConfigValue ( "$$literal" ), "$literal" ); 56assertEquals ( resolveConfigValue ( "$!literal" ), "!literal" ); 57assertEquals ( resolveConfigValue ( "$TOKEN" , { TOKEN :"!literal" }), "!literal" ); 58const missing = `BERRY_AUTH_TEST_ ${ crypto . randomUUID (). replaceAll ( "-" , "_" )} ` ; 59assertEquals ( resolveConfigValue ( `$ ${ missing } ` ), undefined ); 60}); 61 62Deno . test ( "the auth store still resolves API keys from configured environment values" , () => 63store ( async ( path ) => { 64const credential = { type :"api_key" as const , key :"${TOKEN}" , env :{ TOKEN :"secret" } }; 65await writeFile ( path , JSON . stringify ({ a :credential })); 66assertEquals ( await piAuthStore ( path ). read ( "a" ), { ...credential , key :"secret" }); 67}));