okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e28889d12
main
1/** GPU roles (Main / Helper / Off): src/lib/gpu-roles.ts and server/gpu-roles.mjs run the vectors strata_control/gpu_roles.py runs (tests/data/gpu_roles_vectors.json). */ 2import test from 'node:test' ; 3import assert from 'node:assert/strict' ; 4import { readFile } from 'node:fs/promises' ; 5import { transform } from 'esbuild' ; 6 7const { code} = await transform ( await readFile ( new URL ( '../src/lib/gpu-roles.ts' , import . meta. url ), 'utf8' ), { loader :'ts' , format :'esm' }); 8const R = await import ( `data:text/javascript;base64, ${ Buffer . from ( code ). toString ( 'base64' )} ` ); 9const S = await import ( '../server/gpu-roles.mjs' ); 10const V = JSON . parse ( await readFile ( new URL ( '../../../tests/data/gpu_roles_vectors.json' , import . meta. url ), 'utf8' )); 11const roles = d => d === null || d === undefined ?d :Object . fromEntries ( Object . entries ( d ). map (([ k , v ]) => [ Number ( k ), v ])); 12 13test ( 'recommend: the best card is Main, others Helper, 8 GB or less / built-in / unusable start Off' , () => { 14for ( const c of V . recommend ) { 15const r = R . recommend ( V . hw [ c . hw ]); 16assert . deepEqual ( r . roles , roles ( c . roles ), c . hw ); 17assert . equal ( r . main , c . main , c . hw ); 18assert . equal ( r . why , c . why , c . hw ); 19} 20}); 21 22test ( 'setRole: one click, exactly one Main among the cards that are on' , () => { 23for ( const c of V . set_role ) { 24const out = R . setRole ( roles ( c . roles ), V . hw [ c . hw ], c . card , c . role ); 25assert . deepEqual ( out . roles , roles ( c . after ), JSON . stringify ( c )); 26assert . equal ( out . note , c . note , JSON . stringify ( c )); 27} 28}); 29 30test ( 'order: Helpers first, Main LAST, Off left out; one card is a number in the mirror' , () => { 31for ( const c of V . order ) { 32assert . deepEqual ( R . order ( roles ( c . roles ), V . hw [ c . hw ]), c . gpu , JSON . stringify ( c )); 33assert . deepEqual ( S . order ( roles ( c . roles ), V . hw [ c . hw ]), c . gpu , 'server copy: ' + JSON . stringify ( c )); 34} 35const m = R . mirror ({ 0 :'off' , 1 :'main' }, V . hw . two ); 36assert . equal ( m . gpu , 1 ); 37assert . equal ( m . gpu_order , 'as_given' ); 38assert . deepEqual ( R . mirror ({ 0 :'helper' , 1 :'main' }, V . hw . two ). gpu , [ 0 , 1 ]); 39const none = R . mirror ({ 0 :'off' , 1 :'off' }, V . hw . two ); 40assert . deepEqual ([ none . gpu , none . gpu_none ], [[], true ]); 41assert . deepEqual ( S . mirror ({ 0 :'helper' , 1 :'main' }, V . hw . two ), R . mirror ({ 0 :'helper' , 1 :'main' }, V . hw . two )); 42assert . deepEqual ( S . mirror ({ 0 :'off' , 1 :'off' }, V . hw . two ), none ); 43}); 44 45test ( 'warnings: only built-in graphics as Main, a card the engine cannot run, too little memory, CPU only' , () => { 46for ( const c of V . warnings ) { 47const w = R . warnings ( roles ( c . roles ), V . hw [ c . hw ], { spillGb :c . spill_gb ?? 0 }); 48assert . deepEqual ( w . map ( x => x . code ), c . codes , JSON . stringify ( c )); 49if ( c . fix ) assert . deepEqual ( w [ 0 ]. fix , c . fix ); 50for ( const x of w ) assert . ok ( x . text . length < 90 , x . text ); 51} 52// a slower card as Main is understood by people: no warning 53assert . deepEqual ( R . warnings ({ 0 :'main' , 1 :'helper' }, V . hw . two ), []); 54assert . equal ( R . warnings ({ 0 :'off' , 1 :'off' }, V . hw . two , { engineRunsCpuOnly :true })[ 0 ]. text , 'Performance is degraded on CPU only.' ); 55assert . equal ( R . warnings ({ 0 :'off' , 1 :'off' }, V . hw . two )[ 0 ]. text , R . NO_CARD_LINE ); 56}); 57 58test ( 'legacy gpu settings show the roles the engine really uses' , () => { 59for ( const c of V . legacy ) assert . deepEqual ( R . fromLegacy ( c . gpu , V . hw [ c . hw ], c . gpu_order ), roles ( c . roles ), JSON . stringify ( c )); 60}); 61 62test ( 'saved roles: parse; the server copy agrees on which are usable' , () => { 63assert . deepEqual ( R . parseSaved ({ 0 :'helper' , 1 :'main' }), { 0 :'helper' , 1 :'main' }); 64for ( const bad of [{ 0 :'boss' }, { 0 :'main' , 1 :'main' }, 'x' , {}, null ]) { assert . equal ( R . parseSaved ( bad ), null ); assert . equal ( S . parseSaved ( bad ), null ); } 65}); 66 67test ( 'words: the sentence and the one-line summary' , () => { 68assert . equal ( R . sentence ( V . hw . one ), 'Strata will use your RTX 4070 Ti.' ); 69assert . match ( R . sentence ( V . hw . three ), / RTX 4070 Ti is Main: it is faster\. / ); 70assert . match ( R . sentence ( V . hw . three ), / RTX 2060 starts Off: 8 GB or less\. / ); 71assert . equal ( R . describe ({ 0 :'helper' , 1 :'main' }, V . hw . two ), 'Main: GPU 2 RTX 4070 Ti. Helper: GPU 1 RTX 3060.' ); 72assert . equal ( R . noteText ( 'old_main_helper:1' , V . hw . two ), 'RTX 4070 Ti is now a Helper.' ); 73assert . equal ( R . noteText ( 'only_main' , V . hw . two ), 'The only card that is on has to be Main.' ); 74});