char-slop/berry
multipurpose self-modifiable agentic harness
git clone https://git.t4t.associates/char-slop/berry
cb11725
main
1import { assertEquals } from "@std/assert" ; 2import { homedir } from "node:os" ; 3import { BACKGROUND_CONTEXT as context } from "@earendil-works/chord/context" ; 4import { localEnvironment } from "@berry/api/environment.ts" ; 5import { rootedEnvironment } from "@berry/api/testing.ts" ; 6import { references } from "./references.ts" ; 7import { tempDir } from "@berry/util" ; 8 9Deno . test ( "reference paths preserve their spelling and browse directories and symlinks" , async () => { 10await usingrootDir = await tempDir (); 11const root = rootDir . path ; 12await Deno . mkdir ( ` ${ root } /src` ); 13await Deno . writeTextFile ( ` ${ root } /src/main.ts` , "" ); 14await Deno . writeTextFile ( ` ${ root } /README.md` , "" ); 15await Deno . writeTextFile ( ` ${ root } /has spaces` , "" ); 16await Deno . symlink ( ` ${ root } /src` , ` ${ root } /link` ); 17assertEquals ( await references ( localEnvironment ( root ), "" , context ), [ 18"link/" , 19"README.md" , 20"src/" , 21]); 22assertEquals ( await references ( localEnvironment ( root ), "R" , context ), [ 23"README.md" , 24"src/" , 25"src/main.ts" , 26]); 27assertEquals ( await references ( localEnvironment ( root ), "src/m" , context ), [ "src/main.ts" ]); 28assertEquals ( await references ( localEnvironment ( root ), "./src/" , context ), [ "./src/main.ts" ]); 29assertEquals ( await references ( localEnvironment ( ` ${ root } /src` ), "../R" , context ), [ 30"../README.md" , 31"../src/" , 32"../src/main.ts" , 33]); 34assertEquals ( await references ( localEnvironment ( root ), ` ${ root } /src/` , context ), [ 35` ${ root } /src/main.ts` , 36]); 37assertEquals ( await references ( localEnvironment ( root ), "link/" , context ), [ "link/main.ts" ]); 38assertEquals ( await references ( localEnvironment ( root ), "missing/" , context ), []); 39assertEquals ( await references ( localEnvironment ( root ), "README.md/" , context ), []); 40assertEquals ( await references ( localEnvironment ( root ), "bad\u0000path" , context ), []); 41}); 42 43Deno . test ( "home references resolve without changing the completion's tilde" , async () => { 44await usingrootDir = await tempDir ({ dir :homedir (), prefix :"berry-references-" }); 45const root = rootDir . path ; 46await Deno . writeTextFile ( ` ${ root } /test.ts` , "" ); 47const prefix = `~/ ${ root . slice ( homedir (). length + 1 )} /` ; 48assertEquals ( await references ( localEnvironment ( "/" ), prefix , context ), [ ` ${ prefix } test.ts` ]); 49}); 50 51Deno . test ( "references rank exact names, prefixes, substrings, and fuzzy matches across the tree" , async () => { 52await usingrootDir = await tempDir (); 53const root = rootDir . path ; 54for ( 55const path of [ 56"deep/nested/cmp" , 57"web/composer.tsx" , 58"cmp-extra.ts" , 59"a-cmp.ts" , 60"c-many-more-parts.ts" , 61"unrelated.ts" , 62] 63) { 64await Deno . mkdir ( ` ${ root } / ${ path . split ( "/" ). slice ( 0 , - 1 ). join ( "/" )} ` , { recursive :true }); 65await Deno . writeTextFile ( ` ${ root } / ${ path } ` , "" ); 66} 67assertEquals ( await references ( localEnvironment ( root ), "CMP" , context ), [ 68"deep/nested/cmp" , 69"cmp-extra.ts" , 70"a-cmp.ts" , 71"web/composer.tsx" , 72"c-many-more-parts.ts" , 73]); 74assertEquals ( await references ( localEnvironment ( root ), "wb/cmp" , context ), [ "web/composer.tsx" ]); 75assertEquals ( await references ( localEnvironment ( root ), "web/cmp" , context ), [ 76"web/composer.tsx" , 77]); 78assertEquals ( await references ( localEnvironment ( root ), "zzzzz" , context ), []); 79}); 80 81Deno . test ( "recursive references honor nested ignores and do not follow symlink cycles" , async () => { 82await usingrootDir = await tempDir (); 83const root = rootDir . path ; 84for ( const dir of [ ".git" , ".jj" , "node_modules" , "data" , "src" ]) { 85await Deno . mkdir ( ` ${ root } / ${ dir } ` ); 86await Deno . writeTextFile ( ` ${ root } / ${ dir } /target.ts` , "" ); 87} 88await Deno . writeTextFile ( ` ${ root } /.gitignore` , "data/\n*.log\n" ); 89await Deno . writeTextFile ( ` ${ root } /src/.gitignore` , "target.ts\n!keep.log\n" ); 90await Deno . writeTextFile ( ` ${ root } /src/target.log` , "" ); 91await Deno . writeTextFile ( ` ${ root } /src/keep.log` , "" ); 92await Deno . writeTextFile ( ` ${ root } /target.ts` , "" ); 93await Deno . symlink ( root , ` ${ root } /src/loop` ); 94await Deno . symlink ( ` ${ root } /missing` , ` ${ root } /broken` ); 95assertEquals ( await references ( localEnvironment ( root ), "target" , context ), [ "target.ts" ]); 96assertEquals ( await references ( localEnvironment ( root ), "keep" , context ), [ "src/keep.log" ]); 97assertEquals ( await references ( localEnvironment ( root ), "loop" , context ), [ "src/loop/" ]); 98assertEquals ( await references ( localEnvironment ( root ), "broken" , context ), [ "broken" ]); 99}); 100 101Deno . test ( "reference results are limited only after ranking the whole tree" , async () => { 102await usingrootDir = await tempDir (); 103const root = rootDir . path ; 104await Deno . mkdir ( ` ${ root } /z` ); 105await Deno . writeTextFile ( ` ${ root } /z/target` , "" ); 106for ( let i = 0 ; i < 60 ; i ++ ) { 107await Deno . writeTextFile ( ` ${ root } /a-target- ${ String ( i ). padStart ( 2 , "0" )} ` , "" ); 108} 109const result = await references ( localEnvironment ( root ), "target" , context ); 110assertEquals ( result . length , 50 ); 111assertEquals ( result [ 0 ], "z/target" ); 112assertEquals ( 113result . slice ( 1 ), 114Array . from ({ length :49 }, ( _ , i ) => `a-target- ${ String ( i ). padStart ( 2 , "0" )} ` ), 115); 116}); 117 118Deno . test ( "references browse the conversation's environment, not this machine" , async () => { 119await usingrootDir = await tempDir (); 120const root = rootDir . path ; 121await Deno . mkdir ( ` ${ root } /project/src` , { recursive :true }); 122await Deno . mkdir ( ` ${ root } /home` ); 123await Deno . writeTextFile ( ` ${ root } /project/src/main.ts` , "" ); 124await Deno . writeTextFile ( ` ${ root } /home/notes.md` , "" ); 125const env = rootedEnvironment ( root , { cwd :"/project" }); 126assertEquals ( await references ( env , "src/" , context ), [ "src/main.ts" ]); 127assertEquals ( await references ( env , "/project/src/" , context ), [ "/project/src/main.ts" ]); 128assertEquals ( await references ( env , "~/" , context ), [ "~/notes.md" ]); 129});