okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
1import { ROOMY_HOST } from './helpers/hardware.mjs' ; 2import { fileURLToPath } from 'node:url' ; 3import test from 'node:test' ; 4import assert from 'node:assert/strict' ; 5import { spawn , spawnSync } from 'node:child_process' ; 6import { mkdtempSync , mkdirSync , readFileSync , renameSync , rmSync , statSync , writeFileSync } from 'node:fs' ; 7import { join } from 'node:path' ; 8import { tmpdir } from 'node:os' ; 9import { createServer } from 'node:net' ; 10import { readGGUF } from '../server/gguf.mjs' ; 11import { browseLocalModels , inspectLocalModel , createLocalModels } from '../server/local-models.mjs' ; 12import { ggufFixture } from './helpers/local-gguf.mjs' ; 13const cwd = fileURLToPath ( new URL ( '..' , import . meta. url )), pause = ms => new Promise ( r => setTimeout ( r , ms )); 14function fixture ( t ){ const root = mkdtempSync ( join ( tmpdir (), 'strata-local-model-' )); t . after (() => rmSync ( root ,{ recursive :true , force :true })); return root ;} 15 16test ( 'bounded reader gets real metadata and rejects fake extensions, partial tensors, unsupported versions and duplicate metadata' , t => { 17const root = fixture ( t ), file = join ( root , 'huihui.gguf' ); ggufFixture ( file ,{ version :2 }); const info = readGGUF ( file ); assert . equal ( info . metadata [ 'general.architecture' ], 'qwen3next' ); assert . equal ( info . metadata [ 'tokenizer.ggml.tokens' ]. count , 1 ); assert . equal ( info . parameters , 16 ); 18const preview = inspectLocalModel ( file ); assert . equal ( preview . variant . quant , 'Q4_K_M' ); assert . equal ( preview . variant . contextMax , 32768 ); assert . equal ( preview . variant . layerCount , 48 ); assert . equal ( preview . variant . experts , 128 ); assert . equal ( preview . variant . kvKbPerToken , 12 ); assert . equal ( preview . variant . family , 'Local models' ); 19assert . throws (() => inspectLocalModel ( root ), / \.gguf | folder / ); writeFileSync ( file , 'not a model' ); assert . throws (() => inspectLocalModel ( file ), / truncated | not a GGUF / ); 20ggufFixture ( file ,{ truncate :10 }); assert . throws (() => readGGUF ( file ), / incomplete / ); 21ggufFixture ( file ,{ version :1 }); assert . throws (() => readGGUF ( file ), / version / ); 22ggufFixture ( file ,{ extra :[[ 'general.name' , 8 , 'duplicate' ]]}); assert . throws (() => readGGUF ( file ), / duplicate / ); 23ggufFixture ( file ,{ arch :'clip' }); assert . throws (() => inspectLocalModel ( file ), / architecture. * not supported / ); 24ggufFixture ( file ,{ tensorCount :0 }); assert . throws (() => inspectLocalModel ( file ), / missing tensors / ); 25ggufFixture ( file ,{ context :1024 }); assert . throws (() => inspectLocalModel ( file ), / context_length / ); 26const oversized = Buffer . alloc ( 24 ); oversized . write ( 'GGUF' ); oversized . writeUInt32LE ( 3 , 4 ); oversized . writeBigUInt64LE ( 2n ** 63n , 8 ); writeFileSync ( file , oversized ); assert . throws (() => readGGUF ( file ), / unsupported integer / ); 27}); 28 29test ( 'choosing any shard finds the first, validates every shard and catches missing or mismatched sets' , t => { 30const root = fixture ( t ), first = join ( root , 'Orca-00001-of-00002.gguf' ), second = join ( root , 'Orca-00002-of-00002.gguf' ); 31ggufFixture ( first ,{ splitNo :0 , splitCount :2 , tensorName :'a.weight' }); ggufFixture ( second ,{ splitNo :1 , splitCount :2 , tensorName :'b.weight' }); 32const model = inspectLocalModel ( second ); assert . equal ( model . variant . local . shards , 2 ); assert . ok ( model . variant . local . path . endsWith ( '00001-of-00002.gguf' )); assert . equal ( model . variant . source . bytes , statSync ( first ). size + statSync ( second ). size ); 33rmSync ( second ); assert . throws (() => inspectLocalModel ( first ), / Keep every shard / ); 34ggufFixture ( second ,{ splitNo :0 , splitCount :2 }); assert . throws (() => inspectLocalModel ( first ), / matching set / ); 35ggufFixture ( second ,{ splitNo :1 , splitCount :2 , arch :'llama' }); assert . throws (() => inspectLocalModel ( first ), / different architectures / ); 36rmSync ( first ); assert . throws (() => inspectLocalModel ( second ), / first shard / ); 37}); 38 39test ( 'folder browsing shows only folders/GGUFs; reference persistence, deduplication and removal preserve original weights' , t => { 40const root = fixture ( t ), file = join ( root , 'Orca.gguf' ); mkdirSync ( join ( root , 'subfolder' )); writeFileSync ( join ( root , 'private.txt' ), 'keep' ); writeFileSync ( join ( root , '.hidden.gguf' ), 'ignore' ); const bytes = ggufFixture ( file ); 41const listing = browseLocalModels ( root ); assert . deepEqual ( listing . entries . map ( e => e . name ),[ 'subfolder' , 'Orca.gguf' ]); 42const service = createLocalModels ( join ( root , 'data' )), preview = inspectLocalModel ( file ); service . add ( preview ); service . add ( preview ); assert . equal ( service . list (). length , 1 ); assert . equal ( service . ready ( service . list ()[ 0 ]), true ); 43const restarted = createLocalModels ( join ( root , 'data' )); assert . equal ( restarted . list ()[ 0 ]. id , preview . variant . id ); 44renameSync ( file , join ( root , 'moved.gguf' )); assert . equal ( restarted . ready ( restarted . list ()[ 0 ]), false ); renameSync ( join ( root , 'moved.gguf' ), file ); 45restarted . remove ( preview . variant . id ); assert . deepEqual ( readFileSync ( file ), bytes ); assert . equal ( readFileSync ( join ( root , 'private.txt' ), 'utf8' ), 'keep' ); 46}); 47 48test ( 'native library uses the exact imported path and keeps its reference across directory changes' , t => { 49const root = fixture ( t ), file = join ( root , 'real-path.gguf' ); ggufFixture ( file ); const next = join ( root , 'next' ); mkdirSync ( next ); 50const result = spawnSync ( process . execPath ,[ '--input-type=module' , '-e' , ` 51import assert from 'node:assert/strict';import fs from 'node:fs'; 52const catalog=await import('./server/catalog.mjs'),local=await import('./server/local-models.mjs'),models=await import('./server/models.mjs'),store=await import('./server/store.mjs'); 53const preview=local.inspectLocalModel( ${ JSON . stringify ( file )} );catalog.localModels.add(preview);models.localModelAdded(); 54const v=catalog.variant(preview.variant.id);assert.equal(models.pathFor(v),v.local.path);assert.ok(models.isOnDisk(v.id));assert.equal(models.library().variants.find(row=>row.id===v.id).state,'installed'); 55store.changeModelsDirectory( ${ JSON . stringify ( next )} );models.init({send(){}});assert.ok(models.isOnDisk(v.id)); 56models.remove(v.id);assert.equal(catalog.variant(v.id),null);assert.ok(fs.existsSync( ${ JSON . stringify ( file )} )); 57` ],{ cwd, env :{ ...process . env , STRATA_HOME :join ( root , 'host' ), STRATA_MODELS_DIR :join ( root , 'host' , 'models' ), STRATA_REAL_ENGINE :'1' }, encoding :'utf8' }); assert . equal ( result . status , 0 , result . stderr ); 58}); 59 60async function freePort (){ const server = createServer (); await new Promise ( r => server . listen ( 0 , '127.0.0.1' , r )); const port = server . address (). port ; await new Promise ( r => server . close ( r )); return port ;} 61test ( 'HTTP import leaves the running model intact, loads through normal controls, protects active references and preserves files on removal' ,{ timeout :30000 }, async t => { 62const root = fixture ( t ), home = join ( root , 'host' ), file = join ( root , 'huihui-Orca.gguf' ); mkdirSync ( join ( home , 'data' ),{ recursive :true }); ggufFixture ( file ); 63writeFileSync ( join ( home , 'data' , 'state.json' ), JSON . stringify ({ setup :{ complete :true , step :4 , flowVersion :2 }, installed :{ 'qwen38-q4_k_m' :{ simulated :true }}, loaded :'qwen38-q4_k_m' })); 64const port = await freePort (), base = `http://127.0.0.1: ${ port } ` , host = spawn ( process . execPath ,[ 'server/index.mjs' ],{ cwd, env :{ ...process . env , STRATA_HOME :home , STRATA_MODELS_DIR :join ( home , 'models' ), STRATA_REAL_ENGINE :'0' , STRATA_TEST_HARDWARE :ROOMY_HOST , STRATA_PORT :String ( port ), STRATA_BIND :'127.0.0.1' }, stdio :'ignore' }); 65async function call ( path , body , method ){ const response = await fetch ( base + path ,{ method :method ?? ( body === undefined ?'GET' :'POST' ), headers :{ 'content-type' :'application/json' }, body :body === undefined ?undefined :JSON . stringify ( body )}); return { status :response . status , body :await response . json ()};} 66try { 67let ready = false ; for ( let i = 0 ; i < 100 ; i ++ ){ try { ready = ( await call ( '/api/runtime' )). status === 200 ; if ( ready ) break ;} catch {} await pause ( 100 );} assert . ok ( ready ); 68const original = ( await call ( '/api/runtime' )). body . defaultId ; 69assert . equal (( await call ( '/api/local-models/inspect' ,{ path :join ( root , 'missing.gguf' )})). status , 400 ); 70const added = await call ( '/api/local-models' ,{ path :file , name :'My huihui version' }); assert . equal ( added . status , 200 , JSON . stringify ( added . body )); const id = added . body . variantId ; 71assert . equal (( await call ( '/api/runtime' )). body . defaultId , original ); assert . equal (( await call ( '/api/local-models' ,{ path :file })). body . variantId , id ); 72const engine = ( await call ( '/api/engine' )). body , hardware = ( await call ( '/api/hardware' )). body ; 73const config = { beginner :{ ...engine . beginner , contextLength :'32k' }, expert :engine . expert . map ( p => ({ id :p . id , value :p . id === 'ctx.length' ?'32K' :p . value })), extra :{ headroom :'shared' }}; 74const loaded = await call ( '/api/runtime/load' ,{ variantId :id , gpuIds :hardware . gpus . map ( g => g . id ?? ` ${ g . vendor } : ${ g . index } ` ), config}); assert . equal ( loaded . status , 200 , JSON . stringify ( loaded . body )); assert . equal ( loaded . body . state . instances . length , 1 ); 75assert . equal (( await call ( `/api/models/ ${ id } ` , undefined , 'DELETE' )). status , 409 ); 76ggufFixture ( file ,{ name :'Changed metadata' }); assert . equal (( await call ( '/api/local-models' ,{ path :file })). status , 409 ); 77await call ( `/api/runtime/ ${ loaded . body . instance . id } /unload` ,{}); 78const reconnected = await call ( '/api/local-models' ,{ path :file }); assert . equal ( reconnected . status , 200 ); assert . equal ( reconnected . body . variantId , id ); 79assert . equal (( await call ( `/api/models/ ${ id } ` , undefined , 'DELETE' )). status , 200 ); assert . ok ( readFileSync ( file ). length > 0 ); assert . equal (( await call ( '/api/models' )). body . variants . some ( v => v . id === id ), false ); 80assert . equal (( await call ( '/api/runtime' )). body . instances . length , 0 ); 81} finally { host . kill ( 'SIGTERM' ); await new Promise ( r => host . once ( 'exit' , r ));} 82}); 83test ( 'the Flash-Next family\'s own architecture (qwen4exp) is accepted by the importer, not sent to llama-server (#4726)' , t => { 84const root = fixture ( t ), file = join ( root , 'flash-next.gguf' ); 85ggufFixture ( file ,{ arch :'qwen4exp' }); 86const info = inspectLocalModel ( file ); 87assert . equal ( info . variant . local . architecture , 'qwen4exp' ); 88});