okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
66331f9bc
main
1import test from 'node:test' ; 2import assert from 'node:assert/strict' ; 3import { readFile } from 'node:fs/promises' ; 4import { sensorPace , ACTIVE_MS , IDLE_MS , WATCH_MS } from '../server/sensor-pace.mjs' ; 5import { readGpuTelemetry } from '../server/device-probes.mjs' ; 6 7test ( 'an idle app with the Monitor out of sight asks the card rarely (#4066)' , () => { 8const idle = sensorPace ({ live :false , watchedUntil :0 , now :1_000 }); 9assert . equal ( idle . intervalMs , IDLE_MS ); 10assert . equal ( idle . transfer , false ); 11assert . ok ( IDLE_MS >= 10 * ACTIVE_MS ); 12}); 13 14test ( 'a running model or an open Monitor keeps the 3 s pace' , () => { 15assert . equal ( sensorPace ({ live :true , now :1_000 }). intervalMs , ACTIVE_MS ); 16assert . equal ( sensorPace ({ live :true , now :1_000 }). transfer , true ); 17const watched = sensorPace ({ live :false , watchedUntil :1_000 + WATCH_MS , now :1_000 }); 18assert . equal ( watched . intervalMs , ACTIVE_MS ); 19assert . equal ( watched . transfer , false , 'the PCIe sample only means something while a model runs' ); 20assert . equal ( sensorPace ({ live :false , watchedUntil :1_000 , now :1_000 + 1 }). intervalMs , IDLE_MS ); 21}); 22 23const gpus = [{ id :'GPU-1' , vendor :'nvidia' , index :0 , sensorIndex :0 }]; 24const calls = []; 25const run = async ( cmd , args ) => { 26calls . push ( args [ 0 ]); 27return args [ 0 ] === 'dmon' ?'# gpu rxpci txpci\n# Idx MB/s MB/s\n 0 12 3' :'GPU-1, 41, 5, 1024, 30.5, 200, 4, 4, 16' ; 28}; 29 30test ( 'without the PCIe sample one nvidia-smi process is started, with it two' , async () => { 31calls . length = 0 ; 32const idle = await readGpuTelemetry ( gpus , { transfer :false , run}); 33assert . deepEqual ( calls . map ( c => c === 'dmon' ?'dmon' :'query' ), [ 'query' ]); 34assert . equal ( idle [ 0 ]. temperatureC , 41 ); 35calls . length = 0 ; 36await readGpuTelemetry ( gpus , { run}); 37assert . deepEqual ( calls . map ( c => c === 'dmon' ?'dmon' :'query' ), [ 'query' , 'dmon' ]); 38}); 39 40test ( 'the monitor uses the pace, the Monitor screen asks while it is open, and /api/monitor marks it watched' , async () => { 41const monitor = await readFile ( new URL ( '../server/monitor.mjs' , import . meta. url ), 'utf8' ); 42assert . match ( monitor , / sensorPace\( / ); 43assert . doesNotMatch ( monitor , / sensorsAt>=3000 / ); 44const index = await readFile ( new URL ( '../server/index.mjs' , import . meta. url ), 'utf8' ); 45assert . match ( index , / monitor\.watch\(\); if \(url\.searchParams\.get\('drives'\) === '1'\) monitor\.watchDrives\(\); return json\(res, 200, monitor\.snapshot\(\)\) / ); 46assert . match ( index , / monitor\.setLiveProbe\( / ); 47const screen = await readFile ( new URL ( '../src/screens/Monitor.tsx' , import . meta. url ), 'utf8' ); 48assert . match ( screen , / api\.get\('\/api\/monitor\?drives=1'\) / ); 49}); 50 51test ( 'the drive-temperature reader starts only while the Monitor is watched and ends after it is gone (#4661)' , async () => { 52const monitor = await readFile ( new URL ( '../server/monitor.mjs' , import . meta. url ), 'utf8' ); 53assert . match ( monitor , / STRATA_REAL_ENGINE==='1'&&Date\.now\(\)<drivesUntil&&Date\.now\(\)-hostStartedAt>DRIVES_AFTER_MS\?readTemperatures\(\) / ); 54assert . match ( monitor , / stopTemperatures\(\) / ); 55const { readTemperatures, stopTemperatures} = await import ( '../server/temperature-telemetry.mjs' ); 56assert . equal ( typeof stopTemperatures , 'function' ); 57// nothing is started by importing or by stopping: on a PC that never opens the Monitor no PowerShell and no disk query exists 58stopTemperatures (); 59assert . deepEqual ( await readTemperatures ({ platform :'darwin' }), { cpuTempC :null , ssdTempC :null }); 60}); 61 62test ( '#4661: the drive reader asks NVMe drives only, leaves a marker across a crash, and gives up a stuck first pass' , async () => { 63const { readFile} = await import ( 'node:fs/promises' ); 64const { EventEmitter} = await import ( 'node:events' ); 65const { createWindowsSsdReader} = await import ( '../server/temperature-telemetry.mjs' ); 66const source = await readFile ( new URL ( '../server/temperature-telemetry.mjs' , import . meta. url ), 'utf8' ); 67assert . doesNotMatch ( source , / foreach\(\$n in 0\.\.15\) / , 'no blind walk over PhysicalDrive0..15' ); 68assert . match ( source , / BusType -eq 'NVMe' / ); 69const fake = () => Object . assign ( new EventEmitter (), { stdout :Object . assign ( new EventEmitter (), { unref () {} }), unref () {}, kill () { this . killed = true ; } }); 70const marker = () => { const m = { has :false , exists :() => m . has , write :() => { m . has = true ; }, remove :() => { m . has = false ; } }; return m ; }; 71// a healthy first pass: the marker is there while the drives are asked and gone after the first answer 72let clock = 1000 , child = fake (), m = marker (); 73let reader = createWindowsSsdReader ({ spawnFn :() => child , now :() => clock , sentinel :m }); 74assert . equal ( reader . read (), null ); 75assert . equal ( m . has , true , 'the marker is left before the first question' ); 76child . stdout . emit ( 'data' , 'ssd 44\r\n' ); 77assert . equal ( m . has , false , 'the first answer clears it' ); 78assert . equal ( reader . read (), 44 ); 79// the host (or the PC) died in the middle of a question: the marker is still there at the next start, and the drives are never asked again 80let spawned = 0 ; 81m = marker (); m . has = true ; 82reader = createWindowsSsdReader ({ spawnFn :() => { spawned ++ ; return fake (); }, now :() => clock , sentinel :m }); 83assert . equal ( reader . read (), null ); 84assert . equal ( spawned , 0 , 'a marker from a crashed question means nothing is started' ); 85assert . equal ( reader . off , true ); 86// a first pass that never answers is ended for good and the marker stays 87child = fake (); m = marker (); clock = 1000 ; 88reader = createWindowsSsdReader ({ spawnFn :() => child , now :() => clock , sentinel :m , firstPassMs :45000 }); 89reader . read (); 90clock += 46000 ; 91assert . equal ( reader . read (), null ); 92assert . equal ( child . killed , true ); 93assert . equal ( reader . off , true ); 94assert . equal ( m . has , true ); 95// a marker that cannot be written means no question either 96spawned = 0 ; 97reader = createWindowsSsdReader ({ spawnFn :() => { spawned ++ ; return fake (); }, now :() => clock , sentinel :{ exists :() => false , write :() => { throw new Error ( 'read-only' ); }, remove () {} } }); 98assert . equal ( reader . read (), null ); 99assert . equal ( spawned , 0 ); 100}); 101 102test ( '#4661: only the Monitor screen asks for the drives; the start-up fetch and the Models page do not' , async () => { 103const { readFile, readdir} = await import ( 'node:fs/promises' ); 104const { join} = await import ( 'node:path' ); 105const hits = []; 106const walk = async dir => { for ( const entry of await readdir ( dir , { withFileTypes :true })) { const p = join ( dir , entry . name ); if ( entry . isDirectory ()) await walk ( p ); else if ( / \. ( ts | tsx ) $ / . test ( entry . name ) && / drives=1 / . test ( await readFile ( p , 'utf8' ))) hits . push ( entry . name ); } }; 107await walk ( new URL ( '../src' , import . meta. url ). pathname . replace ( / ^\/ ([ A - Za - z ] : ) / , '$1' )); 108assert . deepEqual ( hits , [ 'Monitor.tsx' ]); 109const monitor = await import ( '../server/monitor.mjs' ); 110assert . equal ( typeof monitor . watchDrives , 'function' ); 111assert . ok ( monitor . DRIVES_AFTER_MS >= 60000 ); 112});