Files
muSRFlow/tests/action-state.test.js
T
salman ccb25fb049 Turn the running Fit/Plot button into an Abort that kills hung jobs
musrfit/musrview can hang indefinitely and the only feedback was a disabled
Fit/Plot button. While a job is running (Electron only), the active button now
morphs into an enabled Abort that kills the last launched process: SIGTERM
through the preload bridge, then SIGKILL after a 3s grace period.

- preload: spawnCapture registers children under a caller job token; new
  abortJob(token, signal) kills it (SIGTERM then escalated SIGKILL)
- electron shim: runLocalProcess hands its token to the bridge, exposes
  window.triggerMusrfitAbort, reports signal exits as outcome 'aborted'
- shared lib: setMusrfitActionState morphs label/click and restores the
  original inline handler; finishJobStatus gains a warning-level 'aborted'
  outcome. Web jobs stay disabled (server-side, already time-limited)
2026-09-08 17:21:29 +02:00

221 lines
8.0 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, "..");
const elements = {};
function controlElement() {
return {
attributes: {},
className: "",
dataset: {},
disabled: false,
hidden: false,
textContent: "",
setAttribute: function(name, value) {
this.attributes[name] = String(value);
}
};
}
elements.FitMSR = controlElement();
elements.PlotMSR = controlElement();
elements.musrfitActionActivity = controlElement();
elements.musrfitActionActivityText = controlElement();
elements.appShell = controlElement();
elements.jobOutcomeDialog = Object.assign(controlElement(), {
open: false,
showModal: function() {
this.open = true;
}
});
elements.jobOutcomeTitle = controlElement();
elements.jobOutcomeSummary = controlElement();
elements.jobOutcomeDetails = controlElement();
const sandbox = {
console: { log: function() {}, error: console.error, warn: console.warn },
document: {
getElementById: function(id) {
return elements[id] || null;
}
},
window: { MSREditor: {} }
};
sandbox.global = sandbox;
vm.runInNewContext(
fs.readFileSync(path.join(repoRoot, "muSRFitlib.js"), "utf8"),
sandbox,
{ filename: "muSRFitlib.js" }
);
sandbox.amIWeb = function() {
return false;
};
function testActionControlStates() {
sandbox.setMusrfitActionState("fit", "preparing");
assert.strictEqual(sandbox.window.MusrfitAction.busy, true);
assert.strictEqual(elements.FitMSR.disabled, true);
assert.strictEqual(elements.PlotMSR.disabled, true);
assert.strictEqual(elements.FitMSR.textContent, "Fit");
assert.match(elements.FitMSR.className, /\bis-running\b/);
assert.strictEqual(elements.musrfitActionActivity.hidden, false);
assert.strictEqual(elements.musrfitActionActivityText.textContent, "Preparing fit files");
assert.strictEqual(elements.appShell.attributes["aria-busy"], "true");
sandbox.setMusrfitActionState("fit", "running");
assert.strictEqual(elements.FitMSR.textContent, "Fit");
assert.strictEqual(elements.musrfitActionActivityText.textContent, "Running musrfit");
sandbox.setMusrfitActionState("plot", "running");
assert.strictEqual(elements.PlotMSR.textContent, "Plot");
assert.strictEqual(elements.musrfitActionActivityText.textContent, "Running musrview");
sandbox.setMusrfitActionState("", "idle");
assert.strictEqual(sandbox.window.MusrfitAction.busy, false);
assert.strictEqual(elements.FitMSR.disabled, false);
assert.strictEqual(elements.PlotMSR.disabled, false);
assert.strictEqual(elements.FitMSR.textContent, "Fit");
assert.strictEqual(elements.PlotMSR.textContent, "Plot");
assert.strictEqual(elements.musrfitActionActivity.hidden, true);
assert.strictEqual(elements.appShell.attributes["aria-busy"], "false");
}
async function testDuplicateActionGuard() {
let notice = "";
sandbox.setFitStatus = function(message) {
notice = message;
};
sandbox.window.MusrfitAction.busy = true;
const result = await sandbox.startFit();
assert.strictEqual(result, 0);
assert.match(notice, /already running/);
sandbox.window.MusrfitAction.busy = false;
}
async function testActionLifecycleReleasesControls() {
let completeRun;
sandbox.validateRunSelection = function() {
return true;
};
sandbox.confirmOverwriteDirtyEditor = function() {
return true;
};
sandbox.prepareMSRRunFiles = async function() {
return { ok: 1, finalPath: "/tmp/1285_asy.msr", options: {} };
};
sandbox.runMSRFile = function() {
return new Promise(function(resolve) {
completeRun = resolve;
});
};
const runningPlot = sandbox.startPlot();
await new Promise(function(resolve) {
setImmediate(resolve);
});
assert.strictEqual(sandbox.window.MusrfitAction.busy, true);
assert.strictEqual(sandbox.window.MusrfitAction.phase, "running");
assert.strictEqual(elements.FitMSR.disabled, true);
assert.strictEqual(elements.PlotMSR.disabled, true);
completeRun(1);
assert.strictEqual(await runningPlot, 1);
assert.strictEqual(sandbox.window.MusrfitAction.busy, false);
assert.strictEqual(elements.FitMSR.disabled, false);
assert.strictEqual(elements.PlotMSR.disabled, false);
}
async function testAbortButtonMorph() {
sandbox.window.MusrfitAction.abortSupported = true;
let aborted = [];
sandbox.window.triggerMusrfitAbort = function(action) {
aborted.push(action);
};
sandbox.setMusrfitActionState("fit", "running");
assert.strictEqual(sandbox.window.MusrfitAction.busy, true);
assert.strictEqual(elements.FitMSR.disabled, false);
assert.strictEqual(elements.FitMSR.textContent, "Abort");
assert.match(elements.FitMSR.className, /\bis-running\b/);
assert.strictEqual(elements.PlotMSR.disabled, true);
assert.strictEqual(elements.PlotMSR.textContent, "Plot");
assert.strictEqual(elements.musrfitActionActivityText.textContent, "Running musrfit");
if (typeof elements.FitMSR.onclick === "function") {
elements.FitMSR.onclick({ preventDefault: function() {} });
}
assert.deepStrictEqual(aborted, ["fit"]);
sandbox.setMusrfitActionState("plot", "running");
assert.strictEqual(elements.PlotMSR.disabled, false);
assert.strictEqual(elements.PlotMSR.textContent, "Abort");
assert.match(elements.PlotMSR.className, /\bis-running\b/);
assert.strictEqual(elements.FitMSR.disabled, true);
assert.strictEqual(elements.FitMSR.textContent, "Fit");
if (typeof elements.PlotMSR.onclick === "function") {
elements.PlotMSR.onclick({ preventDefault: function() {} });
}
assert.deepStrictEqual(aborted, ["fit", "plot"]);
sandbox.setMusrfitActionState("", "idle");
assert.strictEqual(sandbox.window.MusrfitAction.busy, false);
assert.strictEqual(elements.FitMSR.disabled, false);
assert.strictEqual(elements.FitMSR.textContent, "Fit");
assert.strictEqual(elements.PlotMSR.disabled, false);
assert.strictEqual(elements.PlotMSR.textContent, "Plot");
sandbox.window.MusrfitAction.abortSupported = false;
delete sandbox.window.triggerMusrfitAbort;
}
function testMissingDataDialog() {
sandbox.renderJobStatus = function() {
};
sandbox.window.MusrfitJob.log = [];
const missing = sandbox.classifyMissingDataOutput(
"PRunDataHandler::FileExistsCheck(): **ERROR** Couldn't find "
+ "'/data/flame26_his_01285' in any standard path."
);
sandbox.finishJobStatus(0, 1, missing.details, missing.outcome);
assert.strictEqual(elements.jobOutcomeDialog.open, true);
assert.strictEqual(elements.jobOutcomeDialog.dataset.level, "warning");
assert.strictEqual(elements.jobOutcomeTitle.textContent, "Data file not found");
assert.strictEqual(elements.jobOutcomeSummary.textContent,
"Data file not found: flame26_his_01285");
assert.match(elements.jobOutcomeDetails.textContent, /Expected path:/);
}
function testActionMarkup() {
const html = fs.readFileSync(path.join(repoRoot, "index.html"), "utf8");
const css = fs.readFileSync(path.join(repoRoot, "ZGUI.css"), "utf8");
assert.match(html, /id="musrfitActionActivity"[\s\S]*?aria-live="polite"/);
assert.match(html, /id="jobOutcomeDialog"/);
assert.match(html, />Open run log<\/button>/);
assert.match(css, /\.action-activity-spinner/);
assert.match(css, /\.action-btn\.is-running:disabled/);
assert.match(css, /\.job-outcome-dialog/);
}
testActionControlStates();
testAbortButtonMorph().then(function() {
return testMissingDataDialog();
}).then(function() {
return testActionMarkup();
}).then(function() {
return testDuplicateActionGuard();
}).then(function() {
return testActionLifecycleReleasesControls();
}).then(function() {
console.log("Main action state tests passed");
}).catch(function(error) {
console.error(error);
process.exitCode = 1;
});