char-slop/berry
multipurpose self-modifiable agentic harness
git clone https://git.t4t.associates/char-slop/berry
c52d3a7
main
1import { assert , assertEquals } from "@std/assert" ; 2import { existsSync } from "node:fs" ; 3import { withDom } from "@berry/util/dom.ts" ; 4import { Grid } from "./grid.ts" ; 5import { Ghostty } from "./ghostty.ts" ; 6 7Deno . test ( "rescaling the grid repaints cached cells without replacing selected text" , async () => { 8await withDom (( window ) => { 9const rows = document . createElement ( "div" ); 10const canvas = document . createElement ( "canvas" ); 11const painted :number [][] = []; 12Object . defineProperty ( canvas , "getContext" , { 13value :() => ({ 14clearRect :() => painted . splice ( 0 ), 15fillRect :( ...rect :number []) => painted . push ( rect ), 16}), 17}); 18const grid = new Grid ( rows , canvas ); 19const metrics = { width :200 , height :100 , ratio :1 , cell :{ width :8.25 , height :18.5 } }; 20grid . fit ( metrics ); 21const bg = [ 61 , 34 , 74 ] as const ; 22grid . update ({ 23cols :20 , 24rows :2 , 25cursor :null , 26changed :new Map ([ 27[ 0 , [{ text :"e\u0301" , bg}, { text :"🦀" , bg, wide :true }, { text :"x" , bg}]], 28[ 1 , [{ text :"next" , bg}]], 29]), 30}); 31assertEquals ( rows . firstChild ! . textContent , "e\u0301🦀x" ); 32const range = window . document . createRange (); 33const text = rows . firstChild ! . firstChild ! . firstChild ! ; 34range . selectNodeContents ( text as unknown as Parameters < typeof range . selectNodeContents >[ 0 ]); 35window . getSelection (). addRange ( range ); 36 37for ( const ratio of [ 1 , 1.25 , 1.5 , 2 , 0.8 ]) { 38grid . fit ({ ...metrics , ratio}); 39assertEquals ( window . getSelection (). toString (), "e\u0301" ); 40assertEquals ( rows . firstChild ! . firstChild ! . firstChild , text ); 41assertEquals ( painted . length , 4 ); 42const [ first , wide , last , next ] = painted ; 43assertEquals ( first [ 0 ], 0 ); 44assertEquals ( first [ 0 ] + first [ 2 ], wide [ 0 ]); 45assertEquals ( wide [ 0 ] + wide [ 2 ], last [ 0 ]); 46assertEquals ( first [ 1 ] + first [ 3 ], next [ 1 ]); 47assertEquals ( last [ 0 ] + last [ 2 ], next [ 2 ]); 48assert ( wide [ 2 ] > first [ 2 ]); 49assert ( painted . flat (). every ( Number . isInteger )); 50} 51 52grid . update ({ cols :20 , rows :2 , cursor :null , changed :new Map () }); 53assertEquals ( painted . length , 4 ); 54assertEquals ( rows . firstChild ! . firstChild ! . firstChild , text ); 55}); 56}); 57 58Deno . test ({ 59name :"screen-filling backgrounds include blank cells through the screen edges" , 60ignore :! existsSync ( new URL ( "./ghostty-vt.wasm" , import . meta. url )), 61async fn () { 62const ghostty = await Ghostty . load ( 63await Deno . readFile ( new URL ( "./ghostty-vt.wasm" , import . meta. url )), 64); 65const terminal = ghostty . terminal ( 20 , 4 ); 66try { 67terminal . write ( new TextEncoder (). encode ( 68"\x1b[?25l\x1b[48;2;61;34;74m" + " " . repeat ( 80 ) + "\x1b[Hé🦀x" , 69)); 70const frame = terminal . frame (); 71await withDom (() => { 72const canvas = document . createElement ( "canvas" ); 73const painted :number [][] = []; 74Object . defineProperty ( canvas , "getContext" , { 75value :() => ({ 76clearRect :() => painted . splice ( 0 ), 77fillRect :( ...rect :number []) => painted . push ( rect ), 78}), 79}); 80const grid = new Grid ( document . createElement ( "div" ), canvas ); 81grid . fit ({ width :165 , height :74 , ratio :1.25 , cell :{ width :8.25 , height :18.5 } }); 82grid . update ( frame ); 83assertEquals ( 84painted . reduce (( area , [, , width , height ]) => area + width * height , 0 ), 85canvas . width * canvas . height , 86); 87assertEquals ( Math . max ( ...painted . map (([ x , , width ]) => x + width )), canvas . width ); 88assertEquals ( Math . max ( ...painted . map (([, y , , height ]) => y + height )), canvas . height ); 89}); 90} finally { 91terminal . free (); 92} 93}, 94});