Add Electron run downloads with writable-folder fallback
Offer HTTPS downloads for missing Fit/Plot inputs and independent-series runs that need fitting. Preserve reused and skipped results without downloading raw data. Prefer the configured Data directory and fall back to persistent per-user storage when it is read-only. Keep user XML unchanged and provide native/WSL search paths through MUSRFULLDATAPATH. Stream cancellable downloads into temporary files, validate lengths, publish without overwriting existing data, and remember exact filenames for offline reuse. Add regression coverage and document the desktop workflow.
This commit is contained in:
1 parent
0990cab5de
commit
20cd71840a
11 files changed
+824
-12
No files matched your search
+14
-1
@@ -5,6 +5,19 @@ release path documented in [`TODO.md`](TODO.md).
|
||||
|
||||
## 1.0.0-rc.5 (unreleased)
|
||||
|
||||
### Desktop run downloads
|
||||
- Fit and Plot can retrieve missing selected runs from musruser.psi.ch over
|
||||
HTTPS after a confirmation showing the file count, total size, and destination.
|
||||
Independent series request data only for entries that need fitting.
|
||||
- Downloads prefer the configured Data directory. Read-only instrument/year
|
||||
folders fall back to a persistent personal `run-data` folder, whose location
|
||||
is shown in Preferences. Data directory settings and user startup XML files
|
||||
are preserved; local tools receive the additional search roots through
|
||||
`MUSRFULLDATAPATH`, with Windows paths translated for WSL2.
|
||||
- Transfers support cancellation, validate their lengths, and publish completed
|
||||
files without replacing existing data. Exact filenames are remembered for
|
||||
offline reuse across sessions, including historical campaign names.
|
||||
|
||||
### Parameter ordering
|
||||
- The model catalogue now lists the physical parameters of a theory function
|
||||
first (asymmetry, then relaxation/lineshape, then precession frequency, then
|
||||
@@ -370,4 +383,4 @@ release path documented in [`TODO.md`](TODO.md).
|
||||
bounds, t0/background, FFT, and missing-data workflows.
|
||||
- Integration checks exercising real `musrfit`, `msr2data`, and `musrview`.
|
||||
- Editor-owned MSR titles are no longer replaced unexpectedly by `musrfit -t`.
|
||||
- First scan-detection and built-in plot-template tasks shipped.
|
||||
- First scan-detection and built-in plot-template tasks shipped.
|
||||
@@ -16,7 +16,7 @@ the command-line tools live.
|
||||
- Locally via the ``PATH``.
|
||||
- On the server behind the CGI endpoint.
|
||||
* - Data access
|
||||
- Any local directory under the configured data root.
|
||||
- Local data plus optional downloads of missing selected runs.
|
||||
- What the server data tree exposes.
|
||||
* - Preferences
|
||||
- Per-user, through **File / Preferences**.
|
||||
@@ -58,4 +58,25 @@ once per user (see ``packaging/wsl2/README.md``).
|
||||
|
||||
The workflow chapters of this manual apply to both editions. Where a step is
|
||||
specific to one edition (handling files, Preferences), the text says so
|
||||
explicitly.
|
||||
explicitly.
|
||||
|
||||
Downloading missing runs
|
||||
------------------------
|
||||
|
||||
Fit and Plot check selected main runs and ADDRUN members before preparing MSR
|
||||
files. If files are missing, the desktop offers to download them over HTTPS
|
||||
from ``musruser.psi.ch`` and continue. Independent series fits request data
|
||||
only for entries that need fitting; reused results and explicit skips do not
|
||||
trigger downloads. Runs absent from the server are still reported as missing.
|
||||
|
||||
The writable Data directory is preferred. When it is read-only, a persistent
|
||||
folder under the user's application data directory is used instead. Exact
|
||||
filenames and formats are remembered across sessions, and incomplete downloads
|
||||
are never published as run files. Expert MSRs with manually specified RUN
|
||||
paths continue to use their existing file-resolution rules.
|
||||
|
||||
Generated download RUN paths are relative to the stored data tree. muSRFlow
|
||||
passes the matching search roots to its tools through ``MUSRFULLDATAPATH``,
|
||||
including translated paths for Windows/WSL2. To run such an MSR from a terminal
|
||||
outside muSRFlow, set that variable to the download root or add the root to
|
||||
your own musrfit startup XML.
|
||||
@@ -13,6 +13,18 @@ Data directory
|
||||
The root of the instrument data tree (for example
|
||||
``/psi.ch/group/lmu/public/data``). Run paths are resolved under it.
|
||||
|
||||
When Fit or Plot needs runs that are missing locally, the desktop can
|
||||
download them from ``musruser.psi.ch`` after confirmation. Downloads go
|
||||
into this directory when the instrument/year folder is writable. Otherwise
|
||||
they go into ``run-data`` under your personal muSRFlow application data
|
||||
directory. The hint under the Data directory shows that fallback path,
|
||||
and the confirmation lists the actual destination and total size.
|
||||
|
||||
Both locations remain available to subsequent fits, including offline.
|
||||
Cancelling a transfer discards its incomplete file. Your selected Data
|
||||
directory and startup XML are preserved; muSRFlow supplies the additional
|
||||
search paths through ``MUSRFULLDATAPATH`` when launching its tools.
|
||||
|
||||
musrfit startup XML
|
||||
The ``musrfit_startup.xml`` used to resolve portable run names. Leave it
|
||||
as **auto-detect** and muSRFlow looks for the standard installation file;
|
||||
@@ -60,4 +72,4 @@ Output file suffixes
|
||||
|
||||
Save stores the values for the next session; **Cancel** closes the dialog
|
||||
without changing anything. Directory changes take effect from the next
|
||||
action that resolves or writes files.
|
||||
action that resolves or writes files.
|
||||
@@ -749,6 +749,7 @@ pairs of a multi-run (global) fit.">
|
||||
<input type="text" id="dataRoot" value="/psi.ch/group/lmu/public/data">
|
||||
<button type="button" id="dataRootBrowse" onclick="selectDataRootFolder();">Browse...</button>
|
||||
</div>
|
||||
<span class="preferences-hint" id="runDataDownloadHint">Missing run files can be downloaded from musruser.psi.ch. Read-only directories use a personal data folder.</span>
|
||||
</label>
|
||||
<label class="settings-field" for="startupXml">
|
||||
<span>musrfit startup XML</span>
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
// Copyright (C) 2026 Zaher Salman
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
const { app, BrowserWindow, Menu, dialog, ipcMain, screen, shell, clipboard } = require('electron');
|
||||
const { app, BrowserWindow, Menu, dialog, ipcMain, screen, shell, clipboard, net } = require('electron');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const childProcess = require('child_process');
|
||||
const wslRuntime = require('./wsl-runtime');
|
||||
const { RunDataDownloader } = require('./run-data-download');
|
||||
let runDataDownloader = null;
|
||||
let runDataJob = null;
|
||||
|
||||
// An SSH-forwarded X display belongs to the remote client, but pc13280 also
|
||||
// exposes the user's persistent local desktop D-Bus session. Electron otherwise
|
||||
@@ -397,6 +400,10 @@ if (relaunchingForForwardedX11) {
|
||||
// (emitted by the web Share flow) launches this app. electron-builder adds
|
||||
// the matching MimeType=x-scheme-handler/musrflow; desktop entry on Linux.
|
||||
app.setAsDefaultProtocolClient("musrflow");
|
||||
runDataDownloader = new RunDataDownloader({
|
||||
directory: path.join(app.getPath('userData'), 'run-data'),
|
||||
fetch: net.fetch.bind(net)
|
||||
});
|
||||
createWindow();
|
||||
setupWslRuntime();
|
||||
});
|
||||
@@ -458,6 +465,64 @@ ipcMain.handle("ensureWslRuntime", function () {
|
||||
return setupWslRuntime();
|
||||
});
|
||||
|
||||
ipcMain.handle('runDataConfiguration', async function(event) {
|
||||
if (event.sender !== mainWindow.webContents) throw new Error('Invalid download window.');
|
||||
return { directory: runDataDownloader.directory, files: await runDataDownloader.localFiles() };
|
||||
});
|
||||
|
||||
ipcMain.handle('acquireRunData', async function(event, input) {
|
||||
if (event.sender !== mainWindow.webContents) throw new Error('Invalid download window.');
|
||||
if (runDataJob) return { ok: false, message: 'A data download is already running.' };
|
||||
if (!input || !/^[A-Za-z0-9_-]{1,80}$/.test(input.token)) {
|
||||
return { ok: false, message: 'Invalid download request.' };
|
||||
}
|
||||
const controller = new AbortController();
|
||||
runDataJob = { token: input.token, controller: controller };
|
||||
try {
|
||||
const progress = function(state) {
|
||||
if (!event.sender.isDestroyed()) {
|
||||
event.sender.send('runDataProgress', Object.assign({ token: input.token }, state));
|
||||
}
|
||||
};
|
||||
const plan = await runDataDownloader.prepare(input.requests, input.dataRoot, controller.signal, progress);
|
||||
if (plan.downloads.length) {
|
||||
const roots = Array.from(new Set(plan.downloads.map(function(file) { return file.storageRoot; })));
|
||||
let detail = plan.downloads.slice(0, 12).map(function(file) {
|
||||
return file.instrument + ' ' + file.year + ' / run ' + file.run;
|
||||
}).join('\n');
|
||||
if (plan.downloads.length > 12) detail += '\n...';
|
||||
detail += '\n\nTotal: ' + (plan.totalBytes / 1000000).toFixed(1) + ' MB';
|
||||
detail += '\nSave to:\n' + roots.join('\n');
|
||||
if (roots.some(function(root) { return root !== path.resolve(input.dataRoot); })) {
|
||||
detail += '\n\nThe Data directory is not writable. Downloads will use your personal muSRFlow data folder.';
|
||||
}
|
||||
if (plan.missing.length) {
|
||||
detail += '\n\n' + plan.missing.length + ' other run file(s) are unavailable on the server.';
|
||||
}
|
||||
controller.signal.throwIfAborted();
|
||||
const choice = await dialog.showMessageBox(mainWindow, {
|
||||
type: 'question', title: 'Download run data',
|
||||
message: 'Download ' + plan.downloads.length + ' missing run file(s) from musruser.psi.ch?',
|
||||
detail: detail, buttons: ['Download and continue', 'Cancel'], defaultId: 0, cancelId: 1
|
||||
});
|
||||
if (choice.response !== 0) return { ok: false, cancelled: true, files: plan.files };
|
||||
}
|
||||
return await runDataDownloader.download(plan, controller.signal, progress);
|
||||
} catch (error) {
|
||||
return { ok: false, cancelled: controller.signal.aborted,
|
||||
message: error.message, files: await runDataDownloader.localFiles() };
|
||||
} finally {
|
||||
runDataJob = null;
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('cancelRunData', function(event, token) {
|
||||
if (event.sender !== mainWindow.webContents) throw new Error('Invalid download window.');
|
||||
if (!runDataJob || runDataJob.token !== token) return false;
|
||||
runDataJob.controller.abort();
|
||||
return true;
|
||||
});
|
||||
|
||||
app.on('window-all-closed', () => {
|
||||
if (process.platform !== 'darwin') {
|
||||
app.quit()
|
||||
|
||||
+164
-2
@@ -78,8 +78,16 @@ var electronRunJobLabel = "";
|
||||
var electronRunJobLogFile = "";
|
||||
var electronRunJobAborted = false;
|
||||
var electronRunJobSeq = 0;
|
||||
var electronRunDataFiles = [];
|
||||
var electronRunDataReady = null;
|
||||
var electronRunDataToken = '';
|
||||
|
||||
function electronRequestAbort(action) {
|
||||
if (electronRunDataToken && window.electronAPI && window.electronAPI.runData) {
|
||||
window.electronAPI.runData.cancel(electronRunDataToken);
|
||||
setFitStatus('Stopping the data download...');
|
||||
return true;
|
||||
}
|
||||
if (electronRunJobToken === "" || typeof nodeChildProcess.abortJob !== "function") {
|
||||
return false;
|
||||
}
|
||||
@@ -346,6 +354,151 @@ window.MusRFlowPlatform.currentDirectory = function() {
|
||||
window.MusRFlowPlatform.msrFileRunner = function(fullPath, options) {
|
||||
return runMSRFileLocal(fullPath, options);
|
||||
};
|
||||
window.MusRFlowPlatform.ensureRunData = ensureLocalRunData;
|
||||
window.MusRFlowPlatform.resolveRunData = resolveLocalRunData;
|
||||
window.MusRFlowPlatform.resolveDataFile = function(runName) {
|
||||
for (let file of electronRunDataFiles) {
|
||||
let resolved = resolveLocalRunData(file.run, file.year, file.instrument);
|
||||
if (resolved && resolved.runName === runName) return resolved.filePath;
|
||||
}
|
||||
return '';
|
||||
};
|
||||
|
||||
function rememberLocalRunData(files) {
|
||||
for (let file of files || []) {
|
||||
let index = electronRunDataFiles.findIndex(function(existing) {
|
||||
return existing.instrument === file.instrument && existing.year === file.year
|
||||
&& existing.run === file.run;
|
||||
});
|
||||
if (index !== -1) electronRunDataFiles.splice(index, 1);
|
||||
electronRunDataFiles.push(file);
|
||||
}
|
||||
}
|
||||
|
||||
function initializeLocalRunData() {
|
||||
if (electronRunDataReady) return electronRunDataReady;
|
||||
let api = window.electronAPI && window.electronAPI.runData;
|
||||
if (!api) return Promise.resolve();
|
||||
electronRunDataReady = api.configuration().then(function(configuration) {
|
||||
rememberLocalRunData(configuration.files);
|
||||
let hint = document.getElementById('runDataDownloadHint');
|
||||
if (hint) {
|
||||
hint.textContent = 'Missing runs can be downloaded from musruser.psi.ch. '
|
||||
+ 'Read-only data directories use: ' + configuration.directory;
|
||||
}
|
||||
}).catch(function(error) {
|
||||
electronRunDataReady = null;
|
||||
console.warn('Could not load downloaded runs: ' + error.message);
|
||||
});
|
||||
return electronRunDataReady;
|
||||
}
|
||||
|
||||
function resolveLocalRunData(run, year, instrument) {
|
||||
let file = electronRunDataFiles.find(function(record) {
|
||||
return record.run === Number(run) && record.year === Number(year)
|
||||
&& record.instrument === instrument;
|
||||
});
|
||||
if (!file) return null;
|
||||
try {
|
||||
let stat = nodeFs.statSync(file.filePath);
|
||||
if (!stat.isFile() || stat.size !== file.size) return null;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
// Keep spaces and Windows drive letters outside the MSR's whitespace-based
|
||||
// grammar. MUSRFULLDATAPATH supplies the real storage roots to the tools.
|
||||
let runName = file.relative_path.replace(/\.(root|bin|mdu)$/i, '');
|
||||
if (file.format === 'PSI-MDU' && /\.bin$/i.test(file.filename)) runName = file.relative_path;
|
||||
return { runName: runName, filePath: file.filePath, format: file.format };
|
||||
}
|
||||
|
||||
function localRunDataSearchPath(viaWsl) {
|
||||
let roots = [];
|
||||
for (let file of electronRunDataFiles) {
|
||||
roots.push(file.storageRoot);
|
||||
roots.push(nodePath.join(file.storageRoot, file.instrument.toLowerCase(), 'his'));
|
||||
}
|
||||
roots = Array.from(new Set(roots));
|
||||
if (viaWsl) roots = roots.map(windowsPathToWsl);
|
||||
let inherited = nodeProcess.env.MUSRFULLDATAPATH || '';
|
||||
if (viaWsl && /^[A-Za-z]:[\\/]/.test(inherited)) inherited = windowsPathToWsl(inherited);
|
||||
if (inherited) roots.push(inherited);
|
||||
return roots.join(':');
|
||||
}
|
||||
|
||||
async function ensureLocalRunData(options) {
|
||||
let api = window.electronAPI && window.electronAPI.runData;
|
||||
if (!api || !options.all.instrName) return { ok: true };
|
||||
await initializeLocalRunData();
|
||||
let all = options.all;
|
||||
let requests = [];
|
||||
let seen = new Set();
|
||||
for (let dataSet of options.runSelection) {
|
||||
for (let run of seriesDataSetPhysicalRuns(dataSet)) {
|
||||
if (seen.has(run)) continue;
|
||||
seen.add(run);
|
||||
let dataPath = seriesDataFilePath(run, all);
|
||||
if (!fileExists(dataPath)) {
|
||||
requests.push({ instrument: all.instrName, year: Number(all.selYear), run: run });
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!requests.length) return { ok: true };
|
||||
|
||||
let action = options.action.toLowerCase();
|
||||
let token = 'data-' + Date.now().toString(36) + '-' + (++electronRunJobSeq);
|
||||
electronRunDataToken = token;
|
||||
setMusrfitActionState(action, 'running');
|
||||
let unsubscribe = api.onProgress(function(state) {
|
||||
if (!state || state.token !== token) return;
|
||||
let message = state.phase === 'checking'
|
||||
? 'Checking server data for run ' + state.run + '...'
|
||||
: 'Downloading ' + state.filename + ' ('
|
||||
+ (100 * state.received / state.totalBytes).toFixed(0) + '%)...';
|
||||
updateJobStatus({ state: 'Downloading data', message: message, noticeLevel: 'info' });
|
||||
let activity = document.getElementById('musrfitActionActivityText');
|
||||
if (activity) activity.textContent = message;
|
||||
});
|
||||
// Retry the automatic first-run guess only while its original parameter
|
||||
// values remain untouched. Imported configurations and explicit models
|
||||
// have already opted out of automatic model selection.
|
||||
let parametersBefore = JSON.stringify(buildFitParameterSyncSpec({}));
|
||||
let selectionBefore = all.runNumbers;
|
||||
try {
|
||||
let result = await api.acquire({ token: token, requests: requests, dataRoot: all.dataRoot });
|
||||
rememberLocalRunData(result.files);
|
||||
if (!result.ok) {
|
||||
return { ok: false, outcome: result.cancelled ? 'aborted' : '',
|
||||
message: result.cancelled ? 'Data download cancelled.' : 'Data download: ' + result.message };
|
||||
}
|
||||
for (let file of result.files || []) appendJobLog('Run data available: ' + file.filePath);
|
||||
let runInput = document.getElementById('runNumbers');
|
||||
if (runInput && runInput.value !== selectionBefore) {
|
||||
return { ok: false, outcome: 'aborted', message: 'Runs changed during download. Start the action again.' };
|
||||
}
|
||||
let pendingGuess = initialParameterGuessState.pendingDownloadGuess;
|
||||
if (automaticInitialFitModel && !initialParameterGuessState.metadata && pendingGuess
|
||||
&& pendingGuess.generation === initialParameterGuessState.generation
|
||||
&& pendingGuess.parameters === parametersBefore
|
||||
&& parametersBefore === JSON.stringify(buildFitParameterSyncSpec({}))) {
|
||||
initialParameterGuessState.appliedGeneration = 0;
|
||||
await scheduleInitialParameterGuess();
|
||||
prepCFG();
|
||||
}
|
||||
if (result.missing && result.missing.length) {
|
||||
let message = result.missing.map(function(file) { return file.message; }).join('\n');
|
||||
appendJobLog(message);
|
||||
if (!options.series) return { ok: false, outcome: 'missing-data', message: message };
|
||||
}
|
||||
return { ok: true };
|
||||
} catch (error) {
|
||||
return { ok: false, message: 'Could not download run data: ' + error.message };
|
||||
} finally {
|
||||
if (typeof unsubscribe === 'function') unsubscribe();
|
||||
electronRunDataToken = '';
|
||||
setMusrfitActionState(action, 'preparing');
|
||||
}
|
||||
}
|
||||
|
||||
// Configuration links produced by Electron always target the canonical web
|
||||
// deployment so they can be opened by another user without Electron installed.
|
||||
@@ -1095,6 +1248,8 @@ function buildToolEnvironment() {
|
||||
: libDir;
|
||||
}
|
||||
env.PATH = localCommandPathBias();
|
||||
let dataSearchPath = localRunDataSearchPath(false);
|
||||
if (dataSearchPath) env.MUSRFULLDATAPATH = dataSearchPath;
|
||||
return env;
|
||||
}
|
||||
|
||||
@@ -1229,7 +1384,11 @@ function prepareLocalLaunch(command, args) {
|
||||
}
|
||||
let relative = command.slice(binDir.length).replace(/^[\\/]+/, "").replace(/\\/g, "/");
|
||||
let inner = MUSRFLOW_WSL_PREFIX + "/bin/" + relative;
|
||||
let wslArgs = ["-d", MUSRFLOW_WSL_DISTRO, "--", inner]
|
||||
let wslArgs = ["-d", MUSRFLOW_WSL_DISTRO, "--"];
|
||||
let dataSearchPath = localRunDataSearchPath(true);
|
||||
if (dataSearchPath) wslArgs.push('env', 'MUSRFULLDATAPATH=' + dataSearchPath);
|
||||
wslArgs.push(inner);
|
||||
wslArgs = wslArgs
|
||||
.concat(args.map(windowsPathToWsl));
|
||||
return { command: "wsl.exe", args: wslArgs, viaWsl: true };
|
||||
}
|
||||
@@ -1250,7 +1409,9 @@ function prepareWslCommandString(command) {
|
||||
rest = rest.replace(/[A-Za-z]:[\\/][^\s"']*/g, function(match) {
|
||||
return windowsPathToWsl(match);
|
||||
});
|
||||
return "wsl.exe -d " + MUSRFLOW_WSL_DISTRO + " -- "
|
||||
let dataSearchPath = localRunDataSearchPath(true);
|
||||
let environment = dataSearchPath ? 'env "MUSRFULLDATAPATH=' + dataSearchPath.replace(/"/g, '\\"') + '" ' : '';
|
||||
return "wsl.exe -d " + MUSRFLOW_WSL_DISTRO + " -- " + environment
|
||||
+ MUSRFLOW_WSL_PREFIX + "/bin/" + exe + rest;
|
||||
}
|
||||
|
||||
@@ -2260,6 +2421,7 @@ function firstLoad() {
|
||||
|
||||
// Load the local startup XML after the Preferences field is initialized.
|
||||
loadLocalPortableRunNaming();
|
||||
initializeLocalRunData();
|
||||
}
|
||||
|
||||
function activeInstrumentName() {
|
||||
|
||||
+54
-4
@@ -593,7 +593,8 @@ var initialParameterGuessState = {
|
||||
appliedGeneration: 0,
|
||||
firstRun: "",
|
||||
metadataKey: "",
|
||||
metadata: null
|
||||
metadata: null,
|
||||
pendingDownloadGuess: null
|
||||
};
|
||||
if (!window.MSREditor) {
|
||||
window.MSREditor = {};
|
||||
@@ -9097,6 +9098,11 @@ function dumpHeaderRequestFromRunDirective(runLine) {
|
||||
if (!info) return null;
|
||||
|
||||
let filePath = fields[1];
|
||||
let platform = window.MusRFlowPlatform;
|
||||
if (platform && typeof platform.resolveDataFile === 'function') {
|
||||
let localPath = platform.resolveDataFile(filePath);
|
||||
if (localPath) return { filePath: localPath, fileFormat: info.dumpFormat };
|
||||
}
|
||||
if (info.extension && !filePath.toLowerCase().endsWith(info.extension)) {
|
||||
filePath += info.extension;
|
||||
}
|
||||
@@ -9507,6 +9513,8 @@ function scheduleInitialParameterGuess() {
|
||||
let metadataKey = headerRequest.filePath + "|" + headerRequest.fileFormat;
|
||||
|
||||
let generation = state.generation;
|
||||
let originalParameters = JSON.stringify(buildFitParameterSyncSpec({}));
|
||||
state.pendingDownloadGuess = null;
|
||||
let useMetadata = function(metadata) {
|
||||
if (state.generation != generation) return false;
|
||||
return applyHeaderBasedInitialGuess(metadata, targets);
|
||||
@@ -9520,6 +9528,7 @@ function scheduleInitialParameterGuess() {
|
||||
if (typeof loadRunHeaderText !== "function") return Promise.resolve(false);
|
||||
let consumeHeader = function(result) {
|
||||
if (!result || !result.ok) {
|
||||
state.pendingDownloadGuess = { generation: generation, parameters: originalParameters };
|
||||
if (result && result.error) console.warn("Initial parameter guess: " + result.error);
|
||||
return false;
|
||||
}
|
||||
@@ -9541,6 +9550,7 @@ function scheduleInitialParameterGuess() {
|
||||
return Promise.resolve(consumeHeader(result));
|
||||
} catch (error) {
|
||||
console.warn("Initial parameter guess: " + error.message);
|
||||
state.pendingDownloadGuess = { generation: generation, parameters: originalParameters };
|
||||
return Promise.resolve(false);
|
||||
}
|
||||
}
|
||||
@@ -11432,6 +11442,16 @@ function runFileNameAuto(run, year, instrName, dataRoot) {
|
||||
}
|
||||
}
|
||||
|
||||
// Downloaded files keep their exact server names and resolve through
|
||||
// per-process search paths, including read-only data mount fallbacks.
|
||||
let platform = window.MusRFlowPlatform;
|
||||
if (platform && typeof platform.resolveRunData === 'function') {
|
||||
let local = platform.resolveRunData(run, year, instrName);
|
||||
if (local) {
|
||||
return ['RUN', local.runName, instrument.beamLine, 'PSI', local.format].join(' ');
|
||||
}
|
||||
}
|
||||
|
||||
let yearText = year.toString();
|
||||
let yearNumber = Number(yearText);
|
||||
let runNumber = Number(run);
|
||||
@@ -11609,7 +11629,7 @@ function runSuffixFromFile(run, instrName, year, dataRoot) {
|
||||
if (parts.length < 2) {
|
||||
return run.toString();
|
||||
}
|
||||
let fileName = parts[1];
|
||||
let fileName = parts[1].replace(/\.(root|bin|mdu)$/i, '');
|
||||
let match = fileName.match(/_(\d+)$/);
|
||||
if (match) {
|
||||
return match[1];
|
||||
@@ -11893,6 +11913,14 @@ async function prepareMSRRunFiles(actionLabel, prepareOptions = {}) {
|
||||
await resolveWebPortableRunSelection(runSelection, All);
|
||||
}
|
||||
|
||||
let platform = window.MusRFlowPlatform;
|
||||
if (!webOrApp && platform && typeof platform.ensureRunData === 'function'
|
||||
&& !isMSREditorSourceAuthoritative('current')
|
||||
&& !isMSREditorSourceAuthoritative('template')) {
|
||||
let data = await platform.ensureRunData({ all: All, runSelection: runSelection, action: actionLabel });
|
||||
if (!data.ok) return data;
|
||||
}
|
||||
|
||||
let fileNameSuffix = fitFileSuffix(All, webOrApp);
|
||||
let workPath = All.workPath && All.workPath !== "" ? All.workPath : defaultWorkPath;
|
||||
|
||||
@@ -12829,6 +12857,28 @@ async function runIndependentSeriesFit(execution, webMode) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Inventory decides which entries need raw data. Reused results and explicit
|
||||
// skips remain offline; download only missing fit/refit inputs, then inspect
|
||||
// again so staging uses the newly available files and exact filenames.
|
||||
let platform = window.MusRFlowPlatform;
|
||||
let missingEntries = plan.entries.filter(function(entry) {
|
||||
return entry.action === 'blocked' && entry.reason === 'required-data-not-found';
|
||||
});
|
||||
if (!webMode && missingEntries.length && platform && typeof platform.ensureRunData === 'function') {
|
||||
let data = await platform.ensureRunData({ all: config.all, action: 'Fit', series: true,
|
||||
runSelection: missingEntries.map(function(entry) { return entry.dataSet; }) });
|
||||
if (!data.ok) {
|
||||
finishJobStatus(0, 1, data.message, data.outcome);
|
||||
return 0;
|
||||
}
|
||||
config.all = Object.assign({}, All);
|
||||
plan = await inspectSeriesFitPlan(config);
|
||||
if (!plan.ok) {
|
||||
finishJobStatus(0, 1, seriesFitFailureMessage(plan.reason, plan));
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Snapshot the per-run statistics for the status bar before anything runs.
|
||||
// Plan entries already parsed them from the existing MSR files, so reused
|
||||
// and skipped runs are visible too; the write-back below refreshes the rows
|
||||
@@ -13096,7 +13146,7 @@ async function startPlot() {
|
||||
beginJobPreparation("Plot");
|
||||
let prepared = await prepareMSRRunFiles("Plot");
|
||||
if (!prepared.ok) {
|
||||
finishJobStatus(0, 1, prepared.message || "Plot file preparation failed.");
|
||||
finishJobStatus(0, 1, prepared.message || "Plot file preparation failed.", prepared.outcome);
|
||||
return(0);
|
||||
}
|
||||
prepared.options.plotOnly = 1;
|
||||
@@ -13148,7 +13198,7 @@ async function startFit() {
|
||||
}
|
||||
let prepared = await prepareMSRRunFiles("Fit");
|
||||
if (!prepared.ok) {
|
||||
finishJobStatus(0, 1, prepared.message || "Fit file preparation failed.");
|
||||
finishJobStatus(0, 1, prepared.message || "Fit file preparation failed.", prepared.outcome);
|
||||
return(0);
|
||||
}
|
||||
prepared.options.plotOnly = 0;
|
||||
|
||||
+2
-1
@@ -15,7 +15,7 @@
|
||||
"checksums:md5": "sh -c 'd=dist; [ -d \"$d\" ] || exit 0; for f in \"$d\"/*; do [ -f \"$f\" ] || continue; case \"$f\" in *.rpm|*.AppImage) base=$(basename \"$f\"); (cd \"$d\" && md5sum \"$base\" > \"$base.md5\");; esac; done'",
|
||||
"checksums:sha256": "sh -c 'd=dist; [ -d \"$d\" ] || exit 0; for f in \"$d\"/*; do [ -f \"$f\" ] || continue; case \"$f\" in *.rpm|*.AppImage) base=$(basename \"$f\"); (cd \"$d\" && sha256sum \"$base\" > \"$base.sha256\");; esac; done'",
|
||||
"checksums": "npm run checksums:md5 && npm run checksums:sha256",
|
||||
"test:electron": "node tests/log-highlighting.test.js && node tests/action-state.test.js && node tests/electron-missing-data.test.js && node tests/electron-msr-title.test.js && node tests/electron-musrfit-threads.test.js && node tests/window-layering.test.js && node tests/msr-generation.test.js && node tests/root-plot-clean.test.js && node tests/parameter-plot.test.js && node tests/help-window.test.js && node tests/release-packaging.test.js && node tests/addrun.test.js && node tests/fit-statistic.test.js && node tests/series-fit-planner.test.js && node tests/series-fit-result.test.js && node tests/series-fit-inventory.test.js && node tests/series-fit-msr-preparation.test.js && node tests/series-artifact-contract.test.js && node tests/series-global-merge.test.js && node tests/series-fit-staging.test.js && node tests/series-fit-runner.test.js && node tests/series-runner-adapters.test.js && node tests/series-fit-routing.test.js && node tests/series-action-chips.test.js && node tests/singlehist-parity.test.js && node tests/url-parameters.test.js && node tests/run-naming.test.js && node tests/electron-portable-naming.test.js && node tests/shared-configuration.test.js && node tests/session-persistence.test.js && node tests/reference-fixtures.test.js && node tests/electron-bridge.test.js && node tests/electron-bundled-runtime.test.js && node tests/wsl-runtime.test.js"
|
||||
"test:electron": "node tests/log-highlighting.test.js && node tests/action-state.test.js && node tests/electron-missing-data.test.js && node tests/electron-run-data.test.js && node tests/electron-msr-title.test.js && node tests/electron-musrfit-threads.test.js && node tests/window-layering.test.js && node tests/msr-generation.test.js && node tests/root-plot-clean.test.js && node tests/parameter-plot.test.js && node tests/help-window.test.js && node tests/release-packaging.test.js && node tests/addrun.test.js && node tests/fit-statistic.test.js && node tests/series-fit-planner.test.js && node tests/series-fit-result.test.js && node tests/series-fit-inventory.test.js && node tests/series-fit-msr-preparation.test.js && node tests/series-artifact-contract.test.js && node tests/series-global-merge.test.js && node tests/series-fit-staging.test.js && node tests/series-fit-runner.test.js && node tests/series-runner-adapters.test.js && node tests/series-fit-routing.test.js && node tests/series-action-chips.test.js && node tests/singlehist-parity.test.js && node tests/url-parameters.test.js && node tests/run-naming.test.js && node tests/electron-portable-naming.test.js && node tests/shared-configuration.test.js && node tests/session-persistence.test.js && node tests/reference-fixtures.test.js && node tests/electron-bridge.test.js && node tests/electron-bundled-runtime.test.js && node tests/wsl-runtime.test.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -53,6 +53,7 @@
|
||||
"muSRFitlib.js",
|
||||
"muSRFitelec.js",
|
||||
"wsl-runtime.js",
|
||||
"run-data-download.js",
|
||||
"muSRFitRoot.js",
|
||||
"muSRFitParameterPlot.js",
|
||||
"muSRFitParameterPlotElec.js",
|
||||
|
||||
+20
@@ -172,6 +172,26 @@ readFile: function (p, encoding, callback) {
|
||||
}
|
||||
},
|
||||
|
||||
// Transfers are owned by the main process. IPC carries metadata and progress
|
||||
// only; it never buffers raw run files in the renderer.
|
||||
runData: {
|
||||
configuration: function () {
|
||||
return ipcRenderer.invoke('runDataConfiguration');
|
||||
},
|
||||
acquire: function (request) {
|
||||
return ipcRenderer.invoke('acquireRunData', request);
|
||||
},
|
||||
cancel: function (token) {
|
||||
return ipcRenderer.invoke('cancelRunData', token);
|
||||
},
|
||||
onProgress: function (listener) {
|
||||
if (typeof listener !== 'function') return;
|
||||
const wrapped = function (event, state) { listener(state); };
|
||||
ipcRenderer.on('runDataProgress', wrapped);
|
||||
return function () { ipcRenderer.removeListener('runDataProgress', wrapped); };
|
||||
}
|
||||
},
|
||||
|
||||
// Windows bundled-runtime lifecycle. The main process imports the shipped
|
||||
// WSL2 rootfs on first run; the renderer can subscribe for progress, read
|
||||
// the current status, or force a retry after a failed import.
|
||||
|
||||
@@ -0,0 +1,221 @@
|
||||
// Copyright (C) 2026 Zaher Salman
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// Main-process run retrieval. Raw bytes stay out of the renderer, and the
|
||||
// index remembers exact server filenames across sessions, including old runs
|
||||
// whose campaign names differ from the desktop's startup templates.
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { randomUUID } = require('crypto');
|
||||
const { Readable, Transform } = require('stream');
|
||||
const { pipeline } = require('stream/promises');
|
||||
|
||||
const ENDPOINT = 'https://musruser.psi.ch/cgi-bin/single_musrfit.cgi';
|
||||
|
||||
class RunDataDownloader {
|
||||
constructor(options) {
|
||||
this.directory = path.resolve(options.directory);
|
||||
this.indexPath = path.join(this.directory, 'index.json');
|
||||
this.fetch = options.fetch;
|
||||
}
|
||||
|
||||
async localFiles() {
|
||||
let records;
|
||||
try {
|
||||
records = JSON.parse(await fs.promises.readFile(this.indexPath, 'utf8'));
|
||||
} catch (error) {
|
||||
if (error.code === 'ENOENT') return [];
|
||||
throw new Error('Could not read the downloaded-run index: ' + error.message);
|
||||
}
|
||||
if (!Array.isArray(records)) throw new Error('The downloaded-run index is invalid.');
|
||||
const files = [];
|
||||
for (const record of records) {
|
||||
if (!record || typeof record.filePath !== 'string') continue;
|
||||
try {
|
||||
const stat = await fs.promises.stat(record.filePath);
|
||||
await fs.promises.access(record.filePath, fs.constants.R_OK);
|
||||
if (stat.isFile() && stat.size > 0 && stat.size === record.size) files.push(record);
|
||||
} catch (error) {
|
||||
// A disconnected mount or a removed download can be retrieved
|
||||
// again; neither should prevent other saved runs from opening.
|
||||
}
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
async request(action, identity, signal) {
|
||||
const response = await this.fetch(ENDPOINT, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
||||
body: new URLSearchParams(Object.assign({ action: action }, identity)).toString(),
|
||||
redirect: 'error',
|
||||
signal: AbortSignal.any([signal, AbortSignal.timeout(
|
||||
action === 'run_file_info' ? 30000 : 300000)])
|
||||
});
|
||||
if (!response.ok) {
|
||||
let diagnostic = {};
|
||||
try {
|
||||
diagnostic = await response.json();
|
||||
} catch (error) {
|
||||
// Preserve the HTTP failure when a proxy returns an HTML page.
|
||||
}
|
||||
const error = new Error(diagnostic.message || 'Data server returned HTTP ' + response.status);
|
||||
error.status = response.status;
|
||||
error.reason = diagnostic.reason || '';
|
||||
throw error;
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
async destination(dataRoot, relativePath) {
|
||||
let lastError;
|
||||
// Test the actual destination, not just the top-level folder: an
|
||||
// instrument/year directory can be read-only within a writable root.
|
||||
for (const root of Array.from(new Set([path.resolve(dataRoot), this.directory]))) {
|
||||
const filePath = path.join(root, ...relativePath.split('/'));
|
||||
const parent = path.dirname(filePath);
|
||||
const probePath = path.join(parent, '.musrflow-write-' + randomUUID());
|
||||
try {
|
||||
await fs.promises.mkdir(parent, { recursive: true });
|
||||
const probe = await fs.promises.open(probePath, 'wx', 0o600);
|
||||
await probe.close();
|
||||
await fs.promises.unlink(probePath);
|
||||
return { filePath: filePath, storageRoot: root };
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
}
|
||||
}
|
||||
throw new Error('Neither the Data directory nor the download fallback is writable: '
|
||||
+ lastError.message);
|
||||
}
|
||||
|
||||
async prepare(requests, dataRoot, signal, onProgress) {
|
||||
if (!Array.isArray(requests) || requests.length > 2000 || typeof dataRoot !== 'string' || !dataRoot) {
|
||||
throw new Error('Invalid run download request.');
|
||||
}
|
||||
const aliases = { lem: 'LEM', gps: 'GPS', flame: 'FLAME', ltf: 'LTF', dolly: 'Dolly',
|
||||
vms: 'VMS', gpd: 'GPD', hal: 'HAL', hifi: 'HAL', alc: 'ALC', misc: 'MISC' };
|
||||
const identities = [];
|
||||
const seen = new Set();
|
||||
for (const input of requests) {
|
||||
const instrument = input && aliases[String(input.instrument).toLowerCase()];
|
||||
const year = input && Number(input.year);
|
||||
const run = input && Number(input.run);
|
||||
if (!instrument || !Number.isInteger(year) || year < 1900 || year > 9999
|
||||
|| !Number.isInteger(run) || run <= 0 || run > 999999999) {
|
||||
throw new Error('Invalid instrument, year, or run number.');
|
||||
}
|
||||
const key = instrument + '/' + year + '/' + run;
|
||||
if (!seen.has(key)) identities.push({ instrument: instrument, year: year, run: run });
|
||||
seen.add(key);
|
||||
}
|
||||
const index = await this.localFiles();
|
||||
const plan = { files: [], downloads: [], missing: [], index: index, totalBytes: 0 };
|
||||
for (const identity of identities) {
|
||||
signal.throwIfAborted();
|
||||
const cached = index.find(function(record) {
|
||||
return record.instrument === identity.instrument && record.year === identity.year
|
||||
&& record.run === identity.run;
|
||||
});
|
||||
if (cached) {
|
||||
plan.files.push(cached);
|
||||
continue;
|
||||
}
|
||||
onProgress({ phase: 'checking', run: identity.run, total: identities.length });
|
||||
let metadata;
|
||||
try {
|
||||
const response = await this.request('run_file_info', identity, signal);
|
||||
metadata = await response.json();
|
||||
} catch (error) {
|
||||
if (error.status !== 404 && error.status !== 409) throw error;
|
||||
plan.missing.push(Object.assign({ message: error.message }, identity));
|
||||
continue;
|
||||
}
|
||||
const relative = metadata.relative_path;
|
||||
const folder = identity.instrument.toLowerCase();
|
||||
if (metadata.ok !== true || metadata.instrument !== identity.instrument
|
||||
|| metadata.year !== identity.year || metadata.run !== identity.run
|
||||
|| !Number.isSafeInteger(metadata.size) || metadata.size <= 0
|
||||
|| !/^[0-9a-f]{64}$/.test(metadata.version)
|
||||
|| typeof relative !== 'string' || !relative.startsWith(folder + '/his/')
|
||||
|| !/^[A-Za-z0-9_+.-]+(?:\/[A-Za-z0-9_+.-]+)*$/.test(relative)
|
||||
|| relative.split('/').some(function(part) { return part === '.' || part === '..'; })
|
||||
|| relative.split('/').pop() !== metadata.filename
|
||||
|| !['MUSR-ROOT', 'PSI-BIN', 'PSI-MDU'].includes(metadata.format)) {
|
||||
throw new Error('The data server returned invalid run metadata.');
|
||||
}
|
||||
const target = await this.destination(dataRoot, relative);
|
||||
plan.downloads.push(Object.assign({}, metadata, target));
|
||||
plan.totalBytes += metadata.size;
|
||||
}
|
||||
return plan;
|
||||
}
|
||||
|
||||
async download(plan, signal, onProgress) {
|
||||
let completedBytes = 0;
|
||||
for (const file of plan.downloads) {
|
||||
signal.throwIfAborted();
|
||||
const partialPath = file.filePath + '.' + randomUUID() + '.part';
|
||||
let received = 0;
|
||||
let lastProgress = 0;
|
||||
try {
|
||||
const response = await this.request('download_run_file', {
|
||||
instrument: file.instrument, year: file.year, run: file.run, version: file.version
|
||||
}, signal);
|
||||
const length = response.headers.get('content-length');
|
||||
if (response.headers.get('content-type') !== 'application/octet-stream'
|
||||
|| (length !== null && Number(length) !== file.size)) {
|
||||
await response.body.cancel();
|
||||
throw new Error('The data server returned an unexpected file response.');
|
||||
}
|
||||
const progress = new Transform({
|
||||
transform: function(chunk, encoding, callback) {
|
||||
received += chunk.length;
|
||||
if (received > file.size) {
|
||||
callback(new Error('Downloaded file is larger than the advertised size.'));
|
||||
return;
|
||||
}
|
||||
if (Date.now() - lastProgress > 100 || received === file.size) {
|
||||
lastProgress = Date.now();
|
||||
onProgress({ phase: 'downloading', filename: file.filename,
|
||||
received: completedBytes + received, totalBytes: plan.totalBytes });
|
||||
}
|
||||
callback(null, chunk);
|
||||
}
|
||||
});
|
||||
await pipeline(Readable.fromWeb(response.body), progress,
|
||||
fs.createWriteStream(partialPath, { flags: 'wx' }), { signal: signal });
|
||||
if (received !== file.size) throw new Error('Downloaded file is incomplete.');
|
||||
signal.throwIfAborted();
|
||||
// Publish only a complete file and never overwrite a run that
|
||||
// appeared locally while the transfer was in progress.
|
||||
await fs.promises.link(partialPath, file.filePath);
|
||||
const previous = plan.index.findIndex(function(record) {
|
||||
return record.instrument === file.instrument && record.year === file.year
|
||||
&& record.run === file.run;
|
||||
});
|
||||
if (previous !== -1) plan.index.splice(previous, 1);
|
||||
plan.index.push(file);
|
||||
await fs.promises.mkdir(this.directory, { recursive: true });
|
||||
const stage = this.indexPath + '.' + randomUUID() + '.tmp';
|
||||
try {
|
||||
await fs.promises.writeFile(stage, JSON.stringify(plan.index), { flag: 'wx', mode: 0o600 });
|
||||
await fs.promises.rename(stage, this.indexPath);
|
||||
} finally {
|
||||
await fs.promises.unlink(stage).catch(function(error) {
|
||||
if (error.code !== 'ENOENT') throw error;
|
||||
});
|
||||
}
|
||||
plan.files.push(file);
|
||||
completedBytes += received;
|
||||
} finally {
|
||||
await fs.promises.unlink(partialPath).catch(function(error) {
|
||||
if (error.code !== 'ENOENT') throw error;
|
||||
});
|
||||
}
|
||||
}
|
||||
return { ok: true, files: plan.files, missing: plan.missing };
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { RunDataDownloader, ENDPOINT };
|
||||
@@ -0,0 +1,246 @@
|
||||
// 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');
|
||||
assert.ok(launch.args[4].includes('/mnt/c/Users/Test User/data'));
|
||||
assert.ok(!launch.args[4].includes('C:'));
|
||||
assert.ok(desktop.prepareWslCommandString('C:\\resources\\runtime\\bin\\msr2data [514] _asy')
|
||||
.includes('env "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 });
|
||||
});
|
||||
Reference in new issue
Block a user