120 lines
4.6 KiB
JavaScript
120 lines
4.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 fakeNode(tagName) {
|
|
const node = {
|
|
tagName: tagName,
|
|
className: "",
|
|
children: [],
|
|
textContent: "",
|
|
appendChild: function(child) { this.children.push(child); return child; }
|
|
};
|
|
Object.defineProperty(node, "innerHTML", {
|
|
set: function() { throw new Error("Log rendering must not use innerHTML"); }
|
|
});
|
|
return node;
|
|
}
|
|
|
|
function loadLibrary() {
|
|
const code = fs.readFileSync(path.join(repoRoot, "muSRFitlib.js"), "utf8");
|
|
const document = {
|
|
createElement: function(tagName) { return fakeNode(tagName); },
|
|
createTextNode: function(text) {
|
|
return { nodeType: 3, textContent: text };
|
|
}
|
|
};
|
|
const sandbox = {
|
|
console: { log: function() {}, error: console.error, warn: console.warn },
|
|
document: document,
|
|
window: { MSREditor: {} }
|
|
};
|
|
sandbox.global = sandbox;
|
|
vm.runInNewContext(code, sandbox, { filename: "muSRFitlib.js" });
|
|
return sandbox;
|
|
}
|
|
|
|
function testLineClassification(lib) {
|
|
assert.strictEqual(lib.jobLogLineKind("=== musrfit ==="), "section");
|
|
assert.strictEqual(lib.jobLogLineKind("$ musrfit -e 514.msr"), "command");
|
|
assert.strictEqual(lib.jobLogLineKind("[exit code 0]"), "success");
|
|
assert.strictEqual(lib.jobLogLineKind("[exit code 2]"), "error");
|
|
assert.strictEqual(lib.jobLogLineKind("Global MSR prepared with warnings."), "warning");
|
|
assert.strictEqual(lib.jobLogLineKind("Fit completed."), "success");
|
|
assert.strictEqual(lib.jobLogLineKind("ordinary musrfit output"), "output");
|
|
}
|
|
|
|
function testMissingDataClassification(lib) {
|
|
const output = [
|
|
">> PMsrHandler::HandleStatisticEntry: **WARNING** There is no STATISTIC block!",
|
|
">> PRunDataHandler::FileExistsCheck(): **ERROR** Couldn't find "
|
|
+ "'/psi.ch/group/lmu/public/data/flame/his/2026/flame26_his_01285' in any standard path.",
|
|
">> musrview **ERROR** Couldn't read all data files, will quit ..."
|
|
].join("\n");
|
|
const result = lib.classifyMissingDataOutput(output);
|
|
|
|
assert.strictEqual(result.outcome, "missing-data");
|
|
assert.deepStrictEqual(Array.from(result.missingFiles), [
|
|
"/psi.ch/group/lmu/public/data/flame/his/2026/flame26_his_01285"
|
|
]);
|
|
assert.strictEqual(result.summary, "Data file not found: flame26_his_01285");
|
|
assert.match(result.details, /Check the instrument, year, run number/);
|
|
|
|
// Missing configuration and executable errors must remain ordinary failures.
|
|
const unrelated = lib.classifyMissingDataOutput("Could not find musrfit_startup.xml");
|
|
assert.strictEqual(unrelated.outcome, "");
|
|
|
|
lib.renderJobStatus = function() {
|
|
};
|
|
lib.window.MusrfitJob.log = [];
|
|
lib.finishJobStatus(0, 1, result.details, result.outcome);
|
|
assert.strictEqual(lib.window.MusrfitJob.state, "Missing data");
|
|
assert.strictEqual(lib.window.MusrfitJob.noticeLevel, "warning");
|
|
}
|
|
|
|
function testSafeSemanticRendering(lib) {
|
|
const target = fakeNode("pre");
|
|
lib.renderJobLogElement(target, [
|
|
"[8/26/2026, 6:15:00 PM] === musrfit ===",
|
|
"$ musrfit -e 514.msr",
|
|
"[exit code 0]",
|
|
"warning: fallback used",
|
|
"stderr: <img src=x onerror=alert(1)>"
|
|
].join("\n"));
|
|
|
|
const lines = target.children.filter(function(child) {
|
|
return child.className && child.className.indexOf("job-log-line") !== -1;
|
|
});
|
|
assert.deepStrictEqual(lines.map(function(line) { return line.className; }), [
|
|
"job-log-line job-log-section",
|
|
"job-log-line job-log-command",
|
|
"job-log-line job-log-success",
|
|
"job-log-line job-log-warning",
|
|
"job-log-line job-log-error"
|
|
]);
|
|
assert.strictEqual(lines[0].children[0].className, "job-log-timestamp");
|
|
assert.strictEqual(lines[0].children[1].textContent, "=== musrfit ===");
|
|
assert.strictEqual(lines[4].children[0].textContent,
|
|
"stderr: <img src=x onerror=alert(1)>");
|
|
}
|
|
|
|
function testHighlightingStyles() {
|
|
const css = fs.readFileSync(path.join(repoRoot, "ZGUI.css"), "utf8");
|
|
for (const className of ["timestamp", "section", "command", "success", "warning", "error"]) {
|
|
assert.match(css, new RegExp("\\.job-log-" + className + "\\s*\\{"));
|
|
}
|
|
}
|
|
|
|
const lib = loadLibrary();
|
|
testLineClassification(lib);
|
|
testMissingDataClassification(lib);
|
|
testSafeSemanticRendering(lib);
|
|
testHighlightingStyles();
|
|
console.log("Job log highlighting tests passed");
|