okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-split-int' into f-split-fitfa6367a98

main
7.7 KiB116 linesraw
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 = {
15  APPDIR, APPIMAGE: '/home/u/Strata.AppImage', ARGV0: 'Strata',
16  LD_LIBRARY_PATH: `${APPDIR}/usr/lib:${APPDIR}/usr/lib/x86_64-linux-gnu:/opt/cuda/lib64`,
17  PATH: `${APPDIR}/usr/bin:/usr/local/bin:/usr/bin`,
18  XDG_DATA_DIRS: `${APPDIR}/usr/share:/usr/share:/usr/local/share`,
19  GTK_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`,
20  GSETTINGS_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', () => {
24  const env = externalEnvironment(leaky);
25  assert.equal(env.APPDIR, undefined);
26  assert.equal(env.APPIMAGE, undefined);
27  assert.equal(env.LD_LIBRARY_PATH, '/opt/cuda/lib64', 'only the AppImage\'s folders go from the list');
28  assert.equal(env.PATH, '/usr/local/bin:/usr/bin');
29  assert.equal(env.XDG_DATA_DIRS, '/usr/share:/usr/local/share');
30  for (const name of ['GTK_PATH', 'GIO_MODULE_DIR', 'GDK_PIXBUF_MODULE_FILE', 'GSETTINGS_SCHEMA_DIR', 'PYTHONHOME']) assert.equal(env[name], undefined, name);
31  assert.equal(env.HOME, '/home/u');
32  assert.equal(env.DISPLAY, ':0');
33});
34
35test('with the launcher\'s record, the original environment is restored exactly', () => {
36  const original = { PATH: '/usr/bin', LD_LIBRARY_PATH: '/mine', HOME: '/home/u', DISPLAY: ':1' };
37  const record = Buffer.from(Object.entries(original).map(([k, v]) => `${k}=${v}`).join('\0') + '\0').toString('base64');
38  const env = externalEnvironment({ ...leaky, STRATA_ORIGINAL_ENV: record });
39  assert.deepEqual(env, original);
40  assert.deepEqual(recordedOriginal({ STRATA_ORIGINAL_ENV: record }), original);
41  assert.equal(recordedOriginal({ STRATA_ORIGINAL_ENV: '!!!' }), null);
42});
43
44test('outside an AppImage nothing changes', () => {
45  const plain = { PATH: '/usr/bin', LD_LIBRARY_PATH: '/opt/x/lib', HOME: '/h' };
46  assert.deepEqual(externalEnvironment(plain), plain);
47  assert.deepEqual(externalEnvironment(plain, { A: '1' }), { ...plain, A: '1' });
48  assert.deepEqual(withoutAppImage(plain), plain);
49});
50
51test('the coding assistant\'s shell does not inherit the AppImage\'s settings either', () => {
52  const env = agentEnvironment({ ...leaky, SOME_TOKEN: 'x' }, { DSH_HOME: '/h' });
53  assert.equal(env.LD_LIBRARY_PATH, undefined);
54  assert.ok(!/\.mount_/.test(JSON.stringify(env)), JSON.stringify(env));
55  assert.equal(env.DSH_HOME, '/h');
56  assert.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 () => {
62  const seen = [];
63  const libssl = failure(127, 'zenity: error while loading shared libraries: libssl.so.3: version `OPENSSL_3.2.0\' not found');
64  const run = async (command, args, options) => { seen.push({ command, env: options?.env }); if (command === 'zenity') throw libssl; return { stdout: '/home/u/Models/\n' }; };
65  const old = { ...process.env };
66  Object.assign(process.env, leaky);
67  try {
68    const picked = await chooseFolder('Pick', { platform: 'linux', run, portal: async () => null });
69    assert.deepEqual(picked, { path: '/home/u/Models' });
70    assert.deepEqual(seen.map(s => s.command), ['zenity', 'kdialog']);
71    for (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
73    const none = await chooseFolder('Pick', { platform: 'linux', run: async () => { throw libssl; }, portal: async () => null });
74    assert.equal(none.unavailable, true);
75    assert.equal(none.failed, true);
76    assert.match(none.reason, /Install zenity or kdialog to pick a folder, or type the path/);
77    const answer = chooserUnavailable(none);
78    assert.equal(answer.code, 'folder-chooser-unavailable');
79    const missingAll = await chooseFolder('Pick', { platform: 'linux', run: async () => { throw failure('ENOENT'); }, portal: async () => null });
80    assert.equal(missingAll.unavailable, true);
81    assert.match(missingAll.reason, /zenity or kdialog/);
82    // a plain exit 1 is still a cancel; so is exit 1 with harmless warnings
83    assert.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
85    const noDisplay = await chooseFolder('Pick', { platform: 'linux', run: async () => { throw failure(1, 'Unable to init server: Could not connect: Connection refused'); }, portal: async () => null });
86    assert.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 () => {
91  const run = async () => { throw failure('ENOENT'); };
92  assert.deepEqual(await chooseFolder('Pick', { platform: 'linux', run, portal: async () => ({ path: '/home/u/Pics' }) }), { path: '/home/u/Pics' });
93  assert.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 () => {
97  const handle = '/org/freedesktop/portal/desktop/request/1_42/t1';
98  const monitor = new EventEmitter();
99  monitor.stdout = new EventEmitter();
100  monitor.kill = () => { monitor.exitCode = 0; };
101  monitor.exitCode = null;
102  const spawnProcess = () => monitor;
103  const run = async (command, args) => {
104    assert.equal(command, 'gdbus');
105    assert.ok(args.includes('org.freedesktop.portal.FileChooser.OpenFile'));
106    queueMicrotask(() => monitor.stdout.emit('data', `${handle}: org.freedesktop.portal.Request.Response (uint32 0, {'uris': <['file:///home/u/My%20Models']>})\n`));
107    return { stdout: `(objectpath '${handle}',)\n` };
108  };
109  assert.deepEqual(await portalFolder('Pick', { run, env: {}, spawnProcess }), { path: '/home/u/My Models' });
110  const cancel = new EventEmitter();
111  cancel.stdout = new EventEmitter(); cancel.kill = () => {}; cancel.exitCode = null;
112  const run2 = async () => { queueMicrotask(() => cancel.stdout.emit('data', `${handle}: org.freedesktop.portal.Request.Response (uint32 1, {})\n`)); return { stdout: `(objectpath '${handle}',)\n` }; };
113  assert.deepEqual(await portalFolder('Pick', { run: run2, env: {}, spawnProcess: () => cancel }), { cancelled: true });
114  const none = new EventEmitter(); none.stdout = new EventEmitter(); none.kill = () => {}; none.exitCode = null;
115  assert.equal(await portalFolder('Pick', { run: async () => { throw failure('ENOENT'); }, env: {}, spawnProcess: () => none }), null);
116});