okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
4fb068a8c
main
1/** Models > This computer: free graphics memory and RAM first, the SSD only when the model does not fit, and a loaded model changes the card. */ 2import test from 'node:test' ; 3import assert from 'node:assert/strict' ; 4import { build } from 'esbuild' ; 5import { mkdtempSync , writeFileSync , rmSync , readFileSync } from 'node:fs' ; 6import { tmpdir } from 'node:os' ; 7import { join } from 'node:path' ; 8import { fileURLToPath , pathToFileURL } from 'node:url' ; 9 10const stdin = { contents :"import { createElement } from 'react'; import { renderToStaticMarkup } from 'react-dom/server'; import { PcFitCard } from './src/components/PcFitCard'; export { setTranslationLanguage } from './src/lib/i18n'; export const render = props => renderToStaticMarkup(createElement(PcFitCard, props));" , resolveDir :fileURLToPath ( new URL ( '..' , import . meta. url )), loader :'ts' }; 11const built = await build ({ stdin, bundle :true , write :false , format :'esm' , platform :'node' , loader :{ '.json' :'json' , '.tsx' :'tsx' }, jsx :'automatic' , logLevel :'silent' , banner :{ js :"import { createRequire } from 'node:module'; const require = createRequire(import.meta.url);" } }); 12const dir = mkdtempSync ( join ( tmpdir (), 'strata-pc-test-' )); 13writeFileSync ( join ( dir , 'card.mjs' ), built . outputFiles [ 0 ]. text ); 14const { render, setTranslationLanguage} = await import ( pathToFileURL ( join ( dir , 'card.mjs' )). href ); 15rmSync ( dir , { recursive :true , force :true }); 16const fit = ( spillGb , vramUseGb = 12 , ramUseGb = 44 ) => ({ memoryGb :vramUseGb + ramUseGb + spillGb , vramUseGb, ramUseGb, spillGb, known :true }); 17const card = props => render ({ gpuName :'RTX 5070' , ramGb :64 , vramGb :12 , ...props }); 18const live = { vramTotalGb :12 , freeVramGb :10.3 , freeRamGb :49.8 , diskReadGbs :null }; 19 20test ( 'free graphics memory and free RAM come first, as real readings' , () => { 21setTranslationLanguage ( 'en' ); 22const html = card ({ fit :fit ( 0 ), live}); 23assert . match ( html , / Free right now / ); 24assert . match ( html , / Graphics memory<\/span><b>10\.3 GB free<\/b> / ); 25assert . match ( html , / RAM<\/span><b>49\.8 GB free<\/b> / ); 26assert . match ( html , / of 12 GB / ); 27assert . doesNotMatch ( card ({ fit :fit ( 0 ), live :null }), / Free right now / , 'no readings: no invented numbers' ); 28}); 29 30test ( 'the card does not repeat the SSD warning the version picker already gives; a version that fits says nothing either' , () => { 31setTranslationLanguage ( 'en' ); 32assert . doesNotMatch ( card ({ fit :fit ( 0 ), live}), / SSD / ); 33assert . doesNotMatch ( card ({ fit :fit ( 0 ), live}), / Fits fully in memory / , 'no "Fits" words (owner, 11-01)' ); 34assert . doesNotMatch ( card ({ fit :fit ( 30 ), live}), / read from the SSD / , 'said once, under the picker' ); 35assert . match ( card ({ fit :fit ( 30 ), live}), / This version would take about 12 GB of graphics memory and 44 GB of RAM\. / ); 36}); 37 38test ( 'a loaded model leaves the free memory and nothing else: no hardware line, no name, no Loaded, no "It takes about", no numbers, no SSD line' , () => { 39setTranslationLanguage ( 'en' ); 40const fits = card ({ fit :fit ( 0 ), live, loaded :[{ id :'iq3_s' , label :'Qwen3.8-Flash-Next · IQ3_S' , fit :fit ( 0 ) }], selectedLoaded :true }); 41assert . match ( fits , / This computer / ); 42assert . match ( fits , / Free right now / ); 43assert . match ( fits , / Graphics memory<\/span><b>10\.3 GB free<\/b> / ); 44for ( const gone of [ / RTX 5070 / , / graphics memory · / , / Qwen3\.8-Flash-Next / , / Loaded / , / It takes about / , / <dl / , / <dt> / , / SSD / , / pc-loaded / ]) assert . doesNotMatch ( fits , gone , String ( gone )); 45// A loaded model that really streams from the SSD keeps how fast it reads right now; nothing else comes back. 46const spills = card ({ fit :fit ( 18 ), live :{ ...live , diskReadGbs :1.2 }, loaded :[{ id :'q' , label :'Qwen3.8-Flash-Next · UD-Q4_K_XL' , fit :fit ( 18 , 12 , 53 ) }], selectedLoaded :true }); 47assert . doesNotMatch ( spills , / About 18 GB will be read / ); 48assert . match ( spills , / Reading from your SSD now: 1\.2 GB\/s / ); 49for ( const gone of [ / <dl / , / <dt> / , / Loaded / , / It takes about / , / RTX 5070 / ]) assert . doesNotMatch ( spills , gone , String ( gone )); 50assert . doesNotMatch ( spills , / This version would take / ); 51}); 52 53test ( 'before anything is loaded the card says what the chosen version would take' , () => { 54setTranslationLanguage ( 'en' ); 55assert . match ( card ({ fit :fit ( 0 ), live}), / This version would take about 12 GB of graphics memory and 44 GB of RAM\. / ); 56}); 57 58test ( 'the numbers come from the host\'s readings, polled while the page is open and read without redrawing the Models page' , () => { 59const wrapper = readFileSync ( fileURLToPath ( new URL ( '../src/components/PcComputer.tsx' , import . meta. url )), 'utf8' ); 60assert . match ( wrapper , / api\.get\('\/api\/monitor'\) / ); 61assert . match ( wrapper , / useStore\(s => tenth\(s\.monitor\?\.hardware\?\.vramUsedGb\)\) / ); 62assert . match ( wrapper , / useStore\(s => tenth\(s\.monitor\?\.hardware\?\.ramUsedGb\)\) / ); 63}); 64 65test ( 'the new words are in all nine languages' , () => { 66const en = JSON . parse ( readFileSync ( fileURLToPath ( new URL ( '../src/locales/pick-en.json' , import . meta. url )), 'utf8' )); 67for ( const lang of [ 'zh' , 'ja' , 'de' , 'es' , 'ru' , 'pt' , 'ko' , 'fr' ]) { 68const cat = JSON . parse ( readFileSync ( fileURLToPath ( new URL ( `../src/locales/pick- ${ lang } .json` , import . meta. url )), 'utf8' )); 69for ( const id of Object . keys ( en )) { 70assert . ok ( cat [ id ], ` ${ lang } ${ id } ` ); 71assert . deepEqual (( cat [ id ]. match ( / \{\d + \} / g ) ?? []). sort (), ( en [ id ]. match ( / \{\d + \} / g ) ?? []). sort (), ` ${ lang } ${ id } slots` ); 72} 73} 74});