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.
345 lines
13 KiB
JavaScript
345 lines
13 KiB
JavaScript
// Copyright (C) 2026 Zaher Salman
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
const assert = require("assert");
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const vm = require("vm");
|
|
|
|
const repoRoot = path.resolve(__dirname, "..");
|
|
|
|
function loadWebURLHelpers() {
|
|
const commonCode = fs.readFileSync(path.join(repoRoot, "muSRFitlib.js"), "utf8");
|
|
const code = fs.readFileSync(path.join(repoRoot, "muSRFitweb.js"), "utf8");
|
|
const sandbox = {
|
|
console: { log: function() {} },
|
|
fetch: async function() {
|
|
throw new Error("network access is not used by URL-parameter tests");
|
|
},
|
|
URL: URL,
|
|
URLSearchParams: URLSearchParams,
|
|
CompressionStream: globalThis.CompressionStream,
|
|
DecompressionStream: globalThis.DecompressionStream,
|
|
TextEncoder: globalThis.TextEncoder,
|
|
TextDecoder: globalThis.TextDecoder,
|
|
Response: globalThis.Response,
|
|
Blob: globalThis.Blob,
|
|
Uint8Array: Uint8Array,
|
|
btoa: globalThis.btoa,
|
|
atob: globalThis.atob,
|
|
window: {
|
|
MSREditor: {},
|
|
location: { search: "", pathname: "/musrflow/", hash: "" },
|
|
history: {
|
|
replaceState: function(state, title, url) {
|
|
this.lastURL = url;
|
|
sandbox.window.location.search = "";
|
|
},
|
|
lastURL: ""
|
|
}
|
|
},
|
|
document: {
|
|
body: { appendChild: function() {} },
|
|
getElementById: function() { return null; }
|
|
}
|
|
};
|
|
sandbox.global = sandbox;
|
|
vm.runInNewContext(commonCode, sandbox, { filename: "muSRFitlib.js" });
|
|
vm.runInNewContext(code, sandbox, { filename: "muSRFitweb.js" });
|
|
return sandbox;
|
|
}
|
|
|
|
function makeSelect(values, selectedValue) {
|
|
return {
|
|
value: selectedValue,
|
|
options: values.map(function(value) { return { value: value }; })
|
|
};
|
|
}
|
|
|
|
function testLegacyParametersPopulateRunsControls() {
|
|
const lib = loadWebURLHelpers();
|
|
const instrumentInput = makeSelect(
|
|
["LTF", "FLAME", "LEM", "GPS", "GPD", "Dolly", "VMS", "HAL"],
|
|
"GPS"
|
|
);
|
|
const yearInput = { value: "2025", min: "1998", max: "2026" };
|
|
const runInput = { value: "" };
|
|
const controls = {
|
|
instrName: instrumentInput,
|
|
selYear: yearInput,
|
|
runNumbers: runInput
|
|
};
|
|
lib.document.getElementById = function(id) {
|
|
return controls[id] || null;
|
|
};
|
|
lib.instPars = {
|
|
LEM: { minYear: "2001" },
|
|
GPS: { minYear: "1993" }
|
|
};
|
|
lib.arrayRunNumbers = function(value) {
|
|
return value === "2229,2231-2233" ? [2229, 2231, 2232, 2233] : [];
|
|
};
|
|
|
|
let histogramRefreshes = 0;
|
|
let pathRefreshes = 0;
|
|
let cfgRefreshes = 0;
|
|
lib.defaultHistograms = function(force) {
|
|
assert.strictEqual(force, 1);
|
|
histogramRefreshes++;
|
|
};
|
|
lib.updateResolvedDataPathPreview = function() { pathRefreshes++; };
|
|
lib.prepCFG = function() { cfgRefreshes++; };
|
|
|
|
const result = lib.applyWebURLParameters(
|
|
"?YEAR=2026&BeamLine=lem&RunNumbers=2229%2C2231-2233"
|
|
);
|
|
|
|
assert.strictEqual(instrumentInput.value, "LEM");
|
|
assert.strictEqual(yearInput.value, "2026");
|
|
assert.strictEqual(runInput.value, "2229,2231-2233");
|
|
assert.strictEqual(result.applied.BeamLine, "LEM");
|
|
assert.strictEqual(result.applied.YEAR, "2026");
|
|
assert.strictEqual(result.applied.RunNumbers, "2229,2231-2233");
|
|
assert.deepStrictEqual(Array.from(result.errors), []);
|
|
assert.strictEqual(histogramRefreshes, 1);
|
|
assert.strictEqual(pathRefreshes, 1);
|
|
assert.strictEqual(cfgRefreshes, 1);
|
|
}
|
|
|
|
function testInvalidAndUnlistedParametersAreIgnoredSafely() {
|
|
const lib = loadWebURLHelpers();
|
|
const instrumentInput = makeSelect(["LEM", "GPS"], "LEM");
|
|
const yearInput = { value: "2026", min: "1998", max: "2026" };
|
|
const runInput = { value: "" };
|
|
const dataRootInput = { value: "/safe/root" };
|
|
const controls = {
|
|
instrName: instrumentInput,
|
|
selYear: yearInput,
|
|
runNumbers: runInput,
|
|
dataRoot: dataRootInput
|
|
};
|
|
lib.document.getElementById = function(id) {
|
|
return controls[id] || null;
|
|
};
|
|
lib.instPars = { LEM: { minYear: "2001" } };
|
|
lib.arrayRunNumbers = function() { return []; };
|
|
|
|
let warning = "";
|
|
let status = "";
|
|
lib.setRunInputWarning = function(message, label) {
|
|
warning = label + ": " + message;
|
|
};
|
|
let statusOptions = null;
|
|
lib.setFitStatus = function(message, options) {
|
|
status = message;
|
|
statusOptions = options;
|
|
};
|
|
|
|
const result = lib.applyWebURLParameters(
|
|
"?BeamLine=UNKNOWN&YEAR=1999&RunNumbers=bad&dataRoot=%2Fevil"
|
|
);
|
|
|
|
assert.strictEqual(instrumentInput.value, "LEM");
|
|
assert.strictEqual(yearInput.value, "2026");
|
|
assert.strictEqual(runInput.value, "");
|
|
// dataRoot is intentionally not on the URL whitelist.
|
|
assert.strictEqual(dataRootInput.value, "/safe/root");
|
|
assert.strictEqual(Object.keys(result.applied).length, 0);
|
|
assert.strictEqual(result.errors.length, 3);
|
|
assert.match(warning, /Unsupported BeamLine/);
|
|
assert.match(warning, /Invalid YEAR/);
|
|
assert.match(warning, /Invalid RunNumbers/);
|
|
assert.strictEqual(status, "Some URL parameters were ignored.");
|
|
assert.strictEqual(statusOptions.level, "warning");
|
|
assert.match(statusOptions.details, /URL parameters:/);
|
|
}
|
|
|
|
|
|
function testPlotActionIsWhitelistedForCompleteRunLinks() {
|
|
const lib = loadWebURLHelpers();
|
|
const instrumentInput = makeSelect(["LEM", "GPS"], "GPS");
|
|
const yearInput = { value: "2026", min: "1993", max: "2026" };
|
|
const runInput = { value: "" };
|
|
const controls = {
|
|
instrName: instrumentInput,
|
|
selYear: yearInput,
|
|
runNumbers: runInput
|
|
};
|
|
lib.document.getElementById = function(id) { return controls[id] || null; };
|
|
lib.instPars = { LEM: { minYear: "2001" }, GPS: { minYear: "1993" } };
|
|
lib.arrayRunNumbers = function(value) { return value === "514" ? [514] : []; };
|
|
lib.defaultHistograms = function() {};
|
|
lib.updateResolvedDataPathPreview = function() {};
|
|
lib.prepCFG = function() {};
|
|
|
|
const result = lib.applyWebURLParameters(
|
|
"?BeamLine=LEM&YEAR=2026&RunNumbers=514&action=plot"
|
|
);
|
|
assert.strictEqual(result.action, "plot");
|
|
assert.deepStrictEqual(Array.from(result.errors), []);
|
|
|
|
const incomplete = lib.applyWebURLParameters("?RunNumbers=514&action=plot");
|
|
assert.strictEqual(incomplete.action, "");
|
|
assert.match(incomplete.errors.join(" "), /requires BeamLine and RunNumbers/);
|
|
|
|
const unsupported = lib.applyWebURLParameters(
|
|
"?BeamLine=LEM&RunNumbers=514&action=fit"
|
|
);
|
|
assert.strictEqual(unsupported.action, "");
|
|
assert.match(unsupported.errors.join(" "), /Unsupported action: fit/);
|
|
}
|
|
|
|
async function testPlotActionWaitsForInitialGuess() {
|
|
const lib = loadWebURLHelpers();
|
|
const order = [];
|
|
let finishGuess = null;
|
|
const guess = new Promise(function(resolve) { finishGuess = resolve; });
|
|
|
|
lib.cfgParams = function() {
|
|
order.push("parameters");
|
|
return guess.then(function() { order.push("guess"); });
|
|
};
|
|
lib.startPlot = async function() {
|
|
order.push("plot");
|
|
return 1;
|
|
};
|
|
|
|
const running = lib.triggerWebURLAction({ action: "plot" });
|
|
assert.deepStrictEqual(order, ["parameters"]);
|
|
finishGuess();
|
|
const result = await running;
|
|
assert.strictEqual(result, 1);
|
|
assert.deepStrictEqual(order, ["parameters", "guess", "plot"]);
|
|
}
|
|
|
|
async function testInitializerConsumesAndRemovesBrowserQueryString() {
|
|
const lib = loadWebURLHelpers();
|
|
const instrumentInput = makeSelect(["LEM"], "LEM");
|
|
const yearInput = { value: "2026", min: "1998", max: "2026" };
|
|
const runInput = { value: "" };
|
|
const controls = {
|
|
instrName: instrumentInput,
|
|
selYear: yearInput,
|
|
runNumbers: runInput
|
|
};
|
|
lib.document.getElementById = function(id) { return controls[id] || null; };
|
|
lib.window.location.search = "?YEAR=2026&BeamLine=LEM&RunNumbers=2229";
|
|
lib.instPars = { LEM: { minYear: "2001" } };
|
|
lib.arrayRunNumbers = function(value) { return value === "2229" ? [2229] : []; };
|
|
lib.defaultHistograms = function() {};
|
|
lib.updateResolvedDataPathPreview = function() {};
|
|
lib.prepCFG = function() {};
|
|
|
|
const result = await lib.initializeWebURLParameters();
|
|
|
|
assert.strictEqual(runInput.value, "2229");
|
|
assert.strictEqual(lib.window.location.search, "");
|
|
assert.strictEqual(lib.window.history.lastURL, "/musrflow/");
|
|
assert.strictEqual(result.applied.RunNumbers, "2229");
|
|
}
|
|
|
|
|
|
async function testSharedConfigurationEncodingRoundTrip() {
|
|
const lib = loadWebURLHelpers();
|
|
const config = {
|
|
v: 1,
|
|
controls: { instrName: "LEM", selYear: "2026", runNumbers: "514-516" },
|
|
components: ["ExponentialCos"],
|
|
parameters: {
|
|
valFrq_514: "1.25", errFrq_514: "0.01", minFrq_514: "0", maxFrq_514: "10",
|
|
valFrq_515: "1.25", errFrq_515: "0.01", minFrq_515: "0", maxFrq_515: "10",
|
|
valFrq_516: "1.25", errFrq_516: "0.01", minFrq_516: "0", maxFrq_516: "10",
|
|
shAlpha: true
|
|
},
|
|
customParameters: ["MyRate"],
|
|
constraints: ["MyRate = Lam*2"]
|
|
};
|
|
const encoded = await lib.encodeWebSharedConfiguration(config);
|
|
assert.strictEqual(encoded.prefix, "#c=");
|
|
const decoded = await lib.decodeWebSharedConfiguration(encoded.prefix + encoded.payload);
|
|
|
|
assert.strictEqual(decoded.error, "");
|
|
assert.strictEqual(decoded.consumed, true);
|
|
assert.strictEqual(JSON.stringify(decoded.config), JSON.stringify(config));
|
|
|
|
const legacy = "#cfg=" + encodeURIComponent(JSON.stringify(config));
|
|
assert.ok((encoded.prefix + encoded.payload).length < legacy.length);
|
|
const legacyDecoded = await lib.decodeWebSharedConfiguration(legacy);
|
|
assert.strictEqual(JSON.stringify(legacyDecoded.config), JSON.stringify(config));
|
|
|
|
const invalid = await lib.decodeWebSharedConfiguration("#cfg=" + encodeURIComponent("{bad"));
|
|
assert.strictEqual(invalid.consumed, true);
|
|
assert.match(invalid.error, /Could not read shared configuration/);
|
|
}
|
|
|
|
async function testSharedConfigurationHashIsConsumed() {
|
|
const lib = loadWebURLHelpers();
|
|
const config = { v: 1, controls: { instrName: "LEM" } };
|
|
const encoded = await lib.encodeWebSharedConfiguration(config);
|
|
lib.window.location.hash = encoded.prefix + encoded.payload;
|
|
let restored = null;
|
|
lib.restoreSharedConfiguration = async function(value) {
|
|
restored = value;
|
|
return true;
|
|
};
|
|
|
|
const result = await lib.initializeWebURLParameters();
|
|
|
|
assert.strictEqual(JSON.stringify(restored), JSON.stringify(config));
|
|
assert.strictEqual(lib.window.history.lastURL, "/musrflow/");
|
|
assert.strictEqual(JSON.stringify(result.sharedConfiguration), JSON.stringify(config));
|
|
}
|
|
|
|
async function testWebDesktopLinkCopiesProtocolURL() {
|
|
const lib = loadWebURLHelpers();
|
|
let clipboardText = "";
|
|
lib.navigator = {
|
|
clipboard: {
|
|
writeText: function(value) { clipboardText = String(value); return Promise.resolve(true); }
|
|
}
|
|
};
|
|
lib.document.getElementById = function(id) {
|
|
if (id === "Runs" || id === "Params") {
|
|
return { querySelectorAll: function() { return []; } };
|
|
}
|
|
return null;
|
|
};
|
|
lib.document.querySelectorAll = function() { return []; };
|
|
lib.prepCFG = function() {};
|
|
lib.getFitComponentValues = function() { return ["ExponentialCos"]; };
|
|
lib.showTransientWarning = function() {};
|
|
lib.setFitStatus = function() {};
|
|
|
|
// The desktop-link share flow emits the same fragment under the registered
|
|
// musrflow:// protocol instead of the page's https URL.
|
|
const copied = await lib.copyWebDesktopConfigurationLink();
|
|
assert.strictEqual(copied, true);
|
|
assert.match(clipboardText, /^musrflow:\/\/open\/#c=/);
|
|
|
|
// The protocol link decodes identically on the web side (as the Electron
|
|
// renderer does when the installed app receives the same link).
|
|
const decoded = await lib.decodeWebSharedConfiguration(clipboardText);
|
|
assert.strictEqual(decoded.error, "");
|
|
assert.strictEqual(decoded.consumed, true);
|
|
assert.strictEqual(decoded.config.v, 1);
|
|
assert.strictEqual(
|
|
JSON.stringify(decoded.config.components), JSON.stringify(["ExponentialCos"]));
|
|
}
|
|
|
|
async function main() {
|
|
testLegacyParametersPopulateRunsControls();
|
|
testInvalidAndUnlistedParametersAreIgnoredSafely();
|
|
testPlotActionIsWhitelistedForCompleteRunLinks();
|
|
await testPlotActionWaitsForInitialGuess();
|
|
await testInitializerConsumesAndRemovesBrowserQueryString();
|
|
await testSharedConfigurationEncodingRoundTrip();
|
|
await testSharedConfigurationHashIsConsumed();
|
|
await testWebDesktopLinkCopiesProtocolURL();
|
|
console.log("URL parameter tests passed");
|
|
}
|
|
|
|
main().catch(function(error) {
|
|
console.error(error);
|
|
process.exit(1);
|
|
});
|