221 lines
7.9 KiB
JavaScript
221 lines
7.9 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 loadFitLibrary() {
|
|
const code = fs.readFileSync(path.join(repoRoot, "muSRFitlib.js"), "utf8");
|
|
const sandbox = {
|
|
console: console,
|
|
window: { MSREditor: {} },
|
|
alert: function(message) {
|
|
throw new Error("Unexpected alert: " + message);
|
|
}
|
|
};
|
|
sandbox.global = sandbox;
|
|
vm.runInNewContext(code, sandbox, { filename: "muSRFitlib.js" });
|
|
return sandbox;
|
|
}
|
|
|
|
function plain(value) {
|
|
return JSON.parse(JSON.stringify(value));
|
|
}
|
|
|
|
function singleHistConfig(runNumbers) {
|
|
return {
|
|
all: {
|
|
fitAsyType: "SingleHist",
|
|
LRBF: "21 25,23 27",
|
|
runNumbers: runNumbers,
|
|
selYear: "2024",
|
|
instrName: "LEM",
|
|
dataRoot: "/psi.ch/group/lmu/public/data",
|
|
theoFunBlock: "asymmetry Asy\nsimplExpo Lam\nTFieldCos Phi Frq",
|
|
Tis: "0",
|
|
Tfs: "8",
|
|
BINS: "100",
|
|
Tpis: "",
|
|
Tpfs: "",
|
|
VIEWBINS: "",
|
|
plotLifetimeCorrection: "1"
|
|
},
|
|
funOptions: [
|
|
{ value: "No1", disabled: false },
|
|
{ value: "NBg1", disabled: false },
|
|
{ value: "Asy", disabled: false },
|
|
{ value: "Lam", disabled: false },
|
|
{ value: "Phi", disabled: false },
|
|
{ value: "Frq", disabled: false },
|
|
{ value: "No2", disabled: false },
|
|
{ value: "NBg2", disabled: false },
|
|
{ value: "PhiH2", disabled: false }
|
|
],
|
|
funLines: [],
|
|
sharedFlags: {}
|
|
};
|
|
}
|
|
|
|
function testRunSelectionGrammar(lib) {
|
|
assert.deepStrictEqual(plain(lib.parseRunSelection("514+515")), [
|
|
{ main: 514, add: [515] }
|
|
]);
|
|
assert.deepStrictEqual(plain(lib.parseRunSelection("514+515+516,520-522,530:534:2")), [
|
|
{ main: 514, add: [515, 516] },
|
|
{ main: 520, add: [] },
|
|
{ main: 521, add: [] },
|
|
{ main: 522, add: [] },
|
|
{ main: 530, add: [] },
|
|
{ main: 532, add: [] },
|
|
{ main: 534, add: [] }
|
|
]);
|
|
assert.deepStrictEqual(Array.from(lib.arrayRunNumbers("514+515,520+521+522")), [514, 520]);
|
|
|
|
// Ranges inside a summed term are intentionally not assigned an ambiguous
|
|
// meaning in the first implementation.
|
|
assert.deepStrictEqual(plain(lib.parseRunSelection("514-516+520")), []);
|
|
assert.deepStrictEqual(plain(lib.parseRunSelection("514+")), []);
|
|
|
|
// URLSearchParams decodes a raw '+' as space. Requiring %2B ensures a shared
|
|
// URL cannot silently turn two runs into the unrelated run number 514515.
|
|
const encoded = new URLSearchParams("?RunNumbers=514%2B515").get("RunNumbers");
|
|
const unescaped = new URLSearchParams("?RunNumbers=514+515").get("RunNumbers");
|
|
assert.deepStrictEqual(plain(lib.parseRunSelection(encoded)), [{ main: 514, add: [515] }]);
|
|
assert.deepStrictEqual(plain(lib.parseRunSelection(unescaped)), []);
|
|
}
|
|
|
|
function testSingleDataSetWritesAddRunDirectly(lib) {
|
|
const msr = lib.buildMSR(singleHistConfig("514+515+516"));
|
|
const addRunLines = msr.match(/^ADDRUN\s+.*$/gm) || [];
|
|
|
|
// There are two histogram RUN blocks, and every histogram must add the same
|
|
// two physical files before fitting.
|
|
assert.strictEqual(addRunLines.length, 4);
|
|
assert.strictEqual(addRunLines.filter(function(line) { return /0515\b/.test(line); }).length, 2);
|
|
assert.strictEqual(addRunLines.filter(function(line) { return /0516\b/.test(line); }).length, 2);
|
|
assert.match(msr, /^RUN\s+.*0514\b.*$/m);
|
|
}
|
|
|
|
function testMultiDataSetTemplateDefersAddRunUntilAfterMsr2data(lib) {
|
|
const selection = lib.parseRunSelection("514+515,520+521+522");
|
|
const template = lib.buildMSR(singleHistConfig("514+515,520+521+522"));
|
|
|
|
// msr2data copies ADDRUN statements unchanged, so the template must contain
|
|
// none. Only the anchor/main runs 514 and 520 are logical global-fit runs.
|
|
assert.doesNotMatch(template, /^ADDRUN\b/m);
|
|
assert.deepStrictEqual(Array.from(lib.arrayRunNumbers("514+515,520+521+522")), [514, 520]);
|
|
|
|
const main514 = lib.runFileNameAuto(514, "2024", "LEM", "/psi.ch/group/lmu/public/data");
|
|
const main520 = lib.runFileNameAuto(520, "2024", "LEM", "/psi.ch/group/lmu/public/data");
|
|
const expanded = [
|
|
"FITPARAMETER",
|
|
"1 Dummy 1 0.1",
|
|
"THEORY",
|
|
"simplExpo 1",
|
|
"GLOBAL",
|
|
"fittype 0",
|
|
main514,
|
|
"forward 21 25",
|
|
main514,
|
|
"forward 23 27",
|
|
main520,
|
|
"forward 21 25",
|
|
main520,
|
|
"forward 23 27",
|
|
"COMMANDS",
|
|
"MINIMIZE",
|
|
"PLOT 0",
|
|
"runs 1 2 3 4",
|
|
""
|
|
].join("\n");
|
|
|
|
const spec = lib.buildAddRunSpec(
|
|
selection, "2024", "LEM", "/psi.ch/group/lmu/public/data"
|
|
);
|
|
assert.deepStrictEqual(plain(lib.missingAddRunTargets(expanded, spec)), []);
|
|
|
|
const decorated = lib.applyAddRunsToMSR(
|
|
expanded, selection, "2024", "LEM", "/psi.ch/group/lmu/public/data"
|
|
);
|
|
const addRunLines = decorated.match(/^ADDRUN\s+.*$/gm) || [];
|
|
assert.strictEqual(addRunLines.length, 6);
|
|
assert.strictEqual(addRunLines.filter(function(line) { return /0515\b/.test(line); }).length, 2);
|
|
assert.strictEqual(addRunLines.filter(function(line) { return /0521\b/.test(line); }).length, 2);
|
|
assert.strictEqual(addRunLines.filter(function(line) { return /0522\b/.test(line); }).length, 2);
|
|
|
|
// Reapplying the transformation must replace rather than duplicate ADDRUNs.
|
|
const decoratedAgain = lib.applyAddRunsToMSR(
|
|
decorated, selection, "2024", "LEM", "/psi.ch/group/lmu/public/data"
|
|
);
|
|
assert.strictEqual((decoratedAgain.match(/^ADDRUN\s+.*$/gm) || []).length, 6);
|
|
}
|
|
|
|
async function testWebRunnerForwardsResolvedAddRunSpec() {
|
|
const code = fs.readFileSync(path.join(repoRoot, "muSRFitweb.js"), "utf8");
|
|
const calls = [];
|
|
const sandbox = {
|
|
console: { log: function() {} },
|
|
URLSearchParams: URLSearchParams,
|
|
fetch: async function(url, options) {
|
|
calls.push({ url: url, options: options || {} });
|
|
return {
|
|
ok: true,
|
|
status: 200,
|
|
statusText: "OK",
|
|
text: async function() {
|
|
return JSON.stringify({ ok: true, code: 0, message: "Plot completed" });
|
|
}
|
|
};
|
|
},
|
|
window: { MSREditor: {} },
|
|
document: {
|
|
body: { appendChild: function() {} },
|
|
getElementById: function() { return null; }
|
|
}
|
|
};
|
|
sandbox.global = sandbox;
|
|
vm.runInNewContext(code, sandbox, { filename: "muSRFitweb.js" });
|
|
|
|
const addRunSpec = JSON.stringify([
|
|
{
|
|
main: 514,
|
|
mainFile: "/psi.ch/group/lmu/public/data/lem/his/2024/lem24_his_0514",
|
|
addLines: [
|
|
"ADDRUN /psi.ch/group/lmu/public/data/lem/his/2024/lem24_his_0515 MUE4 PSI MUSR-ROOT"
|
|
]
|
|
}
|
|
]);
|
|
|
|
const result = await sandbox.runMSRFileWeb("/tmp/job/514+global_asy.msr", {
|
|
useM2D: 1,
|
|
runs: "514 520",
|
|
templateName: "514_tmpl.msr",
|
|
baseRun: 514,
|
|
addRunSpec: addRunSpec,
|
|
plotOnly: 1
|
|
});
|
|
|
|
assert.strictEqual(result.ok, 1);
|
|
assert.strictEqual(calls.length, 1);
|
|
const body = new URLSearchParams(calls[0].options.body);
|
|
assert.strictEqual(body.get("addrun_spec"), addRunSpec);
|
|
}
|
|
|
|
async function runTests() {
|
|
const lib = loadFitLibrary();
|
|
testRunSelectionGrammar(lib);
|
|
testSingleDataSetWritesAddRunDirectly(lib);
|
|
testMultiDataSetTemplateDefersAddRunUntilAfterMsr2data(lib);
|
|
await testWebRunnerForwardsResolvedAddRunSpec();
|
|
console.log("ADDRUN tests passed");
|
|
}
|
|
|
|
runTests().catch(function(error) {
|
|
console.error(error);
|
|
process.exitCode = 1;
|
|
});
|