okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
a1cc285fa
main
1/** Settings shows the last known update check from this computer and never asks the network when it opens. */ 2import test from 'node:test' ; 3import assert from 'node:assert/strict' ; 4import { readFile } from 'node:fs/promises' ; 5import { transform } from 'esbuild' ; 6 7const load = async name => { 8const { code} = await transform ( await readFile ( new URL ( `../src/lib/ ${ name } .ts` , import . meta. url ), 'utf8' ), { loader :'ts' , format :'esm' }); 9return import ( `data:text/javascript;base64, ${ Buffer . from ( code ). toString ( 'base64' )} ` ); 10}; 11const { readUpdateCache, saveUpdateCache, clearUpdateCache} = await load ( 'update-cache' ); 12 13const store = new Map (); 14globalThis . localStorage = { getItem :k => store . get ( k ) ?? null , setItem :( k , v ) => void store . set ( k , String ( v )), removeItem :k => void store . delete ( k ) }; 15const result = ( status = 'available' ) => ({ checkedAt :'2026-10-19T10:00:00Z' , strata :{ status :'current' , message :'ok' }, engine :{ status, message :'x' } }); 16 17test ( 'a saved check is read back for the same installed versions' , () => { 18clearUpdateCache (); 19assert . equal ( readUpdateCache ({ appVersion :'0.2.0' , engineVersion :'0.1.45' }), null , 'never checked' ); 20saveUpdateCache ( result (), { appVersion :'0.2.0' , engineVersion :'0.1.45' }); 21assert . equal ( readUpdateCache ({ appVersion :'0.2.0' , engineVersion :'0.1.45' }). engine . status , 'available' ); 22}); 23 24test ( 'a check made for other versions is ignored, so an old "Update available" does not linger after an update' , () => { 25saveUpdateCache ( result (), { appVersion :'0.2.0' , engineVersion :'0.1.45' }); 26assert . equal ( readUpdateCache ({ appVersion :'0.2.0' , engineVersion :'0.1.46' }), null ); 27assert . equal ( readUpdateCache ({ appVersion :'0.2.1' , engineVersion :'0.1.45' }), null ); 28}); 29 30test ( 'a new Control version, or a version that is not known yet, hides the old result' , () => { 31saveUpdateCache ( result (), { appVersion :'0.2.0' , engineVersion :'0.1.45' , controlVersion :'0.1.45' }); 32assert . equal ( readUpdateCache ({ appVersion :'0.2.0' , engineVersion :'0.1.45' , controlVersion :'0.1.45' }). engine . status , 'available' ); 33assert . equal ( readUpdateCache ({ appVersion :'0.2.0' , engineVersion :'0.1.45' , controlVersion :'0.1.46' }), null , 'Control changed' ); 34assert . equal ( readUpdateCache ({ appVersion :null , engineVersion :'0.1.45' , controlVersion :'0.1.45' }), null , 'app version still loading' ); 35assert . equal ( readUpdateCache ({ appVersion :'0.2.0' , engineVersion :'0.1.45' }), null , 'Control version not known' ); 36}); 37 38test ( 'unreadable or blocked storage is harmless' , () => { 39store . set ( 'strata.update-check.v1' , '{not json' ); 40assert . equal ( readUpdateCache ({}), null ); 41const saved = globalThis . localStorage ; 42globalThis . localStorage = { getItem () { throw new Error ( 'blocked' ); }, setItem () { throw new Error ( 'blocked' ); }, removeItem () { throw new Error ( 'blocked' ); } }; 43assert . equal ( readUpdateCache ({}), null ); 44assert . doesNotThrow (() => saveUpdateCache ( result (), {})); 45assert . doesNotThrow (() => clearUpdateCache ()); 46globalThis . localStorage = saved ; 47}); 48 49test ( 'opening the Engine and Update settings makes no update call: only a button press does' , async () => { 50const src = async name => ( await readFile ( new URL ( `../src/ ${ name } ` , import . meta. url ), 'utf8' )). replace ( / \r\n / g , '\n' ); 51const ops = await src ( 'components/NativeOperations.tsx' ), upd = await src ( 'components/SettingsUpdates.tsx' ), lib = await src ( 'lib/update-check.ts' ); 52assert . doesNotMatch ( ops , / \/api\/updates\/check / , 'NativeOperations has no direct call' ); 53assert . doesNotMatch ( upd , / \/api\/updates\/check / ); 54assert . equal ( lib . match ( / \/api\/updates\/check / g ). length , 1 , 'one place asks the host' ); 55// every caller of the check sits inside a function that only a click starts 56// Settings > Engine no longer checks or updates at all (it lists, switches and deletes installed engines); About does 57assert . doesNotMatch ( ops , / fetchUpdateCheck | checkForUpdates / , 'the Engine tab has no update check' ); 58for ( const [ name , text ] of [[ 'SettingsUpdates' , upd ]]) { 59const body = text . replace ( / async function ( checkForUpdates | check ) \(\) \{ [ \s\S ] * ?\n \}\n / , '' ); 60assert . doesNotMatch ( body , / fetchUpdateCheck\( / , ` ${ name } : the check runs only inside the button handler` ); 61assert . match ( text , / void ( checkForUpdates | check ) \(\) / , ` ${ name } : a button starts it` ); 62} 63// nothing in either component runs a check from an effect 64for ( const text of [ ops , upd ]) for ( const effect of text . match ( / useEffect\(\(\) => \{ [ \s\S ] * ?\n \}, \[ [ ^ \] ] * \]\); / g ) ?? []) assert . doesNotMatch ( effect , / fetchUpdateCheck | updates\/check / ); 65});