Files
muSRFlow/tests/electron-msr-title.test.js
salman ef8a7079d1 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.
2026-08-31 19:36:47 +02:00

145 lines
5.4 KiB
JavaScript

// 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;
});