okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
44d885874
main
1import { fileURLToPath } from 'node:url' ; 2import test from 'node:test' ; 3import assert from 'node:assert/strict' ; 4import { readFile , mkdtemp , mkdir , writeFile , rm } from 'node:fs/promises' ; 5import { tmpdir } from 'node:os' ; 6import { join } from 'node:path' ; 7import { build } from 'esbuild' ; 8import { parseCpuTopology , parseWindowsProcessors , parseNvidiaDevices , parseNvidiaReadings , readGpuTelemetry } from '../server/device-probes.mjs' ; 9import { CONTEXT_TOKENS , expertParams } from '../server/catalog.mjs' ; 10import { buildArgs } from '../server/supervisor.mjs' ; 11async function loadTs ( path ) { 12const result = await build ({ entryPoints :[ fileURLToPath ( new URL ( path , import . meta. url ))], bundle :true , write :false , platform :'node' , format :'esm' }); const code = result . outputFiles [ 0 ]. text ; 13return import ( `data:text/javascript;base64, ${ Buffer . from ( code ). toString ( 'base64' )} ` ); 14} 15const { usageDashboard, calendarDays} = await loadTs ( '../src/lib/usage-dashboard.ts' ); 16const { modelFamilies, modelVersions} = await loadTs ( '../src/lib/model-versions.ts' ); 17const { pickerVersionState, pickerVersions} = await loadTs ( '../src/lib/model-picker.ts' ); 18const { contextTokens} = await loadTs ( '../src/lib/experience.ts' ); 19const { configDocument} = await loadTs ( '../src/lib/config.ts' ); 20 21test ( '128K recommendation reaches saved config and launch arguments while explicit context is preserved' ,() => { 22const engine = { beginner :{ contextLength :'recommended' }, expert :expertParams ( 8 )}; 23const recommended = configDocument ( engine ); 24assert . equal ( contextTokens ( 'recommended' ), 131072 ); 25assert . equal ( CONTEXT_TOKENS . recommended , 131072 ); 26assert . equal ( recommended . expert [ 'ctx.length' ], '128K' ); 27assert . equal ( engine . expert . find ( p => p . id === 'ctx.length' ). recommended , '128K' ); 28for ( const [ contextLength , expected ] of [[ 'recommended' , 131072 ],[ '32k' , 32768 ],[ '256k' , 262144 ]]) { 29const doc = configDocument ({ ...engine , beginner :{ contextLength}}); 30const args = buildArgs ({ modelPath :'/test.gguf' , config :{ beginner :doc . beginner , expert :[]}}); 31assert . equal ( args [ args . indexOf ( '-c' ) + 1 ], String ( expected )); 32assert . equal ( contextTokens ( contextLength ), expected ); 33} 34}); 35 36test ( 'all integer K context sizes reach the engine without a 32K fallback' ,() => { 37for ( const k of [ 4 , 5 , 31 , 32 , 33 , 127 , 128 , 129 , 511 , 1024 ]) { 38assert . equal ( CONTEXT_TOKENS [ ` ${ k } k` ], k * 1024 ); 39const args = buildArgs ({ modelPath :'/test.gguf' , config :{ beginner :{ contextLength :` ${ k } k` }, expert :[]}}); 40assert . equal ( args [ args . indexOf ( '-c' ) + 1 ], String ( k * 1024 )); 41} 42assert . equal ( CONTEXT_TOKENS [ '3k' ], undefined ); 43assert . equal ( CONTEXT_TOKENS [ '33.5k' ], undefined ); 44assert . equal ( CONTEXT_TOKENS [ '1025k' ], undefined ); 45}); 46test ( 'picker covers available, downloaded, running, transitions and retry paths' ,() => { 47const version = { id :'v' , state :'available' , diskGb :1 }; 48assert . equal ( pickerVersionState ( version , null ). label , 'Download' ); 49assert . equal ( pickerVersionState ({ ...version , state :'installed' }, null ). label , 'Downloaded' ); 50assert . equal ( pickerVersionState ({ ...version , state :'loaded' }, 'v' ). label , 'Running' ); 51for ( const state of [ 'downloading' , 'loading' , 'unloading' ]) assert . equal ( pickerVersionState ({ ...version , state}, 'v' ). busy , true ); 52assert . equal ( pickerVersionState ({ ...version , state :'error' }, null ). action , 'Retry download' ); 53assert . equal ( pickerVersionState ({ ...version , state :'error' , installedAt :'2026-10-09' }, null ). action , 'Retry loading' ); 54assert . deepEqual ( pickerVersions ({ loaded :'large' },[{ ...version , id :'small' },{ ...version , id :'large' , diskGb :12 }]). map ( v => v . id ),[ 'large' , 'small' ]); 55}); 56test ( 'physical processor sockets retain per-socket cores and logical threads' ,() => { 57const raw = '# CPU,Core,Socket\n0,0,0\n1,0,0\n2,1,0\n3,1,0\n4,0,1\n5,0,1\n6,1,1\n7,1,1' ; 58const cpus = parseCpuTopology ( raw , 'Xeon' , 'x64' ); 59assert . equal ( cpus . length , 2 ); 60assert . deepEqual ( cpus . map ( c => [ c . cores , c . threads ]),[[ 2 , 4 ],[ 2 , 4 ]]); 61assert . deepEqual ( parseCpuTopology ( '0,-,-' , 'Unknown' , 'arm64' ),[]); 62const windows = parseWindowsProcessors ( JSON . stringify ([{ DeviceID :'CPU0' , Name :'Xeon A' , NumberOfCores :12 , NumberOfLogicalProcessors :24 },{ DeviceID :'CPU1' , Name :'Xeon B' , NumberOfCores :16 , NumberOfLogicalProcessors :32 }]), 'x64' ); 63assert . deepEqual ( windows . map ( c => [ c . id , c . cores , c . threads ]),[[ 'CPU0' , 12 , 24 ],[ 'CPU1' , 16 , 32 ]]); 64}); 65test ( 'GPU readings use UUIDs and never turn unavailable sensors into zero' ,() => { 66const gpus = parseNvidiaDevices ( '0, GPU-a, 00000000:01:00.0, RTX A, 24576, 555\n1, GPU-b, 00000000:02:00.0, RTX B, 49152, 555' ); 67assert . deepEqual ( gpus . map ( g => [ g . id , g . vramGb ]),[[ 'GPU-a' , 24 ],[ 'GPU-b' , 48 ]]); 68const readings = parseNvidiaReadings ( 'GPU-b, 71, 94, 10240\nGPU-a, N/A, 0, 0' ); 69assert . equal ( readings . find ( g => g . id === 'GPU-b' ). temperatureC , 71 ); 70assert . equal ( readings . find ( g => g . id === 'GPU-a' ). temperatureC , null ); 71assert . equal ( readings . find ( g => g . id === 'GPU-a' ). utilizationPct , 0 ); 72assert . equal ( parseNvidiaReadings ( 'GPU-a, 999, -1, N/A' )[ 0 ]. temperatureC , null ); 73}); 74test ( 'multiple Linux sensor paths stay separate and lost sensors become unavailable' , async () => { 75const dir = await mkdtemp ( join ( tmpdir (), 'strata-gpu-' )); 76try { 77const gpus = []; 78for ( const [ id , temp ] of [[ 'a' , 42000 ],[ 'b' , 73000 ]]) { 79const device = join ( dir , id ); await mkdir ( join ( device , 'hwmon' , 'hwmon0' ),{ recursive :true }); 80await writeFile ( join ( device , 'hwmon' , 'hwmon0' , 'temp1_input' ), String ( temp )); 81await writeFile ( join ( device , 'hwmon' , 'hwmon0' , 'temp1_label' ), 'edge' ); 82gpus . push ({ id, vendor :'amd' , sensorPath :device }); 83} 84assert . deepEqual (( await readGpuTelemetry ( gpus )). map ( g => g . temperatureC ),[ 42 , 73 ]); 85await rm ( join ( dir , 'a' ),{ recursive :true }); 86const readings = await readGpuTelemetry ( gpus ); 87assert . equal ( readings [ 0 ]. temperatureC , null ); assert . equal ( readings [ 0 ]. source , 'none' ); assert . equal ( readings [ 1 ]. temperatureC , 73 ); 88} finally { await rm ( dir ,{ recursive :true , force :true });} 89}); 90test ( 'usage totals ignore demo, estimated and copied branch usage' ,() => { 91const now = new Date ( '2026-10-09T12:00:00Z' ); 92const m = { id :'m' , role :'assistant' , at :'2026-10-08T10:00:00Z' , content :'Hello' , requestId :'r1' , model :'model-a' , usageSource :'reported' , inputTokens :100 , tokens :25 }; 93const sessions = [{ id :'one' , messages :[{ id :'u' , role :'user' , at :m . at , content :'Hi' }, m ]}, { id :'branch' , messages :[{ ...m , id :'copy' }]}, { id :'demo' , messages :[{ ...m , id :'d' , requestId :'d' , demo :true , tokens :9999 }]},{ id :'old' , messages :[{ ...m , id :'old' , requestId :'old' , at :'2026-08-01T00:00:00Z' }]},{ id :'show' , showcase :true , messages :[{ ...m , requestId :'show' }]}]; 94const usage = usageDashboard ( sessions , '7d' , now ); 95assert . equal ( usage . total , 125 ); assert . equal ( usage . messages , 3 ); assert . equal ( usage . unreported , 1 ); assert . equal ( usage . activeDays , 1 ); assert . equal ( usage . models [ 0 ]. requests , 1 ); 96assert . equal ( usageDashboard ( sessions , 'all' , now ). total , 250 ); 97assert . equal ( usageDashboard ([{ messages :[{ ...m , usageSource :'estimated' , requestId :'e' }]}], 'all' , now ). total , 0 ); 98}); 99test ( 'empty usage and calendar ranges are stable' ,() => { 100const empty = usageDashboard ([], 'all' , new Date ( '2026-10-09T12:00:00Z' )); 101assert . equal ( empty . messages , 0 ); assert . equal ( empty . peakHour , null ); assert . equal ( empty . favorite , null ); 102assert . deepEqual ( calendarDays ( new Date ( 2026 , 9 , 7 ), new Date ( 2026 , 9 , 9 )),[ '2026-10-07' , '2026-10-08' , '2026-10-09' ]); 103}); 104 105test ( 'family choices follow the catalog and version filtering follows the selection' ,() => { 106const library = { mode :'simulated' , loaded :'a' , variants :[{ id :'a' , family :'Alpha' , diskGb :4 },{ id :'b' , family :'Beta' , diskGb :8 },{ id :'c' , family :'Beta' , diskGb :3 }]}; 107assert . deepEqual ( modelFamilies ( library ),[ 'Alpha' , 'Beta' ]); 108assert . deepEqual ( modelVersions ( library ). map ( v => v . id ),[ 'a' ]); 109assert . deepEqual ( modelVersions ( library , 'Beta' ). map ( v => v . id ),[ 'c' , 'b' ]); 110});