char-slop/berry
multipurpose self-modifiable agentic harness
git clone https://git.t4t.associates/char-slop/berry
b3a4fb7
main
1import { assert , assertEquals } from "@std/assert" ; 2import { withDom } from "@berry/util/dom.ts" ; 3import { TERMINAL_PALETTE } from "@berry/util/palette.ts" ; 4import { ansiOutput } from "./ansi-view.ts" ; 5 6Deno . test ( "coloured output appends in place and resets for replacement snapshots" , async ( t ) => { 7await withDom ( async () => { 8const element = document . createElement ( "pre" ); 9const show = ansiOutput ( element ); 10const colour = ( value :string ) => { 11const span = document . createElement ( "span" ); 12span . style . color = value ; 13return span . style . color ; 14}; 15await t . step ( "colour arrives as soon as a split escape is completed" , () => { 16assertEquals ( element . hidden , true ); 17show ( "start\x1b[38;2;12;" ); 18assertEquals ( element . textContent , "start" ); 19const first = element . firstChild ; 20show ( "start\x1b[38;2;12;34;56mred" ); 21assertEquals ( element . textContent , "startred" ); 22assertEquals ( element . firstChild , first ); 23assertEquals ( 24( element . lastElementChild as HTMLElement ). style . color , 25colour ( "rgb(12, 34, 56)" ), 26); 27const red = element . lastChild ! ; 28const text = red . firstChild ; 29show ( "start\x1b[38;2;12;34;56mredder\x1b[0mplain" ); 30assertEquals ( element . textContent , "startredderplain" ); 31assertEquals ( element . childNodes [ 1 ], red ); 32assertEquals ( red . firstChild , text ); 33assertEquals ( red . textContent , "redder" ); 34assertEquals (( element . lastElementChild as HTMLElement ). style . color , "" ); 35}); 36await t . step ( "a truncated or replaced result resets text, style and pending controls" , () => { 37show ( "\x1b[31mold\x1b]unfinished" ); 38show ( "replacement" ); 39assertEquals ( element . textContent , "replacement" ); 40assertEquals (( element . firstElementChild as HTMLElement ). style . color , "" ); 41show ( "" ); 42assertEquals ( element . hidden , true ); 43assertEquals ( element . childNodes . length , 0 ); 44show ( "new\x1b[32mgreen" ); 45assertEquals ( element . textContent , "newgreen" ); 46assertEquals ( 47( element . lastElementChild as HTMLElement ). style . color , 48colour ( TERMINAL_PALETTE [ 2 ]), 49); 50}); 51await t . step ( "output stays literal text rather than markup" , () => { 52show ( '<script>alert("x")</script>\x1b[31m<a>red</a>' ); 53assertEquals ( element . textContent , '<script>alert("x")</script><a>red</a>' ); 54assertEquals ( element . querySelector ( "script, a" ), null ); 55}); 56await t . step ( "large outputs bound individual text-node edits without splitting emoji" , () => { 57show ( "" ); 58const text = "x" . repeat ( 16383 ) + "😀" + "y" . repeat ( 50000 ); 59show ( text ); 60assertEquals ( element . textContent , text ); 61for ( const span of element . children ) { 62assert ( span . textContent ! . length < 20000 ); 63assert ( ! /[\ud800-\udbff]$ / . test ( span . textContent ! )); 64assert ( ! /^[\udc00-\udfff] / . test ( span . textContent ! )); 65} 66const first = element . firstChild ; 67show ( text + "end" ); 68assertEquals ( element . firstChild , first ); 69assertEquals ( element . textContent , text + "end" ); 70}); 71}); 72});