From ef8a7079d161334a6ae6623a55e3a02b33f60799 Mon Sep 17 00:00:00 2001 From: salman Date: Mon, 31 Aug 2026 19:36:47 +0200 Subject: [PATCH] Fix disabled-FFT FOURIER block and preserve editor MSR titles - buildFourierBlock now returns '' for fftType none/off and renderMSRTemplate drops the whole FOURIER section, since musrfit rejects an empty or 'plot none' block. Regenerate bounds.generated.msr (no FOURIER); verified it converges with real musrfit (exit 0). - runMSRFileLocal only passes musrfit -t (--title-from-data-file) when the MSR has no explicit first-line title. An editor-owned title is now preserved; GUI-generated MSRs (blank/# Run Numbers first line) still derive one. - Add tests/electron-msr-title.test.js regression coverage and wire into test:electron. Verified against real musrfit: titled MSR keeps its title. --- muSRFitelec.js | 30 +++- muSRFitlib.js | 14 ++ package.json | 2 +- tests/electron-msr-title.test.js | 144 ++++++++++++++++++ tests/fixtures/reference/bounds.generated.msr | 6 - 5 files changed, 188 insertions(+), 8 deletions(-) create mode 100644 tests/electron-msr-title.test.js diff --git a/muSRFitelec.js b/muSRFitelec.js index 66fae9f..782d7f6 100644 --- a/muSRFitelec.js +++ b/muSRFitelec.js @@ -639,6 +639,25 @@ function readAsciiFile(filename) { return(content); } +function hasExplicitMSRTitle(filename) { + // Return true when the MSR carries an explicit first-line title that + // musrfit's `-t` (--title-from-data-file) would otherwise replace. The first + // non-blank, non-comment line of a real MSR is its run title; a GUI-generated + // MSR starts with the `# Run Numbers:` comment (or a blank line) and has no + // title to preserve. + try { + let content = nodeFs.readFileSync(filename, "utf8"); + for (let rawLine of content.split(/\r?\n/)) { + let line = rawLine.trim(); + if (line === "") continue; + return line[0] !== "#"; + } + return false; + } catch (e) { + return false; + } +} + function deleteAsciiFile(filename) { // Remove a generated text file when the caller needs to avoid stale artifacts. // This is mainly used before msr2data, because its process status is not a @@ -1184,7 +1203,16 @@ async function runMSRFileLocal(fullPath, options = {}) { // Local fitting estimates N0 and derives the title from the data file. Keep // the Electron path timeout-free; musrfit writes fitted values back to MSR. - let musrfitArgs = ["-e", "-t", fitName]; + // + // `-t` (--title-from-data-file) tells musrfit to overwrite the MSR run title + // with the title of the first run's data file. GUI-generated MSRs have no + // title (they start with the `# Run Numbers:` comment), so deriving one is + // desirable there. But an editor-owned MSR may carry an explicit title the + // user wants preserved, which `-t` would silently clobber. Only pass `-t` + // when the target MSR has no such explicit first-line title. + let musrfitArgs = ["-e"]; + if (!hasExplicitMSRTitle(resolvedPath)) musrfitArgs.push("-t"); + musrfitArgs.push(fitName); // musrview should inspect the resulting fitted MSR. Fourier plotting is // controlled by the caller so the same function can serve normal and FFT UI. diff --git a/muSRFitlib.js b/muSRFitlib.js index 3337c41..692c15a 100644 --- a/muSRFitlib.js +++ b/muSRFitlib.js @@ -7874,6 +7874,14 @@ function buildCommandsBlock(all) { function buildFourierBlock(all) { all = all || {}; + // A disabled FFT has no native musrfit representation: musrfit rejects an + // empty block and `plot none`. Represent "FFT off" by omitting the whole + // FOURIER section (renderMSRTemplate drops the header for an empty block). + let plotType = all.fftType !== undefined ? String(all.fftType).trim() : ""; + if (plotType === "none" || plotType === "off") { + return ""; + } + // Build the FOURIER block as native directives rather than concatenating a // fixed template. Optional range/phase-correction controls can then disappear // cleanly when left at their defaults without producing empty directives. @@ -8283,6 +8291,12 @@ function renderMSRTemplate(blocks) { for (let placeholder in replacements) { msrFile = msrFile.replace(placeholder, replacements[placeholder]); } + // A disabled FFT produces an empty FOURIER block. musrfit has no "FFT off" + // directive and rejects an empty or `plot none` block, so drop the whole + // FOURIER section rather than emitting an invalid directive. + if (!blocks.fourierBlock) { + msrFile = msrFile.replace(/\nFOURIER\n/, "\n"); + } return msrFile; } diff --git a/package.json b/package.json index 70841c7..ace2a38 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "dist:linux": "npm run test:electron && npx --yes electron-builder@26.15.3 --linux rpm AppImage --x64", "dist:rpm": "npm run test:electron && npx --yes electron-builder@26.15.3 --linux rpm --x64", "dist:appimage": "npm run test:electron && npx --yes electron-builder@26.15.3 --linux AppImage --x64", - "test:electron": "node tests/log-highlighting.test.js && node tests/action-state.test.js && node tests/electron-missing-data.test.js && node tests/window-layering.test.js && node tests/msr-generation.test.js && node tests/web-runner.test.js && node tests/root-plot-clean.test.js && node tests/parameter-plot.test.js && node tests/help-window.test.js && node tests/release-packaging.test.js && node tests/addrun.test.js && node tests/fit-statistic.test.js && node tests/series-fit-planner.test.js && node tests/series-fit-result.test.js && node tests/series-fit-inventory.test.js && node tests/series-fit-msr-preparation.test.js && node tests/series-artifact-contract.test.js && node tests/series-global-merge.test.js && node tests/series-fit-staging.test.js && node tests/series-fit-runner.test.js && node tests/series-runner-adapters.test.js && node tests/series-fit-routing.test.js && node tests/singlehist-parity.test.js && node tests/url-parameters.test.js && node tests/shared-configuration.test.js && node tests/reference-fixtures.test.js" + "test:electron": "node tests/log-highlighting.test.js && node tests/action-state.test.js && node tests/electron-missing-data.test.js && node tests/electron-msr-title.test.js && node tests/window-layering.test.js && node tests/msr-generation.test.js && node tests/web-runner.test.js && node tests/root-plot-clean.test.js && node tests/parameter-plot.test.js && node tests/help-window.test.js && node tests/release-packaging.test.js && node tests/addrun.test.js && node tests/fit-statistic.test.js && node tests/series-fit-planner.test.js && node tests/series-fit-result.test.js && node tests/series-fit-inventory.test.js && node tests/series-fit-msr-preparation.test.js && node tests/series-artifact-contract.test.js && node tests/series-global-merge.test.js && node tests/series-fit-staging.test.js && node tests/series-fit-runner.test.js && node tests/series-runner-adapters.test.js && node tests/series-fit-routing.test.js && node tests/singlehist-parity.test.js && node tests/url-parameters.test.js && node tests/shared-configuration.test.js && node tests/reference-fixtures.test.js" }, "repository": { "type": "git", diff --git a/tests/electron-msr-title.test.js b/tests/electron-msr-title.test.js new file mode 100644 index 0000000..b0a52fb --- /dev/null +++ b/tests/electron-msr-title.test.js @@ -0,0 +1,144 @@ +// Copyright (C) 2026 Zaher Salman +// SPDX-License-Identifier: GPL-3.0-or-later + +// Regression coverage for the `musrfit -t` (--title-from-data-file) handling in +// runMSRFileLocal(). `-t` overwrites the MSR run title with the first run's data +// file title, which is desirable for GUI-generated MSRs (which start with the +// `# Run Numbers:` comment and carry no title) but would silently clobber an +// explicit title the user typed into an editor-owned MSR. This test asserts the +// flag is omitted when the target MSR carries an explicit title and retained +// when it does not. + +const assert = require("assert"); +const events = require("events"); +const fs = require("fs"); +const path = require("path"); +const vm = require("vm"); + +const repoRoot = path.resolve(__dirname, ".."); + +const missingOutput = [ + "PRunDataHandler::FileExistsCheck(): **ERROR** Couldn't find " + + "'/mnt/fake/lem/his/2026/lem26_his_0514' in any standard path.", + "musrfit **ERROR** Couldn't read all data files, will quit ..." +].join("\n"); + +function buildSandbox(msrContent) { + const spawnedCommands = []; + const fakeFs = { + appendFileSync: function() { + }, + existsSync: function() { + return false; + }, + readFileSync: function(filename, encoding) { + return msrContent; + }, + writeFileSync: function() { + } + }; + const fakeChildProcess = { + spawn: function(command, args) { + spawnedCommands.push({ command: command, args: args }); + const child = new events.EventEmitter(); + child.stdout = new events.EventEmitter(); + child.stderr = new events.EventEmitter(); + setImmediate(function() { + child.stderr.emit("data", Buffer.from(missingOutput)); + child.emit("close", 1, null); + }); + return child; + } + }; + const sandbox = { + alert: function(message) { + throw new Error("Unexpected alert: " + message); + }, + console: { log: function() {}, error: console.error, warn: console.warn }, + document: { getElementById: function() { return null; } }, + process: { + cwd: function() { return repoRoot; }, + env: {}, + resourcesPath: "" + }, + require: require, + window: { + MSREditor: {}, + MusrfitRunner: { fitActive: false }, + electronIpcRenderer: { on: function() {}, send: function() {} }, + nodeChildProcess: fakeChildProcess, + nodeFs: fakeFs, + nodePath: path + } + }; + sandbox.global = sandbox; + return { sandbox: sandbox, spawnedCommands: spawnedCommands }; +} + +function loadLibraries(sandbox) { + vm.runInNewContext( + fs.readFileSync(path.join(repoRoot, "muSRFitlib.js"), "utf8"), + sandbox, + { filename: "muSRFitlib.js" } + ); + vm.runInNewContext( + fs.readFileSync(path.join(repoRoot, "muSRFitelec.js"), "utf8"), + sandbox, + { filename: "muSRFitelec.js" } + ); +} + +function runFit(sandbox, path) { + sandbox.appendJobLog = function() {}; + sandbox.updateJobStatus = function() {}; + sandbox.setFitStatus = function() {}; + sandbox.finishJobStatus = function() {}; + return sandbox.runMSRFileLocal(path, {}); +} + +function musrfitArgs(sandbox, spawnedCommands) { + return new Promise(function(resolve, reject) { + runFit(sandbox, "/tmp/titled.msr").then(function(result) { + const musrfit = spawnedCommands.find(function(entry) { + return entry.command === "musrfit"; + }); + if (!musrfit) { + reject(new Error("musrfit was not spawned")); + return; + } + resolve(musrfit.args); + }).catch(reject); + }); +} + +Promise.resolve().then(function() { + // GUI-generated MSR: first line is the `# Run Numbers:` comment, no title. + let editorGui = buildSandbox("# Run Numbers: 514 2453 2454\n# Chisq ...\nFITPARAM ..."); + loadLibraries(editorGui.sandbox); + return musrfitArgs(editorGui.sandbox, editorGui.spawnedCommands).then(function(args) { + assert.ok(args.includes("-t"), "GUI MSR without a title must keep the -t flag"); + return "gui-title-kept"; + }); +}).then(function() { + // Editor-owned MSR with an explicit run title on the first line. A real run + // title is a non-comment first line (comments begin with '#'). + let editorTitled = buildSandbox("My Custom Editor Title\n# Run Numbers: 514 2453 2454\nFITPARAM ..."); + loadLibraries(editorTitled.sandbox); + return musrfitArgs(editorTitled.sandbox, editorTitled.spawnedCommands).then(function(args) { + assert.ok(!args.includes("-t"), "Editor-titled MSR must omit the -t flag"); + return "editor-title-preserved"; + }); +}).then(function(first) { + // Blank first line followed by a real title is also an explicit title. + let blankThenTitle = buildSandbox("\nMy Explicit Title\nFITPARAM ..."); + loadLibraries(blankThenTitle.sandbox); + return musrfitArgs(blankThenTitle.sandbox, blankThenTitle.spawnedCommands).then(function(args) { + assert.ok(!args.includes("-t"), "Blank-line-then-title MSR must omit the -t flag"); + return "blank-then-title-kept"; + }); +}).then(function() { + console.log("Electron MSR title handling tests passed"); +}).catch(function(error) { + console.error(error); + process.exitCode = 1; +}); diff --git a/tests/fixtures/reference/bounds.generated.msr b/tests/fixtures/reference/bounds.generated.msr index 8223d99..3b7025c 100644 --- a/tests/fixtures/reference/bounds.generated.msr +++ b/tests/fixtures/reference/bounds.generated.msr @@ -34,12 +34,6 @@ runs 1 use_fit_ranges ############################################################### -FOURIER -units Gauss -fourier_power 12 -dc-corrected FALSE -apodization STRONG -plot none ############################################################### # Created by muSRFlow (ZS https://bitbucket.org/zaher-salman/musrfit-gui)