okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
bcf88b738
main
1import test from 'node:test' ; 2import assert from 'node:assert/strict' ; 3import { EventEmitter } from 'node:events' ; 4import { readFile } from 'node:fs/promises' ; 5import { createRequire } from 'node:module' ; 6import { pathToFileURL } from 'node:url' ; 7import { transform } from 'esbuild' ; 8import { createCloudflare , tunnelHostname } from '../server/cloudflare.mjs' ; 9import { saveSnapshot , snapshotBytes } from '../server/snapshot.mjs' ; 10 11function fixture ( extra = {}) { 12let stopped = 0 , launched ; 13const child = new EventEmitter (); child . stdout = new EventEmitter (); child . stderr = new EventEmitter (); 14child . kill = () => { queueMicrotask (() => child . emit ( 'exit' , 0 )); }; 15const connector = createCloudflare ({ origin :{ start :async () => {}, stop :async () => { stopped ++ ; } }, originUrl :'http://127.0.0.1:8793' , hasKey :() => true , locate :async () => '/test/cloudflared' , launch :( ...args ) => { launched = args ; return child ; }, ...extra }); 16return { connector, child, launched :() => launched , stopped :() => stopped }; 17} 18test ( 'quick tunnel publishes its URL only after registration, and stops its private origin' , async () => { 19const f = fixture (); 20await f . connector . start ({ mode :'quick' }); 21f . child . stderr . emit ( 'data' , 'URL https://quiet-field.trycloud' ); 22f . child . stderr . emit ( 'data' , 'flare.com\n' ); 23assert . equal ( f . connector . state (). status , 'starting' ); 24assert . equal ( f . connector . state (). url , null ); 25f . child . stderr . emit ( 'data' , 'Registered tunnel connection connIndex=0\n' ); 26assert . equal ( f . connector . state (). url , 'https://quiet-field.trycloudflare.com/v1' ); 27assert . equal ( f . connector . state (). status , 'connected' ); 28await assert . rejects ( f . connector . start ({ mode :'quick' }), / Stop / ); 29await f . connector . stop (); 30assert . equal ( f . connector . state (). status , 'idle' ); assert . equal ( f . stopped (), 1 ); 31}); 32test ( 'domain tunnel validates hostnames and keeps the token out of arguments and state' , async () => { 33for ( const value of [ 'https://ai.example.com' , 'example.com/path' , 'localhost' , 'ai.example.com;echo x' ]) assert . throws (() => tunnelHostname ( value )); 34const f = fixture (), token = 'test-only-tunnel-token-12345' ; 35await f . connector . start ({ mode :'domain' , hostname :'ai.example.com' , token}); 36assert . equal ( f . launched ()[ 2 ]. env . TUNNEL_TOKEN , token ); 37assert . ok ( ! JSON . stringify ( f . launched ()[ 1 ]). includes ( token )); 38assert . ok ( ! JSON . stringify ( f . connector . state ()). includes ( token )); 39f . child . stderr . emit ( 'data' , 'Registered tunnel connection\n' ); 40assert . equal ( f . connector . state (). url , 'https://ai.example.com/v1' ); 41await f . connector . stop (); 42}); 43test ( 'missing prerequisites and connector failure leave no inference listener running' , async () => { 44const noKey = fixture ({ hasKey :() => false }); // without a key the tunnel starts (owner decision, 0.2.2) and the state carries a warning 45await noKey . connector . start ({ mode :'quick' }); 46assert . notEqual ( noKey . launched (), undefined ); 47const missing = fixture ({ locate :async () => null }); 48await assert . rejects ( missing . connector . start ({ mode :'quick' }), / connector could not be prepared automatically / i ); 49assert . equal ( missing . connector . state (). status , 'error' ); 50const failed = fixture (); await failed . connector . start ({ mode :'quick' }); 51failed . child . emit ( 'exit' , 1 ); await new Promise ( resolve => setImmediate ( resolve )); 52assert . equal ( failed . connector . state (). status , 'error' ); assert . equal ( failed . stopped (), 1 ); 53}); 54test ( 'snapshot validates PNGs, writes only a dialog-selected path, and respects Cancel' , async () => { 55const png = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aQ1sAAAAASUVORK5CYII=' ; 56assert . throws (() => snapshotBytes ( 'data:image/png;base64,ZXZpbA==' )); 57let writes = []; 58const saved = await saveSnapshot ( png , { platform :'darwin' , run :async () => ({ stdout :'/tmp/user-selected.png\n' }), write :async ( ...args ) => { writes . push ( args ); } }); 59assert . equal ( saved . saved , true ); assert . equal ( writes [ 0 ][ 0 ], '/tmp/user-selected.png' ); assert . deepEqual ( writes [ 0 ][ 1 ], snapshotBytes ( png )); 60const cancelled = await saveSnapshot ( png , { platform :'darwin' , run :async () => { throw { stderr :'User canceled. (-128)' }; }, write :async () => { throw new Error ( 'Must not write' ); } }); 61assert . equal ( cancelled . cancelled , true ); 62}); 63test ( 'shortcut overrides persist, clear, restore, and change the composer send combination' , async () => { 64const memory = new Map (); globalThis . localStorage = { getItem :k => memory . get ( k ), setItem :( k , v ) => memory . set ( k , v ), removeItem :k => memory . delete ( k ) }; 65const require = createRequire ( import . meta. url ); 66const source = ( await readFile ( new URL ( '../src/lib/shortcut-bindings.ts' , import . meta. url ), 'utf8' )). replace ( "from 'react'" , `from ' ${ pathToFileURL ( require . resolve ( 'react' )). href } '` ); 67const { code} = await transform ( source , { loader :'ts' , format :'esm' }); 68const m = await import ( `data:text/javascript;base64, ${ Buffer . from ( code ). toString ( 'base64' )} ` ); 69const enter = { key :'Enter' , ctrlKey :false , metaKey :false , altKey :false , shiftKey :false }; 70assert . equal ( m . composerSends ( enter ), true ); 71m . setBinding ( 'chat:send' , { key :'enter' , mod :true , shift :false , alt :false }); 72assert . equal ( m . composerSends ( enter ), false ); assert . equal ( m . composerSends ({ ...enter , metaKey :true }), true ); 73assert . ok ( memory . get ( 'strata.shortcuts.v1' ). includes ( 'chat:send' )); 74m . setBinding ( 'new' , null ); assert . equal ( m . bindingFor ( 'new' , { key :'n' }), null ); 75m . resetBindings (); assert . equal ( m . composerSends ( enter ), true ); assert . equal ( memory . size , 0 ); 76delete globalThis . localStorage ; 77});