char-slop/berry
multipurpose self-modifiable agentic harness
git clone https://git.t4t.associates/char-slop/berry
d1e4a76
main
1import { assert , assertEquals } from "@std/assert" ; 2import { applyImmutable , type Op } from "@earendil-works/chord/delta" ; 3import { published , servePubsub , type ServerMessage } from "./pubsub.ts" ; 4 5Deno . test ( "a history's snapshot brings its newest entries first, then the older ones before any ops" , async () => { 6const sent :ServerMessage [] = []; 7let receive = ( _ :{ data :string }) => {}; 8const socket = { 9readyState :WebSocket . OPEN , 10send :( frame :string ) => sent . push ( JSON . parse ( frame )), 11addEventListener ( type :string , listener :( event :{ data :string }) => void ) { 12if ( type === "message" ) receive = listener ; 13}, 14close () {}, 15} as unknown as WebSocket ; 16const entry = ( id :number ) => ({ id, text :` ${ id } ` . repeat ( 1000 ) }); 17const history = published ({ docs :{}, entries :Array . from ({ length :200 }, ( _ , i ) => entry ( i )) }); 18servePubsub ( socket , { authorize :() => true , resolve :() => Promise . resolve ( history . feed ()) }); 19receive ({ data :JSON . stringify ({ t :"auth" , token :"token" }) }); 20receive ({ data :JSON . stringify ({ t :"sub" , topic :"conversation:a" }) }); 21while ( ! sent . some (( message ) => message . t === "snapshot" )) await Promise . resolve (); 22history . set ({ ...history . value , entries :[ ...history . value . entries , entry ( 200 )] }); 23 24const [ ready , first , ...rest ] = sent ; 25assertEquals ( ready , { t :"ready" }); 26assert ( first . t === "snapshot" && first . older !== undefined ); 27const tail = ( first . value as typeof history . value ). entries ; 28assertEquals ( tail . at ( - 1 ) ! . id , 199 ); 29assertEquals ( first . older + tail . length , 200 ); 30const older = rest . slice ( 0 , - 1 ); 31assert ( older . length > 1 && older . every (( message ) => message . t === "older" )); 32assertEquals ( rest . at ( - 1 ) ! . t , "ops" ); 33 34let value = first . value as typeof history . value ; 35for ( const message of rest ) { 36if ( message . t === "older" ) { 37// newest first: each chunk ends just before what came already 38assertEquals (( message . entries . at ( - 1 ) as { id :number }). id , value . entries [ 0 ]. id - 1 ); 39value = { ...value , entries :[ ...message . entries as typeof tail , ...value . entries ] }; 40} else if ( message . t === "ops" ) value = applyImmutable ( value , message . ops as Op []); 41} 42assertEquals ( value , history . value ); 43});