Files
muSRFlow/tests/fit-statistic.test.js
salman 7ca50e1538 Show per-run chi2 for independent series fits from the status bar
The status bar previously reported only the last fitted run's statistic
for a series. Series plans already parse one statistic per data set from
the MSR on disk, so every selected run (reused, skipped, or fresh) is now
snapshotted into job state before running, and each rebuilt run is
re-parsed from its new MSR afterwards.

The single status-bar slot now shows a terse worst/best aggregate
(e.g. "chi2/NDF worst 1.24 / best 0.98 * 5 runs"), turns red and reads
"Series did not converge" when any run fails, and becomes a clickable
button that opens a per-run dialog: worst-first or run-order table with
action, statistic, converged state, file, median-outlier and stale-row
marking. Single/global fits are unchanged and keep the exact statistic.

Unit-tested across parsing, aggregation, convergence, snapshot,
write-back, status-bar rendering, and the dialog table; verified in the
hardened Electron renderer.
2026-09-10 13:51:45 +02:00

155 lines
6.6 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: {} }
};
sandbox.global = sandbox;
vm.runInNewContext(code, sandbox, { filename: "muSRFitlib.js" });
return sandbox;
}
function statusElement() {
return {
textContent: "",
hidden: false,
disabled: false,
title: "",
attributes: {},
setAttribute: function(name, value) { this.attributes[name] = String(value); }
};
}
function testStatisticParsing(lib) {
// These strings follow musrfit's overall STATISTIC-line format. Expected and
// per-run values may follow, but the status bar must keep the overall result.
let chisq = lib.parseMSRFitStatistic([
"STATISTIC --- 2026-08-20 21:00:00",
" chisq = 101.2, NDF = 100, chisq/NDF = 1.012000",
" expected chisq = 99.0, NDF = 100, expected chisq/NDF = 0.990000"
].join("\n"));
assert.strictEqual(chisq.found, true);
assert.strictEqual(chisq.converged, true);
assert.strictEqual(chisq.kind, "chisq");
assert.strictEqual(chisq.ndf, "100");
assert.strictEqual(chisq.value, "1.012000");
assert.strictEqual(chisq.text, "χ²/NDF 1.012");
let maxLH = lib.parseMSRFitStatistic([
"STATISTIC --- 2026-08-20 21:00:00",
" maxLH = 997.0, NDF = 1000, maxLH/NDF = 0.997000"
].join("\n"));
assert.strictEqual(maxLH.converged, true);
assert.strictEqual(maxLH.kind, "maxLH");
assert.strictEqual(maxLH.text, "maxLH/NDF 0.997");
let failed = lib.parseMSRFitStatistic([
"STATISTIC --- 2026-08-20 21:00:00",
"*** FIT DID NOT CONVERGE ***"
].join("\n"));
assert.strictEqual(failed.found, true);
assert.strictEqual(failed.converged, false);
assert.strictEqual(failed.text, "Fit did not converge");
let absent = lib.parseMSRFitStatistic("TITLE\nNo statistic yet");
assert.strictEqual(absent.found, false);
assert.strictEqual(absent.text, "");
}
function testStatusRendering(lib) {
const elements = {
appStatusBar: { dataset: {} },
jobStatusState: statusElement(),
jobStatusStatistic: 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 = "Finished";
lib.window.MusrfitJob.mode = "Web";
lib.window.MusrfitJob.message = "Fit completed";
lib.window.MusrfitJob.messageDetail = "Fit completed\nDetailed diagnostic";
lib.window.MusrfitJob.file = "514_asy.msr";
lib.window.MusrfitJob.resultMSR = "/work/514_asy.msr";
lib.window.MusrfitJob.log = ["fit completed"];
lib.window.MusrfitJob.fitStatistic = "χ²/NDF 1.012";
lib.window.MusrfitJob.fitConverged = true;
lib.renderJobStatus();
assert.strictEqual(elements.jobStatusStatistic.hidden, false);
assert.strictEqual(elements.jobStatusStatistic.textContent, "χ²/NDF 1.012");
assert.strictEqual(elements.jobStatusStatistic.title, "χ²/NDF 1.012");
assert.strictEqual(elements.jobStatusMessage.title,
"Fit completed\nDetailed diagnostic\n\nOpen run log.");
assert.strictEqual(elements.jobStatusFile.title, "/work/514_asy.msr");
assert.strictEqual(elements.jobStatusState.title, "Finished\n\nOpen run log.");
assert.strictEqual(elements.jobStatusState.attributes["aria-label"],
"Finished. Open run log.");
assert.strictEqual(elements.jobStatusMessage.attributes["aria-label"],
"Fit completed\nDetailed diagnostic. Open run log.");
assert.strictEqual(elements.jobStatusAnnouncement.textContent,
"Finished. χ²/NDF 1.012. Fit completed");
assert.strictEqual(elements.appStatusBar.dataset.fitState, "converged");
lib.setAppNotice("Could not prepare the global MSR.", {
level: "error",
details: "Could not prepare the global MSR.\nPermission denied: /work/global.msr"
});
assert.strictEqual(elements.appStatusBar.dataset.noticeLevel, "error");
assert.strictEqual(elements.jobStatusMessage.textContent, "Could not prepare the global MSR.");
assert.strictEqual(elements.jobStatusMessage.title,
"Could not prepare the global MSR.\nPermission denied: /work/global.msr\n\nOpen run log.");
lib.setJobLogExpanded(true);
assert.strictEqual(elements.jobStatusState.attributes["aria-expanded"], "true");
assert.strictEqual(elements.jobStatusMessage.attributes["aria-expanded"], "true");
lib.setJobLogExpanded(false);
assert.strictEqual(elements.jobStatusState.attributes["aria-expanded"], "false");
assert.strictEqual(elements.jobStatusMessage.attributes["aria-expanded"], "false");
lib.window.MusrfitJob.fitStatistic = "Fit did not converge";
lib.window.MusrfitJob.fitConverged = false;
lib.renderJobStatus();
assert.strictEqual(elements.appStatusBar.dataset.fitState, "not-converged");
}
function testStatusMarkup() {
const html = fs.readFileSync(path.join(repoRoot, "index.html"), "utf8");
const css = fs.readFileSync(path.join(repoRoot, "ZGUI.css"), "utf8");
assert.match(html, /id="jobStatusStatistic"[^>]*\shidden/);
assert.match(html, /id="jobStatusStatistic"[^>]*onclick="showSeriesStatisticsDialog\(\);"/);
assert.match(html, /id="seriesStatisticDialog"[\s\S]*?id="seriesStatisticTable"/);
assert.match(html, /id="jobStatusMessage"[\s\S]*?onclick="showJobLogWindow\(\);"/);
assert.match(html, /id="jobStatusAnnouncement"[\s\S]*?role="status"[\s\S]*?aria-live="polite"/);
assert.match(html, /id="jobStatusState"[\s\S]*?aria-controls="jobLogWindow"/);
assert.match(css, /\.status-log-button\s*\{[\s\S]*?min-height:\s*26px/);
assert.match(css, /data-notice-level="error"/);
assert.match(css, /data-notice-level="warning"/);
assert.match(css, /data-notice-level="success"/);
}
const lib = loadLibrary();
testStatisticParsing(lib);
testStatusRendering(lib);
testStatusMarkup();
console.log("Fit statistic status tests passed");