okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fa6367a98
main
1/** 2* Programs that are not Strata's own (zenity, kdialog, xdg-open, the assistant's shell) get the environment the Linux AppImage was 3* started from, not the AppImage's libraries and settings (tester report 2026-10-17: zenity died with 4* "libssl.so.3: version OPENSSL_3.2.0 not found" and the folder picker did nothing). 5*/ 6import test from 'node:test' ; 7import assert from 'node:assert/strict' ; 8import { EventEmitter } from 'node:events' ; 9import { externalEnvironment , recordedOriginal , withoutAppImage } from '../server/external-env.mjs' ; 10import { chooseFolder , chooserUnavailable , portalFolder } from '../server/folder-dialog.mjs' ; 11import { agentEnvironment } from '../server/agent/harness-runtime.mjs' ; 12 13const APPDIR = '/tmp/.mount_StrataAbc' ; 14const leaky = { 15APPDIR , APPIMAGE :'/home/u/Strata.AppImage' , ARGV0 :'Strata' , 16LD_LIBRARY_PATH :` ${ APPDIR } /usr/lib: ${ APPDIR } /usr/lib/x86_64-linux-gnu:/opt/cuda/lib64` , 17PATH :` ${ APPDIR } /usr/bin:/usr/local/bin:/usr/bin` , 18XDG_DATA_DIRS :` ${ APPDIR } /usr/share:/usr/share:/usr/local/share` , 19GTK_PATH :` ${ APPDIR } //usr/lib/gtk-3.0` , GIO_MODULE_DIR :` ${ APPDIR } //usr/lib/gio/modules` , GDK_PIXBUF_MODULE_FILE :` ${ APPDIR } //usr/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache` , 20GSETTINGS_SCHEMA_DIR :` ${ APPDIR } //usr/share/glib-2.0/schemas` , PYTHONHOME :` ${ APPDIR } /usr/python` , HOME :'/home/u' , DISPLAY :':0' , 21}; 22 23test ( 'without a record, everything that points into the AppImage is removed and the user\'s own entries stay' , () => { 24const env = externalEnvironment ( leaky ); 25assert . equal ( env . APPDIR , undefined ); 26assert . equal ( env . APPIMAGE , undefined ); 27assert . equal ( env . LD_LIBRARY_PATH , '/opt/cuda/lib64' , 'only the AppImage\'s folders go from the list' ); 28assert . equal ( env . PATH , '/usr/local/bin:/usr/bin' ); 29assert . equal ( env . XDG_DATA_DIRS , '/usr/share:/usr/local/share' ); 30for ( const name of [ 'GTK_PATH' , 'GIO_MODULE_DIR' , 'GDK_PIXBUF_MODULE_FILE' , 'GSETTINGS_SCHEMA_DIR' , 'PYTHONHOME' ]) assert . equal ( env [ name ], undefined , name ); 31assert . equal ( env . HOME , '/home/u' ); 32assert . equal ( env . DISPLAY , ':0' ); 33}); 34 35test ( 'with the launcher\'s record, the original environment is restored exactly' , () => { 36const original = { PATH :'/usr/bin' , LD_LIBRARY_PATH :'/mine' , HOME :'/home/u' , DISPLAY :':1' }; 37const record = Buffer . from ( Object . entries ( original ). map (([ k , v ]) => ` ${ k } = ${ v } ` ). join ( '\0' ) + '\0' ). toString ( 'base64' ); 38const env = externalEnvironment ({ ...leaky , STRATA_ORIGINAL_ENV :record }); 39assert . deepEqual ( env , original ); 40assert . deepEqual ( recordedOriginal ({ STRATA_ORIGINAL_ENV :record }), original ); 41assert . equal ( recordedOriginal ({ STRATA_ORIGINAL_ENV :'!!!' }), null ); 42}); 43 44test ( 'outside an AppImage nothing changes' , () => { 45const plain = { PATH :'/usr/bin' , LD_LIBRARY_PATH :'/opt/x/lib' , HOME :'/h' }; 46assert . deepEqual ( externalEnvironment ( plain ), plain ); 47assert . deepEqual ( externalEnvironment ( plain , { A :'1' }), { ...plain , A :'1' }); 48assert . deepEqual ( withoutAppImage ( plain ), plain ); 49}); 50 51test ( 'the coding assistant\'s shell does not inherit the AppImage\'s settings either' , () => { 52const env = agentEnvironment ({ ...leaky , SOME_TOKEN :'x' }, { DSH_HOME :'/h' }); 53assert . equal ( env . LD_LIBRARY_PATH , undefined ); 54assert . ok ( ! /\.mount_ / . test ( JSON . stringify ( env )), JSON . stringify ( env )); 55assert . equal ( env . DSH_HOME , '/h' ); 56assert . equal ( env . SOME_TOKEN , undefined ); 57}); 58 59const failure = ( code , stderr = '' ) => Object . assign ( new Error ( `exit ${ code } ` ), { code, stderr}); 60 61test ( 'Linux folder chooser: a zenity that breaks is reported, not taken for a cancel, and the next chooser is tried' , async () => { 62const seen = []; 63const libssl = failure ( 127 , 'zenity: error while loading shared libraries: libssl.so.3: version `OPENSSL_3.2.0\' not found' ); 64const run = async ( command , args , options ) => { seen . push ({ command, env :options ?. env }); if ( command === 'zenity' ) throw libssl ; return { stdout :'/home/u/Models/\n' }; }; 65const old = { ...process . env }; 66Object . assign ( process . env , leaky ); 67try { 68const picked = await chooseFolder ( 'Pick' , { platform :'linux' , run, portal :async () => null }); 69assert . deepEqual ( picked , { path :'/home/u/Models' }); 70assert . deepEqual ( seen . map ( s => s . command ), [ 'zenity' , 'kdialog' ]); 71for ( const s of seen ) { assert . equal ( s . env . LD_LIBRARY_PATH , '/opt/cuda/lib64' ); assert . equal ( s . env . APPDIR , undefined ); } 72// every chooser broken or missing: a plain sentence that names what to do, never a silent nothing 73const none = await chooseFolder ( 'Pick' , { platform :'linux' , run :async () => { throw libssl ; }, portal :async () => null }); 74assert . equal ( none . unavailable , true ); 75assert . equal ( none . failed , true ); 76assert . match ( none . reason , / Install zenity or kdialog to pick a folder, or type the path / ); 77const answer = chooserUnavailable ( none ); 78assert . equal ( answer . code , 'folder-chooser-unavailable' ); 79const missingAll = await chooseFolder ( 'Pick' , { platform :'linux' , run :async () => { throw failure ( 'ENOENT' ); }, portal :async () => null }); 80assert . equal ( missingAll . unavailable , true ); 81assert . match ( missingAll . reason , / zenity or kdialog / ); 82// a plain exit 1 is still a cancel; so is exit 1 with harmless warnings 83assert . deepEqual ( await chooseFolder ( 'Pick' , { platform :'linux' , run :async () => { throw failure ( 1 , 'Gtk-Message: Failed to load module "canberra-gtk-module"' ); }, portal :async () => null }), { cancelled :true }); 84// a zenity that cannot reach the display exits 1 too: that is a failure 85const noDisplay = await chooseFolder ( 'Pick' , { platform :'linux' , run :async () => { throw failure ( 1 , 'Unable to init server: Could not connect: Connection refused' ); }, portal :async () => null }); 86assert . equal ( noDisplay . unavailable , true ); 87} finally { for ( const k of Object . keys ( leaky )) { if ( old [ k ] === undefined ) delete process . env [ k ]; else process . env [ k ] = old [ k ]; } } 88}); 89 90test ( 'Linux folder chooser: the desktop portal answers when zenity and kdialog are not installed' , async () => { 91const run = async () => { throw failure ( 'ENOENT' ); }; 92assert . deepEqual ( await chooseFolder ( 'Pick' , { platform :'linux' , run, portal :async () => ({ path :'/home/u/Pics' }) }), { path :'/home/u/Pics' }); 93assert . deepEqual ( await chooseFolder ( 'Pick' , { platform :'linux' , run, portal :async () => ({ cancelled :true }) }), { cancelled :true }); 94}); 95 96test ( 'portalFolder reads the Response signal of its request' , async () => { 97const handle = '/org/freedesktop/portal/desktop/request/1_42/t1' ; 98const monitor = new EventEmitter (); 99monitor . stdout = new EventEmitter (); 100monitor . kill = () => { monitor . exitCode = 0 ; }; 101monitor . exitCode = null ; 102const spawnProcess = () => monitor ; 103const run = async ( command , args ) => { 104assert . equal ( command , 'gdbus' ); 105assert . ok ( args . includes ( 'org.freedesktop.portal.FileChooser.OpenFile' )); 106queueMicrotask (() => monitor . stdout . emit ( 'data' , ` ${ handle } : org.freedesktop.portal.Request.Response (uint32 0, {'uris': <['file:///home/u/My%20Models']>})\n` )); 107return { stdout :`(objectpath ' ${ handle } ',)\n` }; 108}; 109assert . deepEqual ( await portalFolder ( 'Pick' , { run, env :{}, spawnProcess}), { path :'/home/u/My Models' }); 110const cancel = new EventEmitter (); 111cancel . stdout = new EventEmitter (); cancel . kill = () => {}; cancel . exitCode = null ; 112const run2 = async () => { queueMicrotask (() => cancel . stdout . emit ( 'data' , ` ${ handle } : org.freedesktop.portal.Request.Response (uint32 1, {})\n` )); return { stdout :`(objectpath ' ${ handle } ',)\n` }; }; 113assert . deepEqual ( await portalFolder ( 'Pick' , { run :run2 , env :{}, spawnProcess :() => cancel }), { cancelled :true }); 114const none = new EventEmitter (); none . stdout = new EventEmitter (); none . kill = () => {}; none . exitCode = null ; 115assert . equal ( await portalFolder ( 'Pick' , { run :async () => { throw failure ( 'ENOENT' ); }, env :{}, spawnProcess :() => none }), null ); 116});