okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b0fc432ed
main
1import test from 'node:test' ; 2import assert from 'node:assert/strict' ; 3import { execFileSync , spawnSync } from 'node:child_process' ; 4import { fileURLToPath } from 'node:url' ; 5import { existsSync , mkdirSync , mkdtempSync , readFileSync , realpathSync , rmSync , writeFileSync } from 'node:fs' ; 6import { tmpdir } from 'node:os' ; 7import { join , parse } from 'node:path' ; 8import { alignModels , dataFolderFromPick , isDriveRoot , libraryFolderFromPick , makeFolder , rememberDataFolder , syncDataFolder , readDataFolder , terminalSettingsFile } from '../server/model-folder.mjs' ; 9import { nativeFixture } from './helpers/native-fixture.mjs' ; 10 11const scratch = t => { const dir = realpathSync ( mkdtempSync ( join ( tmpdir (), 'strata-folder-' ))); t . after (() => rmSync ( dir , { recursive :true , force :true })); return dir ; }; 12 13test ( 'a whole drive is not filled with models: it becomes Strata (data) and Strata/Models (library)' , () => { 14const root = parse ( tmpdir ()). root ; 15assert . equal ( isDriveRoot ( root ), true ); 16assert . equal ( isDriveRoot ( tmpdir ()), false ); 17assert . deepEqual ( dataFolderFromPick ( root ), { path :join ( root , 'Strata' ), note :` ${ root } is a whole drive, so the models go in ${ join ( root , 'Strata' )} .` }); 18assert . equal ( libraryFolderFromPick ( root ). path , join ( root , 'Strata' , 'Models' )); 19assert . deepEqual ( dataFolderFromPick ( tmpdir ()), { path :tmpdir (), note :null }); 20assert . deepEqual ( libraryFolderFromPick ( tmpdir ()), { path :tmpdir (), note :null }); 21}); 22 23test ( 'a folder that cannot be made is said in words' , t => { 24const dir = scratch ( t ); 25writeFileSync ( join ( dir , 'a-file' ), 'x' ); 26assert . throws (() => makeFolder ( join ( dir , 'a-file' , 'Strata' )), / Strata could not create . * Choose another folder\. / ); 27assert . equal ( makeFolder ( join ( dir , 'new' , 'Strata' )), join ( dir , 'new' , 'Strata' )); 28assert . equal ( existsSync ( join ( dir , 'new' , 'Strata' )), true ); 29}); 30 31test ( 'models and Models are one folder; a disk that cannot hold the link gets a plain models folder' , t => { 32const dir = scratch ( t ), library = join ( dir , 'Models' ); 33mkdirSync ( library ); 34const refuses = () => { throw Object . assign ( new Error ( 'EPERM: operation not permitted, symlink' ), { code :'EPERM' }); }; 35const result = alignModels ( dir , library , { link :refuses }); 36assert . equal ( result . linked , false ); 37assert . equal ( existsSync ( join ( dir , 'models' )), true ); 38// where the names are one folder already (Windows, macOS) nothing is made; where a link works it is used 39const other = scratch ( t ); 40const calls = []; 41assert . equal ( alignModels ( other , join ( other , 'Models' ), { link :( ...args ) => { calls . push ( args ); } }). linked , true ); 42assert . equal ( calls . length , 1 ); 43assert . equal ( calls [ 0 ][ 1 ], join ( other , 'models' )); 44}); 45 46test ( 'the data folder is written into the Control settings file, merged and only when it changes' , t => { 47const dir = scratch ( t ), file = join ( dir , 'config' , 'app-settings.json' ); 48assert . equal ( rememberDataFolder ( file , 'D:\\Strata' ), true ); 49assert . equal ( JSON . parse ( readFileSync ( file , 'utf8' )). data_dir , 'D:\\Strata' ); 50writeFileSync ( file , JSON . stringify ({ installs :[ 'C:\\x' ], data_dir :'C:\\old' })); 51assert . equal ( rememberDataFolder ( file , 'D:\\Strata' ), true ); 52assert . deepEqual ( JSON . parse ( readFileSync ( file , 'utf8' )). installs , [ 'C:\\x' ]); 53assert . equal ( rememberDataFolder ( file , 'D:\\Strata' ), false ); 54writeFileSync ( file , '\uFEFF{"data_dir":"E:\\\\Strata","installs":[]}' ); 55assert . equal ( rememberDataFolder ( file , 'E:\\Strata' ), false ); 56writeFileSync ( file , 'not json' ); 57assert . equal ( rememberDataFolder ( file , 'F:\\Strata' ), true ); 58assert . equal ( rememberDataFolder ( '' , 'F:\\Strata' ), false ); 59}); 60 61test ( 'choosing a folder that is gone or cannot be written says so in words' , async t => { 62const f = nativeFixture (); 63t . after (() => f . cleanup ()); 64await f . control . refresh (); 65assert . throws (() => f . control . setDownloadDirectory ( join ( f . repo , 'not-there' )), / That folder does not exist\. Check the path, or that the drive is connected\. / ); 66assert . throws (() => f . control . setDownloadDirectory ( join ( f . repo , 'setup.py' )), / Choose a folder, not a file\. / ); 67}); 68 69test ( 'Windows: a drive picked as D:\\ becomes D:\\Strata, and the person is not asked for a different folder' , { skip :process . platform !== 'win32' && 'drive-letter paths: Windows only' }, async t => { 70const dir = scratch ( t ); 71const letter = 'QRSTUVWXYZ' . split ( '' ). find ( l => ! existsSync ( ` ${ l } :\\` )); 72assert . ok ( letter , 'a free drive letter' ); 73execFileSync ( 'subst' , [ ` ${ letter } :` , dir ]); 74t . after (() => { try { execFileSync ( 'subst' , [ ` ${ letter } :` , '/D' ]); } catch { /* gone */ } }); 75const f = nativeFixture (); 76t . after (() => f . cleanup ()); 77await f . control . refresh (); 78f . control . setDownloadDirectory ( ` ${ letter } :\\` ); 79const shown = f . control . state (). downloadDirectory ; 80assert . equal ( shown . toLowerCase (), ` ${ letter } :\\Strata` . toLowerCase ()); 81assert . equal ( existsSync ( join ( dir , 'Strata' )), true ); 82assert . equal ( readdirNames ( dir ). includes ( 'Strata' ), true ); 83}); 84 85function readdirNames ( dir ) { return execFileSync ( 'cmd' , [ '/c' , 'dir' , '/b' , dir ]). toString (). split ( / \r ? \n / ). filter ( Boolean ); } 86 87test ( 'Windows: the model library on a whole drive goes to X:\\Strata\\Models' , { skip :process . platform !== 'win32' && 'drive-letter paths: Windows only' }, t => { 88const dir = scratch ( t ), home = scratch ( t ); 89const letter = 'QRSTUVWXYZ' . split ( '' ). find ( l => ! existsSync ( ` ${ l } :\\` )); 90execFileSync ( 'subst' , [ ` ${ letter } :` , dir ]); 91t . after (() => { try { execFileSync ( 'subst' , [ ` ${ letter } :` , '/D' ]); } catch { /* gone */ } }); 92const cwd = fileURLToPath ( new URL ( '..' , import . meta. url )); 93const drive = ` ${ letter } :\\` ; 94const run = spawnSync ( process . execPath , [ '--input-type=module' , '-e' , `const store=await import('./server/store.mjs');console.log(store.validateModelsDirectory( ${ JSON . stringify ( drive )} ));` ], 95{ cwd, env :{ ...process . env , STRATA_HOME :home , STRATA_MODELS_DIR :join ( home , 'models' ) }, encoding :'utf8' }); 96assert . equal ( run . status , 0 , run . stderr ); 97assert . equal ( run . stdout . trim (). toLowerCase (), ` ${ drive } Strata\\Models` . toLowerCase ()); 98assert . equal ( existsSync ( join ( dir , 'Strata' , 'Models' )), true ); 99}); 100 101test ( 'the app and the Terminal share one models folder: the one changed last wins, the other file follows, nothing moves' , t => { 102const dir = scratch ( t ), appFile = join ( dir , 'app-settings.json' ), terminalFile = join ( dir , 'roaming' , 'settings.json' ); 103const c = join ( dir , 'on-c' ), d = join ( dir , 'on-d' ); 104mkdirSync ( c ); mkdirSync ( d ); 105const seed = ( file , folder , at ) => { mkdirSync ( join ( file , '..' ), { recursive :true }); writeFileSync ( file , JSON . stringify ({ data_dir :folder , data_dir_at :at , installs :[ 'keep' ] })); }; 106// the Terminal chose D: after the app chose C: 107seed ( appFile , c , 1000 ); seed ( terminalFile , d , 2000 ); 108assert . deepEqual ( syncDataFolder ({ appFile, terminalFile}), { folder :d , from :'terminal' }); 109assert . deepEqual ( readDataFolder ( appFile ), { folder :d , at :2000 }); 110assert . deepEqual ( JSON . parse ( readFileSync ( appFile , 'utf8' )). installs , [ 'keep' ]); 111assert . deepEqual ( syncDataFolder ({ appFile, terminalFile}), { folder :d , from :null }); 112// the app chose C: later 113seed ( appFile , c , 3000 ); 114assert . deepEqual ( syncDataFolder ({ appFile, terminalFile}), { folder :c , from :'app' }); 115assert . deepEqual ( readDataFolder ( terminalFile ), { folder :c , at :3000 }); 116// the Terminal has none: nothing is written there; the app has none: it takes the Terminal's 117const lone = join ( dir , 'lone.json' ); 118assert . deepEqual ( syncDataFolder ({ appFile :join ( dir , 'none.json' ), terminalFile :lone }), { folder :null , from :null }); 119assert . equal ( existsSync ( lone ), false ); 120seed ( lone , d , 10 ); 121assert . deepEqual ( syncDataFolder ({ appFile :join ( dir , 'none.json' ), terminalFile :lone }), { folder :d , from :'terminal' }); 122// the app's preference (native-host.json) counts when its file names none 123assert . deepEqual ( syncDataFolder ({ appFile :join ( dir , 'none2.json' ), terminalFile :lone , app :{ folder :c , at :99 } }), { folder :c , from :'app' }); 124// a Terminal folder on a disk that is not there is never adopted 125seed ( terminalFile , join ( dir , 'unplugged' ), 9999 ); seed ( appFile , c , 5 ); 126assert . deepEqual ( syncDataFolder ({ appFile, terminalFile}), { folder :c , from :'app' }); 127assert . equal ( readdirSyncSafe ( c ), 0 ); 128}); 129const readdirSyncSafe = folder => execFileSync ( process . execPath , [ '-e' , `console.log(require('fs').readdirSync( ${ JSON . stringify ( folder )} ).length)` ]). toString (). trim () * 1 ; 130 131test ( 'the Terminal settings file is where setup.py and Control keep it' , () => { 132assert . equal ( terminalSettingsFile ({ APPDATA :'C:\R' }, 'win32' ), join ( 'C:\R' , 'Strata' , 'settings.json' )); 133assert . equal ( terminalSettingsFile ({ XDG_CONFIG_HOME :'/x' }, 'linux' ), join ( '/x' , 'strata' , 'settings.json' )); 134assert . equal ( terminalSettingsFile ({ STRATA_TERMINAL_SETTINGS_FILE :'/t.json' }, 'linux' ), '/t.json' ); 135});