Files
muSRFlow/tests/series-statistic.test.js
salman 6d6caf0fd8 Sort series statistics table by run number, drop worst-first toggle
The per-run dialog previously defaulted to worst-first with a button to
switch to run order. Run order is the useful view, so the table now always
sorts by ascending run number and the toggle button and toolbar are removed.
2026-09-10 14:17:38 +02:00

365 lines
14 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(documentOverrides) {
const sandbox = {
console: { log: function() {}, error: console.error, warn: console.warn },
document: {
getElementById: function() { return null; },
createElement: function() {
return {
appendChild: function() {},
className: "",
colSpan: 0,
textContent: "",
title: ""
};
},
querySelectorAll: function() { return []; }
},
window: {}
};
sandbox.global = sandbox;
vm.runInNewContext(
fs.readFileSync(path.join(repoRoot, "muSRFitlib.js"), "utf8"),
sandbox,
{ filename: "muSRFitlib.js" }
);
if (documentOverrides) {
for (const key of Object.keys(documentOverrides)) {
sandbox.document[key] = documentOverrides[key];
}
}
return sandbox;
}
function chisqRow(run, value, extra) {
return Object.assign({
run: run,
action: "reuse",
found: true,
converged: true,
kind: "chisq",
value: String(value),
ndf: "100",
text: "χ²/NDF " + value,
path: "/work/" + run + "_asy.msr",
fresh: false
}, extra);
}
function fittedMSR(run, value) {
return [
"FITPARAM --- " + run,
"STATISTIC --- 2026-08-20 21:00:00",
" chisq = 1.0, NDF = 100, chisq/NDF = " + value
].join("\n");
}
function testSeriesStatisticValue(lib) {
assert.strictEqual(lib.seriesStatisticValue(chisqRow(2531, "1.012")), 1.012);
assert.strictEqual(lib.seriesStatisticValue(null), null);
assert.strictEqual(lib.seriesStatisticValue({ found: true, converged: false,
kind: "chisq", value: "5" }), null);
assert.strictEqual(lib.seriesStatisticValue({ found: true, converged: true,
kind: "", value: "5" }), null);
}
function testSeriesStatisticText(lib) {
assert.strictEqual(lib.seriesFitStatisticText([]), "");
assert.strictEqual(lib.seriesFitStatisticText(null), "");
assert.strictEqual(lib.seriesFitStatisticText([chisqRow(2531, "1.012")]),
"χ²/NDF 1.012", "a single run keeps its exact statistic");
const spread = lib.seriesFitStatisticText([
chisqRow(2531, "1.01"),
chisqRow(2534, "1.24"),
chisqRow(2536, "0.98")
]);
assert.strictEqual(spread, "χ²/NDF worst 1.24 / best 0.98 · 3 runs",
"a converged series reports the worst/best spread");
const mixed = lib.seriesFitStatisticText([
chisqRow(2531, "1.01"),
{ run: 2534, found: true, converged: true, kind: "maxLH",
value: "0.9", ndf: "100", text: "maxLH/NDF 0.9", fresh: false }
]);
assert.strictEqual(mixed, "2 series runs",
"mixed fit objectives fall back to a cohort count");
const notConverged = lib.seriesFitStatisticText([
chisqRow(2531, "1.01"),
{ run: 2534, found: true, converged: false, kind: "chisq",
value: "", ndf: "", text: "", fresh: false }
]);
assert.strictEqual(notConverged, "Series did not converge",
"any non-converged run is called out");
}
function testSeriesFitConverged(lib) {
assert.strictEqual(lib.seriesFitConverged([
chisqRow(2531, "1.01"), chisqRow(2534, "1.1")
]), true);
assert.strictEqual(lib.seriesFitConverged([
chisqRow(2531, "1.01"),
Object.assign(chisqRow(2534, "5"), { converged: false })
]), false);
assert.strictEqual(lib.seriesFitConverged([{ found: false }]), null);
assert.strictEqual(lib.seriesFitConverged([]), null);
assert.strictEqual(lib.seriesFitConverged([{ found: false, converged: false }]),
false, "an explicit failure must win even without a numerical value");
}
function testCollectSeriesStatisticRows(lib) {
const plan = {
mode: "series",
entries: [
{ run: 2531, action: "skip", statistic: { found: true, converged: true,
kind: "chisq", value: "1.01", ndf: "100", text: "χ²/NDF 1.01" },
msrPath: "/work/2531_asy.msr" },
{ run: 2534, action: "fit", statistic: { found: false, converged: null,
kind: "", value: "", ndf: "", text: "" }, msrPath: "" }
]
};
const rows = lib.collectSeriesStatisticRows(plan);
assert.strictEqual(rows.length, 2);
assert.strictEqual(rows[0].run, 2531);
assert.strictEqual(rows[0].action, "skip");
assert.strictEqual(rows[0].converged, true);
assert.strictEqual(rows[0].path, "/work/2531_asy.msr");
assert.strictEqual(rows[0].fresh, false);
assert.strictEqual(rows[1].found, false);
assert.strictEqual(rows[1].action, "fit");
assert.strictEqual(lib.collectSeriesStatisticRows({ entries: [] }).length, 0);
assert.strictEqual(lib.collectSeriesStatisticRows(null).length, 0);
// Real plan entries carry the parsed on-disk statistic on classification,
// not on the entry itself. Reused/skipped rows must surface those values.
const reused = lib.collectSeriesStatisticRows({
entries: [{
run: 2537, action: "skip",
classification: {
statistic: { found: true, converged: true, kind: "chisq",
value: "0.87", ndf: "100", text: "χ²/NDF 0.87" }
},
msrPath: "/work/2537_asy.msr"
}]
})[0];
assert.strictEqual(reused.text, "χ²/NDF 0.87");
assert.strictEqual(reused.kind, "chisq");
assert.strictEqual(reused.value, "0.87");
assert.strictEqual(reused.ndf, "100");
assert.strictEqual(reused.found, true);
assert.strictEqual(reused.converged, true);
}
async function testSynchronizeSeriesStatistics(lib) {
const read = async function(filePath) {
return filePath.indexOf("2534") >= 0 ? fittedMSR(2534, "1.234") : "TITLE\nnone";
};
const rows = [
chisqRow(2531, "1.01"),
Object.assign(chisqRow(2534, "9.99"), { action: "refit" })
];
const sequence = {
completed: [
{ run: 2534, targetPath: "/work/2534_asy.msr", runResult: {} }
]
};
const updated = await lib.synchronizeSeriesStatistics(sequence, {
rows: rows, readAsciiFile: read
});
assert.strictEqual(updated.length, 2);
assert.strictEqual(updated[1].action, "fit", "a rebuilt run is now fitted");
assert.strictEqual(updated[1].fresh, true);
assert.strictEqual(updated[1].value, "1.234");
assert.strictEqual(updated[1].text, "χ²/NDF 1.234");
assert.strictEqual(updated[0].text, "χ²/NDF 1.01", "unchanged runs keep their rows");
// Without a read function the row is still marked fitted but not re-parsed.
const missingRead = await lib.synchronizeSeriesStatistics(sequence, { rows: rows });
assert.strictEqual(missingRead[1].action, "fit");
assert.strictEqual(missingRead[1].value, "1.234");
}
function testApplySeriesFitStatistic(lib) {
const rows = [chisqRow(2531, "1.01"), chisqRow(2534, "1.24")];
const stored = lib.applySeriesFitStatistic(rows);
assert.strictEqual(lib.window.MusrfitJob.fitStatistic,
"χ²/NDF worst 1.24 / best 1.01 · 2 runs");
assert.strictEqual(lib.window.MusrfitJob.fitConverged, true);
assert.strictEqual(lib.window.MusrfitJob.seriesStatistic.length, 2);
assert.ok(stored !== rows, "the stored array must be a separate copy");
}
function testRenderJobStatusSeries(lib) {
const statistic = {
textContent: "",
hidden: false,
disabled: false,
title: "",
className: "",
classList: {
toggled: [],
toggle: function(cls) { this.toggled.push(cls); }
},
attributes: {},
setAttribute: function(name, value) { this.attributes[name] = String(value); }
};
const statusBar = { dataset: {} };
const elements = {
appStatusBar: statusBar,
jobStatusState: { textContent: "", disabled: true, title: "", attributes: {},
setAttribute: function() {} },
jobStatusStatistic: statistic,
jobStatusMessage: { textContent: "", hidden: true, title: "", disabled: true,
attributes: {}, setAttribute: function() {} },
jobStatusFile: { textContent: "", hidden: true, title: "" },
jobStatusDirectory: { textContent: "", hidden: true, title: "" },
jobStatusAnnouncement: { textContent: "" },
resultArtifactList: { hidden: true },
resultPrimaryFiles: { hidden: true },
resultOtherFiles: { hidden: true },
resultArtifactEmpty: { hidden: false },
jobDetailExitCode: { textContent: "" }
};
const dom = loadLibrary({
getElementById: function(id) { return elements[id] || null; }
});
dom.window.MusrfitJob.state = "Finished";
dom.window.MusrfitJob.message = "Series fit completed";
dom.window.MusrfitJob.log = ["done"];
dom.window.MusrfitJob.seriesStatistic = [chisqRow(2531, "1.01"), chisqRow(2534, "1.24")];
dom.window.MusrfitJob.fitStatistic = "χ²/NDF worst 1.24 / best 1.01 · 2 runs";
dom.renderJobStatus();
assert.strictEqual(statistic.hidden, false);
assert.strictEqual(statistic.disabled, false,
"a series aggregate must be clickable");
assert.ok(statistic.classList.toggled.indexOf("status-log-button") >= 0,
"the series aggregate gains the log-button affordance");
assert.ok(/Show per-run statistics\./.test(statistic.attributes["aria-label"]),
"the click target announces the per-run table");
}
function makeTableHarness() {
let appended = [];
const container = { textContent: "", appendChild: function(el) { appended.push(el); } };
const dom = loadLibrary({
getElementById: function(id) {
return id === "seriesStatisticTable" ? container : null;
},
createElement: function(tag) {
const el = { className: "", colSpan: 0, textContent: "", title: "" };
el.appendChild = function(child) {
(el.childElements = el.childElements || []).push(child);
};
return el;
}
});
return { dom: dom, appended: appended, cells: function(tr) { return tr.childElements; } };
}
function testRenderSeriesStatisticsTableRunOrder() {
const h = makeTableHarness();
h.dom.renderSeriesStatisticsTable([
chisqRow(2531, "1.01"),
Object.assign(chisqRow(2534, "0.5"), { fresh: true }),
Object.assign(chisqRow(2536, "5.0")),
Object.assign(chisqRow(2538, "1.02"),
{ converged: false, text: "", value: "" })
]);
assert.strictEqual(h.appended.length, 4);
const runOf = function(index) { return h.cells(h.appended[index])[0].textContent; };
assert.strictEqual(runOf(0), "2531", "run order sorts by number");
assert.strictEqual(runOf(1), "2534");
assert.strictEqual(runOf(2), "2536");
assert.strictEqual(runOf(3), "2538");
assert.ok(/series-stat-outlier/.test(h.appended[2].className),
"a value beyond twice the median is flagged as an outlier");
assert.ok(/series-stat-notconverged/.test(h.appended[3].className),
"a non-converged row is highlighted");
assert.ok(h.appended[3].className.indexOf("series-stat-stale") < 0,
"a row without a comparable statistic is not marked stale");
assert.ok(h.appended[1].className.indexOf("series-stat-stale") < 0,
"a freshly fitted row is not stale");
assert.ok(/series-stat-stale/.test(h.appended[0].className),
"a reused row with an on-disk statistic is flagged stale");
const empty = makeTableHarness();
empty.dom.renderSeriesStatisticsTable([]);
assert.strictEqual(empty.appended.length, 1);
assert.strictEqual(empty.cells(empty.appended[0])[0].textContent,
"No series statistics available.");
}
function testRenderSeriesStatisticsTableRows() {
const h = makeTableHarness();
h.dom.renderSeriesStatisticsTable([
Object.assign(chisqRow(2536, "5.0"), { action: "fit", fresh: true }),
chisqRow(2534, "0.5"),
Object.assign(chisqRow(2538, "1.02"),
{ converged: false, text: "", value: "" })
]);
assert.strictEqual(h.appended.length, 3);
const firstCellOf = function(index) { return h.cells(h.appended[index])[0].textContent; };
const thirdCellOf = function(index) { return h.cells(h.appended[index])[2].textContent; };
assert.strictEqual(firstCellOf(0), "2534", "run order sorts by number");
assert.strictEqual(firstCellOf(1), "2536");
assert.strictEqual(firstCellOf(2), "2538");
assert.strictEqual(thirdCellOf(2), "Fit did not converge");
assert.ok(/series-stat-notconverged/.test(h.appended[2].className));
assert.ok(/series-stat-stale/.test(h.appended[0].className),
"a reused row with an on-disk statistic is flagged stale");
assert.ok(h.appended[1].className.indexOf("series-stat-stale") < 0,
"a freshly fitted row is not stale");
}
function testShowSeriesStatisticsDialog(lib) {
let shown = false;
const dialog = {
open: false,
showModal: function() { shown = true; }
};
let tableRenders = 0;
const dom = loadLibrary({
getElementById: function(id) {
if (id === "seriesStatisticDialog") return dialog;
if (id === "seriesStatisticTable") {
return { textContent: "", appendChild: function() { tableRenders++; } };
}
return null;
}
});
dom.showSeriesStatisticsDialog();
assert.strictEqual(shown, true, "the series dialog must open as a modal");
assert.ok(tableRenders > 0, "the table must be rendered before opening");
}
(async function main() {
const lib = loadLibrary();
testSeriesStatisticValue(lib);
testSeriesStatisticText(lib);
testSeriesFitConverged(lib);
testCollectSeriesStatisticRows(lib);
await testSynchronizeSeriesStatistics(lib);
testApplySeriesFitStatistic(lib);
testRenderJobStatusSeries(lib);
testRenderSeriesStatisticsTableRunOrder();
testRenderSeriesStatisticsTableRows();
testShowSeriesStatisticsDialog(lib);
console.log("series statistic tests passed");
})().catch(function(error) {
console.error(error);
process.exitCode = 1;
});