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.
3987 lines
147 KiB
JavaScript
3987 lines
147 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 loadLibrary() {
|
|
const code = fs.readFileSync(path.join(repoRoot, "muSRFitlib.js"), "utf8");
|
|
const sandbox = {
|
|
console: {
|
|
log: function() {
|
|
},
|
|
error: console.error,
|
|
warn: console.warn
|
|
},
|
|
window: { MSREditor: {} },
|
|
alert: function(message) {
|
|
throw new Error("Unexpected alert: " + message);
|
|
}
|
|
};
|
|
sandbox.global = sandbox;
|
|
vm.runInNewContext(code, sandbox, { filename: "muSRFitlib.js" });
|
|
return sandbox;
|
|
}
|
|
|
|
|
|
function resetMSREditorDocuments(lib) {
|
|
// Tests exercise template/global ownership independently. Reset both document
|
|
// states so one synchronization scenario cannot leak into the next one.
|
|
lib.window.MSREditor.documents.current = {
|
|
sourceOfTruth: "gui",
|
|
locked: false,
|
|
dirty: false,
|
|
lastGeneratedHash: "",
|
|
lastAcceptedHash: "",
|
|
stale: false
|
|
};
|
|
lib.window.MSREditor.documents.template = {
|
|
sourceOfTruth: "gui",
|
|
locked: false,
|
|
dirty: false,
|
|
lastGeneratedHash: "",
|
|
lastAcceptedHash: "",
|
|
stale: false
|
|
};
|
|
lib.window.MSREditor.activeSource = "current";
|
|
lib.window.MSREditor.hasTemplateTab = false;
|
|
lib.syncMSREditorLegacyState();
|
|
}
|
|
|
|
function testElectronPreferencesAndSuffixControls(lib) {
|
|
const html = fs.readFileSync(path.join(repoRoot, "index.html"), "utf8");
|
|
const main = fs.readFileSync(path.join(repoRoot, "main.js"), "utf8");
|
|
const libSource = fs.readFileSync(path.join(repoRoot, "muSRFitlib.js"), "utf8");
|
|
|
|
assert.match(main, /label: 'Preferences\.\.\.'/);
|
|
assert.doesNotMatch(main, /label: 'Select Folder\.\.\.'/);
|
|
assert.match(html, /id="preferencesDialog"/);
|
|
assert.match(html, /id="workPath"/);
|
|
assert.match(html, /id="dataRoot"/);
|
|
assert.match(html, /id="binDir"/);
|
|
assert.match(html, /onclick="selectBinFolder\(\);"/);
|
|
assert.match(html, /initializeBinDir\(\)/);
|
|
assert.match(libSource, /function getConfiguredBinDir\(/);
|
|
assert.match(libSource, /function setConfiguredBinDir\(/);
|
|
assert.match(html, /id="musrfitThreads"[^>]*placeholder="all cores"/);
|
|
assert.match(html, /type="number" id="musrfitThreads"/);
|
|
assert.match(html, /initializeMusrfitThreads\(\)/);
|
|
assert.match(libSource, /function getConfiguredMusrfitThreads\(/);
|
|
assert.match(libSource, /function setConfiguredMusrfitThreads\(/);
|
|
assert.match(html, /id="asymmetrySuffix"[^>]*value="_asy"/);
|
|
assert.match(html, /id="singleHistSuffix"[^>]*value="_singleHist"/);
|
|
assert.match(html, /id="guessInitialParameters"[^>]*checked/);
|
|
assert.match(html, /id="runNumbers"[\s\S]*onchange="handleRunSelectionChange\(this\.value\);"/);
|
|
assert.match(html, /id="preferencesDialog"[\s\S]*id="asymmetrySuffix"[\s\S]*id="singleHistSuffix"/);
|
|
assert.doesNotMatch(html, /id="btnOther"/);
|
|
assert.match(html, /class="results-details jobs-log-wrap"/);
|
|
assert.match(libSource, /function initializeFileSuffixes\(/);
|
|
assert.doesNotMatch(html, /id="fileNamePrefix"/);
|
|
|
|
assert.strictEqual(
|
|
lib.fitFileSuffix({ fitAsyType: "AsymmetryGLB", asymmetrySuffix: "_custom" }, false),
|
|
"_custom"
|
|
);
|
|
assert.strictEqual(
|
|
lib.fitFileSuffix({ fitAsyType: "SingleHist", singleHistSuffix: "_counts" }, false),
|
|
"_counts"
|
|
);
|
|
// Web names are fixed regardless of any values present in the DOM/config.
|
|
assert.strictEqual(
|
|
lib.fitFileSuffix({ fitAsyType: "AsymmetryGLB", asymmetrySuffix: "_custom" }, true),
|
|
"_asy"
|
|
);
|
|
assert.strictEqual(
|
|
lib.fitFileSuffix({ fitAsyType: "SingleHist", singleHistSuffix: "_counts" }, true),
|
|
"_singleHist"
|
|
);
|
|
}
|
|
|
|
function testInitialFrequencyGuess(lib) {
|
|
const rootHeader = [
|
|
"017 - Sample Magnetic Field: 350.002 +- 0.005 G; SP: 350.0",
|
|
"Sample Temperature : 5.0 K"
|
|
].join("\n");
|
|
const rootMeta = lib.parseRunHeaderMetadata(rootHeader);
|
|
assert.ok(Math.abs(rootMeta.fieldG - 350.002) < 1e-9);
|
|
assert.ok(Math.abs(
|
|
lib.guessMuonFrequencyMHz(rootMeta) - 350.002 * 1.355388094e-2
|
|
) < 1e-12);
|
|
|
|
const lemMeta = lib.parseRunHeaderMetadata("Sample Mag. Field : -12.5+-0.1 G");
|
|
assert.strictEqual(lemMeta.fieldG, -12.5);
|
|
assert.ok(Math.abs(lib.guessMuonFrequencyMHz(lemMeta) - 12.5 * 1.355388094e-2) < 1e-12);
|
|
|
|
const teslaMeta = lib.parseRunHeaderMetadata("Sample Magn. Field : 0.15 T");
|
|
assert.strictEqual(teslaMeta.fieldG, 1500);
|
|
const request = lib.dumpHeaderRequestFromRunDirective(
|
|
"RUN /data/lem/lem26_his_0514 MUE4 PSI MUSR-ROOT"
|
|
);
|
|
assert.deepStrictEqual(
|
|
JSON.parse(JSON.stringify(request)),
|
|
{ filePath: "/data/lem/lem26_his_0514.root", fileFormat: "MusrRoot" }
|
|
);
|
|
|
|
const inputs = {
|
|
valFrq_0514: { value: "1.355" },
|
|
errFrq_0514: { value: "0.01355" },
|
|
valFrq_0515: { value: "1.355" },
|
|
errFrq_0515: { value: "0.01355" }
|
|
};
|
|
lib.document = {
|
|
getElementById: function(id) { return inputs[id] || null; }
|
|
};
|
|
const targets = [
|
|
{ parameterName: "Frq_0514", originalValue: "1.355" },
|
|
{ parameterName: "Frq_0515", originalValue: "1.355" }
|
|
];
|
|
assert.strictEqual(lib.applyFrequencyGuess(rootMeta, targets), true);
|
|
assert.strictEqual(inputs.valFrq_0514.value, inputs.valFrq_0515.value);
|
|
assert.strictEqual(lib.All.valFrq_0514, lib.All.valFrq_0515);
|
|
|
|
// The untouched default follows the first-run field: zero field removes the
|
|
// meaningless oscillation, while a later finite-field run restores it.
|
|
const originalGetFitComponentValues = lib.getFitComponentValues;
|
|
const originalFrequencyGuessTargets = lib.frequencyGuessTargets;
|
|
const originalClearTabs = lib.clearTabs;
|
|
const modelSelect = { value: "ExponentialCos" };
|
|
let rebuiltModels = 0;
|
|
lib.document = {
|
|
getElementById: function(id) {
|
|
if (id === "fitFunc1") return modelSelect;
|
|
return inputs[id] || null;
|
|
}
|
|
};
|
|
lib.getFitComponentValues = function() {
|
|
return [modelSelect.value];
|
|
};
|
|
lib.frequencyGuessTargets = function() {
|
|
if (modelSelect.value !== "ExponentialCos") return [];
|
|
return ["Frq_0514", "Frq_0515"].map(function(parameterName) {
|
|
return {
|
|
parameterName: parameterName,
|
|
originalValue: inputs["val" + parameterName].value
|
|
};
|
|
});
|
|
};
|
|
lib.clearTabs = function() {
|
|
rebuiltModels++;
|
|
};
|
|
|
|
lib.automaticInitialFitModel = true;
|
|
assert.strictEqual(lib.applyHeaderBasedInitialGuess({ fieldG: 0.5 }, targets), true);
|
|
assert.strictEqual(modelSelect.value, "Exponential");
|
|
assert.strictEqual(rebuiltModels, 1);
|
|
|
|
assert.strictEqual(lib.applyHeaderBasedInitialGuess({ fieldG: 100 }, []), true);
|
|
assert.strictEqual(modelSelect.value, "ExponentialCos");
|
|
assert.strictEqual(inputs.valFrq_0514.value, lib.formatInitialParameterGuess(
|
|
100 * lib.muonGammaBarMHzPerGauss
|
|
));
|
|
assert.strictEqual(rebuiltModels, 2);
|
|
|
|
lib.markFitModelExplicit();
|
|
assert.strictEqual(lib.automaticInitialFitModel, false);
|
|
assert.strictEqual(lib.applyHeaderBasedInitialGuess({ fieldG: 0 }, []), true);
|
|
assert.strictEqual(modelSelect.value, "ExponentialCos");
|
|
assert.strictEqual(rebuiltModels, 2);
|
|
|
|
lib.getFitComponentValues = originalGetFitComponentValues;
|
|
lib.frequencyGuessTargets = originalFrequencyGuessTargets;
|
|
lib.clearTabs = originalClearTabs;
|
|
lib.automaticInitialFitModel = true;
|
|
|
|
// Keeping the same first run preserves the cached header. Changing it starts
|
|
// a new guess generation and invalidates the old first-run metadata.
|
|
const state = lib.initialParameterGuessState;
|
|
state.generation = 10;
|
|
state.appliedGeneration = 10;
|
|
state.firstRun = "514";
|
|
state.metadataKey = "old-header";
|
|
state.metadata = rootMeta;
|
|
assert.strictEqual(lib.updateInitialParameterGuessFirstRun("514"), false);
|
|
assert.strictEqual(state.generation, 10);
|
|
assert.strictEqual(lib.updateInitialParameterGuessFirstRun("515"), true);
|
|
assert.strictEqual(state.generation, 11);
|
|
assert.strictEqual(state.firstRun, "515");
|
|
assert.strictEqual(state.metadataKey, "");
|
|
assert.strictEqual(state.metadata, null);
|
|
|
|
let headerLog = null;
|
|
lib.console.log = function(label, info) {
|
|
if (/Initial parameter guess header/.test(label)) headerLog = info;
|
|
};
|
|
lib.logInitialParameterGuessHeader(
|
|
{ filePath: "/data/run.root", fileFormat: "MusrRoot" }, rootMeta, false
|
|
);
|
|
assert.strictEqual(headerLog.fieldG, 350.002);
|
|
assert.match(headerLog.header, /Sample Magnetic Field/);
|
|
|
|
state.generation = 1;
|
|
state.appliedGeneration = 0;
|
|
state.firstRun = "";
|
|
state.metadataKey = "";
|
|
state.metadata = null;
|
|
}
|
|
|
|
function testReverseFieldInitialization(lib) {
|
|
// When Frq is expressed through a field parameter (`Frq = gamma_mu * Pfield`),
|
|
// the reverse initialization should set Pfield to the header field (gauss)
|
|
// and skip the direct Frq guess.
|
|
const metadata = { fieldG: 350.002 };
|
|
const valPara = { id: "valPfield", value: "" };
|
|
const errPara = { id: "errPfield", value: "" };
|
|
const funLine = { innerText: "Frq = gamma_mu * Pfield" };
|
|
lib.document = {
|
|
getElementById: function(id) {
|
|
if (id == "valPfield") return valPara;
|
|
if (id == "errPfield") return errPara;
|
|
return null;
|
|
},
|
|
getElementsByClassName: function() {
|
|
return [funLine];
|
|
}
|
|
};
|
|
|
|
assert.strictEqual(lib.parseFrequencyReverseExpression("gamma_mu * Pfield"), "Pfield");
|
|
assert.strictEqual(lib.parseFrequencyReverseExpression("Pfield * gamma_mu"), "Pfield");
|
|
assert.strictEqual(lib.parseFrequencyReverseExpression("foo + gamma_mu"), null);
|
|
assert.strictEqual(lib.parseFrequencyReverseExpression("gamma_mu * (Pfield)"), null);
|
|
assert.strictEqual(lib.parseFrequencyReverseExpression("gamma_mu"), null);
|
|
assert.strictEqual(lib.parseFrequencyReverseExpression(""), null);
|
|
|
|
const reverse = lib.frequencyReverseTargets();
|
|
assert.strictEqual(reverse.length, 1);
|
|
assert.strictEqual(reverse[0].parameterName, "Pfield");
|
|
|
|
assert.strictEqual(lib.applyFieldLinkedFrequencyGuess(metadata, reverse), true);
|
|
assert.strictEqual(valPara.value, lib.formatInitialParameterGuess(350.002));
|
|
assert.strictEqual(errPara.value, lib.formatInitialParameterGuess(3.50002));
|
|
assert.strictEqual(lib.All.valPfield, valPara.value);
|
|
assert.strictEqual(lib.All.errPfield, errPara.value);
|
|
|
|
// A user-provided field value must not be overwritten.
|
|
valPara.value = "142";
|
|
const guarded = lib.applyFieldLinkedFrequencyGuess(metadata, reverse);
|
|
assert.strictEqual(valPara.value, "142");
|
|
assert.strictEqual(guarded, false);
|
|
|
|
// The reverse guess is only consulted when a gamma_mu-linked constraint is
|
|
// actually present in the FUNCTIONS block.
|
|
lib.frequencyReverseTargets = function() {
|
|
return [];
|
|
};
|
|
const originalGetElementById = lib.document.getElementById;
|
|
lib.document.getElementById = function(id) {
|
|
if (id == "valPfield") return valPara;
|
|
if (id == "errPfield") return errPara;
|
|
return originalGetElementById(id);
|
|
};
|
|
lib.All.valPfield = "";
|
|
lib.All.errPfield = "";
|
|
assert.strictEqual(lib.applyHeaderBasedInitialGuess(metadata, []), true);
|
|
assert.strictEqual(lib.All.valPfield, "");
|
|
|
|
// With the reverse target present, applyHeaderBasedInitialGuess initializes
|
|
// the field parameter and leaves any Frq target untouched.
|
|
valPara.value = "";
|
|
errPara.value = "";
|
|
lib.frequencyReverseTargets = function() {
|
|
return [
|
|
{ parameterName: "Pfield", originalValue: "" }
|
|
];
|
|
};
|
|
const frqInput = { value: "9" };
|
|
lib.document.getElementById = function(id) {
|
|
if (id == "valPfield") return valPara;
|
|
if (id == "errPfield") return errPara;
|
|
if (id == "valFrq") return frqInput;
|
|
return originalGetElementById(id);
|
|
};
|
|
assert.strictEqual(lib.applyHeaderBasedInitialGuess(metadata, [
|
|
{ parameterName: "Frq", originalValue: "9" }
|
|
]), true);
|
|
assert.strictEqual(valPara.value, lib.formatInitialParameterGuess(350.002));
|
|
assert.strictEqual(frqInput.value, "9");
|
|
|
|
lib.All = {};
|
|
}
|
|
|
|
function testResultArtifactRendering(lib) {
|
|
const row = { hidden: null };
|
|
const artifact = {
|
|
closest: function() {
|
|
return row;
|
|
},
|
|
removeAttribute: function(name) {
|
|
delete this[name];
|
|
}
|
|
};
|
|
lib.document = {
|
|
getElementById: function(id) {
|
|
return id == "testArtifact" ? artifact : null;
|
|
}
|
|
};
|
|
|
|
lib.renderArtifactValue("testArtifact", "/results/job/514_asy.out");
|
|
assert.strictEqual(artifact.textContent, "514_asy.out");
|
|
assert.strictEqual(artifact.title, "/results/job/514_asy.out");
|
|
assert.strictEqual(artifact.href, "/results/job/514_asy.out");
|
|
assert.strictEqual(row.hidden, false);
|
|
|
|
lib.renderArtifactValue("testArtifact", "");
|
|
assert.strictEqual(artifact.textContent, "-");
|
|
assert.strictEqual(artifact.href, undefined);
|
|
assert.strictEqual(row.hidden, true);
|
|
}
|
|
|
|
function testCompactConstraintsLayout(lib) {
|
|
const html = fs.readFileSync(path.join(repoRoot, "index.html"), "utf8");
|
|
const css = fs.readFileSync(path.join(repoRoot, "ZGUI.css"), "utf8");
|
|
const librarySource = fs.readFileSync(path.join(repoRoot, "muSRFitlib.js"), "utf8");
|
|
|
|
// Constraints should use the same compact header/row vocabulary as Runs and
|
|
// Fit settings, while preserving the IDs consumed by the existing JS logic.
|
|
assert.match(html, /id="Functions"[\s\S]*?compact-settings-header"><b>Constraints<\/b>/);
|
|
assert.match(html, /id="funLineContainer" class="compact-settings-row constraints-options-row"/);
|
|
assert.match(html, /id="funSel"/);
|
|
assert.match(html, /id="funLine"/);
|
|
assert.match(html, /compact-settings-header"><b>Custom parameters<\/b>/);
|
|
assert.match(html, /id="newParamName"/);
|
|
assert.match(html, /id="customParamList"/);
|
|
// Custom parameters are defined first and then consumed by the constraints,
|
|
// so their section must appear above the Constraints row.
|
|
const constraintsIndex = html.indexOf('class="compact-settings-row constraints-options-row"');
|
|
const customIndex = html.indexOf('class="compact-settings-row custom-parameter-row"');
|
|
assert.ok(constraintsIndex > -1 && customIndex > -1 && customIndex < constraintsIndex,
|
|
"Custom parameters must be listed above the Constraints section");
|
|
|
|
// Generated THEORY/FUNCTIONS text is important verification output and must
|
|
// remain visible instead of being placed behind a disclosure control.
|
|
assert.match(html, /compact-settings-header"><b>Preview<\/b>/);
|
|
assert.match(html, /id="constraintsPreview" class="split-panel-grid constraints-preview-grid"/);
|
|
assert.doesNotMatch(html, /<details[^>]*id="constraintsPreview"/);
|
|
assert.match(html, /id="theoFunBlock"/);
|
|
assert.match(html, /id="funBlock"/);
|
|
|
|
// The former W3-era two-column entry helpers should no longer be needed.
|
|
assert.doesNotMatch(html, /functions-entry-grid/);
|
|
assert.doesNotMatch(css, /\.functions-entry-grid/);
|
|
assert.match(css, /\.constraints-options-row/);
|
|
|
|
// All icons are local so Electron does not render icon names when offline.
|
|
assert.doesNotMatch(html, /fonts\.googleapis\.com|material-symbols/);
|
|
assert.doesNotMatch(css, /material-symbols/);
|
|
assert.doesNotMatch(librarySource, /material-symbols/);
|
|
assert.match(html, /class="share-icon"/);
|
|
assert.match(librarySource, /constraint-icon-button delBtn/);
|
|
assert.match(librarySource, /constraint-icon-button edtBtn/);
|
|
assert.match(librarySource, /singleFun\.className = "constraint-line"/);
|
|
assert.match(librarySource, /lineActions\.className = "constraint-line-actions"/);
|
|
assert.match(css, /\.constraint-line\s*\{[\s\S]*?display:\s*flex/);
|
|
assert.match(css, /button\.constraint-icon-button\s*\{[\s\S]*?box-sizing:\s*border-box/);
|
|
assert.match(css, /button\.constraint-icon-button svg\s*\{[\s\S]*?display:\s*block/);
|
|
assert.doesNotMatch(css, /\.constraint-icon-button\s*\{[\s\S]*?float:\s*right/);
|
|
}
|
|
|
|
function testCompactEditorResultsAndStatusLayout(lib) {
|
|
const html = fs.readFileSync(path.join(repoRoot, "index.html"), "utf8");
|
|
const css = fs.readFileSync(path.join(repoRoot, "ZGUI.css"), "utf8");
|
|
const editorStart = html.indexOf('<div id="Editor"');
|
|
const editorEnd = html.indexOf('<div id="Jobs"', editorStart);
|
|
const editorHtml = html.substring(editorStart, editorEnd);
|
|
|
|
// The document selector now owns the editor identity; the old repeated Editor
|
|
// banner should not consume vertical space above the text area.
|
|
assert.doesNotMatch(html, /id="editorSourceTitle"/);
|
|
assert.match(html, /id="btnEditorCurrent"[^>]*>MSR<\/button>/);
|
|
assert.match(html, /id="btnEditorTemplate"[^>]*hidden/);
|
|
assert.match(editorHtml, /aria-label="Sync MSR settings to GUI">MSR → GUI<\/button>/);
|
|
assert.match(editorHtml, /id="msrUnlockButton"[\s\S]*?aria-label="Editor source unlocked" disabled/);
|
|
assert.match(editorHtml, /class="editor-lock-open"/);
|
|
assert.match(editorHtml, /class="editor-lock-closed"/);
|
|
assert.match(editorHtml, />GUI → MSR<\/button>/);
|
|
assert.match(editorHtml, /aria-label="Save visible MSR document"/);
|
|
assert.doesNotMatch(editorHtml, /runCurrentMSRFile/);
|
|
assert.match(css, /\.editor-icon-button/);
|
|
assert.match(css, /\.editor-icon-button[\s\S]*?height:\s*36px;[\s\S]*?width:\s*36px;/);
|
|
assert.match(css, /\.editor-icon-button svg[\s\S]*?height:\s*21px;[\s\S]*?width:\s*21px;/);
|
|
assert.doesNotMatch(css, /\.editor-run-button/);
|
|
|
|
// Transfer actions read left-to-right; document-state actions follow the
|
|
// flexible spacer on the right.
|
|
assert.ok(editorHtml.indexOf("MSR → GUI") < editorHtml.indexOf("GUI → MSR"));
|
|
assert.ok(editorHtml.indexOf("GUI → MSR") < editorHtml.indexOf("editor-toolbar-spacer"));
|
|
assert.ok(editorHtml.indexOf("editor-toolbar-spacer") < editorHtml.indexOf("msrUnlockButton"));
|
|
assert.ok(editorHtml.indexOf("msrUnlockButton") < editorHtml.indexOf("Save visible MSR document"));
|
|
|
|
// Results keep only the fitted MSR permanently visible. The ROOT canvas is a
|
|
// generated companion artifact and therefore belongs inside Other files.
|
|
assert.match(html, /id="resultPrimaryFiles"[\s\S]*?Result MSR[\s\S]*?<\/div>\s*<details id="resultOtherFiles"/);
|
|
assert.match(html, /id="resultOtherFiles"[^>]*>[\s\S]*?<span class="result-file-label">ROOT plot<\/span>/);
|
|
assert.doesNotMatch(html, /Plot image/);
|
|
|
|
// Optional status fields start hidden so the idle bar renders as Mode · Idle.
|
|
assert.match(html, /id="jobStatusMessage"[^>]*hidden/);
|
|
assert.match(html, /id="jobStatusFile" hidden/);
|
|
assert.match(html, /id="jobStatusDirectory" hidden/);
|
|
assert.match(html, /id="jobStatusState"[\s\S]*onclick="showJobLogWindow\(\);"/);
|
|
assert.match(html, /id="jobLogWindow"/);
|
|
}
|
|
|
|
function testCompactJobStatusVisibility(lib) {
|
|
function statusElement() {
|
|
return {
|
|
textContent: "",
|
|
hidden: false,
|
|
disabled: false,
|
|
title: "",
|
|
attributes: {},
|
|
setAttribute: function(name, value) { this.attributes[name] = String(value); }
|
|
};
|
|
}
|
|
|
|
const elements = {
|
|
appStatusBar: { dataset: {} },
|
|
jobStatusState: statusElement(),
|
|
jobStatusMessage: statusElement(),
|
|
jobStatusFile: statusElement(),
|
|
jobStatusDirectory: statusElement(),
|
|
jobStatusAnnouncement: statusElement(),
|
|
resultArtifactList: { hidden: false },
|
|
resultPrimaryFiles: { hidden: false },
|
|
resultOtherFiles: { hidden: false },
|
|
resultArtifactEmpty: { hidden: false }
|
|
};
|
|
lib.document = {
|
|
getElementById: function(id) {
|
|
return elements[id] || null;
|
|
}
|
|
};
|
|
lib.amIWeb = function() {
|
|
return true;
|
|
};
|
|
|
|
lib.window.MusrfitJob.state = "Idle";
|
|
lib.window.MusrfitJob.mode = "Web";
|
|
lib.window.MusrfitJob.message = "Idle";
|
|
lib.window.MusrfitJob.file = "";
|
|
lib.window.MusrfitJob.directory = "";
|
|
lib.window.MusrfitJob.templatePath = "";
|
|
lib.window.MusrfitJob.resultMSR = "";
|
|
lib.window.MusrfitJob.logPath = "";
|
|
lib.window.MusrfitJob.plotPath = "";
|
|
lib.window.MusrfitJob.parameterDataPath = "";
|
|
lib.window.MusrfitJob.packagePath = "";
|
|
lib.renderJobStatus();
|
|
|
|
assert.strictEqual(elements.jobStatusState.textContent, "Idle");
|
|
assert.strictEqual(elements.jobStatusState.disabled, true);
|
|
assert.strictEqual(elements.jobStatusMessage.hidden, true);
|
|
assert.strictEqual(elements.jobStatusFile.hidden, true);
|
|
assert.strictEqual(elements.jobStatusDirectory.hidden, true);
|
|
assert.strictEqual(elements.resultArtifactList.hidden, true);
|
|
|
|
lib.window.MusrfitJob.state = "Running musrfit";
|
|
lib.window.MusrfitJob.message = "Fit";
|
|
lib.window.MusrfitJob.messageDetail = "Fit\nUsing synchronized parameters";
|
|
lib.window.MusrfitJob.file = "514_asy.msr";
|
|
lib.window.MusrfitJob.directory = "/work";
|
|
lib.window.MusrfitJob.resultMSR = "/work/514_asy.msr";
|
|
lib.window.MusrfitJob.logPath = "/work/514_asy.log";
|
|
lib.window.MusrfitJob.log = ["fit started"];
|
|
lib.renderJobStatus();
|
|
|
|
assert.strictEqual(elements.jobStatusState.disabled, false);
|
|
assert.strictEqual(elements.jobStatusState.title, "Running musrfit\n\nOpen run log.");
|
|
assert.strictEqual(elements.jobStatusMessage.hidden, false);
|
|
assert.strictEqual(elements.jobStatusMessage.textContent, "Fit");
|
|
assert.strictEqual(elements.jobStatusMessage.title,
|
|
"Fit\nUsing synchronized parameters\n\nOpen run log.");
|
|
assert.strictEqual(elements.jobStatusFile.hidden, false);
|
|
assert.strictEqual(elements.jobStatusFile.title, "/work/514_asy.msr");
|
|
assert.strictEqual(elements.jobStatusDirectory.hidden, false);
|
|
assert.strictEqual(elements.resultPrimaryFiles.hidden, false);
|
|
assert.strictEqual(elements.resultOtherFiles.hidden, false);
|
|
|
|
// A ROOT-only result should still expose Other files while leaving the primary
|
|
// fitted-MSR area hidden. This guards the artifact grouping used by plot-only jobs.
|
|
lib.window.MusrfitJob.file = "";
|
|
lib.window.MusrfitJob.resultMSR = "";
|
|
lib.window.MusrfitJob.logPath = "";
|
|
lib.window.MusrfitJob.plotPath = "/work/514_asy_0.root";
|
|
lib.renderJobStatus();
|
|
assert.strictEqual(elements.resultPrimaryFiles.hidden, true);
|
|
assert.strictEqual(elements.resultOtherFiles.hidden, false);
|
|
}
|
|
|
|
function testParameterPlotSelection(lib) {
|
|
function makeRow(name) {
|
|
return {
|
|
dataset: { plotParameter: name },
|
|
classList: { toggle: function() {} },
|
|
setAttribute: function() {}
|
|
};
|
|
}
|
|
|
|
const rows = [
|
|
makeRow("Alpha"), makeRow("Lam"), makeRow("Phi"),
|
|
makeRow("Alpha"), makeRow("Lam"), makeRow("Phi")
|
|
];
|
|
const button = {};
|
|
const params = {
|
|
querySelectorAll: function() { return rows; }
|
|
};
|
|
lib.document = {
|
|
getElementById: function(id) {
|
|
if (id === "Params") return params;
|
|
if (id === "plotSelectedParameters") return button;
|
|
return null;
|
|
}
|
|
};
|
|
lib.window.MusrfitJob.parameterDataPath = "/work/fit_par.dat";
|
|
lib.window.MusrfitJob.resultMSR = "/work/fit.msr";
|
|
lib.selectedParameterPlotNames = [];
|
|
lib.parameterPlotSelectionAnchor = "";
|
|
|
|
lib.selectParameterForPlot({}, "Lam");
|
|
assert.deepStrictEqual(Array.from(lib.selectedParameterPlotNames), ["Lam"]);
|
|
assert.strictEqual(button.disabled, false);
|
|
|
|
// Plain-clicking the selected name again toggles it off. The Plot button
|
|
// stays available so it can explain that a parameter must be selected.
|
|
lib.selectParameterForPlot({}, "Lam");
|
|
assert.deepStrictEqual(Array.from(lib.selectedParameterPlotNames), []);
|
|
assert.strictEqual(button.disabled, false);
|
|
let warning = "";
|
|
lib.alert = function(message) { warning = message; };
|
|
lib.openSelectedParameterPlot();
|
|
assert.strictEqual(warning, "Please select at least one parameter to plot.");
|
|
|
|
lib.selectParameterForPlot({}, "Lam");
|
|
lib.selectParameterForPlot({ ctrlKey: true }, "Phi");
|
|
assert.deepStrictEqual(Array.from(lib.selectedParameterPlotNames), ["Lam", "Phi"]);
|
|
|
|
lib.selectParameterForPlot({ shiftKey: true }, "Alpha");
|
|
assert.deepStrictEqual(Array.from(lib.selectedParameterPlotNames), ["Alpha", "Lam", "Phi"]);
|
|
|
|
let opened = null;
|
|
lib.showMusrfitParameterAxisDialog = function(pathName, jobName, names) {
|
|
opened = { pathName: pathName, jobName: jobName, names: Array.from(names) };
|
|
};
|
|
lib.openSelectedParameterPlot();
|
|
assert.deepStrictEqual(opened, {
|
|
pathName: "/work/fit_par.dat",
|
|
jobName: "fit.msr",
|
|
names: ["Alpha", "Lam", "Phi"]
|
|
});
|
|
}
|
|
|
|
function testInlineParameterPlotFromTable(lib) {
|
|
// The parameter table is populated but no fit has produced an on-disk
|
|
// *_par.dat artifact (e.g. after restoring a configuration or when every
|
|
// run was skipped). Plotting must still be allowed by generating the
|
|
// parameter-data table inline from the live table values.
|
|
function makeRow(name) {
|
|
return {
|
|
dataset: { plotParameter: name },
|
|
classList: { toggle: function() {} },
|
|
setAttribute: function() {}
|
|
};
|
|
}
|
|
const rows = [makeRow("Alpha"), makeRow("Lam")];
|
|
const params = {
|
|
querySelector: function(){ return rows[0]; },
|
|
querySelectorAll: function() { return rows; }
|
|
};
|
|
const inputs = [
|
|
{ id: "runNumbers", type: "text", value: "514,515", innerHTML: "" },
|
|
{ id: "shAlpha", type: "checkbox", checked: true },
|
|
{ id: "shLam", type: "checkbox", checked: false },
|
|
{ id: "valAlpha", type: "text", value: "0.25", innerHTML: "" },
|
|
{ id: "valLam_514", type: "text", value: "0.31", innerHTML: "" },
|
|
{ id: "valLam_515", type: "text", value: "0.28", innerHTML: "" }
|
|
];
|
|
lib.document = {
|
|
getElementById: function(id) {
|
|
if (id === "Params") return params;
|
|
if (id === "plotSelectedParameters") return {};
|
|
return inputs.find(function(input) {
|
|
return input.id === id;
|
|
}) || null;
|
|
},
|
|
getElementsByTagName: function(tagName) {
|
|
return tagName === "input" ? inputs : [];
|
|
}
|
|
};
|
|
|
|
const previousAll = lib.All;
|
|
lib.All = {
|
|
runNumbers: "514,515",
|
|
instrName: "",
|
|
selYear: "",
|
|
dataRoot: "",
|
|
shAlpha: "1",
|
|
shLam: "",
|
|
// These deliberately stale values must be replaced from the visible
|
|
// inputs before the inline parameter data are assembled.
|
|
valAlpha: "0.22",
|
|
valLam_514: "0.30",
|
|
valLam_515: "0.27"
|
|
};
|
|
|
|
const built = lib.buildParameterDataFromTable();
|
|
assert.ok(built, "buildParameterDataFromTable must produce inline data");
|
|
assert.strictEqual(built.split("\n")[0], "RUN\tAlpha\tLam");
|
|
const lines = built.trim().split("\n");
|
|
assert.deepStrictEqual(lines[1].split("\t"), ["514", "0.25", "0.31"]);
|
|
assert.deepStrictEqual(lines[2].split("\t"), ["515", "0.25", "0.28"]);
|
|
assert.strictEqual(lib.All.valAlpha, "0.25");
|
|
assert.strictEqual(lib.All.valLam_514, "0.31");
|
|
|
|
// openSelectedParameterPlot with no parameterDataPath must pass an inline
|
|
// source into the axis dialog instead of silently returning.
|
|
lib.window.MusrfitJob.parameterDataPath = "";
|
|
lib.window.MusrfitJob.resultMSR = "";
|
|
lib.selectedParameterPlotNames = ["Alpha", "Lam"];
|
|
let opened = null;
|
|
lib.showMusrfitParameterAxisDialog = function(source, jobName, names) {
|
|
opened = { source: source, jobName: jobName, names: Array.from(names) };
|
|
};
|
|
lib.openSelectedParameterPlot();
|
|
assert.ok(opened, "openSelectedParameterPlot must open the axis dialog when the table is populated");
|
|
assert.strictEqual(opened.source.kind, "inline");
|
|
assert.ok(opened.source.text.indexOf("514\t0.25\t0.31") >= 0);
|
|
assert.deepStrictEqual(opened.names, ["Alpha", "Lam"]);
|
|
|
|
lib.All = previousAll;
|
|
}
|
|
|
|
function testBeginJobPreparationPreservesParameterDataOnPlot(lib) {
|
|
// A Plot-only action does not change the fit, so it must keep both the
|
|
// statistic and the existing parameter table. Only a new Fit invalidates
|
|
// them. This guards the scenario where the user plots parameters, then
|
|
// clicks Plot, then expects "Plot parameters..." to still work because the
|
|
// converged parameter table is unchanged.
|
|
lib.document = { getElementById: function() { return null; } };
|
|
lib.window.MusrfitJob.parameterDataPath = "/work/514+global_asy_par.dat";
|
|
lib.window.MusrfitJob.fitStatistic = "chisq/NDF 1.012";
|
|
lib.window.MusrfitJob.fitConverged = true;
|
|
|
|
lib.beginJobPreparation("Plot");
|
|
assert.strictEqual(lib.window.MusrfitJob.parameterDataPath,
|
|
"/work/514+global_asy_par.dat",
|
|
"a plot-only action must preserve the existing parameter data path");
|
|
assert.strictEqual(lib.window.MusrfitJob.fitStatistic, "chisq/NDF 1.012");
|
|
assert.strictEqual(lib.window.MusrfitJob.fitConverged, true);
|
|
|
|
lib.beginJobPreparation("Fit");
|
|
assert.strictEqual(lib.window.MusrfitJob.parameterDataPath, "",
|
|
"a new fit must invalidate the previous parameter data path");
|
|
assert.strictEqual(lib.window.MusrfitJob.fitStatistic, "");
|
|
assert.strictEqual(lib.window.MusrfitJob.fitConverged, null);
|
|
}
|
|
|
|
function testParseMSRParameters(lib) {
|
|
const msr = fs.readFileSync(path.join(__dirname, "fixtures", "fitted-basic.msr"), "utf8");
|
|
const params = JSON.parse(JSON.stringify(lib.parseMSRParameters(msr)));
|
|
|
|
assert.strictEqual(params.length, 3);
|
|
assert.deepStrictEqual(params[0], {
|
|
order: "1",
|
|
name: "Alpha",
|
|
value: "1.234",
|
|
error: "0.012",
|
|
min: "0.5",
|
|
max: "1.5"
|
|
});
|
|
assert.deepStrictEqual(params[2], {
|
|
order: "3",
|
|
name: "Lam_514",
|
|
value: "0.777",
|
|
error: "0.011",
|
|
min: "0.0",
|
|
max: "5.0"
|
|
});
|
|
|
|
// The parser is also used on editable, not-yet-fitted MSRs. Check both
|
|
// input and fitted forms, with and without optional boundaries.
|
|
const variants = [
|
|
"FITPARAMETER",
|
|
"1 FreeInput 1.0 0.1",
|
|
"2 BoundedInput 2.0 0.2 -1.0 3.0",
|
|
"3 FreeFitted 3.0 0.3 0.31",
|
|
"4 BoundedFitted 4.0 0.4 0.41 -2.0 6.0",
|
|
"THEORY",
|
|
"const 1"
|
|
].join("\n");
|
|
const parsedVariants = JSON.parse(JSON.stringify(lib.parseMSRParameters(variants)));
|
|
|
|
assert.deepStrictEqual(parsedVariants.map(function(p) { return [p.min, p.max]; }), [
|
|
["", ""],
|
|
["-1.0", "3.0"],
|
|
["", ""],
|
|
["-2.0", "6.0"]
|
|
]);
|
|
}
|
|
|
|
function testParseAndApplyMSRGUISettings(lib) {
|
|
const msr = [
|
|
"40 nm sample, T=6.99 K",
|
|
"FITPARAMETER",
|
|
"1 Asy 0.0232 -0.0017 0.0019 0 0.3",
|
|
"THEORY",
|
|
"asymmetry 1",
|
|
"GLOBAL",
|
|
"fittype 0 (single histogram fit)",
|
|
"fit 0.05 10",
|
|
"packing 5",
|
|
"RUN 2026/lem26_his_2321 MUE4 PSI MUSR-ROOT",
|
|
"norm 4",
|
|
"backgr.fit 5",
|
|
"forward 21 25",
|
|
"RUN 2026/lem26_his_2321 MUE4 PSI MUSR-ROOT",
|
|
"norm fun2",
|
|
"backgr.fit 8",
|
|
"forward 23 27",
|
|
"COMMANDS",
|
|
"MAX_LIKELIHOOD",
|
|
"MIGRAD",
|
|
"MINOS",
|
|
"SAVE",
|
|
"PLOT 0",
|
|
"lifetimecorrection",
|
|
"runs 1",
|
|
"range 0 10 -0.25 0.25",
|
|
"view_packing 200",
|
|
"FOURIER",
|
|
"units Gauss",
|
|
"fourier_power 12",
|
|
"apodization STRONG",
|
|
"plot POWER"
|
|
].join("\n");
|
|
|
|
const parsed = JSON.parse(JSON.stringify(lib.parseMSRGUISettings(msr)));
|
|
assert.strictEqual(parsed.ok, true);
|
|
assert.strictEqual(parsed.values.runNumbers, "2321");
|
|
assert.strictEqual(parsed.values.selYear, "2026");
|
|
assert.strictEqual(parsed.values.instrName, "LEM");
|
|
assert.strictEqual(parsed.values.fitAsyType, "SingleHist");
|
|
assert.strictEqual(parsed.values.LRBF, "21 25,23 27");
|
|
assert.strictEqual(parsed.values.Tis, "0.05");
|
|
assert.strictEqual(parsed.values.Tfs, "10");
|
|
assert.strictEqual(parsed.values.BINS, "5");
|
|
assert.strictEqual(parsed.values.minType, "MIGRAD");
|
|
assert.strictEqual(parsed.values.errEst, "MINOS");
|
|
assert.strictEqual(parsed.values.fitObjective, "MAX_LIKELIHOOD");
|
|
assert.strictEqual(parsed.values.saveMinuit, true);
|
|
assert.strictEqual(parsed.values.Tpis, "0");
|
|
assert.strictEqual(parsed.values.Tpfs, "10");
|
|
assert.strictEqual(parsed.values.Ypis, "-0.25");
|
|
assert.strictEqual(parsed.values.Ypfs, "0.25");
|
|
assert.strictEqual(parsed.values.VIEWBINS, "200");
|
|
assert.strictEqual(parsed.values.plotLifetimeCorrection, true);
|
|
assert.strictEqual(parsed.values.plotLogX, false);
|
|
assert.strictEqual(parsed.values.plotLogY, false);
|
|
assert.strictEqual(parsed.values.rrfEnabled, false);
|
|
assert.strictEqual(parsed.values.fftUnits, "Gauss");
|
|
assert.strictEqual(parsed.values.fftPower, "12");
|
|
assert.strictEqual(parsed.values.fftApod, "STRONG");
|
|
assert.strictEqual(parsed.values.fftType, "power");
|
|
assert.deepStrictEqual(parsed.values.plotRuns, ["1"]);
|
|
|
|
const all = {};
|
|
assert.strictEqual(lib.applyMSRGUISettings(parsed, all), 1);
|
|
assert.strictEqual(all.runNumbers, "2321");
|
|
assert.strictEqual(all.selYear, "2026");
|
|
assert.strictEqual(all.instrName, "LEM");
|
|
assert.strictEqual(all.fitAsyType, "SingleHist");
|
|
assert.strictEqual(all.saveMinuit, "1");
|
|
assert.strictEqual(all.Ypis, "-0.25");
|
|
assert.strictEqual(all.Ypfs, "0.25");
|
|
assert.strictEqual(all.plotLogX, "");
|
|
assert.strictEqual(all.rrfEnabled, "");
|
|
assert.deepStrictEqual(all.plotRuns, ["1"]);
|
|
}
|
|
|
|
function testMSRSyncPreservesCompactRunSelection(lib) {
|
|
const runInput = { value: "2407-2410" };
|
|
lib.document = {
|
|
getElementById: function(id) {
|
|
return id === "runNumbers" ? runInput : null;
|
|
}
|
|
};
|
|
|
|
const parsed = {
|
|
values: { runNumbers: "2407,2408,2409,2410" },
|
|
recognized: ["runNumbers"]
|
|
};
|
|
const all = { runNumbers: "2407-2410" };
|
|
assert.strictEqual(lib.applyMSRGUISettings(parsed, all), 1);
|
|
assert.strictEqual(runInput.value, "2407-2410");
|
|
assert.strictEqual(all.runNumbers, "2407-2410");
|
|
|
|
// A different imported run selection must still replace the visible value.
|
|
runInput.value = "2407-2411";
|
|
all.runNumbers = runInput.value;
|
|
lib.applyMSRGUISettings(parsed, all);
|
|
assert.strictEqual(runInput.value, "2407,2408,2409,2410");
|
|
assert.strictEqual(all.runNumbers, "2407,2408,2409,2410");
|
|
}
|
|
|
|
function testParsePlotRunsDirective(lib) {
|
|
const single = [
|
|
"PLOT 0",
|
|
"runs 1"
|
|
].join("\n");
|
|
const parsedSingle = JSON.parse(JSON.stringify(lib.parseMSRGUISettings(single)));
|
|
assert.deepStrictEqual(parsedSingle.values.plotRuns, ["1"]);
|
|
|
|
const multi = [
|
|
"PLOT 2",
|
|
"runs 1 2 3"
|
|
].join("\n");
|
|
const parsedMulti = JSON.parse(JSON.stringify(lib.parseMSRGUISettings(multi)));
|
|
assert.deepStrictEqual(parsedMulti.values.plotRuns, ["1", "2", "3"]);
|
|
|
|
const noRuns = [
|
|
"PLOT 0",
|
|
"range 0 10"
|
|
].join("\n");
|
|
const parsedNoRuns = JSON.parse(JSON.stringify(lib.parseMSRGUISettings(noRuns)));
|
|
assert.strictEqual(parsedNoRuns.values.plotRuns, undefined);
|
|
|
|
const buildWithParsed = lib.buildPlotBlock(
|
|
{ plotRuns: ["1", "3"], Tis: "0", Tfs: "10", Tpis: "0", Tpfs: "10" },
|
|
2, 3
|
|
);
|
|
assert.match(buildWithParsed, /^runs\s+1\s+3\s*$/m);
|
|
|
|
const buildWithoutParsed = lib.buildPlotBlock(
|
|
{ Tis: "0", Tfs: "10", Tpis: "0", Tpfs: "10" },
|
|
2, 3
|
|
);
|
|
assert.match(buildWithoutParsed, /^runs\s+1\s+2\s+3\s*$/m);
|
|
}
|
|
|
|
function testParseMSRTheoryAndFunctions(lib) {
|
|
const msr = [
|
|
"FITPARAMETER",
|
|
"1 Asy 0.0232 -0.0017 0.0019 0 0.3",
|
|
"2 Sgm 0.85 -0.11 0.14 0 10",
|
|
"3 Field 304.4 -1.2 1.3 0 500",
|
|
"4 N0L 595.31 -0.43 0.44",
|
|
"5 NBgL 3.569 -0.049 0.049",
|
|
"6 PhiL -6.4 -4.8 4.8 -360 360",
|
|
"7 AlphaLR 1.0160 -0.0010 0.0011",
|
|
"8 NBgR 4.763 -0.051 0.051",
|
|
"9 PhiR 157.2 -4.9 4.8 -360 360",
|
|
"THEORY",
|
|
"asymmetry 1",
|
|
"simpleGss 2 (rate)",
|
|
"TFieldCos map1 fun1 (phase frequency)",
|
|
"FUNCTIONS",
|
|
"fun1 = gamma_mu * par3",
|
|
"fun2 = par4 * par7",
|
|
"GLOBAL",
|
|
"fittype 0",
|
|
"RUN 2026/lem26_his_2321 MUE4 PSI MUSR-ROOT",
|
|
"norm 4",
|
|
"backgr.fit 5",
|
|
"forward 21 25",
|
|
"map 6 0 0 0 0 0 0 0 0 0",
|
|
"RUN 2026/lem26_his_2321 MUE4 PSI MUSR-ROOT",
|
|
"norm fun2",
|
|
"backgr.fit 8",
|
|
"forward 23 27",
|
|
"map 9 0 0 0 0 0 0 0 0 0",
|
|
"COMMANDS",
|
|
"MIGRAD"
|
|
].join("\n");
|
|
|
|
const parsed = JSON.parse(JSON.stringify(lib.parseMSRTheoryModel(msr)));
|
|
assert.strictEqual(parsed.ok, true);
|
|
assert.deepStrictEqual(parsed.components, ["GaussianCos"]);
|
|
assert.deepStrictEqual(parsed.constraints, [
|
|
{ target: "Frq", expression: "gamma_mu * Field" },
|
|
{ target: "No2", expression: "No1 * AlphaLR" }
|
|
]);
|
|
assert.deepStrictEqual(parsed.customParameters, ["Field", "AlphaLR"]);
|
|
assert.deepStrictEqual(parsed.parameterTargets, {
|
|
1: "Asy",
|
|
2: "Sgm",
|
|
3: "Field",
|
|
4: "No1",
|
|
5: "NBg1",
|
|
6: "Phi",
|
|
7: "AlphaLR",
|
|
8: "NBg2",
|
|
9: "PhiH2"
|
|
});
|
|
|
|
const all = {};
|
|
const previousDocument = lib.document;
|
|
lib.document = undefined;
|
|
assert.strictEqual(lib.applyMSRTheoryModel(parsed, all), 1);
|
|
lib.document = previousDocument;
|
|
assert.strictEqual(all.numComps, "1");
|
|
assert.strictEqual(all.fitFunc1, "GaussianCos");
|
|
|
|
// Rebuilding from the reconstructed GUI state may renumber parameters, but
|
|
// it must preserve the model, detector phase map, and both constraints.
|
|
const modelAll = {
|
|
fitAsyType: "SingleHist",
|
|
numComps: "1",
|
|
fitFunc1: "GaussianCos",
|
|
theoFunBlock: "asymmetry Asy\nsimpleGss Sgm\nTFieldCos Phi Frq",
|
|
runNumbers: "2321",
|
|
selYear: "2026",
|
|
instrName: "LEM",
|
|
dataRoot: "/psi.ch/data",
|
|
LRBF: "21 25,23 27"
|
|
};
|
|
const valuesByNumber = {
|
|
1: ["0.0232", "-0.0017", "0", "0.3"],
|
|
2: ["0.85", "-0.11", "0", "10"],
|
|
3: ["304.4", "-1.2", "0", "500"],
|
|
4: ["595.31", "-0.43", "", ""],
|
|
5: ["3.569", "-0.049", "", ""],
|
|
6: ["-6.4", "-4.8", "-360", "360"],
|
|
7: ["1.0160", "-0.0010", "", ""],
|
|
8: ["4.763", "-0.051", "", ""],
|
|
9: ["157.2", "-4.9", "-360", "360"]
|
|
};
|
|
Object.keys(valuesByNumber).forEach(function(number) {
|
|
const target = parsed.parameterTargets[number] + "_2321";
|
|
const values = valuesByNumber[number];
|
|
modelAll["val" + target] = values[0];
|
|
modelAll["err" + target] = values[1];
|
|
modelAll["min" + target] = values[2];
|
|
modelAll["max" + target] = values[3];
|
|
});
|
|
const regenerated = lib.buildModelBlocks({
|
|
all: modelAll,
|
|
funOptions: [
|
|
{ value: "Asy", disabled: false },
|
|
{ value: "Sgm", disabled: false },
|
|
{ value: "Phi", disabled: false },
|
|
{ value: "Frq", disabled: true },
|
|
{ value: "NBg1", disabled: false },
|
|
{ value: "No1", disabled: false },
|
|
{ value: "NBg2", disabled: false },
|
|
{ value: "No2", disabled: true },
|
|
{ value: "PhiH2", disabled: false },
|
|
{ value: "Field", disabled: false },
|
|
{ value: "AlphaLR", disabled: false }
|
|
],
|
|
funLines: [
|
|
"Frq = gamma_mu * Field",
|
|
"No2 = No1 * AlphaLR"
|
|
],
|
|
sharedFlags: {}
|
|
}, 0);
|
|
assert.match(regenerated.theoryBlock, /^asymmetry\s+1$/m);
|
|
assert.match(regenerated.theoryBlock, /^simpleGss\s+2$/m);
|
|
assert.match(regenerated.theoryBlock, /^TFieldCos\s+map1\s+fun1$/m);
|
|
assert.match(regenerated.functionsBlock, /^fun1 = gamma_mu \* par\d+$/m);
|
|
assert.match(regenerated.functionsBlock, /^fun2 = par\d+ \* par\d+$/m);
|
|
assert.strictEqual((regenerated.runBlock.match(/^RUN\b/gm) || []).length, 2);
|
|
assert.match(regenerated.runBlock, /^norm\s+5$/m);
|
|
assert.match(regenerated.runBlock, /^norm\s+fun2$/m);
|
|
assert.match(regenerated.runBlock, /^map\s+3$/m);
|
|
assert.match(regenerated.runBlock, /^map\s+7$/m);
|
|
assert.doesNotMatch(regenerated.runBlock, /undefined/);
|
|
assert.match(regenerated.parameterBlock, /\bField_2321\s+304\.4\s+-1\.2\s+none\s+0\s+500/);
|
|
assert.match(regenerated.parameterBlock, /\bPhiH2_2321\s+157\.2\s+-4\.9\s+none\s+-360\s+360/);
|
|
|
|
const unsupported = lib.parseMSRTheoryModel([
|
|
"FITPARAMETER",
|
|
"1 Asy 0.2 0.01",
|
|
"THEORY",
|
|
"userFcn 1 7",
|
|
"GLOBAL",
|
|
"fittype 2"
|
|
].join("\n"));
|
|
assert.strictEqual(unsupported.ok, false);
|
|
assert.match(unsupported.error, /not represented uniquely/);
|
|
}
|
|
|
|
function testParseFunctionMappedDetectorPhases(lib) {
|
|
const msr = fs.readFileSync(
|
|
path.join(__dirname, "fixtures", "function-map-singlehist.msr"), "utf8"
|
|
);
|
|
const parsed = JSON.parse(JSON.stringify(lib.parseMSRTheoryModel(msr)));
|
|
|
|
assert.strictEqual(parsed.ok, true);
|
|
assert.deepStrictEqual(parsed.components, ["ExponentialCos"]);
|
|
assert.deepStrictEqual(parsed.constraints, [
|
|
{ target: "Phi", expression: "Ph + map1" },
|
|
{ target: "Frq", expression: "gamma_mu * Field" },
|
|
{ target: "No3", expression: "No1 * alpha_LR" },
|
|
{ target: "No4", expression: "No2 * alpha_TB" }
|
|
]);
|
|
assert.deepStrictEqual(parsed.functionMaps, {
|
|
1: {
|
|
target: "Phi",
|
|
parameters: ["RelPh_L", "RelPh_T", "RelPh_R", "RelPh_B"]
|
|
}
|
|
});
|
|
assert.deepStrictEqual(parsed.customParameters, [
|
|
"Ph", "Field", "RelPh_L", "RelPh_T", "RelPh_R",
|
|
"alpha_LR", "RelPh_B", "alpha_TB"
|
|
]);
|
|
|
|
const previousFunctionMaps = lib.All.msrFunctionMaps;
|
|
lib.All.msrFunctionMaps = parsed.functionMaps;
|
|
assert.strictEqual(lib.legalFunLine("Ph + map1", ["Ph"]), true);
|
|
if (previousFunctionMaps === undefined) delete lib.All.msrFunctionMaps;
|
|
else lib.All.msrFunctionMaps = previousFunctionMaps;
|
|
|
|
const all = {};
|
|
const previousDocument = lib.document;
|
|
lib.document = undefined;
|
|
assert.strictEqual(lib.applyMSRTheoryModel(parsed, all), 1);
|
|
lib.document = previousDocument;
|
|
assert.deepStrictEqual(JSON.parse(JSON.stringify(all.msrFunctionMaps)), parsed.functionMaps);
|
|
|
|
const modelAll = {
|
|
fitAsyType: "SingleHist",
|
|
numComps: "1",
|
|
fitFunc1: "ExponentialCos",
|
|
theoFunBlock: "asymmetry Asy\nsimplExpo Lam\nTFieldCos Phi Frq",
|
|
runNumbers: "2249",
|
|
selYear: "2026",
|
|
instrName: "LEM",
|
|
dataRoot: "/psi.ch/data",
|
|
LRBF: "21 25,22 26,23 27,24 28",
|
|
msrFunctionMaps: parsed.functionMaps
|
|
};
|
|
for (const parameter of lib.parseMSRParameters(msr)) {
|
|
const target = parsed.parameterTargets[parameter.order] + "_2249";
|
|
modelAll["val" + target] = parameter.value;
|
|
modelAll["err" + target] = parameter.error;
|
|
modelAll["min" + target] = parameter.min;
|
|
modelAll["max" + target] = parameter.max;
|
|
}
|
|
const regenerated = lib.buildModelBlocks({
|
|
all: modelAll,
|
|
funOptions: [
|
|
{ value: "Asy", disabled: false },
|
|
{ value: "Lam", disabled: false },
|
|
{ value: "Phi", disabled: true },
|
|
{ value: "Frq", disabled: true },
|
|
{ value: "NBg1", disabled: false },
|
|
{ value: "No1", disabled: false },
|
|
{ value: "NBg2", disabled: false },
|
|
{ value: "No2", disabled: false },
|
|
{ value: "NBg3", disabled: false },
|
|
{ value: "No3", disabled: true },
|
|
{ value: "NBg4", disabled: false },
|
|
{ value: "No4", disabled: true },
|
|
{ value: "Ph", disabled: false },
|
|
{ value: "Field", disabled: false },
|
|
{ value: "RelPh_L", disabled: false },
|
|
{ value: "RelPh_T", disabled: false },
|
|
{ value: "RelPh_R", disabled: false },
|
|
{ value: "alpha_LR", disabled: false },
|
|
{ value: "RelPh_B", disabled: false },
|
|
{ value: "alpha_TB", disabled: false }
|
|
],
|
|
funLines: [
|
|
"Phi = Ph + map1",
|
|
"Frq = gamma_mu * Field",
|
|
"No3 = No1 * alpha_LR",
|
|
"No4 = No2 * alpha_TB"
|
|
],
|
|
sharedFlags: {}
|
|
}, 0);
|
|
|
|
function parameterNumber(name) {
|
|
const match = regenerated.parameterBlock.match(
|
|
new RegExp("^\\s*(\\d+)\\s+" + name + "_2249\\b", "m")
|
|
);
|
|
assert.ok(match, "missing regenerated parameter " + name);
|
|
return match[1];
|
|
}
|
|
assert.match(regenerated.theoryBlock, /^TFieldCos\s+fun1\s+fun2$/m);
|
|
assert.match(regenerated.functionsBlock, /^fun1 = par\d+ \+ map1$/m);
|
|
assert.match(regenerated.functionsBlock, /^fun2 = gamma_mu \* par\d+$/m);
|
|
assert.match(regenerated.functionsBlock, /^fun3 = par\d+ \* par\d+$/m);
|
|
assert.match(regenerated.functionsBlock, /^fun4 = par\d+ \* par\d+$/m);
|
|
assert.deepStrictEqual(
|
|
Array.from(regenerated.runBlock.matchAll(/^map\s+(\d+)$/gm), function(match) {
|
|
return match[1];
|
|
}),
|
|
[
|
|
parameterNumber("RelPh_L"),
|
|
parameterNumber("RelPh_T"),
|
|
parameterNumber("RelPh_R"),
|
|
parameterNumber("RelPh_B")
|
|
]
|
|
);
|
|
assert.strictEqual((regenerated.runBlock.match(/^norm\s+fun3$/gm) || []).length, 1);
|
|
assert.strictEqual((regenerated.runBlock.match(/^norm\s+fun4$/gm) || []).length, 1);
|
|
assert.doesNotMatch(regenerated.runBlock, /undefined/);
|
|
}
|
|
|
|
function testFunctionExpressionValidation(lib) {
|
|
assert.strictEqual(lib.legalFunLine("pow(Asy, 2)", ["Asy"]), true);
|
|
assert.strictEqual(lib.legalFunLine("1e-3 * Asy", ["Asy"]), true);
|
|
assert.strictEqual(lib.legalFunLine("1E+3 / (Asy + .5)", ["Asy"]), true);
|
|
assert.strictEqual(lib.legalFunLine("B + En + T0", []), true);
|
|
|
|
const previousAlert = lib.alert;
|
|
let alertCount = 0;
|
|
lib.alert = function() {
|
|
alertCount += 1;
|
|
};
|
|
assert.strictEqual(lib.legalFunLine("Asy; unknown", ["Asy"]), false);
|
|
assert.strictEqual(alertCount, 1);
|
|
lib.alert = previousAlert;
|
|
}
|
|
|
|
function testParseMSRRunSelectionWithAddRun(lib) {
|
|
const msr = [
|
|
"GLOBAL",
|
|
"fittype 2",
|
|
"RUN /data/lem/his/2026/lem26_his_2321 MUE4 PSI MUSR-ROOT",
|
|
"ADDRUN /data/lem/his/2026/lem26_his_2322 MUE4 PSI MUSR-ROOT",
|
|
"forward 21 25",
|
|
"backward 23 27",
|
|
"RUN /data/lem/his/2026/lem26_his_2323 MUE4 PSI MUSR-ROOT",
|
|
"forward 21 25",
|
|
"backward 23 27",
|
|
"COMMANDS",
|
|
"MIGRAD"
|
|
].join("\n");
|
|
|
|
const parsed = JSON.parse(JSON.stringify(lib.parseMSRGUISettings(msr)));
|
|
assert.strictEqual(parsed.values.runNumbers, "2321+2322,2323");
|
|
assert.strictEqual(parsed.values.selYear, "2026");
|
|
assert.strictEqual(parsed.values.instrName, "LEM");
|
|
assert.strictEqual(parsed.values.fitAsyType, "AsymmetryGLB");
|
|
assert.strictEqual(parsed.values.LRBF, "21 25,23 27");
|
|
}
|
|
|
|
function testParseMSRT0BgRunBlocks(lib) {
|
|
const msr = [
|
|
"GLOBAL",
|
|
"fittype 2 (asymmetry fit)",
|
|
"RUN /data/run414 MUE4 PSI MUSR-ROOT",
|
|
"t0 101 102 # forward and backward",
|
|
"background 10 20 30 40 (background ranges)",
|
|
"RUN /data/run415 MUE4 PSI MUSR-ROOT",
|
|
"t0 103 104",
|
|
"backgr.fix 5.5 6.5",
|
|
"COMMANDS"
|
|
].join("\n");
|
|
|
|
const parsed = JSON.parse(JSON.stringify(lib.parseMSRT0BgRunBlocks(msr)));
|
|
|
|
assert.strictEqual(parsed.ok, true);
|
|
assert.strictEqual(parsed.fitType, 2);
|
|
assert.strictEqual(parsed.runBlocks.length, 2);
|
|
assert.deepStrictEqual(parsed.runBlocks[0].t0, ["101", "102"]);
|
|
assert.deepStrictEqual(parsed.runBlocks[0].background, ["10", "20", "30", "40"]);
|
|
assert.deepStrictEqual(parsed.runBlocks[1].backgrFix, ["5.5", "6.5"]);
|
|
}
|
|
|
|
function testSyncCommonAsymmetryT0BgFromMSR(lib) {
|
|
const all = {
|
|
fitAsyType: "AsymmetryGLB",
|
|
runNumbers: "414,415",
|
|
t0BgT0F_0: "stale",
|
|
t0BgBgMode_1: "fixed"
|
|
};
|
|
const msr = [
|
|
"GLOBAL",
|
|
"fittype 2",
|
|
"RUN /data/run414 MUE4 PSI MUSR-ROOT",
|
|
"t0 101 201 102 202",
|
|
"background 10 20 30 40",
|
|
"RUN /data/run415 MUE4 PSI MUSR-ROOT",
|
|
"t0 101 201 102 202",
|
|
"background 10 20 30 40",
|
|
"COMMANDS"
|
|
].join("\n");
|
|
|
|
assert.strictEqual(lib.syncT0BgFromMSR(msr, all), 1);
|
|
assert.strictEqual(all.t0BgCommonT0F, "101 102");
|
|
assert.strictEqual(all.t0BgCommonT0B, "201 202");
|
|
assert.strictEqual(all.t0BgCommonBgMode, "range");
|
|
assert.strictEqual(all.t0BgCommonBgFStart, "10");
|
|
assert.strictEqual(all.t0BgCommonBgBEnd, "40");
|
|
assert.strictEqual(all.t0BgT0F_0, undefined);
|
|
assert.strictEqual(all.t0BgBgMode_1, undefined);
|
|
}
|
|
|
|
function testSyncPerRunAsymmetryT0BgFromMSR(lib) {
|
|
const all = {
|
|
fitAsyType: "AsymmetryGLB",
|
|
runNumbers: "414,415"
|
|
};
|
|
const msr = [
|
|
"GLOBAL",
|
|
"fittype 2",
|
|
"RUN /data/run414 MUE4 PSI MUSR-ROOT",
|
|
"t0 101 102",
|
|
"background 10 20 30 40",
|
|
"RUN /data/run415 MUE4 PSI MUSR-ROOT",
|
|
"t0 103 104",
|
|
"backgr.fix 5.5 6.5",
|
|
"COMMANDS"
|
|
].join("\n");
|
|
|
|
assert.strictEqual(lib.syncT0BgFromMSR(msr, all), 1);
|
|
assert.strictEqual(all.t0BgCommonT0F, undefined);
|
|
assert.strictEqual(all.t0BgT0F_0, "101");
|
|
assert.strictEqual(all.t0BgT0B_1, "104");
|
|
assert.strictEqual(all.t0BgCommonBgMode, "auto");
|
|
assert.strictEqual(all.t0BgBgMode_0, "range");
|
|
assert.strictEqual(all.t0BgBgFEnd_0, "20");
|
|
assert.strictEqual(all.t0BgBgMode_1, "fixed");
|
|
assert.strictEqual(all.t0BgBgFixF_1, "5.5");
|
|
assert.strictEqual(all.t0BgBgFixB_1, "6.5");
|
|
}
|
|
|
|
function testSyncMultiGroupAsymmetryT0BgFromMSR(lib) {
|
|
const all = {
|
|
fitAsyType: "AsymmetryGLB",
|
|
numComps: "1",
|
|
LRBF: "1,2/3,4",
|
|
runNumbers: "2452"
|
|
};
|
|
// Two RUN blocks share one physical run; per-group values are space-separated.
|
|
const msr = [
|
|
"GLOBAL",
|
|
"fittype 2",
|
|
"RUN /data/run2452 MUE4 PSI MUSR-ROOT",
|
|
"t0 101 102",
|
|
"background 10 20 11 21",
|
|
"RUN /data/run2452 MUE4 PSI MUSR-ROOT",
|
|
"t0 103 104",
|
|
"background 30 40 31 41",
|
|
"COMMANDS"
|
|
].join("\n");
|
|
|
|
assert.strictEqual(lib.syncT0BgFromMSR(msr, all), 1);
|
|
assert.strictEqual(all.t0BgCommonT0F, undefined);
|
|
assert.strictEqual(all.t0BgT0F_0, "101 103");
|
|
assert.strictEqual(all.t0BgT0B_0, "102 104");
|
|
assert.strictEqual(all.t0BgBgMode_0, "range");
|
|
assert.strictEqual(all.t0BgBgFStart_0, "10 30");
|
|
assert.strictEqual(all.t0BgBgFEnd_0, "20 40");
|
|
assert.strictEqual(all.t0BgBgBStart_0, "11 31");
|
|
assert.strictEqual(all.t0BgBgBEnd_0, "21 41");
|
|
assert.strictEqual(all.t0BgCommonBgMode, undefined);
|
|
}
|
|
|
|
function testSyncMultiGroupAsymmetryCommonCollapse(lib) {
|
|
const all = {
|
|
fitAsyType: "AsymmetryGLB",
|
|
numComps: "1",
|
|
LRBF: "1,2/3,4",
|
|
runNumbers: "2452"
|
|
};
|
|
const msr = [
|
|
"GLOBAL",
|
|
"fittype 2",
|
|
"RUN /data/run2452 MUE4 PSI MUSR-ROOT",
|
|
"t0 110 220",
|
|
"backgr.fix 5.5 6.5",
|
|
"RUN /data/run2452 MUE4 PSI MUSR-ROOT",
|
|
"t0 110 220",
|
|
"backgr.fix 5.5 6.5",
|
|
"COMMANDS"
|
|
].join("\n");
|
|
|
|
assert.strictEqual(lib.syncT0BgFromMSR(msr, all), 1);
|
|
assert.strictEqual(all.t0BgCommonT0F, "110");
|
|
assert.strictEqual(all.t0BgCommonT0B, "220");
|
|
assert.strictEqual(all.t0BgCommonBgMode, "fixed");
|
|
assert.strictEqual(all.t0BgCommonBgFixF, "5.5");
|
|
assert.strictEqual(all.t0BgCommonBgFixB, "6.5");
|
|
assert.strictEqual(all.t0BgT0F_0, undefined);
|
|
assert.strictEqual(all.t0BgBgMode_0, undefined);
|
|
}
|
|
|
|
function testSyncMultiGroupAsymmetryRejectsPartial(lib) {
|
|
const all = {
|
|
fitAsyType: "AsymmetryGLB",
|
|
numComps: "1",
|
|
LRBF: "1,2/3,4",
|
|
runNumbers: "2452",
|
|
t0BgT0F_0: "stale-keep-me"
|
|
};
|
|
// Only the first group sets t0: a missing group token would shift the rest,
|
|
// so the whole read-back is refused and the GUI keeps its previous values.
|
|
const msr = [
|
|
"GLOBAL",
|
|
"fittype 2",
|
|
"RUN /data/run2452 MUE4 PSI MUSR-ROOT",
|
|
"t0 101 102",
|
|
"RUN /data/run2452 MUE4 PSI MUSR-ROOT",
|
|
"COMMANDS"
|
|
].join("\n");
|
|
|
|
assert.strictEqual(lib.syncT0BgFromMSR(msr, all), 0);
|
|
assert.strictEqual(all.t0BgT0F_0, "stale-keep-me");
|
|
|
|
// Differing background modes also cannot be stored in one GUI row.
|
|
const mixedMode = {
|
|
fitAsyType: "AsymmetryGLB",
|
|
numComps: "1",
|
|
LRBF: "1,2/3,4",
|
|
runNumbers: "2452",
|
|
t0BgBgMode_0: "stale-mode"
|
|
};
|
|
const mixedMsr = [
|
|
"GLOBAL",
|
|
"fittype 2",
|
|
"RUN /data/run2452 MUE4 PSI MUSR-ROOT",
|
|
"background 10 20 11 21",
|
|
"RUN /data/run2452 MUE4 PSI MUSR-ROOT",
|
|
"backgr.fix 5 6",
|
|
"COMMANDS"
|
|
].join("\n");
|
|
assert.strictEqual(lib.syncT0BgFromMSR(mixedMsr, mixedMode), 0);
|
|
assert.strictEqual(mixedMode.t0BgBgMode_0, "stale-mode");
|
|
}
|
|
|
|
function testSyncSingleHistogramT0BgFromMSR(lib) {
|
|
const all = {
|
|
fitAsyType: "SingleHist",
|
|
LRBF: "1 5,2,3"
|
|
};
|
|
const msr = [
|
|
"GLOBAL",
|
|
"fittype 0",
|
|
"RUN /data/run MUE4 PSI MUSR-ROOT",
|
|
"t0 100 100.5",
|
|
"backgr.fit 2",
|
|
"RUN /data/run MUE4 PSI MUSR-ROOT",
|
|
"t0 101",
|
|
"backgr.fit 4",
|
|
"RUN /data/run MUE4 PSI MUSR-ROOT",
|
|
"t0 102",
|
|
"backgr.fit 6",
|
|
"COMMANDS"
|
|
].join("\n");
|
|
|
|
assert.strictEqual(lib.syncT0BgFromMSR(msr, all), 1);
|
|
assert.strictEqual(all.t0BgCommonBgMode, "fit");
|
|
assert.strictEqual(all.t0BgBgMode_0, undefined);
|
|
assert.strictEqual(all.t0BgT0_0, "100 100.5");
|
|
assert.strictEqual(all.t0BgT0_1, "101");
|
|
assert.strictEqual(all.t0BgT0_2, "102");
|
|
}
|
|
|
|
function testGroupedAsymmetryT0Generation(lib) {
|
|
const lines = lib.buildT0BgRunLines({
|
|
t0BgCommonT0F: "101 102",
|
|
t0BgCommonT0B: "201 202",
|
|
t0BgCommonBgMode: "auto"
|
|
}, 2, 0);
|
|
|
|
assert.deepStrictEqual(JSON.parse(JSON.stringify(lines)), ["t0 101 201 102 202"]);
|
|
}
|
|
|
|
function testGroupedAsymmetryT0Validation(lib) {
|
|
const validation = lib.validateT0BgOverrides({
|
|
fitAsyType: "AsymmetryGLB",
|
|
runNumbers: "414",
|
|
LRBF: "1 5,3 7",
|
|
t0BgCommonT0F: "101 102",
|
|
t0BgCommonT0B: "201"
|
|
}, [414]);
|
|
|
|
assert.strictEqual(validation.ok, 0);
|
|
assert.match(validation.message, /same number of forward and backward values/);
|
|
}
|
|
|
|
function testSyncT0BgRejectsAmbiguousMSRWithoutMutation(lib) {
|
|
const all = {
|
|
fitAsyType: "AsymmetryGLB",
|
|
runNumbers: "414",
|
|
t0BgCommonT0F: "keep",
|
|
t0BgCommonT0B: "these"
|
|
};
|
|
const msr = [
|
|
"GLOBAL",
|
|
"fittype 2",
|
|
"RUN /data/run414 MUE4 PSI MUSR-ROOT",
|
|
"t0 101 102",
|
|
"t0 103 104",
|
|
"COMMANDS"
|
|
].join("\n");
|
|
|
|
assert.strictEqual(lib.syncT0BgFromMSR(msr, all), 0);
|
|
assert.strictEqual(all.t0BgCommonT0F, "keep");
|
|
assert.strictEqual(all.t0BgCommonT0B, "these");
|
|
}
|
|
|
|
function testGeneratedGlobalMSRValidation(lib) {
|
|
const validMSR = [
|
|
"FITPARAMETER",
|
|
"1 Alpha 1.0 0.01",
|
|
"THEORY",
|
|
"asymmetry 1",
|
|
"GLOBAL",
|
|
"fittype 2",
|
|
"RUN /data/run PSI PSI-BIN",
|
|
"COMMANDS",
|
|
"MIGRAD",
|
|
"PLOT 2",
|
|
"runs 1"
|
|
].join("\n");
|
|
|
|
const missingRunBlock = validMSR.replace(/^RUN .*$/m, "");
|
|
|
|
assert.strictEqual(lib.isValidGeneratedGlobalMSR(validMSR), true);
|
|
assert.strictEqual(lib.isValidGeneratedGlobalMSR(missingRunBlock), false);
|
|
assert.strictEqual(lib.isValidGeneratedGlobalMSR(""), false);
|
|
}
|
|
|
|
function testCurrentDataPathRules(lib) {
|
|
const root = "/psi.ch/group/lmu/public/data";
|
|
const cases = [
|
|
[514, "2025", "LEM", "lem/his/2025/lem25_his_0514", "MUE4", "MUSR-ROOT"],
|
|
[514, "2022", "GPS", "gps/his/2022/deltat_tdc_gps_0514", "PIM3", "PSI-BIN"],
|
|
[514, "2023", "GPS", "gps/his/2023/deltatbc_tdc_gps_2023_0514", "PIM3", "MUSR-ROOT"],
|
|
[514, "2008", "LTF", "ltf/his/2008/deltat_pta_ltf_0514", "PIM3", "PSI-BIN"],
|
|
[514, "2009", "LTF", "ltf/his/2009/deltat_tdc_ltf_0514", "PIM3", "PSI-BIN"],
|
|
[514, "2007", "Dolly", "dolly/his/2007/deltat_pta_dolly_0514", "PIE1", "PSI-BIN"],
|
|
[514, "2008", "Dolly", "dolly/his/2008/deltat_tdc_dolly_0514", "PIE1", "PSI-BIN"],
|
|
[514, "2023", "Dolly", "dolly/his/2023/deltat_tdc_dolly_2023_0514", "PIE1", "MUSR-ROOT"],
|
|
[514, "2025", "VMS", "vms/his/2025/deltat_tdc_vms_2025_0514", "PIE1", "MUSR-ROOT"],
|
|
[514, "2022", "GPD", "gpd/his/2022/deltat_tdc_gpd_0514", "PIE1", "PSI-BIN"],
|
|
[514, "2023", "GPD", "gpd/his/2023/deltatbc_tdc_gpd_2023_0514", "PIE1", "MUSR-ROOT"],
|
|
[514, "2022", "HAL", "hal/his/2022/tdc_hifi_2022_00514", "PIE3", "PSI-MDU"],
|
|
[514, "2023", "HAL", "hal/his/2023/deltat_tdc_hifi_2023_0514", "PIE3", "MUSR-ROOT"],
|
|
[514, "2025", "FLAME", "flame/his/2025/deltat_tdc_flame_2025_0514", "PIM3", "MUSR-ROOT"],
|
|
[514, "2026", "FLAME", "flame/his/2026/flame26_his_00514", "PIM3", "MUSR-ROOT"]
|
|
];
|
|
|
|
for (const entry of cases) {
|
|
const expected = ["RUN", root + "/" + entry[3], entry[4], "PSI", entry[5]].join(" ");
|
|
assert.strictEqual(lib.runFileNameAuto(entry[0], entry[1], entry[2], root), expected);
|
|
}
|
|
|
|
assert.strictEqual(
|
|
lib.runFileNameAuto(514, "2025", "LEM", "/psi.ch/data/"),
|
|
"RUN /psi.ch/data/lem/his/2025/lem25_his_0514 MUE4 PSI MUSR-ROOT"
|
|
);
|
|
}
|
|
|
|
function testLEMDefaultHistograms(lib) {
|
|
const elements = {
|
|
instrName: { value: "LEM" },
|
|
LRBF: { value: "" }
|
|
};
|
|
lib.document = {
|
|
getElementById: function(id) {
|
|
return elements[id] || null;
|
|
}
|
|
};
|
|
|
|
lib.defaultHistograms(1);
|
|
|
|
assert.strictEqual(elements.LRBF.value, "21 25,23 27");
|
|
}
|
|
|
|
function testNormalizeMSRPlotRuns(lib) {
|
|
const globalMSR = [
|
|
"FITPARAMETER",
|
|
"1 Alpha 1.0 0.01",
|
|
"THEORY",
|
|
"asymmetry 1",
|
|
"GLOBAL",
|
|
"fittype 2",
|
|
"RUN /data/run1 PSI PSI-BIN",
|
|
"RUN /data/run2 PSI PSI-BIN",
|
|
"COMMANDS",
|
|
"MIGRAD",
|
|
"PLOT 2",
|
|
"runs 1"
|
|
].join("\n");
|
|
|
|
const normalizedGlobal = lib.normalizeMSRPlotRuns(
|
|
globalMSR,
|
|
{ fitAsyType: "AsymmetryGLB" },
|
|
[414, 415, 416]
|
|
);
|
|
|
|
const singleHistMSR = [
|
|
"FITPARAMETER",
|
|
"1 No1 1.0 0.01",
|
|
"THEORY",
|
|
"simplExpo 1",
|
|
"GLOBAL",
|
|
"fittype 0",
|
|
"RUN /data/run PSI PSI-BIN",
|
|
"COMMANDS",
|
|
"MIGRAD",
|
|
"PLOT 0",
|
|
"runs 1"
|
|
].join("\n");
|
|
|
|
const normalizedSingleHist = lib.normalizeMSRPlotRuns(
|
|
singleHistMSR,
|
|
{ fitAsyType: "SingleHist", LRBF: "1,2,3,4" },
|
|
[514]
|
|
);
|
|
|
|
assert.match(normalizedGlobal, /^runs\s+1\s+2\s+3$/m);
|
|
assert.match(normalizedSingleHist, /^runs\s+1\s+2\s+3\s+4$/m);
|
|
}
|
|
|
|
function testFitParameterLineOmitsUnsetBounds(lib) {
|
|
assert.strictEqual(
|
|
lib.formatFitParameterLine(1, "Alpha", "1.0409", "0.0020", "none", "none"),
|
|
" 1 Alpha 1.0409 0.0020\n"
|
|
);
|
|
assert.strictEqual(
|
|
lib.formatFitParameterLine(2, "Asy", "0.22", "0.01", "", "0.33"),
|
|
" 2 Asy 0.22 0.01 none none 0.33\n"
|
|
);
|
|
assert.strictEqual(
|
|
lib.formatFitParameterLine(3, "Lam", "0.1", "0.01", "0", "none"),
|
|
" 3 Lam 0.1 0.01 none 0 none\n"
|
|
);
|
|
assert.strictEqual(
|
|
lib.formatFitParameterLine(4, "Frq", "1.5", "0.02", "0.5", "2.5"),
|
|
" 4 Frq 1.5 0.02 none 0.5 2.5\n"
|
|
);
|
|
}
|
|
|
|
function testBuildCommandsBlockFitOptions(lib) {
|
|
// The compact GUI defaults to MINIMIZE + HESSE with SAVE enabled. Missing
|
|
// fields intentionally use the same defaults for backward compatibility.
|
|
assert.strictEqual(
|
|
lib.buildCommandsBlock({}),
|
|
"MINIMIZE\nHESSE\nSAVE"
|
|
);
|
|
|
|
assert.strictEqual(
|
|
lib.buildCommandsBlock({
|
|
minType: "SIMPLEX",
|
|
errEst: "HESSE_MINOS",
|
|
minStrategy: "2",
|
|
fitObjective: "MAX_LIKELIHOOD",
|
|
saveMinuit: ""
|
|
}),
|
|
"STRATEGY 2\nMAX_LIKELIHOOD\nSIMPLEX\nHESSE\nMINOS"
|
|
);
|
|
|
|
assert.strictEqual(
|
|
lib.buildCommandsBlock({
|
|
minType: "MIGRAD",
|
|
errEst: "NONE",
|
|
saveMinuit: "1"
|
|
}),
|
|
"MIGRAD\nSAVE"
|
|
);
|
|
}
|
|
|
|
function testBuildFourierBlockOptions(lib) {
|
|
// Gauss is the GUI/default Fourier unit even when the caller omits fftUnits.
|
|
assert.match(lib.buildFourierBlock({}), /^units\s+Gauss$/m);
|
|
|
|
// Keep the GUI-to-MSR mapping deterministic: an unchecked checkbox must
|
|
// explicitly disable DC correction rather than inheriting a site default.
|
|
assert.strictEqual(
|
|
lib.buildFourierBlock({
|
|
fftUnits: "MHz",
|
|
fftPower: "12",
|
|
fftDCCorrect: "",
|
|
fftApod: "STRONG",
|
|
fftType: "power",
|
|
fftPhase: "8.5"
|
|
}),
|
|
"units MHz\n"
|
|
+ "fourier_power 12\n"
|
|
+ "dc-corrected FALSE\n"
|
|
+ "apodization STRONG\n"
|
|
+ "plot power\n"
|
|
+ "phase 8.5\n"
|
|
);
|
|
|
|
assert.match(
|
|
lib.buildFourierBlock({
|
|
fftUnits: "Tesla",
|
|
fftPower: "13",
|
|
fftDCCorrect: "1",
|
|
fftApod: "WEAK",
|
|
fftType: "real",
|
|
fftPhase: "0",
|
|
fftPhaseCorrection: "all",
|
|
fftxi: "0.25",
|
|
fftxf: "25"
|
|
}),
|
|
/units\s+Tesla\nfourier_power\s+13\ndc-corrected\s+TRUE[\s\S]*range_for_phase_correction all\nrange\s+0\.25 25\n/
|
|
);
|
|
|
|
// Advanced phase expressions are passed through verbatim. A custom automatic
|
|
// phase-correction interval maps to the native two-value directive.
|
|
assert.match(
|
|
lib.buildFourierBlock({
|
|
fftUnits: "MHz",
|
|
fftPower: "12",
|
|
fftApod: "STRONG",
|
|
fftType: "real_and_imag",
|
|
fftPhase: "par(7,5,16)",
|
|
fftPhaseCorrection: "custom",
|
|
fftPhaseCorrectionStart: "0.5",
|
|
fftPhaseCorrectionEnd: "12"
|
|
}),
|
|
/phase\s+par\(7,5,16\)\nrange_for_phase_correction 0\.5 12\n/
|
|
);
|
|
}
|
|
|
|
function testExactNameFitParameterSync(lib) {
|
|
const spec = {
|
|
parameters: [
|
|
{ name: "Lam_0514", value: "0.14", step: "0.014", min: "0", max: "5" },
|
|
{ name: "Lam_0520", value: "0.52", step: "0.052", min: "0", max: "6" },
|
|
{ name: "Stale_0999", value: "9", step: "0.9", min: "", max: "" }
|
|
]
|
|
};
|
|
const generated = [
|
|
"FITPARAMETER",
|
|
" 1 Lam_0514 0.1 0.01",
|
|
" 2 Lam_0520 0.1 0.01",
|
|
" 3 Internal 3.0 0.3",
|
|
"THEORY",
|
|
"simplExpo 1",
|
|
""
|
|
].join("\n");
|
|
const synced = lib.applyFitParameterSpecToMSR(generated, spec);
|
|
assert.match(synced, /Lam_0514\s+0\.14\s+0\.014\s+none\s+0\s+5/);
|
|
assert.match(synced, /Lam_0520\s+0\.52\s+0\.052\s+none\s+0\s+6/);
|
|
assert.match(synced, /Internal\s+3\.0\s+0\.3/);
|
|
assert.doesNotMatch(synced, /Stale_0999/);
|
|
}
|
|
|
|
function testBuildMSRUsesGUIParameterValues(lib) {
|
|
const config = {
|
|
all: {
|
|
fitAsyType: "AsymmetryGLB",
|
|
Tis: "0",
|
|
Tfs: "8",
|
|
BINS: "100",
|
|
minType: "MIGRAD",
|
|
errEst: "HESSE",
|
|
fftUnits: "MHz",
|
|
fftPower: "12",
|
|
fftApod: "STRONG",
|
|
fftType: "power",
|
|
fftPhase: "8.5",
|
|
Tpis: "",
|
|
Tpfs: "",
|
|
VIEWBINS: "",
|
|
theoFunBlock: "asymmetry Asy\nsimplExpo Lam",
|
|
runNumbers: "514",
|
|
selYear: "2024",
|
|
instrName: "LEM",
|
|
dataRoot: "/psi.ch/data",
|
|
LRBF: "1 5,3 7",
|
|
valAlpha: "1.234",
|
|
errAlpha: "0.012",
|
|
minAlpha: "0.5",
|
|
maxAlpha: "1.5",
|
|
valAsy: "0.198",
|
|
errAsy: "0.006",
|
|
minAsy: "0.0",
|
|
maxAsy: "0.4",
|
|
valLam: "0.777",
|
|
errLam: "0.011",
|
|
minLam: "0.0",
|
|
maxLam: "5.0"
|
|
},
|
|
funOptions: [
|
|
{ value: "Alpha", disabled: false },
|
|
{ value: "Asy", disabled: false },
|
|
{ value: "Lam", disabled: false }
|
|
],
|
|
funLines: [],
|
|
sharedFlags: {}
|
|
};
|
|
const msr = lib.buildMSR(config);
|
|
|
|
assert.match(msr, /\bAlpha_0514\s+1\.234\s+0\.012\s+none\s+0\.5\s+1\.5/);
|
|
assert.match(msr, /\bAsy_0514\s+0\.198\s+0\.006\s+none\s+0\.0\s+0\.4/);
|
|
assert.match(msr, /\bLam_0514\s+0\.777\s+0\.011\s+none\s+0\.0\s+5\.0/);
|
|
assert.match(msr, /^RUN\s+\/psi\.ch\/data\/lem\/his\/2024\/lem24_his_0514\s+MUE4\s+PSI\s+MUSR-ROOT$/m);
|
|
assert.doesNotMatch(msr, /\bAlpha_0514\s+1\.234\s+0\.012\s+0\.5\s+1\.5/);
|
|
assert.doesNotMatch(msr, /\bAsy_0514\s+0\.22\s+0\.0022/);
|
|
|
|
const model = lib.buildModelBlocks(config, lib.getFitType(config.all));
|
|
assert.match(model.parameterBlock, /\bLam_0514\s+0\.777\s+0\.011\s+none\s+0\.0\s+5\.0/);
|
|
assert.match(model.theoryBlock, /asymmetry 2/);
|
|
assert.match(model.runBlock, /alpha\s+1/);
|
|
}
|
|
|
|
function testImportedMSRParameterOrderAndComments(lib) {
|
|
// An imported MSR whose FITPARAMETER block lists parameters in an order that
|
|
// differs from the GUI catalogue order, with comment lines interleaved and a
|
|
// trailing group comment after the last parameter.
|
|
const importedMsr = [
|
|
"FITPARAMETER",
|
|
" 1 Lam_0514 0.777 0.011 none 0.0 5.0",
|
|
"# detector asymmetry",
|
|
" 2 Asy_0514 0.198 0.006 none 0.0 0.4",
|
|
"# the alpha funnel",
|
|
" 3 Alpha_0514 1.234 0.012 none 0.5 1.5",
|
|
"# trailing group comment",
|
|
"",
|
|
"THEORY",
|
|
"asymmetry 2",
|
|
"GLOBAL",
|
|
"fittype 2",
|
|
"RUN /data/run514 MUE4 PSI MUSR-ROOT",
|
|
"COMMANDS",
|
|
"MIGRAD",
|
|
"PLOT 2",
|
|
"runs 1 2",
|
|
""
|
|
].join("\n");
|
|
|
|
lib.captureImportedMSRFitParameterStructure(importedMsr);
|
|
assert.deepStrictEqual(
|
|
JSON.parse(JSON.stringify(lib.importedMSRFitParameterOrder)),
|
|
["Lam", "Asy", "Alpha"]
|
|
);
|
|
assert.deepStrictEqual(
|
|
JSON.parse(JSON.stringify(lib.importedMSRFitParameterComments)),
|
|
[
|
|
{ before: [], after: [] },
|
|
{ before: ["# detector asymmetry"], after: [] },
|
|
{ before: ["# the alpha funnel"], after: ["# trailing group comment"] }
|
|
]
|
|
);
|
|
|
|
// A dropdown currently in catalogue order must be reordered to the file's
|
|
// sequence (appending moves the option, mirroring a <select> node).
|
|
const funSel = {
|
|
options: [
|
|
{ value: "Alpha", disabled: false },
|
|
{ value: "Asy", disabled: false },
|
|
{ value: "Lam", disabled: false }
|
|
],
|
|
appendChild: function(option) {
|
|
const index = this.options.indexOf(option);
|
|
if (index >= 0) this.options.splice(index, 1);
|
|
this.options.push(option);
|
|
}
|
|
};
|
|
lib.applyImportedMSRParameterOrder(funSel);
|
|
assert.deepStrictEqual(
|
|
funSel.options.map(function(option) { return option.value; }),
|
|
["Lam", "Asy", "Alpha"]
|
|
);
|
|
|
|
// Regenerated MSR: the parameter block must replay the imported order and
|
|
// re-attach each comment above its parameter plus the trailing comment.
|
|
const config = {
|
|
all: {
|
|
fitAsyType: "AsymmetryGLB",
|
|
theoFunBlock: "asymmetry Asy",
|
|
runNumbers: "514",
|
|
selYear: "2024",
|
|
instrName: "LEM",
|
|
dataRoot: "/psi.ch/data",
|
|
LRBF: "1 5,3 7",
|
|
valAlpha: "1.234", errAlpha: "0.012", minAlpha: "0.5", maxAlpha: "1.5",
|
|
valAsy: "0.198", errAsy: "0.006", minAsy: "0.0", maxAsy: "0.4",
|
|
valLam: "0.777", errLam: "0.011", minLam: "0.0", maxLam: "5.0"
|
|
},
|
|
funOptions: [
|
|
{ value: "Lam", disabled: false },
|
|
{ value: "Asy", disabled: false },
|
|
{ value: "Alpha", disabled: false }
|
|
],
|
|
funLines: [],
|
|
sharedFlags: {}
|
|
};
|
|
const model = lib.buildModelBlocks(config, lib.getFitType(config.all));
|
|
|
|
assert.ok(model.parameterBlock.indexOf("Lam_0514") < model.parameterBlock.indexOf("Asy_0514"));
|
|
assert.ok(model.parameterBlock.indexOf("Asy_0514") < model.parameterBlock.indexOf("Alpha_0514"));
|
|
assert.ok(model.parameterBlock.indexOf("Lam_0514") < model.parameterBlock.indexOf("# detector asymmetry"));
|
|
assert.ok(model.parameterBlock.indexOf("# detector asymmetry") < model.parameterBlock.indexOf("Asy_0514"));
|
|
assert.ok(model.parameterBlock.indexOf("Asy_0514") < model.parameterBlock.indexOf("# the alpha funnel"));
|
|
assert.ok(model.parameterBlock.indexOf("# the alpha funnel") < model.parameterBlock.indexOf("Alpha_0514"));
|
|
assert.ok(model.parameterBlock.indexOf("Alpha_0514") < model.parameterBlock.indexOf("# trailing group comment"));
|
|
|
|
// Without a captured structure regeneration falls back to catalogue order
|
|
// and injects no comments.
|
|
lib.resetImportedMSRParameterStructure();
|
|
const catalogueConfig = Object.assign({}, JSON.parse(JSON.stringify(config)), {
|
|
funOptions: [
|
|
{ value: "Alpha", disabled: false },
|
|
{ value: "Asy", disabled: false },
|
|
{ value: "Lam", disabled: false }
|
|
]
|
|
});
|
|
const catalogueModel = lib.buildModelBlocks(catalogueConfig, lib.getFitType(catalogueConfig.all));
|
|
assert.ok(catalogueModel.parameterBlock.indexOf("Alpha_0514") < catalogueModel.parameterBlock.indexOf("Asy_0514"));
|
|
assert.ok(catalogueModel.parameterBlock.indexOf("Asy_0514") < catalogueModel.parameterBlock.indexOf("Lam_0514"));
|
|
assert.strictEqual(catalogueModel.parameterBlock.indexOf("# detector asymmetry"), -1);
|
|
assert.strictEqual(catalogueModel.parameterBlock.indexOf("# trailing group comment"), -1);
|
|
|
|
// collectFitConfigFromDOM applies the reorder before returning funOptions,
|
|
// so the series/single generation paths inherit the imported sequence.
|
|
lib.captureImportedMSRFitParameterStructure(importedMsr);
|
|
const select = {
|
|
options: [
|
|
{ value: "Alpha", disabled: false },
|
|
{ value: "Asy", disabled: false },
|
|
{ value: "Lam", disabled: false }
|
|
],
|
|
appendChild: function(option) {
|
|
const index = this.options.indexOf(option);
|
|
if (index >= 0) this.options.splice(index, 1);
|
|
this.options.push(option);
|
|
}
|
|
};
|
|
lib.document = {
|
|
getElementById: function(id) {
|
|
return id === "funSel" ? select : null;
|
|
},
|
|
getElementsByClassName: function() {
|
|
return [];
|
|
},
|
|
querySelectorAll: function() {
|
|
return [];
|
|
}
|
|
};
|
|
const collected = lib.collectFitConfigFromDOM();
|
|
assert.deepStrictEqual(
|
|
JSON.parse(JSON.stringify(collected.funOptions.map(function(option) { return option.value; }))),
|
|
["Lam", "Asy", "Alpha"]
|
|
);
|
|
delete lib.document;
|
|
lib.resetImportedMSRParameterStructure();
|
|
}
|
|
|
|
function testPhysicalParameterOrder(lib) {
|
|
function selectFrom(values) {
|
|
return {
|
|
options: values.map(function(value) {
|
|
return { value: value, disabled: false };
|
|
}),
|
|
appendChild: function(option) {
|
|
const index = this.options.indexOf(option);
|
|
if (index >= 0) this.options.splice(index, 1);
|
|
this.options.push(option);
|
|
}
|
|
};
|
|
}
|
|
function valuesOf(funSel) {
|
|
return funSel.options.map(function(option) { return option.value; });
|
|
}
|
|
|
|
// Asymmetry fit catalogue is [Alpha, Asy, Lam, Frq, Phi]; physical roles
|
|
// sort to asymmetry, damping, frequency, phase, and the fit-level alpha
|
|
// normalization last.
|
|
let funSel = selectFrom(["Alpha", "Asy", "Lam", "Phi", "Frq"]);
|
|
lib.applyPhysicalParameterOrder(funSel, []);
|
|
assert.deepStrictEqual(valuesOf(funSel), ["Asy", "Lam", "Frq", "Phi", "Alpha"]);
|
|
|
|
// Multi-group asymmetry: physical shared params, then phases, then the
|
|
// per-group alphas, One and ratios. Indices map to their base role.
|
|
funSel = selectFrom(["Asy1", "Alpha1", "Alpha2", "One", "r2", "Phi1", "Phi2", "Lam"]);
|
|
lib.applyPhysicalParameterOrder(funSel, []);
|
|
assert.deepStrictEqual(valuesOf(funSel), ["Asy1", "Lam", "Phi1", "Phi2", "Alpha1", "Alpha2", "One", "r2"]);
|
|
|
|
// SingleHistogram: N0/background belong to the detector histograms and come
|
|
// after the physical parameters and phases; the extra histogram phase PhiH2
|
|
// ranks with the physical Phi.
|
|
funSel = selectFrom(["Asy", "Lam", "Phi", "PhiH2", "NBg1", "No1", "NBg2", "No2"]);
|
|
lib.applyPhysicalParameterOrder(funSel, []);
|
|
assert.deepStrictEqual(valuesOf(funSel), ["Asy", "Lam", "Phi", "PhiH2", "NBg1", "No1", "NBg2", "No2"]);
|
|
|
|
// Multiple components keep their block order: role sequence within one
|
|
// component, components in ascending order (Asy1 Lam1 Frq1 Phi1 then
|
|
// Asy2 Lam2 Frq2 Phi2), never role-major across components.
|
|
funSel = selectFrom(["Asy1", "Lam1", "Phi1", "Frq1", "Asy2", "Lam2", "Phi2", "Frq2"]);
|
|
lib.applyPhysicalParameterOrder(funSel, []);
|
|
assert.deepStrictEqual(valuesOf(funSel), ["Asy1", "Lam1", "Frq1", "Phi1", "Asy2", "Lam2", "Frq2", "Phi2"]);
|
|
|
|
// Three components mixing two physics models stay grouped per component.
|
|
funSel = selectFrom(["Asy1", "Sgm1", "Phi1", "Frq1", "Asy2", "Sgm2", "Phi2", "Frq2", "Asy3", "Sgm3", "Phi3", "Frq3"]);
|
|
lib.applyPhysicalParameterOrder(funSel, []);
|
|
assert.deepStrictEqual(valuesOf(funSel), ["Asy1", "Sgm1", "Frq1", "Phi1", "Asy2", "Sgm2", "Frq2", "Phi2", "Asy3", "Sgm3", "Frq3", "Phi3"]);
|
|
|
|
// A fit-level alpha normalization belongs to the whole fit and follows
|
|
// every component block rather than the first component only.
|
|
funSel = selectFrom(["Alpha", "Asy1", "Lam1", "Frq1", "Phi1", "Asy2", "Lam2", "Frq2", "Phi2"]);
|
|
lib.applyPhysicalParameterOrder(funSel, []);
|
|
assert.deepStrictEqual(valuesOf(funSel), ["Asy1", "Lam1", "Frq1", "Phi1", "Asy2", "Lam2", "Frq2", "Phi2", "Alpha"]);
|
|
|
|
// A custom parameter introduced through a constraint inherits the rank of
|
|
// the built-in parameter it is linked to (Bfield placed with Frq).
|
|
funSel = selectFrom(["Asy", "Lam", "Frq", "Bfield", "Alpha"]);
|
|
lib.applyPhysicalParameterOrder(funSel, ["Frq = gamma_mu * Bfield"]);
|
|
assert.deepStrictEqual(valuesOf(funSel), ["Asy", "Lam", "Frq", "Bfield", "Alpha"]);
|
|
|
|
// When a parameter is linked to several built-ins it ranks with the
|
|
// highest-priority (lowest rank) of them, here Lam over Frq.
|
|
funSel = selectFrom(["Asy", "Lam", "Frq", "Bfield", "Phi"]);
|
|
lib.applyPhysicalParameterOrder(funSel, ["Bfield = gamma_mu * Frq", "Bfield = B_LF * Lam"]);
|
|
assert.deepStrictEqual(valuesOf(funSel), ["Asy", "Lam", "Bfield", "Frq", "Phi"]);
|
|
|
|
// Unlinked custom parameters stay in catalogue order at the end.
|
|
funSel = selectFrom(["Asy", "Lam", "CustomA", "CustomB"]);
|
|
lib.applyPhysicalParameterOrder(funSel, []);
|
|
assert.deepStrictEqual(valuesOf(funSel), ["Asy", "Lam", "CustomA", "CustomB"]);
|
|
|
|
// An imported order always wins over the physical ordering.
|
|
lib.importedMSRFitParameterOrder = ["Alpha", "Frq", "Lam", "Asy", "Phi"];
|
|
funSel = selectFrom(["Alpha", "Asy", "Lam", "Phi", "Frq"]);
|
|
lib.applyPhysicalParameterOrder(funSel, []);
|
|
assert.deepStrictEqual(valuesOf(funSel), ["Alpha", "Asy", "Lam", "Phi", "Frq"]);
|
|
lib.resetImportedMSRParameterStructure();
|
|
|
|
// collectFitConfigFromDOM applies the physical reorder before building
|
|
// funOptions, so generated MSR blocks follow the role sequence; the
|
|
// constraint lines are read from the DOM funLine elements.
|
|
const select = selectFrom(["Alpha", "Asy", "Lam", "Frq", "Bfield"]);
|
|
lib.document = {
|
|
getElementById: function(id) {
|
|
return id === "funSel" ? select : null;
|
|
},
|
|
getElementsByClassName: function() {
|
|
return [{ innerText: "Frq = gamma_mu * Bfield" }];
|
|
},
|
|
querySelectorAll: function() {
|
|
return [];
|
|
}
|
|
};
|
|
const collected = lib.collectFitConfigFromDOM();
|
|
assert.deepStrictEqual(
|
|
JSON.parse(JSON.stringify(collected.funOptions.map(function(option) { return option.value; }))),
|
|
["Asy", "Lam", "Frq", "Bfield", "Alpha"]
|
|
);
|
|
delete lib.document;
|
|
}
|
|
|
|
function testSingleHistogramPlotRunsAllHistograms(lib) {
|
|
const plotBlock = lib.buildPlotBlock({
|
|
fitAsyType: "SingleHist",
|
|
LRBF: "1,2,3,4",
|
|
Tpis: "",
|
|
Tpfs: "",
|
|
VIEWBINS: "",
|
|
plotLogX: "1",
|
|
plotLogY: "1",
|
|
plotLifetimeCorrection: "1"
|
|
}, 0);
|
|
|
|
assert.match(plotBlock, /^PLOT\s+0/m);
|
|
assert.match(plotBlock, /^runs\s+1\s+2\s+3\s+4$/m);
|
|
assert.match(plotBlock, /^logx$/m);
|
|
assert.match(plotBlock, /^logy$/m);
|
|
assert.match(plotBlock, /^lifetimecorrection$/m);
|
|
}
|
|
|
|
function testPlotYAxisRangeGeneration(lib) {
|
|
const explicitRanges = lib.buildPlotBlock({
|
|
fitAsyType: "AsymmetryGLB",
|
|
LRBF: "1,2",
|
|
Tis: "0.05",
|
|
Tfs: "10",
|
|
Tpis: "0",
|
|
Tpfs: "8",
|
|
Ypis: "-0.25",
|
|
Ypfs: "0.25",
|
|
VIEWBINS: ""
|
|
}, 2, 1);
|
|
assert.match(explicitRanges, /^range\s+0\s+8\s+-0\.25\s+0\.25$/m);
|
|
|
|
const automaticTime = lib.buildPlotBlock({
|
|
fitAsyType: "AsymmetryGLB",
|
|
LRBF: "1,2",
|
|
Tis: "0.05",
|
|
Tfs: "10",
|
|
Tpis: "",
|
|
Tpfs: "",
|
|
Ypis: "-0.1",
|
|
Ypfs: "0.3",
|
|
VIEWBINS: ""
|
|
}, 2, 1);
|
|
assert.match(automaticTime, /^range\s+0\.05\s+10\s+-0\.1\s+0\.3$/m);
|
|
assert.doesNotMatch(automaticTime, /^use_fit_ranges$/m);
|
|
|
|
const noYAxisRange = lib.parseMSRGUISettings([
|
|
"GLOBAL",
|
|
"fittype 2",
|
|
"PLOT 2",
|
|
"range 0 8"
|
|
].join("\n"));
|
|
assert.strictEqual(noYAxisRange.values.Ypis, "");
|
|
assert.strictEqual(noYAxisRange.values.Ypfs, "");
|
|
}
|
|
|
|
function testRRFPlotIsExplicitlyOptIn(lib) {
|
|
// RRF uses the native G token for the Gauss default.
|
|
assert.strictEqual(lib.getRRFPlotConfig({
|
|
rrfEnabled: "1", rrfFrq: "13.9", rrfPack: "75"
|
|
}).units, "G");
|
|
|
|
const enabled = lib.buildPlotBlock({
|
|
fitAsyType: "AsymmetryGLB",
|
|
LRBF: "1 5,3 7",
|
|
Tpis: "",
|
|
Tpfs: "",
|
|
VIEWBINS: "",
|
|
rrfEnabled: "1",
|
|
rrfFrq: "13.9",
|
|
rrfUnits: "MHz",
|
|
rrfPack: "75",
|
|
rrfPhase: "par4"
|
|
}, 2, 1);
|
|
assert.match(enabled, /^rrf_freq\s+13\.9\s+MHz$/m);
|
|
assert.match(enabled, /^rrf_packing\s+75$/m);
|
|
assert.match(enabled, /^rrf_phase\s+par4$/m);
|
|
|
|
// Values left in the dormant controls must not leak into generated MSRs.
|
|
const disabled = lib.buildPlotBlock({
|
|
fitAsyType: "AsymmetryGLB",
|
|
LRBF: "1 5,3 7",
|
|
Tpis: "",
|
|
Tpfs: "",
|
|
VIEWBINS: "",
|
|
rrfEnabled: "",
|
|
rrfFrq: "13.9",
|
|
rrfUnits: "MHz",
|
|
rrfPack: "75",
|
|
rrfPhase: "12"
|
|
}, 2, 1);
|
|
assert.doesNotMatch(disabled, /^rrf_/m);
|
|
|
|
// Frequency and packing are required when enabled, whereas phase is optional.
|
|
assert.strictEqual(lib.getRRFPlotConfig({
|
|
rrfEnabled: "1", rrfFrq: "13.9", rrfUnits: "MHz", rrfPack: ""
|
|
}).valid, false);
|
|
const noPhase = lib.buildRRFPlotLines({
|
|
rrfEnabled: "1", rrfFrq: "13.9", rrfUnits: "MHz", rrfPack: "75", rrfPhase: ""
|
|
});
|
|
assert.strictEqual(Array.from(noPhase).some(function(line) { return /^rrf_phase/.test(line); }), false);
|
|
}
|
|
|
|
function testFFTAndRRFSettingsLiveInRuns(lib) {
|
|
const html = fs.readFileSync(path.join(repoRoot, "index.html"), "utf8");
|
|
const runsStart = html.indexOf('id="Runs"');
|
|
const paramsStart = html.indexOf('id="Params"');
|
|
const runsHtml = html.slice(runsStart, paramsStart);
|
|
const runsHeader = runsHtml.indexOf('<b>Runs</b>');
|
|
const fitRangeStart = runsHtml.indexOf('<b>Fit range</b>');
|
|
const fitModelStart = runsHtml.indexOf('<b>Fit model</b>');
|
|
const fitOptionsStart = runsHtml.indexOf('<b>Fit options</b>');
|
|
const viewStart = runsHtml.indexOf('<b>View settings</b>');
|
|
const fitRangeHtml = runsHtml.slice(fitRangeStart, fitModelStart);
|
|
const plotPos = runsHtml.indexOf('id="plotOptionsDisclosure"');
|
|
const fftPos = runsHtml.indexOf('id="fftOptionsDisclosure"');
|
|
const rrfPos = runsHtml.indexOf('id="rrfOptionsDisclosure"');
|
|
|
|
// Plot/FFT/RRF are sibling selectors inside Runs and reuse one inline panel;
|
|
// Plot is no longer part of the Fit range row.
|
|
assert.doesNotMatch(html, /id="btnFFTRRF"/);
|
|
assert.doesNotMatch(html, /id="FFTRRF"\s+class="tabcontent"/);
|
|
assert.doesNotMatch(fitRangeHtml, /id="plotOptionsDisclosure"/);
|
|
// Runs should follow the workflow order: selection, range, model, options, view.
|
|
assert.ok(runsHeader < fitRangeStart);
|
|
assert.ok(fitRangeStart < fitModelStart);
|
|
assert.ok(fitModelStart < fitOptionsStart);
|
|
assert.ok(fitOptionsStart < viewStart);
|
|
assert.ok(plotPos > viewStart);
|
|
assert.ok(plotPos < fftPos && fftPos < rrfPos);
|
|
assert.match(runsHtml, /id="viewSettingsPanel"[^>]*hidden/);
|
|
assert.match(runsHtml, /id="plotOptionsPanel"[^>]*hidden/);
|
|
assert.match(runsHtml, /id="Ypis"[^>]*aria-label="Plot Y minimum"/);
|
|
assert.match(runsHtml, /id="Ypfs"[^>]*aria-label="Plot Y maximum"/);
|
|
assert.match(runsHtml, /id="fftOptionsPanel"[^>]*hidden/);
|
|
assert.match(runsHtml, /id="rrfOptionsPanel"[^>]*hidden/);
|
|
assert.doesNotMatch(runsHtml, /id="plotSettingsTime"/);
|
|
assert.match(runsHtml, /selected="selected"\s+value="Gauss"/);
|
|
assert.match(runsHtml, /selected="selected"\s+value="G">Gauss/);
|
|
}
|
|
|
|
function testViewSettingsDismissOnOutsideClick(lib) {
|
|
function button() {
|
|
const attrs = { "aria-expanded": "false" };
|
|
return {
|
|
getAttribute: function(name) { return attrs[name]; },
|
|
setAttribute: function(name, value) { attrs[name] = value; },
|
|
contains: function(target) { return target === this; }
|
|
};
|
|
}
|
|
const elements = {
|
|
plotOptionsDisclosure: button(),
|
|
fftOptionsDisclosure: button(),
|
|
rrfOptionsDisclosure: button(),
|
|
plotOptionsPanel: { hidden: true },
|
|
fftOptionsPanel: { hidden: true },
|
|
rrfOptionsPanel: { hidden: true },
|
|
viewSettingsPanel: {
|
|
hidden: true,
|
|
contains: function(target) { return target === this; }
|
|
}
|
|
};
|
|
let listener = null;
|
|
lib.document = {
|
|
getElementById: function(id) { return elements[id] || null; },
|
|
addEventListener: function(type, callback) {
|
|
if (type === "click") listener = callback;
|
|
}
|
|
};
|
|
|
|
// One shared panel is reused: selecting FFT replaces Plot instead of opening
|
|
// a second popup, and selecting the active group again collapses the panel.
|
|
lib.toggleViewSettingsPanel("plot");
|
|
assert.strictEqual(elements.viewSettingsPanel.hidden, false);
|
|
assert.strictEqual(elements.plotOptionsPanel.hidden, false);
|
|
assert.strictEqual(elements.plotOptionsDisclosure.getAttribute("aria-expanded"), "true");
|
|
|
|
lib.toggleViewSettingsPanel("fft");
|
|
assert.strictEqual(elements.plotOptionsPanel.hidden, true);
|
|
assert.strictEqual(elements.fftOptionsPanel.hidden, false);
|
|
assert.strictEqual(elements.plotOptionsDisclosure.getAttribute("aria-expanded"), "false");
|
|
assert.strictEqual(elements.fftOptionsDisclosure.getAttribute("aria-expanded"), "true");
|
|
|
|
lib.configureViewSettingsDisclosures();
|
|
assert.strictEqual(typeof listener, "function");
|
|
|
|
// Clicking inside the shared panel keeps it open; clicking elsewhere closes it.
|
|
listener({ target: elements.viewSettingsPanel });
|
|
assert.strictEqual(elements.viewSettingsPanel.hidden, false);
|
|
listener({ target: {} });
|
|
assert.strictEqual(elements.viewSettingsPanel.hidden, true);
|
|
assert.strictEqual(elements.fftOptionsDisclosure.getAttribute("aria-expanded"), "false");
|
|
}
|
|
|
|
function testRRFControlsLiveInDisclosure(lib) {
|
|
const html = fs.readFileSync(path.join(repoRoot, "index.html"), "utf8");
|
|
assert.match(html, /id="rrfOptionsDisclosure"/);
|
|
assert.match(html, /id="rrfEnabled"[^>]*type="checkbox"|type="checkbox"[^>]*id="rrfEnabled"/);
|
|
assert.doesNotMatch(html, /id="rrfSettings"[^>]*hidden/);
|
|
assert.match(html, /id="rrfFrq"[^>]*required/);
|
|
assert.match(html, /id="rrfPack"[^>]*required/);
|
|
}
|
|
|
|
function testLifetimeCorrectionRequiresSingleHistogram(lib) {
|
|
const plotBlock = lib.buildPlotBlock({
|
|
fitAsyType: "AsymmetryGLB",
|
|
LRBF: "1 5,3 7",
|
|
Tpis: "",
|
|
Tpfs: "",
|
|
VIEWBINS: "",
|
|
plotLifetimeCorrection: "1"
|
|
}, 2);
|
|
|
|
assert.doesNotMatch(plotBlock, /^lifetimecorrection$/m);
|
|
}
|
|
|
|
function testNaNParameterErrorsNeverReachMSROrGUI(lib) {
|
|
// A failed fit (e.g. a non-converged error matrix) writes NaN into parameter
|
|
// steps. Reading such a file must turn each NaN step into 1% of the value.
|
|
const msr = [
|
|
"FITPARAMETER",
|
|
" 1 Asy 0.10 nan",
|
|
" 2 Lam 0.5 NaN",
|
|
" 3 Frq 1.3 0.02",
|
|
" 4 Alpha 1.0 0",
|
|
" 5 NaNVal nan 0.1",
|
|
" 6 NaNBounds 2.0 0.2 none nan inf",
|
|
"THEORY",
|
|
"asymmetry 1",
|
|
"GLOBAL",
|
|
"fittype 2"
|
|
].join("\n");
|
|
const params = lib.parseMSRParameters(msr);
|
|
assert.strictEqual(params[0].error, "0.001");
|
|
assert.strictEqual(params[1].error, "0.005");
|
|
assert.strictEqual(params[2].error, "0.02");
|
|
assert.strictEqual(params[3].error, "0");
|
|
assert.strictEqual(params[4].value, "");
|
|
assert.deepStrictEqual(params[5].min, "");
|
|
assert.deepStrictEqual(params[5].max, "");
|
|
|
|
// Generation never writes NaN even if All carries a stale NaN step.
|
|
const blocks = lib.buildModelBlocks({
|
|
all: {
|
|
fitAsyType: "AsymmetryGLB",
|
|
LRBF: "21 25,23 27",
|
|
theoFunBlock: "asymmetry Asy\nsimplExpo Lam",
|
|
runNumbers: "2452",
|
|
selYear: "2026",
|
|
instrName: "LEM",
|
|
dataRoot: "/mnt/fake",
|
|
valAsy_2452: "0.1",
|
|
errAsy_2452: "nan",
|
|
valLam_2452: "0.5",
|
|
errLam_2452: "NaN"
|
|
},
|
|
funOptions: [
|
|
{ value: "Alpha", disabled: false },
|
|
{ value: "Asy", disabled: false },
|
|
{ value: "Lam", disabled: false }
|
|
],
|
|
funLines: [],
|
|
sharedFlags: {}
|
|
}, 2);
|
|
assert.match(blocks.parameterBlock, /^ 2 Asy_2452 0.1 0.001$/m);
|
|
assert.match(blocks.parameterBlock, /^ 3 Lam_2452 0.5 0.005$/m);
|
|
assert.doesNotMatch(blocks.parameterBlock, /nan/i);
|
|
|
|
// A NaN value or boundary in All must fall back to the catalogue default
|
|
// instead of leaking into the generated MSR.
|
|
const defaultCleansed = lib.buildModelBlocks({
|
|
all: {
|
|
fitAsyType: "AsymmetryGLB",
|
|
LRBF: "21 25,23 27",
|
|
theoFunBlock: "asymmetry Asy\nsimplExpo Lam",
|
|
runNumbers: "2452",
|
|
selYear: "2026",
|
|
instrName: "LEM",
|
|
dataRoot: "/mnt/fake",
|
|
valAsy_2452: "nan",
|
|
errAsy_2452: "nan",
|
|
valLam_2452: "0.5",
|
|
errLam_2452: "NaN",
|
|
minLam_2452: "NaN",
|
|
maxLam_2452: "inf"
|
|
},
|
|
funOptions: [
|
|
{ value: "Alpha", disabled: false },
|
|
{ value: "Asy", disabled: false },
|
|
{ value: "Lam", disabled: false }
|
|
],
|
|
funLines: [],
|
|
sharedFlags: {}
|
|
}, 2);
|
|
assert.match(defaultCleansed.parameterBlock, /^ 2 Asy_2452 0.22 0.0022$/m);
|
|
assert.match(defaultCleansed.parameterBlock, /^ 3 Lam_2452 0.5 0.005$/m);
|
|
assert.doesNotMatch(defaultCleansed.parameterBlock, /nan/i);
|
|
assert.doesNotMatch(defaultCleansed.parameterBlock, /inf/i);
|
|
|
|
// The GUI parameter table never carries NaN either.
|
|
lib.All = {};
|
|
const elements = {
|
|
valAsy_2452: { value: "" },
|
|
errAsy_2452: { value: "" },
|
|
minAsy_2452: { value: "" },
|
|
maxAsy_2452: { value: "" },
|
|
valFrq_2452: { value: "" },
|
|
errFrq_2452: { value: "" },
|
|
minFrq_2452: { value: "" },
|
|
maxFrq_2452: { value: "" }
|
|
};
|
|
lib.document = {
|
|
getElementById: function(id) {
|
|
return elements[id] || null;
|
|
}
|
|
};
|
|
lib.cfgParams = function() {};
|
|
lib.applyMSRParametersToGUI([
|
|
{ order: "1", name: "Asy_2452", value: "0.1", error: "nan", min: "", max: "" },
|
|
{ order: "2", name: "Frq_2452", value: "1.3", error: "0.02", min: "NaN", max: "inf" }
|
|
]);
|
|
assert.strictEqual(lib.All.errAsy_2452, "0.001");
|
|
assert.strictEqual(elements.errAsy_2452.value, "0.001");
|
|
assert.strictEqual(lib.All.minFrq_2452, "");
|
|
assert.strictEqual(lib.All.maxFrq_2452, "");
|
|
assert.strictEqual(elements.minFrq_2452.value, "");
|
|
assert.strictEqual(elements.maxFrq_2452.value, "");
|
|
|
|
// A NaN value read from an MSR keeps the GUI's last good value.
|
|
lib.All.valAsy_2452 = "0.9";
|
|
lib.applyMSRParametersToGUI([
|
|
{ order: "1", name: "Asy_2452", value: "NaN", error: "NaN", min: "", max: "" }
|
|
]);
|
|
assert.strictEqual(lib.All.valAsy_2452, "0.9");
|
|
assert.strictEqual(lib.All.errAsy_2452, "0.009");
|
|
assert.strictEqual(elements.valAsy_2452.value, "0.9");
|
|
}
|
|
|
|
function testParseLifetimeCorrectionDefaultsOnForSingleHist(lib) {
|
|
// A single-histogram MSR whose PLOT block omits the directive must still
|
|
// restore the GUI default to ON.
|
|
const singleHist = [
|
|
"GLOBAL",
|
|
"fittype 0 (single histogram fit)",
|
|
"PLOT 0",
|
|
"runs 1"
|
|
].join("\n");
|
|
const parsedSingle = JSON.parse(JSON.stringify(lib.parseMSRGUISettings(singleHist)));
|
|
assert.strictEqual(parsedSingle.ok, true);
|
|
assert.strictEqual(parsedSingle.values.fitAsyType, "SingleHist");
|
|
assert.strictEqual(parsedSingle.values.plotLifetimeCorrection, true);
|
|
|
|
const all = {};
|
|
lib.document = undefined;
|
|
assert.strictEqual(lib.applyMSRGUISettings(parsedSingle, all), 1);
|
|
assert.strictEqual(all.plotLifetimeCorrection, "1");
|
|
|
|
// An explicit directive stays honoured.
|
|
const explicit = [
|
|
"GLOBAL",
|
|
"fittype 0 (single histogram fit)",
|
|
"PLOT 0",
|
|
"lifetimecorrection",
|
|
"runs 1"
|
|
].join("\n");
|
|
const parsedExplicit = JSON.parse(JSON.stringify(lib.parseMSRGUISettings(explicit)));
|
|
assert.strictEqual(parsedExplicit.values.plotLifetimeCorrection, true);
|
|
|
|
// A non-single-histogram fit does not default to lifetime correction.
|
|
const asymmetry = [
|
|
"GLOBAL",
|
|
"fittype 2 (asymmetry global fit)",
|
|
"PLOT 2",
|
|
"runs 1"
|
|
].join("\n");
|
|
const parsedAsymmetry = JSON.parse(JSON.stringify(lib.parseMSRGUISettings(asymmetry)));
|
|
assert.strictEqual(parsedAsymmetry.values.fitAsyType, "AsymmetryGLB");
|
|
assert.strictEqual(parsedAsymmetry.values.plotLifetimeCorrection, false);
|
|
}
|
|
|
|
function testT0BgOverridesInAsymmetryRunBlock(lib) {
|
|
const config = {
|
|
all: {
|
|
fitAsyType: "AsymmetryGLB",
|
|
LRBF: "1 5,3 7",
|
|
runNumbers: "414",
|
|
selYear: "2024",
|
|
instrName: "LEM",
|
|
theoFunBlock: "asymmetry Asy",
|
|
t0BgT0F_0: "1618.0",
|
|
t0BgT0B_0: "1628.0",
|
|
t0BgBgMode_0: "range",
|
|
t0BgBgFStart_0: "10",
|
|
t0BgBgFEnd_0: "20",
|
|
t0BgBgBStart_0: "30",
|
|
t0BgBgBEnd_0: "40"
|
|
},
|
|
funOptions: [
|
|
{ value: "Alpha", disabled: false },
|
|
{ value: "Asy", disabled: false }
|
|
],
|
|
funLines: [],
|
|
sharedFlags: {}
|
|
};
|
|
|
|
const model = lib.buildModelBlocks(config, lib.getFitType(config.all));
|
|
|
|
assert.match(model.runBlock, /^t0\s+1618\.0\s+1628\.0$/m);
|
|
assert.match(model.runBlock, /^background\s+10\s+20\s+30\s+40$/m);
|
|
}
|
|
|
|
function testT0BgOverridesInSingleHistogramRunBlock(lib) {
|
|
const baseConfig = {
|
|
all: {
|
|
fitAsyType: "SingleHist",
|
|
LRBF: "2",
|
|
runNumbers: "514",
|
|
selYear: "2024",
|
|
instrName: "LEM",
|
|
theoFunBlock: "asymmetry Asy"
|
|
},
|
|
funOptions: [
|
|
{ value: "No1", disabled: false },
|
|
{ value: "NBg1", disabled: false },
|
|
{ value: "Asy", disabled: false }
|
|
],
|
|
funLines: [],
|
|
sharedFlags: {}
|
|
};
|
|
|
|
let defaultModel = lib.buildModelBlocks(baseConfig, lib.getFitType(baseConfig.all));
|
|
assert.match(defaultModel.runBlock, /^backgr\.fit\s+2$/m);
|
|
|
|
baseConfig.all.t0BgT0_0 = "1618.0";
|
|
baseConfig.all.t0BgBgMode_0 = "fixed";
|
|
baseConfig.all.t0BgBgFix_0 = "10.5";
|
|
|
|
let fixedModel = lib.buildModelBlocks(baseConfig, lib.getFitType(baseConfig.all));
|
|
assert.match(fixedModel.runBlock, /^t0\s+1618\.0$/m);
|
|
assert.match(fixedModel.runBlock, /^backgr\.fix\s+10\.5$/m);
|
|
assert.doesNotMatch(fixedModel.runBlock, /^backgr\.fit/m);
|
|
}
|
|
|
|
function testCommonT0BgValuesApplyToRunBlocks(lib) {
|
|
const config = {
|
|
all: {
|
|
fitAsyType: "AsymmetryGLB",
|
|
LRBF: "1 5,3 7",
|
|
runNumbers: "414",
|
|
selYear: "2024",
|
|
instrName: "LEM",
|
|
theoFunBlock: "asymmetry Asy",
|
|
t0BgCommonT0F: "1600",
|
|
t0BgCommonT0B: "1601",
|
|
t0BgCommonBgMode: "fixed",
|
|
t0BgCommonBgFixF: "11",
|
|
t0BgCommonBgFixB: "12"
|
|
},
|
|
funOptions: [
|
|
{ value: "Alpha", disabled: false },
|
|
{ value: "Asy", disabled: false }
|
|
],
|
|
funLines: [],
|
|
sharedFlags: {}
|
|
};
|
|
|
|
const model = lib.buildModelBlocks(config, lib.getFitType(config.all));
|
|
|
|
assert.match(model.runBlock, /^t0\s+1600\s+1601$/m);
|
|
assert.match(model.runBlock, /^backgr\.fix\s+11\s+12$/m);
|
|
}
|
|
|
|
function testT0BgOverridesAppliedToGlobalMSR(lib) {
|
|
const globalMSR = [
|
|
"FITPARAMETER",
|
|
"1 Alpha 1.0 0.01",
|
|
"THEORY",
|
|
"asymmetry 1",
|
|
"GLOBAL",
|
|
"fittype 2",
|
|
"RUN /data/run414 PSI PSI-BIN",
|
|
"t0 1 2",
|
|
"background 3 4 5 6",
|
|
"RUN /data/run415 PSI PSI-BIN",
|
|
"t0 1 2",
|
|
"background 3 4 5 6",
|
|
"COMMANDS",
|
|
"MIGRAD",
|
|
"PLOT 2",
|
|
"runs 1 2"
|
|
].join("\n");
|
|
|
|
const updated = lib.applyT0BgOverridesToMSR(globalMSR, {
|
|
fitAsyType: "AsymmetryGLB",
|
|
runNumbers: "414,415",
|
|
t0BgT0F_0: "100",
|
|
t0BgT0B_0: "101",
|
|
t0BgBgMode_1: "fixed",
|
|
t0BgBgFixF_1: "7",
|
|
t0BgBgFixB_1: "8"
|
|
}, [414, 415]);
|
|
|
|
assert.match(updated, /RUN \/data\/run414 PSI PSI-BIN\nt0\s+100\s+101\nRUN \/data\/run415/);
|
|
assert.match(updated, /RUN \/data\/run415 PSI PSI-BIN\nbackgr\.fix\s+7\s+8\nCOMMANDS/);
|
|
assert.doesNotMatch(updated, /^background\s+3\s+4\s+5\s+6$/m);
|
|
}
|
|
|
|
function testCommonT0BgValuesAppliedToGlobalMSR(lib) {
|
|
const globalMSR = [
|
|
"FITPARAMETER",
|
|
"1 Alpha 1.0 0.01",
|
|
"THEORY",
|
|
"asymmetry 1",
|
|
"GLOBAL",
|
|
"fittype 2",
|
|
"RUN /data/run414 PSI PSI-BIN",
|
|
"RUN /data/run415 PSI PSI-BIN",
|
|
"COMMANDS",
|
|
"MIGRAD",
|
|
"PLOT 2",
|
|
"runs 1 2"
|
|
].join("\n");
|
|
|
|
const updated = lib.applyT0BgOverridesToMSR(globalMSR, {
|
|
fitAsyType: "AsymmetryGLB",
|
|
runNumbers: "414,415",
|
|
t0BgCommonT0F: "100",
|
|
t0BgCommonT0B: "101"
|
|
}, [414, 415]);
|
|
|
|
assert.match(updated, /RUN \/data\/run414 PSI PSI-BIN\nt0\s+100\s+101\nRUN \/data\/run415/);
|
|
assert.match(updated, /RUN \/data\/run415 PSI PSI-BIN\nt0\s+100\s+101\nCOMMANDS/);
|
|
}
|
|
|
|
function testT0BgValidationRejectsIncompletePairs(lib) {
|
|
const incompleteT0 = lib.validateT0BgOverrides({
|
|
fitAsyType: "AsymmetryGLB",
|
|
runNumbers: "414,415",
|
|
t0BgCommonT0F: "100"
|
|
}, [414, 415]);
|
|
|
|
assert.strictEqual(incompleteT0.ok, 0);
|
|
assert.match(incompleteT0.message, /Common asymmetry t0/);
|
|
|
|
const incompleteSingleRange = lib.validateT0BgOverrides({
|
|
fitAsyType: "SingleHist",
|
|
LRBF: "1,2",
|
|
runNumbers: "514",
|
|
t0BgCommonBgMode: "range",
|
|
t0BgCommonBgStart: "10"
|
|
}, [514]);
|
|
|
|
assert.strictEqual(incompleteSingleRange.ok, 0);
|
|
assert.match(incompleteSingleRange.message, /Common single-histogram background range/);
|
|
|
|
const completeFixed = lib.validateT0BgOverrides({
|
|
fitAsyType: "AsymmetryGLB",
|
|
runNumbers: "414,415",
|
|
t0BgCommonBgMode: "fixed",
|
|
t0BgCommonBgFixF: "7",
|
|
t0BgCommonBgFixB: "8"
|
|
}, [414, 415]);
|
|
|
|
assert.strictEqual(completeFixed.ok, 1);
|
|
}
|
|
|
|
function testT0BgUseCommonIgnoresStaleRowBackground(lib) {
|
|
const config = {
|
|
all: {
|
|
fitAsyType: "AsymmetryGLB",
|
|
LRBF: "1 5,3 7",
|
|
runNumbers: "414",
|
|
selYear: "2024",
|
|
instrName: "LEM",
|
|
theoFunBlock: "asymmetry Asy",
|
|
t0BgCommonBgMode: "fixed",
|
|
t0BgCommonBgFixF: "11",
|
|
t0BgCommonBgFixB: "12",
|
|
t0BgBgMode_0: "common",
|
|
t0BgBgFixF_0: "7",
|
|
t0BgBgFixB_0: "8"
|
|
},
|
|
funOptions: [
|
|
{ value: "Alpha", disabled: false },
|
|
{ value: "Asy", disabled: false }
|
|
],
|
|
funLines: [],
|
|
sharedFlags: {}
|
|
};
|
|
|
|
const model = lib.buildModelBlocks(config, lib.getFitType(config.all));
|
|
|
|
assert.match(model.runBlock, /^backgr\.fix\s+11\s+12$/m);
|
|
assert.doesNotMatch(model.runBlock, /^backgr\.fix\s+7\s+8$/m);
|
|
|
|
const globalMSR = [
|
|
"FITPARAMETER",
|
|
"1 Alpha 1.0 0.01",
|
|
"THEORY",
|
|
"asymmetry 1",
|
|
"GLOBAL",
|
|
"fittype 2",
|
|
"RUN /data/run414 PSI PSI-BIN",
|
|
"background 1 2 3 4",
|
|
"RUN /data/run415 PSI PSI-BIN",
|
|
"background 5 6 7 8",
|
|
"COMMANDS",
|
|
"MIGRAD"
|
|
].join("\n");
|
|
|
|
const unchanged = lib.applyT0BgOverridesToMSR(globalMSR, {
|
|
fitAsyType: "AsymmetryGLB",
|
|
runNumbers: "414,415",
|
|
t0BgBgMode_0: "common",
|
|
t0BgBgFixF_0: "7",
|
|
t0BgBgFixB_0: "8"
|
|
}, [414, 415]);
|
|
|
|
assert.strictEqual(unchanged, globalMSR);
|
|
}
|
|
|
|
function testSeedRunParametersFromShared(lib) {
|
|
// Unsharing a parameter must seed every per-run copy from the shared row's
|
|
// current values so share -> edit -> unshare leaves all runs identical while
|
|
// each remains an independent fit parameter.
|
|
const sharedInputs = {
|
|
valAlpha: { value: "1" },
|
|
errAlpha: { value: "0" },
|
|
minAlpha: { value: "" },
|
|
maxAlpha: { value: "" }
|
|
};
|
|
lib.document = {
|
|
getElementById: function(id) { return sharedInputs[id] || null; }
|
|
};
|
|
const previousAll = lib.All;
|
|
lib.All = {
|
|
runNumbers: "514,515",
|
|
instrName: "",
|
|
selYear: "",
|
|
dataRoot: ""
|
|
};
|
|
|
|
lib.seedRunParametersFromShared("Alpha");
|
|
|
|
assert.strictEqual(lib.All.valAlpha_514, "1");
|
|
assert.strictEqual(lib.All.errAlpha_514, "0");
|
|
assert.strictEqual(lib.All.valAlpha_515, "1");
|
|
assert.strictEqual(lib.All.errAlpha_515, "0");
|
|
assert.strictEqual(lib.All.minAlpha_514, "");
|
|
assert.strictEqual(lib.All.maxAlpha_515, "");
|
|
|
|
lib.All = previousAll;
|
|
}
|
|
|
|
function testApplyMSRParametersUpdatesParameterTable(lib) {
|
|
const elements = {
|
|
valAlpha: { value: "old-value" },
|
|
errAlpha: { value: "old-error" },
|
|
minAlpha: { value: "old-min" },
|
|
maxAlpha: { value: "old-max" }
|
|
};
|
|
let cfgParamsCalled = false;
|
|
|
|
lib.document = {
|
|
getElementById: function(id) {
|
|
return elements[id] || null;
|
|
}
|
|
};
|
|
lib.cfgParams = function() {
|
|
cfgParamsCalled = true;
|
|
};
|
|
|
|
const result = lib.applyMSRParametersToGUI([
|
|
{
|
|
order: "1",
|
|
name: "Alpha",
|
|
value: "1.234",
|
|
error: "0.012",
|
|
min: "0.5",
|
|
max: "1.5"
|
|
}
|
|
]);
|
|
|
|
assert.strictEqual(result, 1);
|
|
assert.strictEqual(cfgParamsCalled, true);
|
|
assert.strictEqual(lib.All.valAlpha, "1.234");
|
|
assert.strictEqual(lib.All.errAlpha, "0.012");
|
|
assert.strictEqual(lib.All.minAlpha, "0.5");
|
|
assert.strictEqual(lib.All.maxAlpha, "1.5");
|
|
assert.strictEqual(elements.valAlpha.value, "1.234");
|
|
assert.strictEqual(elements.errAlpha.value, "0.012");
|
|
assert.strictEqual(elements.minAlpha.value, "0.5");
|
|
assert.strictEqual(elements.maxAlpha.value, "1.5");
|
|
}
|
|
|
|
function testCustomParameterNameReservation(lib) {
|
|
// "Field" and "Energy" are default-value entries in defVals but are not
|
|
// theory parameters, so they must be usable as custom parameter names.
|
|
// Real built-in names (from funcDictionary theory blocks) stay reserved.
|
|
function makeElements(name) {
|
|
const funSel = {
|
|
options: [],
|
|
appendChild: function() {}
|
|
};
|
|
const container = {
|
|
innerHTML: ""
|
|
};
|
|
const elements = {
|
|
newParamName: { value: name, focus: function() {} },
|
|
funSel: funSel,
|
|
customParamList: container
|
|
};
|
|
lib.document = {
|
|
getElementById: function(id) {
|
|
return elements[id] || null;
|
|
},
|
|
createElement: function() {
|
|
return { id: "", textContent: "", value: "" };
|
|
}
|
|
};
|
|
lib.cfgParams = function() {};
|
|
lib.markAllCurrentSeriesRunsEdited = function() {};
|
|
return { elements: elements, container: container };
|
|
}
|
|
|
|
lib.customParams = [];
|
|
let warning = "";
|
|
lib.alert = function(message) { warning = message; };
|
|
|
|
// Field: previously blocked via defVals.Field, must now be accepted.
|
|
makeElements("Field");
|
|
lib.addCustomParam();
|
|
assert.strictEqual(lib.customParams.includes("Field"), true);
|
|
|
|
// Energy: likewise previously blocked via defVals.Energy.
|
|
makeElements("Energy");
|
|
lib.addCustomParam();
|
|
assert.strictEqual(lib.customParams.includes("Energy"), true);
|
|
|
|
// A real built-in theory parameter is still rejected.
|
|
const builtIn = ["Asy", "Lam", "Sgm", "Phi", "Frq"];
|
|
for (let name of builtIn) {
|
|
makeElements(name);
|
|
lib.addCustomParam();
|
|
assert.strictEqual(warning, "'" + name + "' is a built-in parameter name. Choose a different name.");
|
|
assert.strictEqual(lib.customParams.includes(name), false);
|
|
warning = "";
|
|
}
|
|
}
|
|
|
|
function testSyncSeriesParametersFromCombinedMSR(lib) {
|
|
const elements = {
|
|
valAsy_0514: { value: "old-514" },
|
|
errAsy_0514: { value: "old-err" },
|
|
valAsy_0515: { value: "old-515" },
|
|
errAsy_0515: { value: "old-err" },
|
|
valzeta: { value: "old-zeta" },
|
|
errzeta: { value: "old-err" }
|
|
};
|
|
let cfgParamsCalled = 0;
|
|
const statusUpdates = [];
|
|
|
|
lib.document = {
|
|
getElementById: function(id) {
|
|
return elements[id] || null;
|
|
}
|
|
};
|
|
lib.cfgParams = function() {
|
|
cfgParamsCalled++;
|
|
};
|
|
lib.appendJobLog = function() {};
|
|
lib.updateJobStatus = function(values) {
|
|
statusUpdates.push(values);
|
|
};
|
|
lib.All.runNumbers = "514,515";
|
|
|
|
const combinedText = [
|
|
"FITPARAMETER",
|
|
" 1 zeta 0.10 0.001 none",
|
|
" 2 Asy_0514 0.213 0.002 none",
|
|
" 3 Asy_0515 0.255 0.003 none",
|
|
"THEORY",
|
|
"asymmetry map1",
|
|
"GLOBAL",
|
|
"fittype 2",
|
|
"RUN /data/run514 MUE4 PSI MUSR-ROOT",
|
|
"COMMANDS",
|
|
"MIGRAD",
|
|
"PLOT 2",
|
|
"runs 1 2",
|
|
""
|
|
].join("\n");
|
|
const result = lib.syncSeriesParametersToGUI(combinedText);
|
|
|
|
assert.strictEqual(result, 1);
|
|
assert.strictEqual(cfgParamsCalled, 1);
|
|
assert.strictEqual(lib.All.valzeta, "0.10");
|
|
assert.strictEqual(lib.All.valAsy_0514, "0.213");
|
|
assert.strictEqual(lib.All.valAsy_0515, "0.255");
|
|
assert.strictEqual(elements.valAsy_0514.value, "0.213");
|
|
assert.strictEqual(elements.valAsy_0515.value, "0.255");
|
|
assert.strictEqual(elements.valzeta.value, "0.10");
|
|
assert.strictEqual(statusUpdates.some(function(update) {
|
|
return /combined series MSR \(3 parameters\)/.test(update.parameterSync || "");
|
|
}), true);
|
|
}
|
|
|
|
function testDirtyEditorOverwriteGuard(lib) {
|
|
resetMSREditorDocuments(lib);
|
|
let confirmMessage = "";
|
|
lib.getMSRDocumentState("current").dirty = true;
|
|
lib.syncMSREditorLegacyState();
|
|
lib.confirm = function(message) {
|
|
confirmMessage = message;
|
|
return false;
|
|
};
|
|
|
|
assert.strictEqual(lib.confirmOverwriteDirtyEditor("Fit"), false);
|
|
assert.match(confirmMessage, /Fit will regenerate the MSR file/);
|
|
|
|
lib.confirm = function() {
|
|
return true;
|
|
};
|
|
|
|
assert.strictEqual(lib.confirmOverwriteDirtyEditor("Fit"), true);
|
|
|
|
lib.getMSRDocumentState("current").dirty = false;
|
|
lib.syncMSREditorLegacyState();
|
|
lib.confirm = function() {
|
|
throw new Error("confirm should not be called for a clean editor");
|
|
};
|
|
|
|
assert.strictEqual(lib.confirmOverwriteDirtyEditor("Plot"), true);
|
|
}
|
|
|
|
function testMSREditorEnterInsertsTextNewline(lib) {
|
|
let insertedNode = null;
|
|
let inputDispatched = null;
|
|
const editor = {
|
|
contains: function(node) {
|
|
return node === rangeContainer;
|
|
},
|
|
dispatchEvent: function(event) {
|
|
inputDispatched = event;
|
|
}
|
|
};
|
|
const rangeContainer = {};
|
|
const textNode = { nodeType: 3 };
|
|
const range = {
|
|
commonAncestorContainer: rangeContainer,
|
|
deleteContents: function() {
|
|
},
|
|
insertNode: function(node) {
|
|
insertedNode = node;
|
|
},
|
|
setStartAfter: function() {
|
|
},
|
|
collapse: function() {
|
|
}
|
|
};
|
|
const selection = {
|
|
rangeCount: 1,
|
|
getRangeAt: function() {
|
|
return range;
|
|
},
|
|
removeAllRanges: function() {
|
|
},
|
|
addRange: function() {
|
|
}
|
|
};
|
|
let prevented = false;
|
|
const previousWindowSelection = lib.window.getSelection;
|
|
const previousDocument = lib.document;
|
|
lib.document = {
|
|
getElementById: function(id) {
|
|
return id === "msrEditorText" ? editor : null;
|
|
},
|
|
createTextNode: function(data) {
|
|
return { nodeType: 3, data: data };
|
|
},
|
|
createEvent: function() {
|
|
const event = { type: "", initEvent: function(type) { this.type = type; } };
|
|
return event;
|
|
}
|
|
};
|
|
lib.window.getSelection = function() {
|
|
return selection;
|
|
};
|
|
|
|
lib.handleMSREditorKeydown({
|
|
key: "Enter",
|
|
preventDefault: function() {
|
|
prevented = true;
|
|
}
|
|
});
|
|
assert.strictEqual(prevented, true);
|
|
assert.strictEqual(insertedNode.data, "\n");
|
|
assert.strictEqual(inputDispatched.type, "input");
|
|
|
|
// Non-Enter keys are left to the browser default (no preventDefault).
|
|
prevented = false;
|
|
insertedNode = null;
|
|
lib.handleMSREditorKeydown({
|
|
key: "a",
|
|
preventDefault: function() {
|
|
prevented = true;
|
|
}
|
|
});
|
|
assert.strictEqual(prevented, false);
|
|
assert.strictEqual(insertedNode, null);
|
|
|
|
// Enter outside the editor (no selection inside it) leaves the default.
|
|
prevented = false;
|
|
selection.rangeCount = 0;
|
|
lib.handleMSREditorKeydown({
|
|
key: "Enter",
|
|
preventDefault: function() {
|
|
prevented = true;
|
|
}
|
|
});
|
|
assert.strictEqual(prevented, false);
|
|
|
|
lib.window.getSelection = previousWindowSelection;
|
|
lib.document = previousDocument;
|
|
}
|
|
|
|
function testLoadMSRIntoEditorClearsDirtyAndUpdatesTable(lib) {
|
|
resetMSREditorDocuments(lib);
|
|
const elements = {
|
|
msrEditorText: { value: "" },
|
|
valAlpha: { value: "old-value" },
|
|
errAlpha: { value: "old-error" },
|
|
minAlpha: { value: "old-min" },
|
|
maxAlpha: { value: "old-max" }
|
|
};
|
|
|
|
lib.document = {
|
|
getElementById: function(id) {
|
|
return elements[id] || null;
|
|
}
|
|
};
|
|
lib.cfgParams = function() {
|
|
};
|
|
lib.window.MSREditor.dirty = true;
|
|
|
|
lib.loadMSRIntoEditor("FITPARAMETER\n1 Alpha 1.234 0.012 0.5 1.5\nTHEORY\n", "/tmp/fitted.msr");
|
|
|
|
assert.strictEqual(elements.msrEditorText.value, "FITPARAMETER\n1 Alpha 1.234 0.012 0.5 1.5\nTHEORY\n");
|
|
assert.strictEqual(lib.window.MSREditor.currentPath, "/tmp/fitted.msr");
|
|
assert.strictEqual(lib.window.MSREditor.dirty, false);
|
|
assert.strictEqual(elements.valAlpha.value, "1.234");
|
|
assert.strictEqual(elements.errAlpha.value, "0.012");
|
|
}
|
|
|
|
function testMSREditorSourceTabsKeepInactiveEdits(lib) {
|
|
resetMSREditorDocuments(lib);
|
|
const elements = {
|
|
msrEditorText: { value: "manual global edit" },
|
|
editorSourceTabs: { style: { display: "" } },
|
|
btnEditorCurrent: { className: "" },
|
|
btnEditorTemplate: { className: "", hidden: true }
|
|
};
|
|
|
|
lib.document = {
|
|
getElementById: function(id) {
|
|
return elements[id] || null;
|
|
}
|
|
};
|
|
lib.cfgParams = function() {
|
|
};
|
|
|
|
lib.window.MSREditor.activeSource = "current";
|
|
lib.window.MSREditor.currentPath = "/tmp/global.msr";
|
|
lib.window.MSREditor.currentContent = "generated global";
|
|
lib.window.MSREditor.templateContent = "template input";
|
|
lib.window.MSREditor.hasTemplateTab = true;
|
|
lib.getMSRDocumentState("current").dirty = true;
|
|
lib.getMSRDocumentState("template").dirty = false;
|
|
lib.syncMSREditorLegacyState();
|
|
|
|
lib.showMSREditorSource("template");
|
|
|
|
assert.strictEqual(lib.window.MSREditor.activeSource, "template");
|
|
assert.strictEqual(lib.window.MSREditor.currentContent, "manual global edit");
|
|
assert.strictEqual(elements.msrEditorText.value, "template input");
|
|
assert.strictEqual(elements.editorSourceTabs.style.display, "inline-flex");
|
|
assert.strictEqual(elements.btnEditorTemplate.hidden, false);
|
|
assert.match(elements.btnEditorTemplate.className, /\bactive\b/);
|
|
assert.strictEqual(lib.window.MSREditor.dirty, true);
|
|
|
|
elements.msrEditorText.value = "manual template edit";
|
|
lib.handleMSREditorInput();
|
|
|
|
// Editing the template transfers ownership only for the template and marks
|
|
// the downstream global file stale without locking or overwriting it.
|
|
assert.strictEqual(lib.getMSRDocumentState("template").sourceOfTruth, "editor");
|
|
assert.strictEqual(lib.getMSRDocumentState("template").locked, true);
|
|
assert.strictEqual(lib.getMSRDocumentState("current").sourceOfTruth, "gui");
|
|
assert.strictEqual(lib.getMSRDocumentState("current").locked, false);
|
|
assert.strictEqual(lib.getMSRDocumentState("current").stale, true);
|
|
|
|
lib.showMSREditorSource("current");
|
|
|
|
assert.strictEqual(lib.window.MSREditor.activeSource, "current");
|
|
assert.strictEqual(lib.window.MSREditor.templateContent, "manual template edit");
|
|
assert.strictEqual(elements.msrEditorText.value, "manual global edit");
|
|
assert.match(elements.btnEditorCurrent.className, /\bactive\b/);
|
|
assert.strictEqual(elements.btnEditorCurrent.textContent, "Global · stale");
|
|
assert.strictEqual(lib.window.MSREditor.currentDirty, true);
|
|
assert.strictEqual(lib.window.MSREditor.templateDirty, true);
|
|
assert.strictEqual(lib.window.MSREditor.dirty, true);
|
|
}
|
|
|
|
|
|
function testManualEditorInputLocksEditorSource(lib) {
|
|
resetMSREditorDocuments(lib);
|
|
const elements = {
|
|
msrEditorText: { value: "manual edit" },
|
|
msrSourceState: { hidden: true },
|
|
msrSourceStateLabel: { textContent: "" },
|
|
msrUnlockButton: {
|
|
disabled: true,
|
|
className: "editor-icon-button editor-lock-button",
|
|
setAttribute: function(name, value) {
|
|
this[name] = value;
|
|
}
|
|
}
|
|
};
|
|
lib.document = {
|
|
getElementById: function(id) {
|
|
return elements[id] || null;
|
|
}
|
|
};
|
|
lib.window.MSREditor.activeSource = "current";
|
|
const currentState = lib.getMSRDocumentState("current");
|
|
currentState.sourceOfTruth = "gui";
|
|
currentState.locked = false;
|
|
currentState.dirty = false;
|
|
currentState.lastGeneratedHash = lib.hashMSRText("generated text");
|
|
currentState.lastAcceptedHash = lib.hashMSRText("accepted text");
|
|
lib.syncMSREditorLegacyState();
|
|
|
|
lib.handleMSREditorInput();
|
|
|
|
assert.strictEqual(lib.window.MSREditor.sourceOfTruth, "editor");
|
|
assert.strictEqual(lib.window.MSREditor.locked, true);
|
|
assert.strictEqual(lib.window.MSREditor.currentDirty, true);
|
|
assert.strictEqual(elements.msrSourceState.hidden, false);
|
|
assert.strictEqual(elements.msrSourceStateLabel.textContent, "Editor source");
|
|
assert.strictEqual(elements.msrUnlockButton.disabled, false);
|
|
assert.match(elements.msrUnlockButton.className, /\blocked\b/);
|
|
assert.strictEqual(elements.msrUnlockButton["aria-label"], "Editor source locked; unlock");
|
|
|
|
lib.confirm = function() {
|
|
throw new Error("locked editor source should not ask before fitting");
|
|
};
|
|
assert.strictEqual(lib.confirmOverwriteDirtyEditor("Fit"), true);
|
|
|
|
lib.unlockMSREditorSource();
|
|
assert.strictEqual(elements.msrSourceState.hidden, true);
|
|
assert.strictEqual(elements.msrUnlockButton.disabled, true);
|
|
assert.doesNotMatch(elements.msrUnlockButton.className, /\blocked\b/);
|
|
assert.strictEqual(elements.msrUnlockButton["aria-label"], "Editor source unlocked");
|
|
}
|
|
|
|
async function testPrepareUsesLockedEditorSource(lib) {
|
|
resetMSREditorDocuments(lib);
|
|
const writtenFiles = {};
|
|
const editorText = "Manual title\nFITPARAMETER\n1 Alpha 1 0.01\nTHEORY\nasymmetry 1\n";
|
|
const elements = {
|
|
msrEditorText: { value: editorText },
|
|
msrSourceState: { hidden: true },
|
|
msrSourceStateLabel: { textContent: "" }
|
|
};
|
|
lib.document = {
|
|
getElementById: function(id) {
|
|
return elements[id] || null;
|
|
}
|
|
};
|
|
lib.amIWeb = function() {
|
|
return true;
|
|
};
|
|
lib.loadWebDataRoot = async function() {
|
|
return 1;
|
|
};
|
|
lib.prepCFG = function() {
|
|
lib.All.runNumbers = "514";
|
|
lib.All.fitAsyType = "Asymmetry";
|
|
lib.All.fileNamePrefix = "_asy";
|
|
lib.All.workPath = ".";
|
|
};
|
|
lib.writeAsciiFile = async function(filePath, content) {
|
|
writtenFiles[filePath] = content;
|
|
return 1;
|
|
};
|
|
lib.readAsciiFile = async function(filePath) {
|
|
return writtenFiles[filePath];
|
|
};
|
|
lib.validateT0BgOverrides = function() {
|
|
throw new Error("GUI t0/Bg validation should be skipped for editor-owned MSR text");
|
|
};
|
|
lib.createMSR = function() {
|
|
throw new Error("GUI MSR generation should be skipped for editor-owned MSR text");
|
|
};
|
|
lib.updateJobStatus = function() {
|
|
};
|
|
lib.setFitStatus = function() {
|
|
};
|
|
lib.syncParametersFromMSR = function() {
|
|
return 1;
|
|
};
|
|
lib.syncT0BgFromMSR = function() {
|
|
return 0;
|
|
};
|
|
lib.window.MusrfitWebJob.firstRun = "";
|
|
lib.window.MusrfitWebJob.workPath = "";
|
|
lib.window.MSREditor.activeSource = "current";
|
|
lib.getMSRDocumentState("current").sourceOfTruth = "editor";
|
|
lib.getMSRDocumentState("current").locked = true;
|
|
lib.syncMSREditorLegacyState();
|
|
|
|
const prepared = await lib.prepareMSRRunFiles("Fit");
|
|
|
|
assert.strictEqual(prepared.ok, 1);
|
|
assert.strictEqual(prepared.options.editorSource, 1);
|
|
assert.match(prepared.finalPath, /\/514_asy\.msr$/);
|
|
assert.strictEqual(writtenFiles[prepared.finalPath], editorText);
|
|
assert.strictEqual(lib.window.MSREditor.sourceOfTruth, "editor");
|
|
assert.strictEqual(lib.window.MSREditor.locked, true);
|
|
}
|
|
|
|
function testSyncCurrentEditorToGUIKeepsEditorAuthoritative(lib) {
|
|
resetMSREditorDocuments(lib);
|
|
const editorText = "Manual title\nFITPARAMETER\n1 Alpha 1.25 0.01\nTHEORY\nasymmetry 1\n";
|
|
const elements = {
|
|
msrEditorText: { value: editorText },
|
|
msrSourceState: { hidden: true },
|
|
msrSourceStateLabel: { textContent: "" }
|
|
};
|
|
let statusMessage = "";
|
|
let statusOptions = null;
|
|
|
|
lib.document = {
|
|
getElementById: function(id) {
|
|
return elements[id] || null;
|
|
}
|
|
};
|
|
lib.syncParametersFromMSR = function(text) {
|
|
assert.strictEqual(text, editorText);
|
|
return 1;
|
|
};
|
|
lib.syncT0BgFromMSR = function(text) {
|
|
assert.strictEqual(text, editorText);
|
|
return 0;
|
|
};
|
|
lib.setFitStatus = function(message, options) {
|
|
statusMessage = message;
|
|
statusOptions = options;
|
|
};
|
|
|
|
lib.window.MSREditor.activeSource = "current";
|
|
const currentState = lib.getMSRDocumentState("current");
|
|
currentState.sourceOfTruth = "editor";
|
|
currentState.locked = true;
|
|
currentState.dirty = true;
|
|
currentState.lastAcceptedHash = "";
|
|
lib.syncMSREditorLegacyState();
|
|
|
|
assert.strictEqual(lib.syncCurrentEditorToGUI(), 1);
|
|
assert.strictEqual(lib.window.MSREditor.sourceOfTruth, "editor");
|
|
assert.strictEqual(lib.window.MSREditor.locked, true);
|
|
assert.strictEqual(lib.window.MSREditor.currentDirty, false);
|
|
assert.strictEqual(lib.window.MSREditor.dirty, false);
|
|
assert.strictEqual(lib.window.MSREditor.lastAcceptedHash, lib.hashMSRText(editorText));
|
|
assert.strictEqual(elements.msrSourceState.hidden, false);
|
|
assert.strictEqual(statusMessage, "MSR fields synchronized to the GUI.");
|
|
assert.strictEqual(statusOptions.level, "success");
|
|
assert.match(statusOptions.details, /Editor remains the source of truth/);
|
|
}
|
|
|
|
async function testPrepareUsesLockedTemplateSource(lib) {
|
|
resetMSREditorDocuments(lib);
|
|
const writtenFiles = {};
|
|
const templateText = "Edited template\nFITPARAMETER\n1 Alpha 1 0.01\nTHEORY\nasymmetry 1\n";
|
|
const elements = {
|
|
msrEditorText: { value: templateText },
|
|
msrSourceState: { hidden: true },
|
|
msrSourceStateLabel: { textContent: "" },
|
|
editorSourceTabs: { style: { display: "" } },
|
|
btnEditorCurrent: { className: "", textContent: "" },
|
|
btnEditorTemplate: { className: "", textContent: "", hidden: true }
|
|
};
|
|
|
|
lib.document = {
|
|
getElementById: function(id) {
|
|
return elements[id] || null;
|
|
}
|
|
};
|
|
lib.amIWeb = function() {
|
|
return true;
|
|
};
|
|
lib.loadWebDataRoot = async function() {
|
|
return 1;
|
|
};
|
|
lib.prepCFG = function() {
|
|
lib.All.runNumbers = "514,516";
|
|
lib.All.fitAsyType = "AsymmetryGLB";
|
|
lib.All.fileNamePrefix = "_asy";
|
|
lib.All.workPath = ".";
|
|
};
|
|
lib.writeAsciiFile = async function(filePath, content) {
|
|
writtenFiles[filePath] = content;
|
|
return 1;
|
|
};
|
|
lib.readAsciiFile = async function(filePath) {
|
|
return writtenFiles[filePath];
|
|
};
|
|
lib.validateT0BgOverrides = function() {
|
|
throw new Error("GUI t0/Bg validation should be skipped for an editor-owned template");
|
|
};
|
|
lib.createMSR = function() {
|
|
throw new Error("GUI template generation should be skipped for an editor-owned template");
|
|
};
|
|
lib.updateJobStatus = function() {
|
|
};
|
|
lib.loadMSRIntoEditor = function() {
|
|
};
|
|
lib.setFitStatus = function() {
|
|
};
|
|
lib.window.MusrfitWebJob.firstRun = "";
|
|
lib.window.MusrfitWebJob.workPath = "";
|
|
lib.window.MSREditor.hasTemplateTab = true;
|
|
lib.window.MSREditor.activeSource = "template";
|
|
lib.window.MSREditor.templateContent = templateText;
|
|
lib.window.MSREditor.templatePath = "/tmp/514_tmpl.msr";
|
|
lib.getMSRDocumentState("template").sourceOfTruth = "editor";
|
|
lib.getMSRDocumentState("template").locked = true;
|
|
lib.getMSRDocumentState("current").stale = true;
|
|
lib.syncMSREditorLegacyState();
|
|
|
|
const prepared = await lib.prepareMSRRunFiles("Fit");
|
|
|
|
assert.strictEqual(prepared.ok, 1);
|
|
assert.strictEqual(prepared.options.useM2D, 1);
|
|
assert.strictEqual(prepared.options.templateEditorSource, 1);
|
|
assert.strictEqual(writtenFiles[prepared.options.baseRun + "_tmpl.msr"], undefined);
|
|
assert.strictEqual(writtenFiles[lib.window.MSREditor.templatePath], templateText);
|
|
assert.strictEqual(writtenFiles[prepared.finalPath], templateText);
|
|
assert.strictEqual(lib.getMSRDocumentState("template").sourceOfTruth, "editor");
|
|
assert.strictEqual(lib.getMSRDocumentState("template").locked, true);
|
|
assert.strictEqual(lib.getMSRDocumentState("current").sourceOfTruth, "gui");
|
|
assert.strictEqual(lib.getMSRDocumentState("current").locked, false);
|
|
assert.strictEqual(lib.getMSRDocumentState("current").stale, false);
|
|
}
|
|
|
|
function testMSREditorHighlighting(lib) {
|
|
const highlighted = lib.highlightMSRText([
|
|
"# full-line comment",
|
|
"FITPARAMETER",
|
|
"1 Alpha 1.25 0.01 none 0 2 # fitted asymmetry",
|
|
"THEORY",
|
|
"asymmetry 1",
|
|
"simplExpo map1 (rate)",
|
|
"FUNCTIONS",
|
|
"fun1 = sqrt(par1) * gamma_mu + pi + B + En + T0",
|
|
"GLOBAL",
|
|
"fittype 2",
|
|
"RUN /data/run MUE4 PSI MUSR-ROOT",
|
|
"alpha fun1",
|
|
"backgr.fix 10.5 11.5",
|
|
"COMMANDS",
|
|
"MAX_LIKELIHOOD",
|
|
"FIX Alpha, 1",
|
|
"FOURIER",
|
|
"phase parR7 parR(3, 2, 4)",
|
|
"PLOT 2 (asymmetry plot)",
|
|
"sub_ranges 0 8 0 8",
|
|
"logy"
|
|
].join("\n"));
|
|
|
|
assert.match(highlighted, /<span class="msr-comment"># full-line comment<\/span>/);
|
|
assert.match(highlighted, /<span class="msr-keyword">FITPARAMETER<\/span>/);
|
|
assert.match(highlighted, /<span class="msr-parameter">Alpha<\/span>/);
|
|
assert.match(highlighted, /<span class="msr-value">none<\/span>/);
|
|
assert.match(highlighted, /<span class="msr-comment"># fitted asymmetry<\/span>/);
|
|
assert.match(highlighted, /<span class="msr-function">simplExpo<\/span>/);
|
|
assert.match(highlighted, /<span class="msr-parameter">map1<\/span>/);
|
|
assert.match(highlighted, /<span class="msr-function">sqrt<\/span>/);
|
|
assert.match(highlighted, /<span class="msr-parameter">par1<\/span>/);
|
|
assert.match(highlighted, /<span class="msr-value">gamma_mu<\/span>/);
|
|
assert.match(highlighted, /<span class="msr-value">pi<\/span>/);
|
|
assert.match(highlighted, /<span class="msr-value">T0<\/span>/);
|
|
assert.match(highlighted, /<span class="msr-directive">backgr\.fix<\/span>/);
|
|
assert.match(highlighted, /<span class="msr-command">MAX_LIKELIHOOD<\/span>/);
|
|
assert.match(highlighted, /<span class="msr-parameter">parR7<\/span>/);
|
|
assert.match(highlighted, /<span class="msr-parameter">parR\(3, 2, 4\)<\/span>/);
|
|
assert.match(highlighted, /<span class="msr-directive">sub_ranges<\/span>/);
|
|
assert.match(highlighted, /<span class="msr-comment"> \(asymmetry plot\)<\/span>/);
|
|
}
|
|
|
|
function testRunsSectionUsesCompactEmptyDefault(lib) {
|
|
const html = fs.readFileSync(path.join(repoRoot, "index.html"), "utf8");
|
|
const runInputMatch = html.match(/<input[^>]*id="runNumbers"[^>]*>/);
|
|
|
|
assert.match(html, /class="compact-settings-header"><b>Runs<\/b>/);
|
|
assert.ok(runInputMatch, "Runs input should be present");
|
|
assert.match(runInputMatch[0], /value=""/);
|
|
assert.match(runInputMatch[0], /placeholder="e\.g\. 514, 516, 520-525, 530\+531"/);
|
|
assert.doesNotMatch(runInputMatch[0], /value="514"/);
|
|
}
|
|
|
|
function testRunListParsingAndActionValidation(lib) {
|
|
assert.deepStrictEqual(Array.from(lib.arrayRunNumbers("")), []);
|
|
assert.deepStrictEqual(Array.from(lib.arrayRunNumbers("514, 516, 520-522")), [514, 516, 520, 521, 522]);
|
|
assert.deepStrictEqual(Array.from(lib.arrayRunNumbers("520:524:2")), [520, 522, 524]);
|
|
assert.deepStrictEqual(Array.from(lib.arrayRunNumbers("520:524:0")), []);
|
|
assert.deepStrictEqual(Array.from(lib.arrayRunNumbers("524-520")), []);
|
|
assert.strictEqual(lib.legalRunNumbers("514, 516-520"), true);
|
|
assert.strictEqual(lib.legalRunNumbers("514;516"), false);
|
|
|
|
const warning = { hidden: true };
|
|
const label = { textContent: "" };
|
|
const message = { textContent: "" };
|
|
const attributes = {};
|
|
let focused = false;
|
|
const runInput = {
|
|
value: "",
|
|
setAttribute: function(name, value) {
|
|
attributes[name] = value;
|
|
},
|
|
removeAttribute: function(name) {
|
|
delete attributes[name];
|
|
},
|
|
focus: function() {
|
|
focused = true;
|
|
}
|
|
};
|
|
const elements = {
|
|
dataPathResolutionWarning: warning,
|
|
dataPathResolutionLabel: label,
|
|
dataPathResolutionMessage: message,
|
|
runNumbers: runInput
|
|
};
|
|
lib.document = {
|
|
getElementById: function(id) {
|
|
return elements[id] || null;
|
|
}
|
|
};
|
|
lib.setFitStatus = function() {
|
|
};
|
|
|
|
assert.strictEqual(lib.validateRunSelection("Fit"), 0);
|
|
assert.strictEqual(warning.hidden, false);
|
|
assert.strictEqual(label.textContent, "Runs");
|
|
assert.strictEqual(message.textContent, "Enter at least one run.");
|
|
assert.strictEqual(attributes["aria-invalid"], "true");
|
|
assert.strictEqual(focused, true);
|
|
|
|
runInput.value = "514,516";
|
|
assert.strictEqual(lib.validateRunSelection("Fit"), 1);
|
|
assert.strictEqual(warning.hidden, true);
|
|
assert.strictEqual(message.textContent, "");
|
|
assert.strictEqual(attributes["aria-invalid"], undefined);
|
|
}
|
|
|
|
function testFFTOptionsIndicator(lib) {
|
|
const classes = new Set();
|
|
const disclosure = {
|
|
classList: {
|
|
toggle: function(name, enabled) {
|
|
if (enabled) {
|
|
classes.add(name);
|
|
} else {
|
|
classes.delete(name);
|
|
}
|
|
}
|
|
}
|
|
};
|
|
const elements = {
|
|
fftOptionsDisclosure: disclosure,
|
|
fftType: { value: "power" },
|
|
fftApod: { value: "STRONG" },
|
|
fftPhase: { value: "8.5" },
|
|
fftDCCorrect: { checked: false },
|
|
fftxi: { value: "" },
|
|
fftxf: { value: "" },
|
|
fftUnits: { value: "Gauss" },
|
|
fftPower: { value: "12" },
|
|
fftPhaseCorrection: { value: "off" },
|
|
fftPhaseCorrectionRange: { hidden: false }
|
|
};
|
|
lib.document = {
|
|
getElementById: function(id) {
|
|
return elements[id] || null;
|
|
}
|
|
};
|
|
|
|
// Default FFT settings should not advertise an override.
|
|
lib.updateFFTOptionsIndicator();
|
|
assert.strictEqual(classes.has("has-overrides"), false);
|
|
|
|
elements.fftPhase.value = "10";
|
|
lib.updateFFTOptionsIndicator();
|
|
assert.strictEqual(classes.has("has-overrides"), true);
|
|
elements.fftPhase.value = "8.5";
|
|
|
|
elements.fftUnits.value = "MHz";
|
|
lib.updateFFTOptionsIndicator();
|
|
assert.strictEqual(classes.has("has-overrides"), true);
|
|
|
|
elements.fftUnits.value = "Gauss";
|
|
elements.fftxi.value = "0.5";
|
|
lib.updateFFTOptionsIndicator();
|
|
assert.strictEqual(classes.has("has-overrides"), true);
|
|
|
|
elements.fftxi.value = "";
|
|
elements.fftPower.value = "13";
|
|
lib.updateFFTOptionsIndicator();
|
|
assert.strictEqual(classes.has("has-overrides"), true);
|
|
|
|
elements.fftPower.value = "12";
|
|
elements.fftPhaseCorrection.value = "all";
|
|
lib.updateFFTOptionsIndicator();
|
|
assert.strictEqual(classes.has("has-overrides"), true);
|
|
|
|
// Custom phase-correction fields are revealed only in Custom mode. Values are
|
|
// not cleared when the row is hidden, so toggling modes preserves user input.
|
|
elements.fftPhaseCorrection.value = "custom";
|
|
lib.updateFFTPhaseCorrectionVisibility();
|
|
assert.strictEqual(elements.fftPhaseCorrectionRange.hidden, false);
|
|
elements.fftPhaseCorrection.value = "off";
|
|
lib.updateFFTPhaseCorrectionVisibility();
|
|
assert.strictEqual(elements.fftPhaseCorrectionRange.hidden, true);
|
|
assert.strictEqual(classes.has("has-overrides"), false);
|
|
}
|
|
|
|
function testRunPathDiagnosticIsSilentOnSuccess(lib) {
|
|
const warning = { hidden: false };
|
|
const message = { textContent: "stale" };
|
|
const runInput = { value: "514" };
|
|
const yearInput = { value: "2025", min: "" };
|
|
const instrumentInput = { value: "LEM" };
|
|
const rootInput = { value: "/psi.ch/data" };
|
|
const elements = {
|
|
dataPathResolutionWarning: warning,
|
|
dataPathResolutionMessage: message,
|
|
runNumbers: runInput,
|
|
selYear: yearInput,
|
|
instrName: instrumentInput,
|
|
dataRoot: rootInput
|
|
};
|
|
lib.document = {
|
|
getElementById: function(id) {
|
|
return elements[id] || null;
|
|
}
|
|
};
|
|
|
|
// A valid configuration should consume no permanent UI space.
|
|
lib.updateResolvedDataPathPreview();
|
|
assert.strictEqual(warning.hidden, true);
|
|
assert.strictEqual(message.textContent, "");
|
|
assert.strictEqual(yearInput.min, "2001");
|
|
|
|
// An impossible instrument/year combination becomes a compact warning.
|
|
yearInput.value = "1999";
|
|
lib.updateResolvedDataPathPreview();
|
|
assert.strictEqual(warning.hidden, false);
|
|
assert.match(message.textContent, /supported from 2001/);
|
|
}
|
|
|
|
async function testWebJobFolderChangesWithFirstRun(lib) {
|
|
let selectedRuns = "514,515";
|
|
const writtenFiles = {};
|
|
lib.amIWeb = function() {
|
|
return true;
|
|
};
|
|
lib.loadWebDataRoot = async function() {
|
|
return 1;
|
|
};
|
|
lib.prepCFG = function() {
|
|
lib.All.runNumbers = selectedRuns;
|
|
lib.All.fitAsyType = "AsymmetryGLB";
|
|
lib.All.fileNamePrefix = "_asy";
|
|
lib.All.workPath = ".";
|
|
};
|
|
lib.arrayRunNumbers = function(runText) {
|
|
return runText.split(",").map(Number);
|
|
};
|
|
lib.validateT0BgOverrides = function() {
|
|
return { ok: 1 };
|
|
};
|
|
lib.createMSR = function() {
|
|
return "FITPARAMETER\nTHEORY\nGLOBAL\nRUN\nCOMMANDS\nPLOT\n";
|
|
};
|
|
lib.writeAsciiFile = async function(filePath, content) {
|
|
writtenFiles[filePath] = content;
|
|
return 1;
|
|
};
|
|
lib.readAsciiFile = async function(filePath) {
|
|
return writtenFiles[filePath];
|
|
};
|
|
lib.updateJobStatus = function() {
|
|
};
|
|
lib.loadMSRIntoEditor = function() {
|
|
};
|
|
lib.setFitStatus = function() {
|
|
};
|
|
|
|
const firstPreparation = await lib.prepareMSRRunFiles("Fit");
|
|
selectedRuns = "514,516";
|
|
const repeatedPreparation = await lib.prepareMSRRunFiles("Plot");
|
|
selectedRuns = "515,516";
|
|
const changedRunPreparation = await lib.prepareMSRRunFiles("Fit");
|
|
|
|
assert.strictEqual(firstPreparation.finalPath, repeatedPreparation.finalPath);
|
|
assert.notStrictEqual(firstPreparation.finalPath, changedRunPreparation.finalPath);
|
|
assert.match(firstPreparation.finalPath, /\/514\+global_asy\.msr$/);
|
|
assert.match(changedRunPreparation.finalPath, /\/515\+global_asy\.msr$/);
|
|
assert.strictEqual(lib.window.MusrfitWebJob.firstRun, "515");
|
|
}
|
|
|
|
function testParseDataRangeInGlobalBlock(lib) {
|
|
const msr = [
|
|
"GLOBAL",
|
|
"fittype 2",
|
|
"fit 0.05 10",
|
|
"packing 5",
|
|
"data 207 7994 167 8009"
|
|
].join("\n");
|
|
const parsed = JSON.parse(JSON.stringify(lib.parseMSRGUISettings(msr)));
|
|
assert.deepStrictEqual(parsed.values.globalDataRange, ["207", "7994", "167", "8009"]);
|
|
|
|
const all = {};
|
|
lib.applyMSRGUISettings(parsed, all);
|
|
assert.deepStrictEqual(all.globalDataRange, ["207", "7994", "167", "8009"]);
|
|
|
|
const blk = lib.buildGlobalBlock(all, 2);
|
|
assert.match(blk, /data\s+207\s+7994\s+167\s+8009/);
|
|
|
|
const noData = [
|
|
"GLOBAL",
|
|
"fittype 0",
|
|
"fit 0.05 10",
|
|
"packing 5"
|
|
].join("\n");
|
|
const parsedNoData = JSON.parse(JSON.stringify(lib.parseMSRGUISettings(noData)));
|
|
assert.strictEqual(parsedNoData.values.globalDataRange, undefined);
|
|
|
|
const allNoData = {};
|
|
lib.applyMSRGUISettings(parsedNoData, allNoData);
|
|
const blkNoData = lib.buildGlobalBlock(allNoData, 0);
|
|
assert.doesNotMatch(blkNoData, /^data\b/m);
|
|
}
|
|
|
|
function testParseDataRangeInRunBlock(lib) {
|
|
const msr = [
|
|
"GLOBAL",
|
|
"fittype 0",
|
|
"RUN 2026/lem26_his_2321 MUE4 PSI MUSR-ROOT",
|
|
"t0 1657",
|
|
"background 500 1000",
|
|
"data 1657 96500",
|
|
"RUN 2026/lem26_his_2321 MUE4 PSI MUSR-ROOT",
|
|
"t0 1657",
|
|
"background 500 1000",
|
|
"data 1657 80000"
|
|
].join("\n");
|
|
const parsed = JSON.parse(JSON.stringify(lib.parseMSRT0BgRunBlocks(msr)));
|
|
assert.strictEqual(parsed.ok, true);
|
|
assert.deepStrictEqual(parsed.runBlocks[0].dataRange, ["1657", "96500"]);
|
|
assert.deepStrictEqual(parsed.runBlocks[1].dataRange, ["1657", "80000"]);
|
|
}
|
|
|
|
function testBuildModelBlocksPreservesDataRange(lib) {
|
|
const config = {
|
|
all: {
|
|
runNumbers: "514",
|
|
selYear: "2024",
|
|
instrName: "LEM",
|
|
dataRoot: "/data",
|
|
fitAsyType: "SingleHist",
|
|
LRBF: "21 25,23 27",
|
|
t0BgDataStart_0: "1657",
|
|
t0BgDataEnd_0: "96500",
|
|
t0BgDataStart_1: "1657",
|
|
t0BgDataEnd_1: "80000",
|
|
theoFunBlock: "asymmetry 1",
|
|
Tis: "0.05", Tfs: "10", BINS: "5"
|
|
},
|
|
funOptions: [],
|
|
funLines: [],
|
|
sharedFlags: {}
|
|
};
|
|
const model = lib.buildModelBlocks(config, 0);
|
|
assert.match(model.runBlock, /data\s+1657\s+96500/);
|
|
assert.match(model.runBlock, /data\s+1657\s+80000/);
|
|
|
|
const configCommonData = {
|
|
all: {
|
|
runNumbers: "514",
|
|
selYear: "2024",
|
|
instrName: "LEM",
|
|
dataRoot: "/data",
|
|
fitAsyType: "SingleHist",
|
|
LRBF: "21 25,23 27",
|
|
t0BgCommonDataStart: "1657",
|
|
t0BgCommonDataEnd: "99999",
|
|
theoFunBlock: "asymmetry 1",
|
|
Tis: "0.05", Tfs: "10", BINS: "5"
|
|
},
|
|
funOptions: [],
|
|
funLines: [],
|
|
sharedFlags: {}
|
|
};
|
|
const modelCommonData = lib.buildModelBlocks(configCommonData, 0);
|
|
assert.match(modelCommonData.runBlock, /data\s+1657\s+99999/);
|
|
|
|
const configNoData = {
|
|
all: {
|
|
runNumbers: "514",
|
|
selYear: "2024",
|
|
instrName: "LEM",
|
|
dataRoot: "/data",
|
|
fitAsyType: "SingleHist",
|
|
LRBF: "21 25,23 27",
|
|
theoFunBlock: "asymmetry 1",
|
|
Tis: "0.05", Tfs: "10", BINS: "5"
|
|
},
|
|
funOptions: [],
|
|
funLines: [],
|
|
sharedFlags: {}
|
|
};
|
|
const modelNoData = lib.buildModelBlocks(configNoData, 0);
|
|
assert.doesNotMatch(modelNoData.runBlock, /^data\b/m);
|
|
}
|
|
|
|
function testBuildAsymmetryDataRangeRunBlock(lib) {
|
|
const lines = lib.buildT0BgRunLines({
|
|
t0BgCommonDataFStart: "1655",
|
|
t0BgCommonDataFEnd: "100897",
|
|
t0BgCommonDataBStart: "1668",
|
|
t0BgCommonDataBEnd: "100897"
|
|
}, 2, 0);
|
|
assert.deepStrictEqual(JSON.parse(JSON.stringify(lines)), ["data 1655 100897 1668 100897"]);
|
|
|
|
const rowLines = lib.buildT0BgRunLines({
|
|
t0BgCommonDataFStart: "1655",
|
|
t0BgCommonDataFEnd: "100897",
|
|
t0BgCommonDataBStart: "1668",
|
|
t0BgCommonDataBEnd: "100897",
|
|
t0BgDataFStart_0: "10",
|
|
t0BgDataFEnd_0: "20",
|
|
t0BgDataBStart_0: "30",
|
|
t0BgDataBEnd_0: "40"
|
|
}, 2, 0);
|
|
assert.deepStrictEqual(JSON.parse(JSON.stringify(rowLines)), ["data 10 20 30 40"]);
|
|
|
|
const noData = lib.buildT0BgRunLines({}, 2, 0);
|
|
assert.deepStrictEqual(JSON.parse(JSON.stringify(noData)), []);
|
|
}
|
|
|
|
function testSyncCommonDataRangeFromMSR(lib) {
|
|
const all = {
|
|
fitAsyType: "AsymmetryGLB",
|
|
runNumbers: "414,415"
|
|
};
|
|
const msr = [
|
|
"GLOBAL",
|
|
"fittype 2",
|
|
"RUN /data/run414 MUE4 PSI MUSR-ROOT",
|
|
"t0 1 2",
|
|
"background 3 4 5 6",
|
|
"data 1655 100897 1668 100897",
|
|
"RUN /data/run415 MUE4 PSI MUSR-ROOT",
|
|
"t0 1 2",
|
|
"background 3 4 5 6",
|
|
"data 1655 100897 1668 100897",
|
|
"COMMANDS"
|
|
].join("\n");
|
|
|
|
assert.strictEqual(lib.syncT0BgFromMSR(msr, all), 1);
|
|
assert.strictEqual(all.t0BgCommonDataFStart, "1655");
|
|
assert.strictEqual(all.t0BgCommonDataFEnd, "100897");
|
|
assert.strictEqual(all.t0BgCommonDataBStart, "1668");
|
|
assert.strictEqual(all.t0BgCommonDataBEnd, "100897");
|
|
assert.strictEqual(all.t0BgDataFStart_0, undefined);
|
|
}
|
|
|
|
function testSyncPerRunDataRangeFromMSR(lib) {
|
|
const all = {
|
|
fitAsyType: "AsymmetryGLB",
|
|
runNumbers: "414,415"
|
|
};
|
|
const msr = [
|
|
"GLOBAL",
|
|
"fittype 2",
|
|
"RUN /data/run414 MUE4 PSI MUSR-ROOT",
|
|
"t0 1 2",
|
|
"background 3 4 5 6",
|
|
"data 1655 100897 1668 100897",
|
|
"RUN /data/run415 MUE4 PSI MUSR-ROOT",
|
|
"t0 1 2",
|
|
"background 3 4 5 6",
|
|
"data 200 300 400 500",
|
|
"COMMANDS"
|
|
].join("\n");
|
|
|
|
assert.strictEqual(lib.syncT0BgFromMSR(msr, all), 1);
|
|
assert.strictEqual(all.t0BgDataFStart_0, "1655");
|
|
assert.strictEqual(all.t0BgDataFEnd_0, "100897");
|
|
assert.strictEqual(all.t0BgDataBStart_1, "400");
|
|
assert.strictEqual(all.t0BgDataBEnd_1, "500");
|
|
assert.strictEqual(all.t0BgCommonDataFStart, undefined);
|
|
}
|
|
|
|
function testSyncSingleHistogramDataRangeFromMSR(lib) {
|
|
const all = {
|
|
fitAsyType: "SingleHist",
|
|
LRBF: "1,2"
|
|
};
|
|
const msr = [
|
|
"GLOBAL",
|
|
"fittype 0",
|
|
"RUN /data/run MUE4 PSI MUSR-ROOT",
|
|
"t0 100",
|
|
"background 500 1000",
|
|
"data 1657 96500",
|
|
"RUN /data/run MUE4 PSI MUSR-ROOT",
|
|
"t0 101",
|
|
"background 500 1000",
|
|
"data 2000 80000",
|
|
"COMMANDS"
|
|
].join("\n");
|
|
|
|
assert.strictEqual(lib.syncT0BgFromMSR(msr, all), 1);
|
|
assert.strictEqual(all.t0BgDataStart_0, "1657");
|
|
assert.strictEqual(all.t0BgDataEnd_0, "96500");
|
|
assert.strictEqual(all.t0BgDataStart_1, "2000");
|
|
assert.strictEqual(all.t0BgDataEnd_1, "80000");
|
|
assert.strictEqual(all.t0BgCommonDataStart, undefined);
|
|
}
|
|
|
|
function testT0BgDataRangeValidation(lib) {
|
|
const incompleteCommon = lib.validateT0BgOverrides({
|
|
fitAsyType: "SingleHist",
|
|
LRBF: "1,2",
|
|
runNumbers: "514",
|
|
t0BgCommonDataStart: "1657"
|
|
}, [514]);
|
|
assert.strictEqual(incompleteCommon.ok, 0);
|
|
assert.match(incompleteCommon.message, /Common single-histogram data range is incomplete/);
|
|
|
|
const incompleteRow = lib.validateT0BgOverrides({
|
|
fitAsyType: "SingleHist",
|
|
LRBF: "1,2",
|
|
runNumbers: "514",
|
|
t0BgDataStart_0: "1657"
|
|
}, [514]);
|
|
assert.strictEqual(incompleteRow.ok, 0);
|
|
assert.match(incompleteRow.message, /Histogram 1 data range override is incomplete/);
|
|
|
|
const incompleteAsym = lib.validateT0BgOverrides({
|
|
fitAsyType: "AsymmetryGLB",
|
|
runNumbers: "414,415",
|
|
t0BgCommonDataFStart: "1655",
|
|
t0BgCommonDataFEnd: "100897",
|
|
t0BgCommonDataBStart: "1668"
|
|
}, [414, 415]);
|
|
assert.strictEqual(incompleteAsym.ok, 0);
|
|
assert.match(incompleteAsym.message, /Common backward data range is incomplete/);
|
|
|
|
const complete = lib.validateT0BgOverrides({
|
|
fitAsyType: "AsymmetryGLB",
|
|
runNumbers: "414,415",
|
|
t0BgCommonDataFStart: "1655",
|
|
t0BgCommonDataFEnd: "100897",
|
|
t0BgCommonDataBStart: "1668",
|
|
t0BgCommonDataBEnd: "100897"
|
|
}, [414, 415]);
|
|
assert.strictEqual(complete.ok, 1);
|
|
}
|
|
|
|
function testApplyT0BgOverridesToMSRReplacesDataLines(lib) {
|
|
const globalMSR = [
|
|
"FITPARAMETER",
|
|
"1 Alpha 1.0 0.01",
|
|
"THEORY",
|
|
"asymmetry 1",
|
|
"GLOBAL",
|
|
"fittype 2",
|
|
"RUN /data/run414 MUE4 PSI MUSR-ROOT",
|
|
"t0 1 2",
|
|
"background 3 4 5 6",
|
|
"data 100 200 300 400",
|
|
"RUN /data/run415 MUE4 PSI MUSR-ROOT",
|
|
"t0 1 2",
|
|
"background 3 4 5 6",
|
|
"data 500 600 700 800",
|
|
"COMMANDS",
|
|
"MIGRAD"
|
|
].join("\n");
|
|
|
|
const updated = lib.applyT0BgOverridesToMSR(globalMSR, {
|
|
fitAsyType: "AsymmetryGLB",
|
|
runNumbers: "414,415",
|
|
t0BgDataFStart_0: "10",
|
|
t0BgDataFEnd_0: "20",
|
|
t0BgDataBStart_0: "30",
|
|
t0BgDataBEnd_0: "40",
|
|
t0BgDataFStart_1: "50",
|
|
t0BgDataFEnd_1: "60",
|
|
t0BgDataBStart_1: "70",
|
|
t0BgDataBEnd_1: "80"
|
|
}, [414, 415]);
|
|
|
|
assert.match(updated, /RUN \/data\/run414 MUE4 PSI MUSR-ROOT\n.*data\s+10\s+20\s+30\s+40/);
|
|
assert.match(updated, /RUN \/data\/run415 MUE4 PSI MUSR-ROOT\n.*data\s+50\s+60\s+70\s+80/);
|
|
assert.doesNotMatch(updated, /^data\s+100\s+200\s+300\s+400$/m);
|
|
}
|
|
|
|
function testMultiAsymmetryRoundTrip(lib) {
|
|
const msr = fs.readFileSync(
|
|
path.join(__dirname, "fixtures", "reference", "multiasym.generated.msr"), "utf8"
|
|
);
|
|
|
|
// The two forward,backward groups of run 2452 reconstruct to the catalogue
|
|
// ExponentialCos model. The generated amplitude function fun1 = Asy1 * map1
|
|
// is validated structurally but intentionally not exposed as an editable GUI
|
|
// constraint (the dropdown holds Asy1, not Asy), and its map is not stored
|
|
// as a user function map either: regeneration rebuilds it from the ratio list.
|
|
const theory = JSON.parse(JSON.stringify(lib.parseMSRTheoryModel(msr)));
|
|
assert.strictEqual(theory.ok, true);
|
|
assert.deepStrictEqual(theory.components, ["ExponentialCos"]);
|
|
assert.deepStrictEqual(theory.constraints, []);
|
|
assert.deepStrictEqual(theory.functionMaps, {});
|
|
assert.deepStrictEqual(theory.customParameters, ["Asy1", "One", "r2"]);
|
|
assert.strictEqual(theory.parameterTargets["1"], "Asy1");
|
|
assert.strictEqual(theory.parameterTargets["4"], "Alpha1");
|
|
assert.strictEqual(theory.parameterTargets["5"], "Alpha2");
|
|
assert.strictEqual(theory.parameterTargets["6"], "One");
|
|
assert.strictEqual(theory.parameterTargets["7"], "r2");
|
|
assert.strictEqual(theory.parameterTargets["8"], "Phi1");
|
|
assert.strictEqual(theory.parameterTargets["9"], "Phi2");
|
|
|
|
// The GUI recovers the slash-separated forward,backward pair list.
|
|
const gui = JSON.parse(JSON.stringify(lib.parseMSRGUISettings(msr)));
|
|
assert.strictEqual(gui.values.fitAsyType, "AsymmetryGLB");
|
|
assert.strictEqual(gui.values.LRBF, "1,2/3,4");
|
|
assert.strictEqual(gui.values.runNumbers, "2452");
|
|
assert.strictEqual(gui.values.selYear, "2026");
|
|
assert.strictEqual(gui.values.instrName, "LEM");
|
|
|
|
// The fit-edit-fit loop regenerates an identical file. The GUI keeps its own
|
|
// data root (the MSR stores only instrument/year/run), so restore it here.
|
|
const all = {};
|
|
all.theoFunBlock = "asymmetry Asy1\nsimplExpo Lam\nTFieldCos Phi Frq";
|
|
const previousDocument = lib.document;
|
|
lib.document = undefined;
|
|
assert.strictEqual(lib.applyMSRGUISettings(gui, all), 1);
|
|
assert.strictEqual(lib.applyMSRTheoryModel(theory, all), 1);
|
|
lib.document = previousDocument;
|
|
all.dataRoot = "/mnt/fake";
|
|
|
|
const modelAll = JSON.parse(JSON.stringify(all));
|
|
for (const parameter of lib.parseMSRParameters(msr)) {
|
|
const target = theory.parameterTargets[parameter.order] + "_2452";
|
|
modelAll["val" + target] = parameter.value;
|
|
modelAll["err" + target] = parameter.error;
|
|
modelAll["min" + target] = parameter.min;
|
|
modelAll["max" + target] = parameter.max;
|
|
}
|
|
const regenerated = lib.buildMSR({
|
|
all: modelAll,
|
|
funOptions: [
|
|
{ value: "Asy1", disabled: false },
|
|
{ value: "Lam", disabled: false },
|
|
{ value: "Frq", disabled: false },
|
|
{ value: "Alpha1", disabled: false },
|
|
{ value: "Alpha2", disabled: false },
|
|
{ value: "One", disabled: false },
|
|
{ value: "r2", disabled: false },
|
|
{ value: "Phi1", disabled: false },
|
|
{ value: "Phi2", disabled: false }
|
|
],
|
|
funLines: [],
|
|
sharedFlags: {}
|
|
});
|
|
assert.strictEqual(regenerated, msr);
|
|
|
|
// The builder also normalizes a legacy plain "Asy" THEORY placeholder the
|
|
// way the catalogue plus updateTheoryBlock would produce it.
|
|
const blocks = lib.buildModelBlocks({
|
|
all: {
|
|
fitAsyType: "AsymmetryGLB",
|
|
numComps: "1",
|
|
LRBF: "1,2/3,4",
|
|
theoFunBlock: "asymmetry Asy\nsimplExpo Lam\nTFieldCos Phi Frq",
|
|
runNumbers: "2452",
|
|
selYear: "2026",
|
|
instrName: "LEM",
|
|
dataRoot: "/mnt/fake"
|
|
},
|
|
funOptions: [
|
|
{ value: "Asy1", disabled: false },
|
|
{ value: "Lam", disabled: false },
|
|
{ value: "Frq", disabled: false },
|
|
{ value: "Alpha1", disabled: false },
|
|
{ value: "Alpha2", disabled: false },
|
|
{ value: "One", disabled: false },
|
|
{ value: "r2", disabled: false },
|
|
{ value: "Phi1", disabled: false },
|
|
{ value: "Phi2", disabled: false }
|
|
],
|
|
funLines: [],
|
|
sharedFlags: {}
|
|
}, 2);
|
|
assert.match(blocks.theoryBlock, /^asymmetry fun1$/m);
|
|
assert.match(blocks.theoryBlock, /^TFieldCos map2 3$/m);
|
|
assert.strictEqual(blocks.functionsBlock.trim(), "fun1 = par1 * map1");
|
|
assert.doesNotMatch(blocks.runBlock, /undefined/);
|
|
}
|
|
|
|
const lib = loadLibrary();
|
|
testElectronPreferencesAndSuffixControls(lib);
|
|
testInitialFrequencyGuess(lib);
|
|
testReverseFieldInitialization(lib);
|
|
testResultArtifactRendering(lib);
|
|
testCompactConstraintsLayout(lib);
|
|
testCompactEditorResultsAndStatusLayout(lib);
|
|
testCompactJobStatusVisibility(lib);
|
|
testParameterPlotSelection(lib);
|
|
testInlineParameterPlotFromTable(lib);
|
|
testBeginJobPreparationPreservesParameterDataOnPlot(lib);
|
|
testParseMSRParameters(lib);
|
|
testNaNParameterErrorsNeverReachMSROrGUI(lib);
|
|
testParseAndApplyMSRGUISettings(lib);
|
|
testMSRSyncPreservesCompactRunSelection(lib);
|
|
testParsePlotRunsDirective(lib);
|
|
testParseDataRangeInGlobalBlock(lib);
|
|
testParseDataRangeInRunBlock(lib);
|
|
testBuildModelBlocksPreservesDataRange(lib);
|
|
testBuildAsymmetryDataRangeRunBlock(lib);
|
|
testSyncCommonDataRangeFromMSR(lib);
|
|
testSyncPerRunDataRangeFromMSR(lib);
|
|
testSyncSingleHistogramDataRangeFromMSR(lib);
|
|
testT0BgDataRangeValidation(lib);
|
|
testApplyT0BgOverridesToMSRReplacesDataLines(lib);
|
|
testParseMSRTheoryAndFunctions(lib);
|
|
testMultiAsymmetryRoundTrip(lib);
|
|
testParseFunctionMappedDetectorPhases(lib);
|
|
testFunctionExpressionValidation(lib);
|
|
testParseMSRRunSelectionWithAddRun(lib);
|
|
testParseMSRT0BgRunBlocks(lib);
|
|
testSyncCommonAsymmetryT0BgFromMSR(lib);
|
|
testSyncPerRunAsymmetryT0BgFromMSR(lib);
|
|
testSyncMultiGroupAsymmetryT0BgFromMSR(lib);
|
|
testSyncMultiGroupAsymmetryCommonCollapse(lib);
|
|
testSyncMultiGroupAsymmetryRejectsPartial(lib);
|
|
testSyncSingleHistogramT0BgFromMSR(lib);
|
|
testGroupedAsymmetryT0Generation(lib);
|
|
testGroupedAsymmetryT0Validation(lib);
|
|
testSyncT0BgRejectsAmbiguousMSRWithoutMutation(lib);
|
|
testGeneratedGlobalMSRValidation(lib);
|
|
testCurrentDataPathRules(lib);
|
|
testLEMDefaultHistograms(lib);
|
|
testNormalizeMSRPlotRuns(lib);
|
|
testFitParameterLineOmitsUnsetBounds(lib);
|
|
testBuildCommandsBlockFitOptions(lib);
|
|
testBuildFourierBlockOptions(lib);
|
|
testExactNameFitParameterSync(lib);
|
|
testBuildMSRUsesGUIParameterValues(lib);
|
|
testImportedMSRParameterOrderAndComments(lib);
|
|
testPhysicalParameterOrder(lib);
|
|
testSingleHistogramPlotRunsAllHistograms(lib);
|
|
testPlotYAxisRangeGeneration(lib);
|
|
testRRFPlotIsExplicitlyOptIn(lib);
|
|
testFFTAndRRFSettingsLiveInRuns(lib);
|
|
testViewSettingsDismissOnOutsideClick(lib);
|
|
testRRFControlsLiveInDisclosure(lib);
|
|
testLifetimeCorrectionRequiresSingleHistogram(lib);
|
|
testParseLifetimeCorrectionDefaultsOnForSingleHist(lib);
|
|
testT0BgOverridesInAsymmetryRunBlock(lib);
|
|
testT0BgOverridesInSingleHistogramRunBlock(lib);
|
|
testCommonT0BgValuesApplyToRunBlocks(lib);
|
|
testT0BgOverridesAppliedToGlobalMSR(lib);
|
|
testCommonT0BgValuesAppliedToGlobalMSR(lib);
|
|
testT0BgValidationRejectsIncompletePairs(lib);
|
|
testT0BgUseCommonIgnoresStaleRowBackground(lib);
|
|
testSeedRunParametersFromShared(lib);
|
|
testApplyMSRParametersUpdatesParameterTable(lib);
|
|
testCustomParameterNameReservation(lib);
|
|
testSyncSeriesParametersFromCombinedMSR(lib);
|
|
testDirtyEditorOverwriteGuard(lib);
|
|
testMSREditorEnterInsertsTextNewline(lib);
|
|
testLoadMSRIntoEditorClearsDirtyAndUpdatesTable(lib);
|
|
testMSREditorSourceTabsKeepInactiveEdits(lib);
|
|
testManualEditorInputLocksEditorSource(lib);
|
|
testSyncCurrentEditorToGUIKeepsEditorAuthoritative(lib);
|
|
testMSREditorHighlighting(lib);
|
|
testRunsSectionUsesCompactEmptyDefault(lib);
|
|
testRunListParsingAndActionValidation(lib);
|
|
testFFTOptionsIndicator(lib);
|
|
testRunPathDiagnosticIsSilentOnSuccess(lib);
|
|
testPrepareUsesLockedEditorSource(lib).then(function() {
|
|
return testPrepareUsesLockedTemplateSource(lib);
|
|
}).then(function() {
|
|
return testWebJobFolderChangesWithFirstRun(lib);
|
|
}).then(function() {
|
|
console.log("MSR generation tests passed");
|
|
}).catch(function(error) {
|
|
console.error(error);
|
|
process.exitCode = 1;
|
|
});
|