Pass ROOTSYS, MUSRFITPATH, LD_LIBRARY_PATH and the Linux executable path inside WSL for direct and shell-based launches. Preserve Windows-native tool environments and downloaded-data paths. Include boost-devel in the WSL runtime and verify ROOT can parse the plotting header during packaging. Document the non-destructive dependency update for already imported runtimes. Validated the Electron suite, all five bundled executables, shell-based msr2data, a real fit, and PNG generation inside the bundled filesystem.
249 lines
13 KiB
JavaScript
249 lines
13 KiB
JavaScript
// Copyright (C) 2026 Zaher Salman
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
// File-transfer and desktop workflow tests use real temporary files and web
|
|
// streams, including interruption, a read-only destination, and offline reuse.
|
|
const assert = require('assert');
|
|
const fs = require('fs');
|
|
const os = require('os');
|
|
const path = require('path');
|
|
const vm = require('vm');
|
|
const { RunDataDownloader, ENDPOINT } = require('../run-data-download');
|
|
|
|
const repoRoot = path.resolve(__dirname, '..');
|
|
const testRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'musrflow-run-data-'));
|
|
const bytes = Buffer.alloc(200003);
|
|
for (let index = 0; index < bytes.length; index++) bytes[index] = index % 256;
|
|
const requests = [];
|
|
let transferMode = 'normal';
|
|
|
|
async function fetchRun(url, options) {
|
|
assert.strictEqual(url, ENDPOINT);
|
|
assert.strictEqual(options.redirect, 'error');
|
|
const input = Object.fromEntries(new URLSearchParams(options.body));
|
|
requests.push(input);
|
|
if (input.run === '999') {
|
|
return new Response(JSON.stringify({ message: 'Data file not found', reason: 'run-file-not-found' }), {
|
|
status: 404, headers: { 'content-type': 'application/json' }
|
|
});
|
|
}
|
|
const filename = 'lem26_his_' + input.run.padStart(4, '0') + '.root';
|
|
if (input.action === 'run_file_info') {
|
|
return new Response(JSON.stringify({ ok: true, instrument: 'LEM', year: 2026,
|
|
run: Number(input.run), filename: filename, relative_path: 'lem/his/2026/' + filename,
|
|
format: 'MUSR-ROOT', size: bytes.length, mtime: 123456, version: 'a'.repeat(64) }), {
|
|
headers: { 'content-type': 'application/json' }
|
|
});
|
|
}
|
|
assert.strictEqual(input.version, 'a'.repeat(64));
|
|
if (transferMode === 'json') return new Response('{}', { headers: { 'content-type': 'application/json' } });
|
|
let sent = false;
|
|
const body = transferMode === 'abort' ? new ReadableStream({
|
|
pull: function(controller) {
|
|
if (!sent) {
|
|
controller.enqueue(bytes.subarray(0, 100));
|
|
sent = true;
|
|
}
|
|
// The test aborts after this first chunk, while a real transfer
|
|
// would still be waiting for network data.
|
|
}
|
|
}) : (transferMode === 'short' ? bytes.subarray(0, 1234) : bytes);
|
|
return new Response(body, { headers: {
|
|
'content-type': 'application/octet-stream', 'content-length': String(bytes.length)
|
|
} });
|
|
}
|
|
|
|
function identity(run) {
|
|
return { instrument: 'LEM', year: 2026, run: run };
|
|
}
|
|
|
|
function loadDesktop(files, root) {
|
|
const elements = { runNumbers: { value: '514' } };
|
|
let acquired = 0;
|
|
const sandbox = {
|
|
require: require, console: { log: function() {}, warn: function() {} },
|
|
process: { platform: 'linux', cwd: function() { return root; },
|
|
env: { HOME: root, MUSRFULLDATAPATH: '/existing/search', PATH: process.env.PATH } },
|
|
document: { getElementById: function(id) { return elements[id] || null; } },
|
|
window: { nodeFs: fs, nodePath: path, nodeChildProcess: {},
|
|
electronIpcRenderer: { on: function() {}, send: function() {} },
|
|
electronAPI: { runData: {
|
|
configuration: async function() { return { directory: root, files: files }; },
|
|
onProgress: function() { return function() {}; },
|
|
acquire: async function() { acquired++; return { ok: false, cancelled: true }; }
|
|
} }
|
|
}
|
|
};
|
|
vm.runInNewContext(fs.readFileSync(path.join(repoRoot, 'muSRFitlib.js'), 'utf8'), sandbox);
|
|
vm.runInNewContext(fs.readFileSync(path.join(repoRoot, 'muSRFitelec.js'), 'utf8'), sandbox);
|
|
sandbox.acquireCount = function() { return acquired; };
|
|
return sandbox;
|
|
}
|
|
|
|
async function run() {
|
|
const personal = path.join(testRoot, 'personal data');
|
|
const primary = path.join(testRoot, 'primary data');
|
|
const downloader = new RunDataDownloader({ directory: personal, fetch: fetchRun });
|
|
const progress = [];
|
|
const controller = new AbortController();
|
|
const plan = await downloader.prepare([identity(514), identity(515), identity(514)], primary,
|
|
controller.signal, function(state) { progress.push(state); });
|
|
assert.strictEqual(plan.downloads.length, 2, 'deduplicate physical runs before retrieval');
|
|
assert.strictEqual(plan.totalBytes, bytes.length * 2);
|
|
assert.ok(plan.downloads.every(function(file) { return file.storageRoot === primary; }));
|
|
const downloaded = await downloader.download(plan, controller.signal, function(state) { progress.push(state); });
|
|
assert.strictEqual(downloaded.files.length, 2);
|
|
for (const file of downloaded.files) assert.deepStrictEqual(fs.readFileSync(file.filePath), bytes);
|
|
assert.strictEqual(progress[progress.length - 1].received, bytes.length * 2);
|
|
|
|
// A fresh app process can use its indexed downloads without contacting the
|
|
// server, including when the network is unavailable.
|
|
const offline = new RunDataDownloader({ directory: personal, fetch: async function() {
|
|
throw new Error('Network access should not be needed');
|
|
} });
|
|
const reused = await offline.prepare([identity(514)], primary, controller.signal, function() {});
|
|
assert.strictEqual(reused.downloads.length, 0);
|
|
assert.strictEqual(reused.files.length, 1);
|
|
|
|
// Use a file as an impossible directory too, so the fallback test remains
|
|
// meaningful even when this suite is run by root in a build container.
|
|
const blocked = path.join(testRoot, 'not-a-directory');
|
|
fs.writeFileSync(blocked, 'keep me');
|
|
const fallback = await downloader.prepare([identity(516)], blocked, controller.signal, function() {});
|
|
assert.strictEqual(fallback.downloads[0].storageRoot, personal);
|
|
await downloader.download(fallback, controller.signal, function() {});
|
|
assert.strictEqual(fs.readFileSync(blocked, 'utf8'), 'keep me');
|
|
if (!process.getuid || process.getuid() !== 0) {
|
|
const readOnly = path.join(testRoot, 'read-only');
|
|
fs.mkdirSync(readOnly, { mode: 0o555 });
|
|
const readOnlyPlan = await downloader.prepare([identity(517)], readOnly, controller.signal, function() {});
|
|
assert.strictEqual(readOnlyPlan.downloads[0].storageRoot, personal);
|
|
fs.chmodSync(readOnly, 0o755);
|
|
}
|
|
|
|
const missing = await downloader.prepare([identity(999)], primary, controller.signal, function() {});
|
|
assert.strictEqual(missing.downloads.length, 0);
|
|
assert.strictEqual(missing.missing[0].run, 999);
|
|
|
|
for (const mode of ['short', 'json', 'abort']) {
|
|
const interruptedPlan = await downloader.prepare([identity(520)], primary, controller.signal, function() {});
|
|
const interruption = new AbortController();
|
|
transferMode = mode;
|
|
await assert.rejects(downloader.download(interruptedPlan, interruption.signal, function() {
|
|
if (mode === 'abort') interruption.abort();
|
|
}));
|
|
transferMode = 'normal';
|
|
assert.strictEqual(fs.existsSync(interruptedPlan.downloads[0].filePath), false);
|
|
assert.ok(!fs.readdirSync(path.dirname(interruptedPlan.downloads[0].filePath)).some(function(name) {
|
|
return name.endsWith('.part');
|
|
}), 'failed transfers leave no partial data files');
|
|
}
|
|
const collision = await downloader.prepare([identity(521)], primary, controller.signal, function() {});
|
|
fs.writeFileSync(collision.downloads[0].filePath, 'original local data');
|
|
await assert.rejects(downloader.download(collision, controller.signal, function() {}), /EEXIST/);
|
|
assert.strictEqual(fs.readFileSync(collision.downloads[0].filePath, 'utf8'), 'original local data');
|
|
|
|
const desktop = loadDesktop(await downloader.localFiles(), personal);
|
|
await desktop.initializeLocalRunData();
|
|
const runLine = desktop.runFileNameAuto(514, '2026', 'LEM', blocked);
|
|
assert.strictEqual(runLine, 'RUN lem/his/2026/lem26_his_0514 MUE4 PSI MUSR-ROOT');
|
|
assert.strictEqual(desktop.runSuffixFromFile(514, 'LEM', '2026', blocked), '0514');
|
|
assert.strictEqual(desktop.seriesDataFilePath(514, { selYear: '2026', instrName: 'LEM', dataRoot: blocked }),
|
|
downloaded.files[0].filePath);
|
|
const header = desktop.dumpHeaderRequestFromRunDirective(runLine);
|
|
assert.strictEqual(header.filePath, downloaded.files[0].filePath);
|
|
const environment = desktop.buildToolEnvironment();
|
|
assert.ok(environment.MUSRFULLDATAPATH.includes(primary));
|
|
assert.ok(environment.MUSRFULLDATAPATH.includes(personal));
|
|
assert.ok(environment.MUSRFULLDATAPATH.endsWith('/existing/search'));
|
|
const available = await desktop.ensureLocalRunData({ action: 'Fit',
|
|
all: { instrName: 'LEM', selYear: '2026', dataRoot: blocked, runNumbers: '514' },
|
|
runSelection: [{ main: 514, add: [] }] });
|
|
assert.strictEqual(available.ok, true);
|
|
assert.strictEqual(desktop.acquireCount(), 0);
|
|
const cancelled = await desktop.ensureLocalRunData({ action: 'Plot',
|
|
all: { instrName: 'LEM', selYear: '2026', dataRoot: blocked, runNumbers: '514' },
|
|
runSelection: [{ main: 530, add: [531] }] });
|
|
assert.strictEqual(cancelled.outcome, 'aborted');
|
|
|
|
// A delayed header guess cannot replace a parameter that the user edited
|
|
// before starting the download, even if they make no edits during it.
|
|
let guesses = 0;
|
|
desktop.buildFitParameterSyncSpec = function() { return { parameters: [{ value: 'edited' }] }; };
|
|
desktop.initialParameterGuessState.pendingDownloadGuess = {
|
|
generation: desktop.initialParameterGuessState.generation,
|
|
parameters: JSON.stringify({ parameters: [{ value: 'default' }] })
|
|
};
|
|
desktop.window.electronAPI.runData.acquire = async function() { return { ok: true, files: [] }; };
|
|
desktop.scheduleInitialParameterGuess = async function() { guesses++; };
|
|
await desktop.ensureLocalRunData({ action: 'Fit',
|
|
all: { instrName: 'LEM', selYear: '2026', dataRoot: blocked, runNumbers: '514' },
|
|
runSelection: [{ main: 530, add: [] }] });
|
|
assert.strictEqual(guesses, 0);
|
|
|
|
const outcomes = [];
|
|
desktop.validateRunSelection = function() { return true; };
|
|
desktop.confirmOverwriteDirtyEditor = function() { return true; };
|
|
desktop.beginJobPreparation = function() {};
|
|
desktop.prepareMSRRunFiles = async function() {
|
|
return { ok: false, outcome: 'missing-data', message: 'Run data is unavailable.' };
|
|
};
|
|
desktop.finishJobStatus = function(ok, code, message, outcome) { outcomes.push(outcome); };
|
|
desktop.setMusrfitActionState('', 'idle');
|
|
await desktop.startPlot();
|
|
assert.deepStrictEqual(outcomes, ['missing-data']);
|
|
|
|
// The series planner may reuse or explicitly skip results without raw
|
|
// data. Only a blocked fit's main/ADDRUN selection enters the preflight.
|
|
let inspections = 0;
|
|
const offered = [];
|
|
desktop.All = { instrName: 'LEM', selYear: '2026', dataRoot: blocked, runNumbers: '514,530+531', workPath: testRoot };
|
|
desktop.collectSharedParameterControls = function() { return {}; };
|
|
desktop.collectSeriesActionOverrides = function() { return {}; };
|
|
desktop.collectSeriesGUIEditedRuns = function() { return {}; };
|
|
desktop.updateJobStatus = function() {};
|
|
desktop.appendJobLog = function() {};
|
|
desktop.finishJobStatus = function() {};
|
|
desktop.inspectSeriesFitPlan = async function() {
|
|
inspections++;
|
|
if (inspections === 2) return { ok: false, reason: 'test-after-preflight' };
|
|
return { ok: true, entries: [
|
|
{ action: 'skip', reusable: true, dataSet: { main: 514, add: [] } },
|
|
{ action: 'blocked', reason: 'required-data-not-found', dataSet: { main: 530, add: [531] } }
|
|
] };
|
|
};
|
|
desktop.window.MusRFlowPlatform.ensureRunData = async function(options) {
|
|
offered.push(...options.runSelection);
|
|
return { ok: true };
|
|
};
|
|
await desktop.runIndependentSeriesFit({ dataSetCount: 2 }, false);
|
|
assert.strictEqual(inspections, 2);
|
|
assert.strictEqual(JSON.stringify(offered), JSON.stringify([{ main: 530, add: [531] }]));
|
|
|
|
// WSL receives search paths as a single env argument, so Windows drive
|
|
// letters and spaces in a user profile never become MSR tokens.
|
|
desktop.nodeProcess.platform = 'win32';
|
|
desktop.nodeProcess.env.MUSRFULLDATAPATH = '';
|
|
desktop.nodePath = path.win32;
|
|
desktop.electronRunDataFiles = [{ storageRoot: 'C:\\Users\\Test User\\data', instrument: 'LEM' }];
|
|
desktop.bundledRuntimeRoot = function() { return 'C:\\resources\\runtime'; };
|
|
desktop.bundledRuntimeBinDir = function() { return 'C:\\resources\\runtime\\bin'; };
|
|
const launch = desktop.prepareLocalLaunch('C:\\resources\\runtime\\bin\\musrfit', ['514_asy.msr']);
|
|
assert.strictEqual(launch.command, 'wsl.exe');
|
|
assert.strictEqual(launch.args[3], 'env');
|
|
const dataEnvironment = launch.args.find(arg => arg.startsWith('MUSRFULLDATAPATH='));
|
|
assert.ok(dataEnvironment.includes('/mnt/c/Users/Test User/data'));
|
|
assert.ok(!dataEnvironment.includes('C:'));
|
|
assert.ok(launch.args.includes('LD_LIBRARY_PATH=/opt/musrfit-runtime/lib'));
|
|
assert.ok(desktop.prepareWslCommandString('C:\\resources\\runtime\\bin\\msr2data [514] _asy')
|
|
.includes('"MUSRFULLDATAPATH=/mnt/c/Users/Test User/data'));
|
|
console.log('Electron run download, fallback, offline reuse, and workflow tests passed');
|
|
}
|
|
|
|
run().catch(function(error) {
|
|
console.error(error);
|
|
process.exitCode = 1;
|
|
}).finally(function() {
|
|
fs.rmSync(testRoot, { recursive: true, force: true });
|
|
});
|