okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
74e25e165
main
1import test from 'node:test' ; 2import assert from 'node:assert/strict' ; 3import { spawn } from 'node:child_process' ; 4import { readFile } from 'node:fs/promises' ; 5import { mkdtemp , rm } from 'node:fs/promises' ; 6import { createServer } from 'node:net' ; 7import { tmpdir } from 'node:os' ; 8import { join } from 'node:path' ; 9import { fileURLToPath } from 'node:url' ; 10import { transform } from 'esbuild' ; 11import { ROOMY_HOST } from './helpers/hardware.mjs' ; 12import { plainNoModel } from '../server/no-model-text.mjs' ; 13 14/** Sending a chat while no model can answer: say so in plain words, with the one next step, and start no run. */ 15const root = fileURLToPath ( new URL ( '..' , import . meta. url )); 16const delay = ms => new Promise ( r => setTimeout ( r , ms )); 17async function freePort () { const s = createServer (); await new Promise ( r => s . listen ( 0 , '127.0.0.1' , r )); const p = s . address (). port ; await new Promise ( r => s . close ( r )); return p ; } 18 19test ( 'the engine\'s own "no model" wording is turned into plain words with the next step' , () => { 20assert . equal ( plainNoModel ( 'Choose a loaded model ID.' ), 'No model is loaded. Load one in Models first.' ); 21assert . equal ( plainNoModel ( 'This model is not ready.' ), 'Your model is still loading. You can chat in a moment.' ); 22assert . equal ( plainNoModel ( 'Something else happened.' ), 'Something else happened.' ); 23}); 24 25test ( 'the composer\'s notice covers every state: downloading, loading, none, and a model that can answer' , async () => { 26const { noModelNotice} = await import ( `data:text/javascript;base64, ${ Buffer . from (( await transform ( await readFile ( new URL ( '../src/lib/no-model.ts' , import . meta . url ), 'utf8' ), { loader : 'ts' , format : 'esm' })). code ). toString ( 'base64' )} ` ); 27const none = noModelNotice ({ instances :[], jobs :[] }); 28assert . deepEqual ( none , { title :'No model is loaded' , message :'No model is loaded. Load one in Models first.' }); 29assert . match ( noModelNotice ({ instances :[], jobs :[{ kind :'install' , state :'running' , result :{ install :{ download :{ percent :76 } } } }] }). message , / downloading \(76%\)\. You can chat when it has finished / ); 30assert . match ( noModelNotice ({ instances :[{ status :'loading' }], jobs :[] }). message , / still loading / ); 31assert . match ( noModelNotice ({ instances :[{ status :'error' }, { status :'stopped' }], jobs :[] }). title , / No model is loaded / , 'a model that failed or stopped cannot answer' ); 32assert . equal ( noModelNotice ({ instances :[{ status :'loading' }, { status :'ready' }], jobs :[] }), null ); 33}); 34 35test ( 'sendMessage checks for a model before it adds the message, picks a chat or calls the host, and waits for the first read of the engine' , async () => { 36const source = ( await readFile ( new URL ( '../src/lib/store.ts' , import . meta. url ), 'utf8' )). replace ( / \r\n / g , '\n' ); 37const start = source . indexOf ( 'export async function sendMessage' ); 38const body = source . slice ( start , source . indexOf ( '\n}\n' , start )); 39const guard = body . indexOf ( 'noModelNotice(' ); 40assert . ok ( guard > 0 , 'the guard is in sendMessage, the one entry for typed messages, loops, plans, goals and Resume' ); 41for ( const later of [ 'newSession()' , 'api.' , 'fetch(' , 'streamReal' , 'appendMessage' , 'takeChatPhotos' ]) { 42const at = body . indexOf ( later ); 43assert . ok ( at < 0 || at > guard , ` ${ later } comes after the guard` ); 44} 45assert . match ( body , / !nativeState\(\) && !state\.remoteModel && !state\.library\?\.loaded\) \{ pushToast\('info', 'Still starting up' / , 'before the first read of the engine nothing is claimed and nothing is sent' ); 46}); 47 48test ( 'over HTTP, a chat with no model loaded gets one plain error and no reply' , async t => { 49const home = await mkdtemp ( join ( tmpdir (), 'strata-nomodel-' )); 50const port = await freePort (); 51const host = spawn ( process . execPath , [ 'server/index.mjs' ], { cwd :root , stdio :'ignore' , env :{ ...process . env , STRATA_HOME :home , STRATA_MODELS_DIR :join ( home , 'models' ), STRATA_PORT :String ( port ), STRATA_SHARE_PORT :String ( await freePort ()), STRATA_TUNNEL_PORT :String ( await freePort ()), STRATA_REAL_ENGINE :'0' , STRATA_TEST_HARDWARE :ROOMY_HOST } }); 52t . after ( async () => { host . kill (); await new Promise ( r => host . once ( 'exit' , r )); await rm ( home , { recursive :true , force :true }). catch (() => {}); }); 53let up = false ; 54for ( let i = 0 ; i < 150 && ! up ; i ++ ) { try { up = ( await fetch ( `http://127.0.0.1: ${ port } /api/health` )). ok ; } catch { await delay ( 100 ); } } 55assert . ok ( up , 'host started' ); 56const response = await fetch ( `http://127.0.0.1: ${ port } /api/chat/stream` , { method :'POST' , headers :{ 'content-type' :'application/json' }, body :JSON . stringify ({ message :'hello' , sessionId :'chat-1' , mode :'manual' }) }); 57const frames = ( await response . text ()). split ( '\n' ). filter ( Boolean ). map ( line => JSON . parse ( line )); 58assert . ok ( ! frames . some ( f => f . type === 'start' || f . type === 'delta' ), `no reply started: ${ JSON . stringify ( frames )} ` ); 59const failure = frames . find ( f => f . type === 'error' ); 60assert . ok ( failure , `an error frame: ${ JSON . stringify ( frames )} ` ); 61assert . match ( failure . message , / No model is loaded | still loading / ); 62assert . doesNotMatch ( failure . message , / model ID | Choose a loaded / ); 63});