okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3f8124586
main
1import { fileURLToPath } from 'node:url' ; 2import test from 'node:test' ; 3import assert from 'node:assert/strict' ; 4import { mkdtempSync , mkdirSync , writeFileSync , realpathSync , rmSync , symlinkSync } from 'node:fs' ; 5import { spawnSync } from 'node:child_process' ; 6import { tmpdir } from 'node:os' ; 7import { join } from 'node:path' ; 8import { defaultModelDirectory } from '../server/model-directory-default.mjs' ; 9test ( 'macOS and Linux default to Documents/Strata/Models' ,() => { 10for ( const platform of [ 'darwin' , 'linux' ]) assert . equal ( defaultModelDirectory ({ platform, home :'/home/me' , env :{}, read :() => { throw new Error ( 'no configuration' );}}), '/home/me/Documents/Strata/Models' ); 11}); 12test ( 'Windows uses the known Documents folder including OneDrive redirection' ,() => { 13assert . equal ( defaultModelDirectory ({ platform :'win32' , home :'C:\\Users\\Me' , env :{}, windowsDocuments :() => 'C:\\Users\\Me\\OneDrive\\Documents' }), 'C:\\Users\\Me\\OneDrive\\Documents\\Strata\\Models' ); 14assert . equal ( defaultModelDirectory ({ platform :'win32' , home :'C:\\Users\\Me' , env :{}, windowsDocuments :() => { throw new Error ( 'unavailable' );}}), 'C:\\Users\\Me\\Documents\\Strata\\Models' ); 15}); 16test ( 'Linux respects localized XDG Documents and rejects shell expressions' ,() => { 17assert . equal ( defaultModelDirectory ({ platform :'linux' , home :'/home/me' , env :{ XDG_CONFIG_HOME :'/custom' }, read :path => { assert . equal ( path , '/custom/user-dirs.dirs' ); return 'XDG_DOCUMENTS_DIR="$HOME/Dokumente"' ;}}), '/home/me/Dokumente/Strata/Models' ); 18assert . equal ( defaultModelDirectory ({ platform :'linux' , home :'/home/me' , env :{ XDG_DOCUMENTS_DIR :'${HOME}/Work' }, read :() => '' }), '/home/me/Work/Strata/Models' ); 19assert . equal ( defaultModelDirectory ({ platform :'linux' , home :'/home/me' , env :{ XDG_DOCUMENTS_DIR :'$(echo /private)' }, read :() => '' }), '/home/me/Documents/Strata/Models' ); 20}); 21test ( 'new hosts create the default; custom folders and legacy weights remain in place' , t => { 22const root = realpathSync ( mkdtempSync ( join ( tmpdir (), 'strata-documents-' ))); t . after (() => rmSync ( root ,{ recursive :true , force :true })); 23function boot ( name , state , weights = false ){ const home = join ( root , name ), documents = join ( root , name + '-documents' ); mkdirSync ( join ( home , 'data' ),{ recursive :true }); mkdirSync ( join ( home , 'models' ),{ recursive :true }); if ( state ) writeFileSync ( join ( home , 'data' , 'state.json' ), JSON . stringify ( state )); if ( weights ){ mkdirSync ( join ( home , 'models' , 'native' , 'qwen' ),{ recursive :true }); writeFileSync ( join ( home , 'models' , 'native' , 'qwen' , 'model.gguf' ), 'GGUFfixture' );} const env = { ...process . env , STRATA_HOME :home , STRATA_DOCUMENTS_DIR :documents , STRATA_TEST_NATIVE :'1' , STRATA_TEST_DEFAULT :join ( documents , 'Strata' , 'Models' )}; delete env . STRATA_MODELS_DIR ; const result = spawnSync ( process . execPath ,[ '--input-type=module' , '-e' , "const store=await import('./server/store.mjs');if(process.env.STRATA_TEST_NATIVE==='1'&&store.MODELS_DIR===process.env.STRATA_TEST_DEFAULT){const {statSync}=await import('node:fs');const {join}=await import('node:path');const native=store.nativeDataDirectory(store.MODELS_DIR);const a=statSync(join(native,'models')),b=statSync(store.MODELS_DIR);if(a.ino!==b.ino||a.dev!==b.dev)throw new Error('Native weights miss requested model folder');}console.log(store.MODELS_DIR);" ],{ cwd :fileURLToPath ( new URL ( '..' , import . meta. url )), env, encoding :'utf8' , windowsHide :true }); assert . equal ( result . status , 0 , result . stderr ); return { path :result . stdout . trim (), home, documents};} 24const fresh = boot ( 'fresh' ); assert . equal ( fresh . path , join ( fresh . documents , 'Strata' , 'Models' )); 25const target = join ( root , 'redirected-target' ); mkdirSync ( target ); symlinkSync ( target , join ( root , 'redirected-documents' ), process . platform === 'win32' ?'junction' :'dir' ); assert . equal ( boot ( 'redirected' ). path , join ( target , 'Strata' , 'Models' )); 26const custom = join ( root , 'custom' ); mkdirSync ( custom ); assert . equal ( boot ( 'custom-host' ,{ modelsDir :custom }). path , custom ); 27const legacy = boot ( 'legacy' ,{}, true ); assert . equal ( legacy . path , join ( legacy . home , 'models' )); 28}); 29 30 31test ( 'legacy model libraries resolve to a guarded native data root without moving weights' , t => { 32const root = realpathSync ( mkdtempSync ( join ( tmpdir (), 'strata-legacy-models-' ))); t . after (() => rmSync ( root ,{ recursive :true , force :true })); 33for ( const choice of [ 'detected' , 'saved' , 'environment' ]){ 34const home = join ( root , choice ), models = join ( home , 'models' ); mkdirSync ( join ( home , 'data' ),{ recursive :true }); mkdirSync ( models ); 35mkdirSync ( join ( models , 'IQ3_XXS' )); writeFileSync ( join ( models , 'IQ3_XXS' , 'model.gguf' ), Buffer . from ([ 71 , 71 , 85 , 70 , 0 , 255 , 128 , 1 ])); 36mkdirSync ( join ( root , 'documents' , 'Strata' , 'Models' ),{ recursive :true }); 37if ( choice === 'saved' ) writeFileSync ( join ( home , 'data' , 'state.json' ), JSON . stringify ({ modelsDir :models })); 38const env = { ...process . env , STRATA_HOME :home , STRATA_DOCUMENTS_DIR :join ( root , 'documents' )}; delete env . STRATA_MODELS_DIR ; 39if ( choice === 'environment' ) env . STRATA_MODELS_DIR = models ; 40const result = spawnSync ( process . execPath ,[ '--input-type=module' , '-e' , ` 41import assert from 'node:assert/strict';import {join,basename} from 'node:path'; 42import {readFileSync,realpathSync,readdirSync,statSync} from 'node:fs'; 43const store=await import('./server/store.mjs');const models=realpathSync(join(store.HOME,'models')); 44assert.equal(store.MODELS_DIR,models); 45const data=store.nativeDataDirectory(models); 46assert.match(basename(data),/^strata/i,'a new native data root must satisfy the installer guard'); 47assert.equal(realpathSync(join(data,'models')),models,'native setup must write to the existing library'); 48const weight=join(models,'IQ3_XXS','model.gguf'),nativeWeight=join(data,'models','IQ3_XXS','model.gguf'); 49assert.deepEqual(readFileSync(nativeWeight),Buffer.from([71,71,85,70,0,255,128,1]),'native setup sees the original weight bytes'); 50assert.equal(statSync(nativeWeight).ino,statSync(weight).ino,'the alias reuses the original file'); 51assert.equal(store.nativeDataDirectory(models),data,'resolution is idempotent'); 52assert.deepEqual(readFileSync(weight),Buffer.from([71,71,85,70,0,255,128,1])); 53assert.deepEqual(readdirSync(models),['IQ3_XXS'],'weights stay in the original library'); 54` ],{ cwd :fileURLToPath ( new URL ( '..' , import . meta. url )), env, encoding :'utf8' , windowsHide :true }); 55assert . equal ( result . status , 0 , result . stderr ); 56} 57}); 58 59 60test ( 'native data roots leave explicit custom libraries and new installer folders unchanged' , t => { 61const root = realpathSync ( mkdtempSync ( join ( tmpdir (), 'strata-custom-models-' ))); t . after (() => rmSync ( root ,{ recursive :true , force :true })); 62const custom = join ( root , 'chosen-library' ); mkdirSync ( custom ); writeFileSync ( join ( custom , 'keep.gguf' ), 'GGUFfixture' ); 63for ( const choice of [ 'saved' , 'environment' ]){ 64const home = join ( root , choice ); mkdirSync ( join ( home , 'data' ),{ recursive :true }); 65if ( choice === 'saved' ) writeFileSync ( join ( home , 'data' , 'state.json' ), JSON . stringify ({ modelsDir :custom })); 66const env = { ...process . env , STRATA_HOME :home , STRATA_DOCUMENTS_DIR :join ( root , 'documents' ), STRATA_TEST_CUSTOM :custom }; delete env . STRATA_MODELS_DIR ; 67if ( choice === 'environment' ) env . STRATA_MODELS_DIR = custom ; 68const result = spawnSync ( process . execPath ,[ '--input-type=module' , '-e' , ` 69import assert from 'node:assert/strict';import {join} from 'node:path';import {existsSync,readFileSync} from 'node:fs'; 70const store=await import('./server/store.mjs'),custom=process.env.STRATA_TEST_CUSTOM; 71assert.equal(store.MODELS_DIR,custom);assert.equal(store.nativeDataDirectory(custom),custom); 72const requested=join(store.HOME,'Strata-new');assert.equal(store.nativeDataDirectory(requested),requested); 73assert.equal(existsSync(requested),false);assert.equal(existsSync(join(store.HOME,'Strata-data')),false); 74assert.equal(readFileSync(join(custom,'keep.gguf'),'utf8'),'GGUFfixture'); 75` ],{ cwd :fileURLToPath ( new URL ( '..' , import . meta. url )), env, encoding :'utf8' , windowsHide :true }); 76assert . equal ( result . status , 0 , result . stderr ); 77} 78}); 79 80test ( 'legacy native aliases never overwrite a conflicting library' , t => { 81const root = realpathSync ( mkdtempSync ( join ( tmpdir (), 'strata-conflicting-models-' ))); t . after (() => rmSync ( root ,{ recursive :true , force :true })); 82for ( const kind of [ 'directory' , 'link' ]){ 83const home = join ( root , kind ), original = join ( home , 'models' ), other = join ( home , 'other-library' ), alias = join ( home , 'Strata-data' , 'models' ); mkdirSync ( original ,{ recursive :true }); mkdirSync ( other ); mkdirSync ( join ( home , 'Strata-data' )); 84if ( kind === 'link' ) symlinkSync ( other , alias , process . platform === 'win32' ?'junction' :'dir' ); else mkdirSync ( alias ); 85writeFileSync ( join ( original , 'original.gguf' ), 'GGUForiginal' ); writeFileSync ( join ( other , 'other.gguf' ), 'GGUFother' ); 86if ( kind === 'directory' ) writeFileSync ( join ( alias , 'other.gguf' ), 'GGUFconflict' ); 87const env = { ...process . env , STRATA_HOME :home , STRATA_DOCUMENTS_DIR :join ( home , 'documents' )}; delete env . STRATA_MODELS_DIR ; 88const result = spawnSync ( process . execPath ,[ '--input-type=module' , '-e' , ` 89import assert from 'node:assert/strict';import {join} from 'node:path';import {readFileSync,realpathSync} from 'node:fs'; 90const store=await import('./server/store.mjs'); 91assert.throws(()=>store.nativeDataDirectory(store.MODELS_DIR),/conflicts with another library/); 92assert.equal(realpathSync(store.MODELS_DIR),realpathSync(join(store.HOME,'models'))); 93assert.equal(readFileSync(join(store.MODELS_DIR,'original.gguf'),'utf8'),'GGUForiginal'); 94assert.equal(readFileSync(join(store.HOME,'other-library','other.gguf'),'utf8'),'GGUFother'); 95assert.equal(readFileSync(join(store.HOME,'Strata-data','models','other.gguf'),'utf8'), ${ JSON . stringify ( kind === 'link' ? 'GGUFother' : 'GGUFconflict' )} ); 96` ],{ cwd :fileURLToPath ( new URL ( '..' , import . meta. url )), env, encoding :'utf8' , windowsHide :true }); 97assert . equal ( result . status , 0 , result . stderr ); 98} 99}); 100 101test ( 'an unavailable Documents default retains the upstream fallback without creating a legacy alias' , t => { 102const root = realpathSync ( mkdtempSync ( join ( tmpdir (), 'strata-fallback-models-' ))); t . after (() => rmSync ( root ,{ recursive :true , force :true })); 103const home = join ( root , 'home' ), documents = join ( root , 'blocked-documents' ); mkdirSync ( home ); writeFileSync ( documents , 'not a directory' ); 104const env = { ...process . env , STRATA_HOME :home , STRATA_DOCUMENTS_DIR :documents }; delete env . STRATA_MODELS_DIR ; 105const result = spawnSync ( process . execPath ,[ '--input-type=module' , '-e' , ` 106import assert from 'node:assert/strict';import {join} from 'node:path';import {existsSync} from 'node:fs'; 107const store=await import('./server/store.mjs'); 108assert.match(store.MODELS_DIR_NOTICE,/Strata could not use/); 109assert.equal(store.nativeDataDirectory(store.MODELS_DIR),store.MODELS_DIR); 110assert.equal(existsSync(join(store.HOME,'Strata-data')),false); 111` ],{ cwd :fileURLToPath ( new URL ( '..' , import . meta. url )), env, encoding :'utf8' , windowsHide :true }); 112assert . equal ( result . status , 0 , result . stderr ); 113});