Files
muSRFlow/tests/electron-musrfit-threads.test.js
salman 184c5b9eb1 Add musrflow:// configuration links and musrfit thread preference
Register the musrflow:// URL scheme at startup (electron-builder writes the
matching MimeType=x-scheme-handler desktop entry for the packaged Linux
builds), accept musrflow://open/#c= links on the launch path and macOS
open-url alongside the existing http(s)/bare-fragment forms, and add a web
Share button that emits the identical encoded fragment under musrflow://open/
so a browser can hand a configuration to the installed desktop app.

Add a musrfit threads field to the Electron Preferences dialog. A set count
is passed to local fits as `musrfit -u <n>`; blank keeps the previous
all-cores behaviour and the web edition ignores it.

Document both features (preferences, runs/fitting, configuration sharing,
desktop deployment, CHANGELOG) and rebuild the Sphinx HTML and PDF.
2026-10-10 10:40:41 +02:00

171 lines
6.6 KiB
JavaScript

// Copyright (C) 2026 Zaher Salman
// SPDX-License-Identifier: GPL-3.0-or-later
// Regression coverage for the local musrfit OpenMP thread preference. The
// Electron Preferences dialog stores a thread count that runMSRFileLocal()
// forwards to musrfit as `-u <n>`. A blank/auto setting must omit `-u` so the
// historical "use all cores" behavior is preserved, while an explicit count
// must be passed through unchanged.
const assert = require("assert");
const events = require("events");
const fs = require("fs");
const path = require("path");
const vm = require("vm");
const repoRoot = path.resolve(__dirname, "..");
const missingOutput = [
"PRunDataHandler::FileExistsCheck(): **ERROR** Couldn't find "
+ "'/mnt/fake/lem/his/2026/lem26_his_0514' in any standard path.",
"musrfit **ERROR** Couldn't read all data files, will quit ..."
].join("\n");
function localStoreFactory() {
const store = {};
return {
getItem: function(key) {
return Object.prototype.hasOwnProperty.call(store, key) ? store[key] : null;
},
setItem: function(key, value) { store[key] = String(value); },
removeItem: function(key) { delete store[key]; }
};
}
function buildSandbox(msrContent, elements) {
const spawnedCommands = [];
const fakeFs = {
appendFileSync: function() {},
existsSync: function() { return false; },
readFileSync: function() { return msrContent; },
writeFileSync: function() {}
};
const fakeChildProcess = {
spawn: function(command, args) {
spawnedCommands.push({ command: command, args: args });
const child = new events.EventEmitter();
child.stdout = new events.EventEmitter();
child.stderr = new events.EventEmitter();
setImmediate(function() {
child.stderr.emit("data", Buffer.from(missingOutput));
child.emit("close", 1, null);
});
return child;
}
};
const dom = elements || {};
const localStorage = localStoreFactory();
const sandbox = {
alert: function(message) {
throw new Error("Unexpected alert: " + message);
},
console: { log: function() {}, error: console.error, warn: console.warn },
document: {
getElementById: function(id) {
return Object.prototype.hasOwnProperty.call(dom, id) ? dom[id] : null;
}
},
process: { cwd: function() { return repoRoot; }, env: {}, resourcesPath: "" },
require: require,
window: {
MSREditor: {},
MusrfitRunner: { fitActive: false },
MusrFlowPlatform: { webMode: false },
localStorage: localStorage,
electronIpcRenderer: { on: function() {}, send: function() {} },
nodeChildProcess: fakeChildProcess,
nodeFs: fakeFs,
nodePath: path
}
};
sandbox.global = sandbox;
return { sandbox: sandbox, spawnedCommands: spawnedCommands, localStorage: localStorage };
}
function loadLibraries(sandbox) {
vm.runInNewContext(
fs.readFileSync(path.join(repoRoot, "muSRFitlib.js"), "utf8"),
sandbox,
{ filename: "muSRFitlib.js" }
);
vm.runInNewContext(
fs.readFileSync(path.join(repoRoot, "muSRFitelec.js"), "utf8"),
sandbox,
{ filename: "muSRFitelec.js" }
);
}
function runFit(sandbox, msrPath) {
sandbox.appendJobLog = function() {};
sandbox.updateJobStatus = function() {};
sandbox.setFitStatus = function() {};
sandbox.finishJobStatus = function() {};
return sandbox.runMSRFileLocal(msrPath, {});
}
function musrfitArgs(sandbox, spawnedCommands, msrPath) {
return runFit(sandbox, msrPath).then(function() {
const musrfit = spawnedCommands.find(function(entry) {
return entry.command === "musrfit";
});
if (!musrfit) throw new Error("musrfit was not spawned");
return musrfit.args;
});
}
const guiMsr = "# Run Numbers: 514 2453 2454\n# Chisq ...\nFITPARAM ...";
Promise.resolve().then(function() {
// No preference set: the flag must be absent (all cores).
let env = buildSandbox(guiMsr);
loadLibraries(env.sandbox);
assert.strictEqual(env.sandbox.getConfiguredMusrfitThreads(), 0);
return musrfitArgs(env.sandbox, env.spawnedCommands, "/tmp/no-threads.msr").then(function(args) {
assert.ok(!args.includes("-u"), "auto setting must omit -u");
return "auto-omits-flag";
});
}).then(function() {
// Explicit thread count stored under the preference key is passed as -u N.
let env = buildSandbox(guiMsr);
loadLibraries(env.sandbox);
env.localStorage.setItem("musrflow.musrfitThreads", "4");
assert.strictEqual(env.sandbox.getConfiguredMusrfitThreads(), 4);
return musrfitArgs(env.sandbox, env.spawnedCommands, "/tmp/four-threads.msr").then(function(args) {
const idx = args.indexOf("-u");
assert.ok(idx >= 0, "configured thread count must add -u");
assert.strictEqual(args[idx + 1], "4", "-u must be followed by the configured count");
return "explicit-sets-flag";
});
}).then(function() {
// setConfiguredMusrfitThreads normalizes and persists; clearing removes it.
let env = buildSandbox(guiMsr, { musrfitThreads: { value: "" } });
loadLibraries(env.sandbox);
env.sandbox.setConfiguredMusrfitThreads(" 8 ", true);
assert.strictEqual(env.localStorage.getItem("musrflow.musrfitThreads"), "8");
assert.strictEqual(env.sandbox.getConfiguredMusrfitThreads(), 8);
env.sandbox.setConfiguredMusrfitThreads("not-a-number", true);
assert.strictEqual(env.localStorage.getItem("musrflow.musrfitThreads"), null);
assert.strictEqual(env.sandbox.getConfiguredMusrfitThreads(), 0);
env.sandbox.setConfiguredMusrfitThreads(-3, true);
assert.strictEqual(env.sandbox.getConfiguredMusrfitThreads(), 0);
assert.strictEqual(env.sandbox.normalizeMusrfitThreads("0"), 0);
assert.strictEqual(env.sandbox.normalizeMusrfitThreads(""), 0);
return "normalize-and-persist";
}).then(function() {
// initializeMusrfitThreads reflects the stored value into the field.
let env = buildSandbox(guiMsr, { musrfitThreads: { value: "" } });
loadLibraries(env.sandbox);
env.localStorage.setItem("musrflow.musrfitThreads", "6");
env.sandbox.initializeMusrfitThreads();
assert.strictEqual(env.sandbox.document.getElementById("musrfitThreads").value, "6");
return "field-initialized";
}).then(function() {
console.log("Electron musrfit thread preference tests passed");
}).catch(function(error) {
console.error(error);
process.exitCode = 1;
});