Honor explicit reuse, refit, and skip choices while collecting reusable fitted runs without unnecessary refits. Keep web archives job-scoped, omit redundant Electron archives, and preserve failed-fit status while opening diagnostic plots. Extend regression coverage and document deferred post-1.0 security work.
347 lines
15 KiB
JavaScript
347 lines
15 KiB
JavaScript
// Copyright (C) 2026 Zaher Salman
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
const assert = require("assert");
|
|
const events = require("events");
|
|
const fs = require("fs");
|
|
const os = require("os");
|
|
const path = require("path");
|
|
const vm = require("vm");
|
|
|
|
const repoRoot = path.resolve(__dirname, "..");
|
|
|
|
function runnableMSR(run, converged) {
|
|
const lines = [
|
|
"FITPARAMETER",
|
|
"1 Asy_" + String(run).padStart(4, "0") + " 0.2 0.01",
|
|
"THEORY",
|
|
"asymmetry 1",
|
|
"GLOBAL",
|
|
"fittype 2",
|
|
"RUN /data/run" + run + " MUE4 PSI MUSR-ROOT",
|
|
"COMMANDS",
|
|
"MIGRAD",
|
|
"PLOT 2",
|
|
"runs 1"
|
|
];
|
|
if (converged) {
|
|
lines.push("STATISTIC --- 2026-08-30 12:00:00");
|
|
lines.push("chisq = 101.2, NDF = 100, chisq/NDF = 1.012");
|
|
}
|
|
return lines.join("\n") + "\n";
|
|
}
|
|
|
|
function withMusrfitRunComment(msrText) {
|
|
return msrText.replace(
|
|
/^(RUN\s+\S+\s+\S+\s+\S+\s+\S+)$/m,
|
|
"$1 (name beamline institute data-file-format)"
|
|
);
|
|
}
|
|
|
|
function loadElectronSandbox(fakeChildProcess, statusUpdates) {
|
|
const sandbox = {
|
|
alert: function(message) { throw new Error("Unexpected alert: " + message); },
|
|
console: console,
|
|
document: { getElementById: function() { return null; } },
|
|
process: process,
|
|
require: require,
|
|
window: {
|
|
MSREditor: {},
|
|
electronIpcRenderer: { on: function() {}, send: function() {} },
|
|
nodeChildProcess: fakeChildProcess,
|
|
nodeFs: fs,
|
|
nodePath: path
|
|
}
|
|
};
|
|
sandbox.global = 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" }
|
|
);
|
|
sandbox.appendJobLog = function() {};
|
|
sandbox.updateJobStatus = function(values) { statusUpdates.push(values); };
|
|
sandbox.finishJobStatus = function(values) { statusUpdates.push(values); };
|
|
return sandbox;
|
|
}
|
|
|
|
async function testElectronAdapterFitsOnlyOneMSR() {
|
|
const tempDirectory = fs.mkdtempSync(path.join(os.tmpdir(), "musrflow-series-runner-"));
|
|
const spawned = [];
|
|
const statusUpdates = [];
|
|
try {
|
|
const targetPath = path.join(tempDirectory, "515_asy.msr");
|
|
const fakeChildProcess = {
|
|
spawn: function(command, args, options) {
|
|
spawned.push({ command: command, args: args.slice(), cwd: options.cwd });
|
|
const child = new events.EventEmitter();
|
|
child.stdout = new events.EventEmitter();
|
|
child.stderr = new events.EventEmitter();
|
|
setImmediate(function() {
|
|
const fittedPath = path.join(options.cwd, args[args.length - 1]);
|
|
fs.writeFileSync(fittedPath,
|
|
withMusrfitRunComment(runnableMSR(515, true)), "utf8");
|
|
child.stdout.emit("data", Buffer.from("fit completed\n"));
|
|
child.emit("close", 0, null);
|
|
});
|
|
return child;
|
|
},
|
|
spawnSync: function() {
|
|
throw new Error("msr2data must not run");
|
|
}
|
|
};
|
|
const sandbox = loadElectronSandbox(fakeChildProcess, statusUpdates);
|
|
|
|
const result = await sandbox.runPreparedSeriesFitSequence(
|
|
{
|
|
ok: true,
|
|
referenceRun: 514,
|
|
excluded: [{ run: 514, action: "skip" }],
|
|
candidates: [{
|
|
run: 515,
|
|
action: "fit",
|
|
targetPath: targetPath,
|
|
text: runnableMSR(515, false)
|
|
}]
|
|
},
|
|
sandbox.window.MusrfitSeriesTransactionAdapter,
|
|
sandbox.window.runSeriesMSRCandidateLocal,
|
|
{ transactionId: "electron-run" }
|
|
);
|
|
|
|
assert.strictEqual(result.ok, true);
|
|
assert.strictEqual(result.reason, "series-sequence-completed");
|
|
assert.strictEqual(spawned.length, 1);
|
|
assert.match(spawned[0].command, /musrfit$/);
|
|
assert.deepStrictEqual(Array.from(spawned[0].args), ["-e", "-t", "515_asy.msr"]);
|
|
assert.strictEqual(spawned[0].cwd, tempDirectory);
|
|
assert.strictEqual(statusUpdates.length, 0,
|
|
"per-run adapter must not finish the aggregate job status");
|
|
assert.strictEqual(sandbox.window.MusrfitRunner.fitActive, false);
|
|
assert.strictEqual(fs.readFileSync(targetPath, "utf8"),
|
|
withMusrfitRunComment(runnableMSR(515, true)));
|
|
assert.notStrictEqual(
|
|
sandbox.seriesRunDirectiveIdentity(runnableMSR(515, false)),
|
|
sandbox.seriesRunDirectiveIdentity(runnableMSR(516, false)),
|
|
"a genuine data path/run change must still change the identity"
|
|
);
|
|
assert.match(fs.readFileSync(path.join(tempDirectory, "515_asy.out"), "utf8"),
|
|
/musrfit run 515 exited with code 0/);
|
|
assert.strictEqual(fs.readdirSync(tempDirectory).some(function(name) {
|
|
return /musrflow-(?:stage|backup)/.test(name);
|
|
}), false);
|
|
} finally {
|
|
fs.rmSync(tempDirectory, { recursive: true, force: true });
|
|
}
|
|
}
|
|
|
|
async function testElectronAdapterRetainsRejectedNewMSR() {
|
|
const tempDirectory = fs.mkdtempSync(path.join(os.tmpdir(), "musrflow-series-retain-"));
|
|
const statusUpdates = [];
|
|
try {
|
|
const targetPath = path.join(tempDirectory, "514_asy.msr");
|
|
const rejectedOutput = runnableMSR(514, false);
|
|
const fakeChildProcess = {
|
|
spawn: function(command, args, options) {
|
|
const child = new events.EventEmitter();
|
|
child.stdout = new events.EventEmitter();
|
|
child.stderr = new events.EventEmitter();
|
|
setImmediate(function() {
|
|
fs.writeFileSync(path.join(options.cwd, args[args.length - 1]),
|
|
rejectedOutput, "utf8");
|
|
child.stdout.emit("data", Buffer.from("fit completed\n"));
|
|
child.emit("close", 0, null);
|
|
});
|
|
return child;
|
|
},
|
|
spawnSync: function() {
|
|
throw new Error("msr2data must not run");
|
|
}
|
|
};
|
|
const sandbox = loadElectronSandbox(fakeChildProcess, statusUpdates);
|
|
const result = await sandbox.runPreparedSeriesFitSequence(
|
|
{
|
|
ok: true,
|
|
referenceRun: 0,
|
|
excluded: [],
|
|
candidates: [{
|
|
run: 514,
|
|
action: "fit",
|
|
targetPath: targetPath,
|
|
text: runnableMSR(514, false)
|
|
}]
|
|
},
|
|
sandbox.window.MusrfitSeriesTransactionAdapter,
|
|
sandbox.window.runSeriesMSRCandidateLocal,
|
|
{ transactionId: "electron-retain" }
|
|
);
|
|
|
|
assert.strictEqual(result.ok, false);
|
|
assert.strictEqual(result.reason, "series-candidate-run-failed-retained");
|
|
assert.strictEqual(result.failed.retained, true);
|
|
assert.strictEqual(result.failed.runResult.reason,
|
|
"series-candidate-not-converged");
|
|
assert.strictEqual(fs.readFileSync(targetPath, "utf8"), rejectedOutput,
|
|
"a newly created rejected MSR must remain available for inspection");
|
|
assert.strictEqual(fs.existsSync(path.join(tempDirectory, "514_asy.out")), true);
|
|
assert.strictEqual(fs.readdirSync(tempDirectory).some(function(name) {
|
|
return /musrflow-(?:stage|backup)/.test(name);
|
|
}), false);
|
|
} finally {
|
|
fs.rmSync(tempDirectory, { recursive: true, force: true });
|
|
}
|
|
}
|
|
|
|
async function testElectronSeriesArtifactCollection() {
|
|
const tempDirectory = fs.mkdtempSync(path.join(os.tmpdir(), "musrflow-series-collect-"));
|
|
const statusUpdates = [];
|
|
const syncCalls = [];
|
|
const asyncCalls = [];
|
|
try {
|
|
for (const run of [514, 515]) {
|
|
fs.writeFileSync(path.join(tempDirectory, run + "_asy.msr"),
|
|
runnableMSR(run, true), "utf8");
|
|
}
|
|
const genuineGlobal = path.join(tempDirectory, "514+global_asy.msr");
|
|
const genuineTemplate = path.join(tempDirectory, "514+global_tmpl.msr");
|
|
fs.writeFileSync(genuineGlobal, "genuine global fit\n", "utf8");
|
|
fs.writeFileSync(genuineTemplate, "genuine global template\n", "utf8");
|
|
const fakeChildProcess = {
|
|
spawnSync: function(command, args, options) {
|
|
syncCalls.push({ command: command, args: args.slice(), cwd: options.cwd });
|
|
fs.writeFileSync(path.join(options.cwd, "514+series_asy_par.dat"), [
|
|
"dataT dataTErr Alpha_0514 Alpha_0514PosErr Alpha_0514NegErr "
|
|
+ "my_parameter_0514 my_parameter_0514PosErr CHISQ NDF CHISQred RUN",
|
|
"5 0.1 0.2 0.01 0.01 1.5 0.1 100 95 1.0526 514",
|
|
"6 0.1 0.21 0.01 0.01 1.6 0.1 102 95 1.0737 515",
|
|
""
|
|
].join("\n"), "utf8");
|
|
return { status: 7, stdout: "parameter data written\n", stderr: "" };
|
|
},
|
|
spawn: function(command, args, options) {
|
|
asyncCalls.push({ command: command, args: args.slice(), cwd: options.cwd });
|
|
const child = new events.EventEmitter();
|
|
child.stdout = new events.EventEmitter();
|
|
child.stderr = new events.EventEmitter();
|
|
setImmediate(function() { child.emit("close", 0, null); });
|
|
return child;
|
|
}
|
|
};
|
|
const sandbox = loadElectronSandbox(fakeChildProcess, statusUpdates);
|
|
const result = await sandbox.window.collectIndependentSeriesArtifactsLocal({
|
|
ok: true,
|
|
workPath: tempDirectory,
|
|
fileNameSuffix: "_asy",
|
|
runs: ["514", "515"],
|
|
msrPaths: [
|
|
path.join(tempDirectory, "514_asy.msr"),
|
|
path.join(tempDirectory, "515_asy.msr")
|
|
],
|
|
fftv: "n"
|
|
});
|
|
|
|
assert.strictEqual(result.ok, true);
|
|
assert.strictEqual(result.parameterDataPath,
|
|
path.join(tempDirectory, "514+series_asy_par.dat"));
|
|
assert.strictEqual(result.combinedMSRPath,
|
|
path.join(tempDirectory, "514+series_asy.msr"));
|
|
assert.strictEqual(result.plotStarted, true);
|
|
assert.strictEqual(result.templatePath,
|
|
path.join(tempDirectory, "514_asy.msr"));
|
|
assert.strictEqual(result.packagePath, "",
|
|
"Electron keeps artifacts in the working directory without packaging them");
|
|
assert.match(result.warnings.join("\n"), /returned code 7/);
|
|
assert.strictEqual(syncCalls.length, 1);
|
|
assert.deepStrictEqual(Array.from(syncCalls[0].args),
|
|
["[514 515]", "_asy", "new", "data", "-o", "514+series_asy_par.dat"]);
|
|
assert.deepStrictEqual(Array.from(asyncCalls[0].args), ["514+series_asy.msr"]);
|
|
const parameterHeader = fs.readFileSync(result.parameterDataPath, "utf8")
|
|
.split(/\r?\n/)[0];
|
|
assert.strictEqual(parameterHeader,
|
|
"dataT dataTErr Alpha AlphaPosErr AlphaNegErr "
|
|
+ "my_parameter my_parameterPosErr CHISQ NDF CHISQred RUN");
|
|
const combinedText = fs.readFileSync(result.combinedMSRPath, "utf8");
|
|
assert.match(combinedText, /^runs\s+1\s+2$/m);
|
|
assert.match(combinedText, /^# Common parameters for all runs$/m);
|
|
assert.match(combinedText, /^# Specific parameters for run 514$/m);
|
|
assert.match(combinedText, /^ 1 Asy_0514 0\.2 0\.01$/m);
|
|
assert.match(combinedText, /^ 2 Asy_0515 0\.2 0\.01$/m);
|
|
assert.match(combinedText, /^asymmetry map1$/m);
|
|
assert.strictEqual(fs.existsSync(path.join(tempDirectory, "514+series_asy.msr")), true);
|
|
assert.strictEqual(fs.readFileSync(genuineGlobal, "utf8"), "genuine global fit\n");
|
|
assert.strictEqual(fs.readFileSync(genuineTemplate, "utf8"), "genuine global template\n");
|
|
assert.strictEqual(fs.readdirSync(tempDirectory).some(function(name) {
|
|
return /^514\+global_musrflow_series_/.test(name);
|
|
}), false);
|
|
await new Promise(function(resolve) { setImmediate(resolve); });
|
|
} finally {
|
|
fs.rmSync(tempDirectory, { recursive: true, force: true });
|
|
}
|
|
}
|
|
|
|
async function testElectronParameterCollectionSurvivesDisplayMergeFailure() {
|
|
const tempDirectory = fs.mkdtempSync(path.join(os.tmpdir(), "musrflow-series-data-only-"));
|
|
const syncCalls = [];
|
|
const asyncCalls = [];
|
|
try {
|
|
fs.writeFileSync(path.join(tempDirectory, "514_asy.msr"),
|
|
runnableMSR(514, true), "utf8");
|
|
fs.writeFileSync(path.join(tempDirectory, "515_asy.msr"),
|
|
runnableMSR(515, true).replace("asymmetry 1", "asymmetry 1\nsimplExpo 1"),
|
|
"utf8");
|
|
const sandbox = loadElectronSandbox({
|
|
spawnSync: function(command, args, options) {
|
|
syncCalls.push(args.slice());
|
|
fs.writeFileSync(path.join(options.cwd, "514+series_asy_par.dat"),
|
|
"RUN Asy_\n514 0.2\n515 0.21\n", "utf8");
|
|
return { status: 0, stdout: "", stderr: "" };
|
|
},
|
|
spawn: function(command, args) {
|
|
asyncCalls.push(args.slice());
|
|
const child = new events.EventEmitter();
|
|
child.stdout = new events.EventEmitter();
|
|
child.stderr = new events.EventEmitter();
|
|
return child;
|
|
}
|
|
}, []);
|
|
const result = await sandbox.window.collectIndependentSeriesArtifactsLocal({
|
|
ok: true,
|
|
workPath: tempDirectory,
|
|
fileNameSuffix: "_asy",
|
|
runs: ["514", "515"],
|
|
msrPaths: [
|
|
path.join(tempDirectory, "514_asy.msr"),
|
|
path.join(tempDirectory, "515_asy.msr")
|
|
],
|
|
fftv: "n"
|
|
});
|
|
|
|
assert.strictEqual(result.ok, true);
|
|
assert.strictEqual(result.parameterDataPath,
|
|
path.join(tempDirectory, "514+series_asy_par.dat"));
|
|
assert.strictEqual(result.combinedMSRPath, "");
|
|
assert.strictEqual(result.plotStarted, false);
|
|
assert.strictEqual(syncCalls.length, 1);
|
|
assert.strictEqual(asyncCalls.length, 0);
|
|
assert.strictEqual(result.packagePath, "");
|
|
assert.match(result.warnings.join("\n"), /series-merge-theory-mismatch/);
|
|
} finally {
|
|
fs.rmSync(tempDirectory, { recursive: true, force: true });
|
|
}
|
|
}
|
|
|
|
Promise.resolve().then(testElectronAdapterFitsOnlyOneMSR)
|
|
.then(testElectronAdapterRetainsRejectedNewMSR)
|
|
.then(testElectronSeriesArtifactCollection)
|
|
.then(testElectronParameterCollectionSurvivesDisplayMergeFailure).then(function() {
|
|
console.log("series runner adapter tests passed");
|
|
}).catch(function(error) {
|
|
console.error(error);
|
|
process.exit(1);
|
|
});
|