okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3cabb9e3f
main
1/** The engine inside the installer: installed when missing or older, never over a newer or equal one, never without a matching manifest. */ 2import test from 'node:test' ; 3import assert from 'node:assert/strict' ; 4import { mkdtempSync , mkdirSync , writeFileSync , readFileSync , existsSync , readdirSync , rmSync } from 'node:fs' ; 5import { createHash } from 'node:crypto' ; 6import { tmpdir } from 'node:os' ; 7import { join } from 'node:path' ; 8import { installBundledEngine , removeVerifiedBundle , cleanupAfterInstall , compareVersions , wrongMakeNotice } from '../server/bundled-engine.mjs' ; 9 10// the make of card is read from this computer unless a test names it; the existing tests are written for an NVIDIA PC 11process . env . STRATA_ENGINE_BACKEND = 'cuda' ; 12const sha = buffer => createHash ( 'sha256' ). update ( buffer ). digest ( 'hex' ); 13function makePackage ( root , version , { body= 'engine-' + version , backend= 'cuda' , platform= 'windows-x86_64' , variant= 'cuda13' , minDriver= null } = {}) { 14const name = ` ${ version } - ${ platform } - ${ backend } - ${ variant } ` ; 15const dir = join ( root , name ); 16mkdirSync ( dir , { recursive :true }); 17const files = { 'strata.exe' :Buffer . from ( body ), 'strata_kq.dll' :Buffer . from ( 'kq-' + body ), 'BUILD.json' :Buffer . from ( '{}' ) }; 18for ( const [ file , data ] of Object . entries ( files )) writeFileSync ( join ( dir , file ), data ); 19writeFileSync ( join ( dir , 'engine-component.json' ), JSON . stringify ({ 20schema_version :2 , id :'strata-engine' , name :'strata-engine' , version, platform, backend, archs :[ 'sm_86' ], ...( minDriver ?{ min_driver :{ nvidia :minDriver } } :{}), build_id :'x' , entrypoint :'strata.exe' , 21files :Object . entries ( files ). map (([ path , data ]) => ({ path, sha256 :sha ( data ), bytes :data . length })), signature :null })); 22return name ; 23} 24const setup = () => { 25const base = mkdtempSync ( join ( tmpdir (), 'st-bundled-engine-' )); 26const bundle = join ( base , 'engine-bundle' ), components = join ( base , 'components' ); 27mkdirSync ( bundle ); mkdirSync ( components ); 28return { base, bundle, components, engine :join ( components , 'engine' ), cleanup :() => rmSync ( base , { recursive :true , force :true }) }; 29}; 30 31test ( 'versions compare by number, not text' , () => { 32assert . equal ( compareVersions ( '0.1.45' , '0.1.9' ), 1 ); 33assert . equal ( compareVersions ( '0.1.45' , '0.1.45.1' ), - 1 ); 34assert . equal ( compareVersions ( '0.1.45' , '0.1.45' ), 0 ); 35}); 36 37test ( 'no engine installed: the bundled one is installed, hash-checked, with nothing left over' , async () => { 38const t = setup (); 39try { 40const name = makePackage ( t . bundle , '0.1.45' ); 41const result = await installBundledEngine ({ bundleDir :t . bundle , components :t . components }); 42assert . equal ( result . action , 'installed' ); 43assert . equal ( readFileSync ( join ( t . engine , name , 'strata.exe' ), 'utf8' ), 'engine-0.1.45' ); 44assert . ok ( existsSync ( join ( t . engine , name , 'engine-component.json' ))); 45assert . deepEqual ( readdirSync ( t . engine ), [ name ], 'no temporary folder remains' ); 46assert . equal (( await installBundledEngine ({ bundleDir :t . bundle , components :t . components })). action , 'kept' , 'a second start does nothing' ); 47} finally { t . cleanup (); } 48}); 49 50test ( 'an older engine is installed: the bundled one is added beside it' , async () => { 51const t = setup (); 52try { 53const old = makePackage ( t . engine , '0.1.44' ); 54const name = makePackage ( t . bundle , '0.1.45' ); 55assert . equal (( await installBundledEngine ({ bundleDir :t . bundle , components :t . components })). action , 'installed' ); 56assert . deepEqual ( readdirSync ( t . engine ). sort (), [ old , name ]. sort ()); 57} finally { t . cleanup (); } 58}); 59 60test ( 'a newer or equal engine is never overwritten or touched' , async () => { 61const t = setup (); 62try { 63const newer = makePackage ( t . engine , '0.1.46' , { body :'mine' }); 64makePackage ( t . bundle , '0.1.45' ); 65assert . equal (( await installBundledEngine ({ bundleDir :t . bundle , components :t . components })). action , 'kept' ); 66assert . deepEqual ( readdirSync ( t . engine ), [ newer ]); 67assert . equal ( readFileSync ( join ( t . engine , newer , 'strata.exe' ), 'utf8' ), 'mine' ); 68// same version, other build: the installed one stays 69const same = makePackage ( t . engine , '0.1.45' , { body :'installed-build' }); 70assert . equal (( await installBundledEngine ({ bundleDir :t . bundle , components :t . components })). action , 'kept' ); 71assert . equal ( readFileSync ( join ( t . engine , same , 'strata.exe' ), 'utf8' ), 'installed-build' ); 72} finally { t . cleanup (); } 73}); 74 75test ( 'a damaged copy of the same package is put right; an engine for another backend does not count' , async () => { 76const t = setup (); 77try { 78const name = makePackage ( t . bundle , '0.1.45' ); 79makePackage ( t . engine , '0.1.45' ); 80writeFileSync ( join ( t . engine , name , 'strata.exe' ), 'cut' ); // wrong size 81assert . equal (( await installBundledEngine ({ bundleDir :t . bundle , components :t . components })). action , 'repaired' ); 82assert . equal ( readFileSync ( join ( t . engine , name , 'strata.exe' ), 'utf8' ), 'engine-0.1.45' ); 83const t2 = setup (); 84try { 85makePackage ( t2 . engine , '0.1.50' , { backend :'cpu' }); 86makePackage ( t2 . bundle , '0.1.45' ); 87assert . equal (( await installBundledEngine ({ bundleDir :t2 . bundle , components :t2 . components })). action , 'installed' ); 88} finally { t2 . cleanup (); } 89} finally { t . cleanup (); } 90}); 91 92test ( 'a bundle that does not match its manifest is refused and installs nothing' , async () => { 93const t = setup (); 94try { 95const name = makePackage ( t . bundle , '0.1.45' ); 96writeFileSync ( join ( t . bundle , name , 'strata_kq.dll' ), 'kq-engine-0.1.4X' ); // same size, other bytes 97const result = await installBundledEngine ({ bundleDir :t . bundle , components :t . components }); 98assert . equal ( result . action , 'refused' ); 99assert . equal ( existsSync ( join ( t . engine , name )), false ); 100assert . equal (( await installBundledEngine ({ bundleDir :join ( t . base , 'nope' ), components :t . components })). action , 'none' ); 101} finally { t . cleanup (); } 102}); 103 104test ( 'CUDA 13 and CUDA 12 builds of one version are different engines: both are installed when the driver runs both' , async () => { 105const t = setup (); 106try { 107const n13 = makePackage ( t . bundle , '0.1.45' , { platform :'linux-x86_64' , variant :'cuda13' , minDriver :'580.65' , body :'thirteen' }); 108const n12 = makePackage ( t . bundle , '0.1.45' , { platform :'linux-x86_64' , variant :'cuda12' , minDriver :'575.51' , body :'twelve' }); 109assert . equal (( await installBundledEngine ({ bundleDir :t . bundle , components :t . components , driver :'580.178.04' })). action , 'installed' ); 110assert . deepEqual ( readdirSync ( t . engine ). sort (), [ n12 , n13 ]. sort ()); 111assert . equal (( await installBundledEngine ({ bundleDir :t . bundle , components :t . components , driver :'580.178.04' })). action , 'kept' , 'a second start does nothing' ); 112} finally { t . cleanup (); } 113}); 114 115test ( 'an older driver gets only the build it can run; a driver below both gets the lowest build so the app can name the driver' , async () => { 116const t = setup (), u = setup (), v = setup (); 117try { 118const n13 = makePackage ( t . bundle , '0.1.45' , { platform :'linux-x86_64' , variant :'cuda13' , minDriver :'580.65' }); 119const n12 = makePackage ( t . bundle , '0.1.45' , { platform :'linux-x86_64' , variant :'cuda12' , minDriver :'575.51' }); 120await installBundledEngine ({ bundleDir :t . bundle , components :t . components , driver :'575.57.08' }); 121assert . deepEqual ( readdirSync ( t . engine ), [ n12 ], 'driver 575 runs CUDA 12 only' ); 122for ( const x of [ u ]) { makePackage ( x . bundle , '0.1.45' , { platform :'linux-x86_64' , variant :'cuda13' , minDriver :'580.65' }); makePackage ( x . bundle , '0.1.45' , { platform :'linux-x86_64' , variant :'cuda12' , minDriver :'575.51' }); } 123await installBundledEngine ({ bundleDir :u . bundle , components :u . components , driver :'550.120' }); 124assert . deepEqual ( readdirSync ( u . engine ), [ n12 ], 'driver 550 runs neither: the CUDA 12 build is installed so Control can say 575 is needed' ); 125// no driver at all (a PC without NVIDIA, or nvidia-smi cannot be read): nothing is held back 126makePackage ( v . bundle , '0.1.45' , { platform :'linux-x86_64' , variant :'cuda13' , minDriver :'580.65' }); makePackage ( v . bundle , '0.1.45' , { platform :'linux-x86_64' , variant :'cuda12' , minDriver :'575.51' }); 127await installBundledEngine ({ bundleDir :v . bundle , components :v . components , driver :null }); 128assert . deepEqual ( readdirSync ( v . engine ). sort (), [ n12 , n13 ]. sort ()); 129} finally { t . cleanup (); u . cleanup (); v . cleanup (); } 130}); 131 132// The app update (latest.json) is built without engine-bundle (tauri.update.conf.json), so one update fits every installer variant. 133test ( 'an engine-less app update (no engine-bundle folder, or one holding only .gitignore) leaves the installed engines exactly as they are' , async () => { 134const t = setup (); 135try { 136const amd = makePackage ( t . engine , '0.1.45.3' , { platform :'windows-x86_64' , backend :'hip' , variant :'rx9070' }); 137const before = readFileSync ( join ( t . engine , amd , 'engine-component.json' ), 'utf8' ); 138assert . equal (( await installBundledEngine ({ bundleDir :join ( t . base , 'no-such-engine-bundle' ), components :t . components })). action , 'none' ); 139writeFileSync ( join ( t . bundle , '.gitignore' ), '*\n!.gitignore\n' ); 140assert . equal (( await installBundledEngine ({ bundleDir :t . bundle , components :t . components , platform :'windows-x86_64' })). action , 'none' ); 141assert . deepEqual ( readdirSync ( t . engine ), [ amd ]); 142assert . equal ( readFileSync ( join ( t . engine , amd , 'engine-component.json' ), 'utf8' ), before ); 143} finally { t . cleanup (); } 144}); 145 146test ( 'why the update carries no engine: a CUDA installer on an AMD PC would copy its CUDA engine beside the AMD one (no NVIDIA driver to filter by)' , async () => { 147const t = setup (); 148try { 149const amd = makePackage ( t . engine , '0.1.45.3' , { platform :'windows-x86_64' , backend :'hip' , variant :'rx9070' }); 150const cuda = makePackage ( t . bundle , '0.1.45.3' , { platform :'windows-x86_64' , variant :'cuda13' , minDriver :'580.65' }); 151assert . equal (( await installBundledEngine ({ bundleDir :t . bundle , components :t . components , driver :null })). action , 'installed' ); 152assert . deepEqual ( readdirSync ( t . engine ). sort (), [ amd , cuda ]. sort (), 'the AMD engine stays; a useless CUDA engine is added' ); 153} finally { t . cleanup (); } 154}); 155 156test ( 'an AMD build on an NVIDIA PC is not unpacked and the sentence names the right download' , async () => { 157const t = setup (); 158try { 159const name = makePackage ( t . bundle , '0.2.0' , { platform :'linux-x86_64' , backend :'hip' , variant :'rx9070' }); 160const manifest = JSON . parse ( readFileSync ( join ( t . bundle , name , 'engine-component.json' ), 'utf8' )); 161manifest . archs = [ 'gfx1201' ]; 162writeFileSync ( join ( t . bundle , name , 'engine-component.json' ), JSON . stringify ( manifest )); 163const result = await installBundledEngine ({ bundleDir :t . bundle , components :t . components , backend :'cuda' }); 164assert . equal ( result . action , 'wrong-make' ); 165assert . equal ( result . notice , 'This Strata is for AMD RX 9000 cards. Your PC has an NVIDIA card: download the NVIDIA version from https://github.com/Niko1221/Strata/releases' ); 166assert . equal ( existsSync ( t . engine ), false , 'nothing was copied' ); 167// the same build on an AMD PC is unpacked; an unknown card is not a reason to refuse 168assert . equal (( await installBundledEngine ({ bundleDir :t . bundle , components :t . components , backend :'hip' })). action , 'installed' ); 169assert . equal (( await installBundledEngine ({ bundleDir :t . bundle , components :t . components , backend :null })). action , 'kept' ); 170} finally { t . cleanup (); } 171}); 172 173test ( 'wrongMakeNotice names the make, not a list of reasons' , () => { 174assert . equal ( wrongMakeNotice ({ backend :'cuda' , archs :[ 'sm_86' ] }, 'hip' ), 'This Strata is for NVIDIA cards. Your PC has an AMD card: download the AMD version from https://github.com/Niko1221/Strata/releases' ); 175}); 176 177test ( 'Windows: the display adapters decide the make, and a leftover nvidia-smi does not outvote them (#3688)' , async () => { 178const { gpuBackendHere, windowsDisplayVendors} = await import ( '../server/bundled-engine.mjs' ); 179assert . equal ( gpuBackendHere ({}, { vendors :new Set ([ '1002' ]) }), 'hip' ); 180assert . equal ( gpuBackendHere ({}, { vendors :new Set ([ '10de' , '1002' ]) }), 'cuda' ); 181assert . equal ( gpuBackendHere ({}, { vendors :new Set ([ '8086' ]) }), 'sycl' ); 182assert . equal ( gpuBackendHere ({ STRATA_ENGINE_BACKEND :'cuda' }, { vendors :new Set ([ '1002' ]) }), 'cuda' , 'an explicit choice wins' ); 183const out = 'MatchingDeviceId REG_SZ PCI/VEN_1002&DEV_7551&SUBSYS_0' + String . fromCharCode ( 10 ) + 'MatchingDeviceId REG_SZ PCI/VEN_8086&DEV_46A6' ; 184assert . deepEqual ([ ...windowsDisplayVendors ({ platform :'win32' , run :() => out })]. sort (), [ '1002' , '8086' ]); 185assert . equal ( windowsDisplayVendors ({ platform :'linux' , run :() => { throw new Error ( 'must not run' ); } }). size , 0 ); 186assert . equal ( windowsDisplayVendors ({ platform :'win32' , run :() => { throw new Error ( 'no registry' ); } }). size , 0 ); 187}); 188 189test ( 'after an update, the engine bundle of the old install folder is removed only when the same engine is complete in the store' , async () => { 190const t = setup (); 191try { 192const name = makePackage ( t . bundle , '0.1.48' ); 193writeFileSync ( join ( t . bundle , '.gitignore' ), 'ignore-all' ); 194const lines = []; 195// 1. nothing in the store yet: kept, and one line says why 196let r = await removeVerifiedBundle ({ bundleDir :t . bundle , components :t . components , log :l => lines . push ( l ) }); 197assert . equal ( r . removed , false ); 198assert . equal ( lines . length , 1 ); 199assert . match ( lines [ 0 ], / kept in the install folder. * not \(or not completely\) in the engine store / ); 200assert . ok ( existsSync ( join ( t . bundle , name ))); 201// 2. the engine is copied (the app's own first start does this) 202assert . equal (( await installBundledEngine ({ bundleDir :t . bundle , components :t . components })). action , 'installed' ); 203// 3. a store file with the right size and the wrong content: kept (the hash is checked, not only the size) 204const target = join ( t . engine , name , 'strata.exe' ); 205const good = readFileSync ( target ); 206writeFileSync ( target , Buffer . alloc ( good . length , 'x' )); 207lines . length = 0 ; 208r = await removeVerifiedBundle ({ bundleDir :t . bundle , components :t . components , log :l => lines . push ( l ) }); 209assert . equal ( r . removed , false ); 210assert . equal ( lines . length , 1 , 'exactly one line' ); 211assert . ok ( existsSync ( join ( t . bundle , name ))); 212// 4. a store file with the wrong size: kept 213writeFileSync ( target , Buffer . concat ([ good , Buffer . from ( 'extra' )])); 214assert . equal (( await removeVerifiedBundle ({ bundleDir :t . bundle , components :t . components })). removed , false ); 215// 5. the store copy is whole: the bundle goes, its .gitignore stays, and the store is untouched 216writeFileSync ( target , good ); 217lines . length = 0 ; 218r = await removeVerifiedBundle ({ bundleDir :t . bundle , components :t . components , log :l => lines . push ( l ) }); 219assert . equal ( r . removed , true ); 220assert . equal ( existsSync ( join ( t . bundle , name )), false ); 221assert . ok ( existsSync ( join ( t . bundle , '.gitignore' ))); 222assert . equal ( readFileSync ( target , 'utf8' ), 'engine-0.1.48' ); 223assert . match ( lines . at ( - 1 ), / was removed; the same engine is complete in the engine store / ); 224// 6. nothing left to do 225assert . equal (( await removeVerifiedBundle ({ bundleDir :t . bundle , components :t . components })). removed , false ); 226// 7. a different version in the store does not count 227const other = makePackage ( t . bundle , '0.1.49' ); 228assert . equal (( await removeVerifiedBundle ({ bundleDir :t . bundle , components :t . components })). removed , false ); 229assert . ok ( existsSync ( join ( t . bundle , other ))); 230} finally { t . cleanup (); } 231}); 232 233test ( 'the bundle cleanup lines reach the output of the host (the desktop log copies it), for "removed" and for "kept"' , async () => { 234const t = setup (); 235const seen = [], real = console . log ; 236try { 237const name = makePackage ( t . bundle , '0.1.48' ); 238console . log = line => { seen . push ( String ( line )); }; 239// kept: the engine is not in the store yet 240await cleanupAfterInstall ( Promise . resolve ({ action :'kept' }), { bundleDir :t . bundle , components :t . components }); 241assert . equal ( seen . length , 1 ); 242assert . match ( seen [ 0 ], / ^engine bundle: kept in the install folder / ); 243// removed: the engine is in the store and whole 244const installed = await installBundledEngine ({ bundleDir :t . bundle , components :t . components }); 245assert . equal ( installed . action , 'installed' ); 246seen . length = 0 ; 247await cleanupAfterInstall ( Promise . resolve ({ action :'kept' }), { bundleDir :t . bundle , components :t . components }); 248assert . equal ( seen . length , 1 ); 249assert . match ( seen [ 0 ], / ^engine bundle: the copy that came with the installer \(. * \) was removed / ); 250assert . equal ( existsSync ( join ( t . bundle , name )), false ); 251// nothing to say when the engine step did nothing (no bundle) or was refused 252seen . length = 0 ; 253await cleanupAfterInstall ( Promise . resolve ({ action :'none' }), { bundleDir :t . bundle , components :t . components }); 254await cleanupAfterInstall ( Promise . resolve ({ action :'wrong-make' }), { bundleDir :t . bundle , components :t . components }); 255await cleanupAfterInstall ( Promise . reject ( new Error ( 'boom' )), { bundleDir :t . bundle , components :t . components }); 256assert . deepEqual ( seen , []); 257} finally { console . log = real ; t . cleanup (); } 258}); 259 260test ( 'macOS: the engine bundle inside a signed .app stays (deleting it breaks the signature); outside an .app, or on another system, it is removed as before' , async () => { 261const t = setup (); 262const platform = Object . getOwnPropertyDescriptor ( process , 'platform' ); 263const as = value => Object . defineProperty ( process , 'platform' , { value, configurable :true }); 264try { 265const app = join ( t . base , 'Strata.app' , 'Contents' , 'Resources' , 'engine-bundle' ); 266mkdirSync ( app , { recursive :true }); 267const inApp = makePackage ( app , '0.1.48' ); 268const outside = makePackage ( t . bundle , '0.1.48' ); 269// the engine is in the store (the app's own first start copies it), so the bundle would be removable 270assert . equal (( await installBundledEngine ({ bundleDir :t . bundle , components :t . components })). action , 'installed' ); 271const lines = []; 272// 1. macOS, bundle inside Strata.app: kept, with one line that says why 273as ( 'darwin' ); 274let r = await removeVerifiedBundle ({ bundleDir :app , components :t . components , log :l => lines . push ( l ) }); 275assert . equal ( r . removed , false ); 276assert . ok ( existsSync ( join ( app , inApp , 'strata.exe' )), 'the signed app keeps its engine files' ); 277assert . equal ( lines . length , 1 ); 278assert . match ( lines [ 0 ], / kept in the install folder. * macOS app bundles must remain unchanged after signing / ); 279// 2. macOS, bundle outside an .app: removed as before 280r = await removeVerifiedBundle ({ bundleDir :t . bundle , components :t . components , log :() => {} }); 281assert . equal ( r . removed , true ); 282assert . equal ( existsSync ( join ( t . bundle , outside )), false ); 283// 3. another system, bundle inside a folder called .app: removed as before (the guard is for macOS only) 284as ( 'linux' ); 285r = await removeVerifiedBundle ({ bundleDir :app , components :t . components , log :() => {} }); 286assert . equal ( r . removed , true ); 287assert . equal ( existsSync ( join ( app , inApp )), false ); 288} finally { 289Object . defineProperty ( process , 'platform' , platform ); 290t . cleanup (); 291} 292}); 293 294test ( 'a card taken out of the PC does not decide the make; two present makes accept a build for either (#3892)' , async () => { 295const { gpuBackendsHere, windowsDisplayVendors} = await import ( '../server/bundled-engine.mjs' ); 296const nl = String . fromCharCode ( 10 ); 297const registry = 'MatchingDeviceId REG_SZ PCI/VEN_10DE&DEV_2882' + nl + 'MatchingDeviceId REG_SZ PCI/VEN_1002&DEV_7550' ; 298// the NVIDIA card was swapped for a Radeon: Windows still lists its driver entry, but only the AMD card is present 299assert . deepEqual ([ ...windowsDisplayVendors ({ platform :'win32' , run :() => registry , present :() => new Set ([ '1002' ]) })], [ '1002' ]); 300assert . equal ( gpuBackendsHere ({}, { vendors :new Set ([ '1002' ]) })[ 0 ], 'hip' ); 301// both cards present: both makes count, NVIDIA first 302assert . deepEqual ([ ...windowsDisplayVendors ({ platform :'win32' , run :() => registry , present :() => new Set ([ '1002' , '10de' ]) })]. sort (), [ '1002' , '10de' ]); 303assert . deepEqual ( gpuBackendsHere ({}, { vendors :new Set ([ '10de' , '1002' ]) }), [ 'cuda' , 'hip' ]); 304// present cannot be read: the registry answer stands 305assert . equal ( windowsDisplayVendors ({ platform :'win32' , run :() => registry , present :() => new Set () }). size , 2 ); 306// the second source is only asked when two makes are listed 307assert . equal ( windowsDisplayVendors ({ platform :'win32' , run :() => 'MatchingDeviceId REG_SZ PCI/VEN_1002&DEV_7550' , present :() => { throw new Error ( 'must not run' ); } }). size , 1 ); 308assert . deepEqual ( gpuBackendsHere ({ STRATA_ENGINE_BACKEND :'hip' }, { vendors :new Set ([ '10de' , '1002' ]) }), [ 'hip' ]); 309 310const t = setup (); 311try { 312const name = makePackage ( t . bundle , '0.2.0' , { platform :'windows-x86_64' , backend :'hip' , variant :'rx9070' }); 313const manifest = JSON . parse ( readFileSync ( join ( t . bundle , name , 'engine-component.json' ), 'utf8' )); 314manifest . archs = [ 'gfx1201' ]; 315writeFileSync ( join ( t . bundle , name , 'engine-component.json' ), JSON . stringify ( manifest )); 316// an AMD build on a PC with an NVIDIA desktop card and an AMD model card is unpacked, not refused 317const result = await installBundledEngine ({ bundleDir :t . bundle , components :t . components , backend :[ 'cuda' , 'hip' ] }); 318assert . equal ( result . action , 'installed' ); 319} finally { t . cleanup (); } 320});