Files
muSRFlow/tests/cgi-integration.test.js
T

340 lines
14 KiB
JavaScript

// Copyright (C) 2026 Zaher Salman
// SPDX-License-Identifier: GPL-3.0-or-later
//
// CGI-level integration tests: exercises cgi/single_musrfit.cgi with real
// musrfit, msr2data, and musrview binaries. Verifies the full Perl
// orchestration layer end-to-end: write → run → parameter export → plot.
//
// Skips gracefully when binaries or fake data are unavailable.
const assert = require("assert");
const childProcess = require("child_process");
const fs = require("fs");
const os = require("os");
const path = require("path");
const vm = require("vm");
const repoRoot = path.resolve(__dirname, "..");
const fixturesDir = path.join(repoRoot, "tests", "fixtures", "reference");
const cgiPath = path.join(repoRoot, "cgi", "single_musrfit.cgi");
const perlTestLibrary = path.join(repoRoot, "tests", "perl");
const perlLibraryPath = [perlTestLibrary, process.env.PERL5LIB]
.filter(Boolean).join(path.delimiter);
const MUSRFIT = "/usr/local/bin/musrfit";
const MSR2DATA = "/usr/local/bin/msr2data";
const MUSRVIEW = "/usr/local/bin/musrview";
const LEM24_SRC = "/psi.ch/data/lem/his/2024/lem24_his_0514";
const LEM26_DATA = "/mnt/fake/lem/his/2026/lem26_his_0514";
const FAKE_DATA_ROOT = "/mnt/fake/lem/his/2026";
const SINGLE_RUN = 514;
const GLOBAL_RUNS = [];
for (let r = 2452; r <= 2460; r++) GLOBAL_RUNS.push(r);
const SERIES_RUNS = [2452, 2453, 2454];
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
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: {}, MusrfitJob: {} },
alert: function(m) { throw new Error("Unexpected alert: " + m); }
};
sandbox.global = sandbox;
vm.runInNewContext(code, sandbox, { filename: "muSRFitlib.js" });
return sandbox;
}
const lib = loadLibrary();
// Set up a temp web-root with a dedicated job directory.
const testRoot = fs.mkdtempSync(path.join(os.tmpdir(), "musrflow-cgi-integ-"));
const webRoot = path.join(testRoot, "web");
const tmpRoot = path.join(webRoot, "tmp");
function makeJobDir(name) {
const dir = path.join(tmpRoot, name);
fs.mkdirSync(dir, { recursive: true });
return dir;
}
/**
* Invoke the real Perl CGI via stdin POST, returning the parsed JSON body.
* `extraEnv` overrides environment variables (MUSRFIT_BIN, etc.) for the
* CGI subprocess.
*/
function runCGI(body, extraEnv) {
const result = childProcess.spawnSync(
"perl", [cgiPath],
{
cwd: repoRoot,
encoding: "utf8",
input: body,
env: Object.assign({}, process.env, {
REQUEST_METHOD: "POST",
PERL5LIB: perlLibraryPath,
CONTENT_TYPE: "application/x-www-form-urlencoded",
CONTENT_LENGTH: String(Buffer.byteLength(body)),
MUSRFIT_WEBROOT: webRoot,
MUSRFIT_TMPROOT: tmpRoot,
MUSRFIT_TMP_URL: "/tmp",
MUSRFIT_RUNTIME_HOME: tmpRoot,
MUSRFIT_BIN: MUSRFIT,
MSR2DATA_BIN: MSR2DATA,
MUSRVIEW_BIN: MUSRVIEW,
MUSRFIT_CMD_TIMEOUT: "300"
}, extraEnv || {})
}
);
if (result.status !== 0) {
throw new Error("CGI exited " + result.status + ":\n" + result.stderr);
}
const responseBody = result.stdout.split(/\r?\n\r?\n/).slice(1).join("\n\n");
return JSON.parse(responseBody);
}
/**
* Stage a fixture MSR into a job directory, rewriting the LEM24 data path
* to the local LEM26 fake data root.
*/
function stageFixture(jobDir, fixtureName, outputName) {
const src = fs.readFileSync(path.join(fixturesDir, fixtureName), "utf8");
const staged = src.split(LEM24_SRC).join(LEM26_DATA);
fs.writeFileSync(path.join(jobDir, outputName), staged, "utf8");
}
/**
* Build a URL-encoded body for the CGI "write" action. Lines are passed as
* line1=…&line2=… etc.
*/
function writeBody(fn, lines) {
const params = new URLSearchParams();
params.set("action", "write");
params.set("fn", fn);
lines.forEach(function(line, i) {
params.set("line" + (i + 1), line);
});
return params.toString();
}
// ---------------------------------------------------------------------------
// Gate: skip when tools or data are unavailable.
// ---------------------------------------------------------------------------
const musrfitAvailable = fs.existsSync(MUSRFIT);
const msr2dataAvailable = fs.existsSync(MSR2DATA);
const musrviewAvailable = fs.existsSync(MUSRVIEW);
const dataAvailable = fs.existsSync(LEM26_DATA + ".root");
const globalDataAvailable = GLOBAL_RUNS.every(function(r) {
return fs.existsSync(path.join(FAKE_DATA_ROOT, "lem26_his_" + r + ".root"));
});
if (!musrfitAvailable || !msr2dataAvailable || !dataAvailable || !globalDataAvailable) {
console.log("cgi-integration tests skipped (musrfit: " + musrfitAvailable
+ ", msr2data: " + msr2dataAvailable
+ ", musrview: " + musrviewAvailable
+ ", single data: " + dataAvailable
+ ", global data: " + globalDataAvailable + ")");
process.exit(0);
}
// ---------------------------------------------------------------------------
// 1. Single-run fit through the CGI write → run pipeline
// ---------------------------------------------------------------------------
(function testSingleRunCGI() {
const jobName = "cgi-integ-single";
const jobDir = makeJobDir(jobName);
const fn = "/tmp/" + jobName + "/input.msr";
const fnAbs = path.join(jobDir, "input.msr");
// Read the asymmetry fixture, rewrite data paths, split into lines.
const src = fs.readFileSync(
path.join(fixturesDir, "asymmetry.generated.msr"), "utf8"
);
const staged = src.split(LEM24_SRC).join(LEM26_DATA);
const lines = staged.split("\n");
// 1a. Write action: stage the MSR.
const writeRes = runCGI(writeBody(fn, lines));
assert.strictEqual(writeRes.ok, true, "write action should succeed");
assert.strictEqual(writeRes.code, 0);
assert.ok(fs.existsSync(fnAbs), "MSR file should exist on disk after write");
// 1b. Run action: fit the staged MSR via real musrfit + musrview.
const runParams = new URLSearchParams();
runParams.set("action", "run");
runParams.set("fn", fn);
runParams.set("runs", String(SINGLE_RUN));
runParams.set("plot_only", "0");
runParams.set("plot_refresh", "0");
const runRes = runCGI(runParams.toString());
assert.strictEqual(runRes.ok, true, "run action should succeed: " + runRes.message);
assert.strictEqual(runRes.code, 0, "run code should be 0");
assert.ok(runRes.root_url, "root_url should be set");
assert.ok(runRes.parameter_url, "parameter_url should be set");
// Verify the fitted MSR has a STATISTIC block.
const fitted = fs.readFileSync(fnAbs, "utf8");
const stat = lib.parseMSRFitStatistic(fitted);
assert.strictEqual(stat.found, true, "STATISTIC block not found after CGI fit");
assert.strictEqual(stat.converged, true, "fit did not converge");
assert.strictEqual(stat.kind, "chisq", "expected a chisq statistic");
// Verify the parameter data file was created and is non-empty.
const parameterAbs = path.join(jobDir, "input_par.dat");
assert.ok(fs.existsSync(parameterAbs), "parameter data file not created");
const paramStat = fs.statSync(parameterAbs);
assert.ok(paramStat.size > 0, "parameter data file is empty");
// The CGI always writes a command log to the .out file on disk.
const outAbs = path.join(jobDir, "input.out");
assert.ok(fs.existsSync(outAbs), ".out command log file should exist");
const outContent = fs.readFileSync(outAbs, "utf8");
assert.ok(outContent.indexOf("musrfit") !== -1,
".out log should mention musrfit");
assert.ok(outContent.indexOf("musrview") !== -1,
".out log should mention musrview");
// Verify the canonical musrview output was preserved under an immutable
// mode-specific name before its URL was returned to the browser.
const plotFiles = fs.readdirSync(jobDir).filter(function(f) {
return /^input_spectrum_[0-9a-f]{16}_\d+\.root$/.test(f);
});
assert.ok(plotFiles.length > 0, "ROOT plot file not created");
console.log("cgi single-run: chisq/NDF " + stat.value + " OK");
})();
// ---------------------------------------------------------------------------
// 2. Global fit through the CGI write → run (msr2data) pipeline
// ---------------------------------------------------------------------------
(function testGlobalCGI() {
const jobName = "cgi-integ-global";
const jobDir = makeJobDir(jobName);
// msr2data looks for {base_run}_tmpl.msr in the work directory.
const templateName = GLOBAL_RUNS[0] + "_tmpl.msr";
const fn = "/tmp/" + jobName + "/" + templateName;
const fnAbs = path.join(jobDir, templateName);
// Stage the template (rewrite data paths).
const src = fs.readFileSync(
path.join(fixturesDir, "global.generated.msr"), "utf8"
);
const staged = src.split(LEM24_SRC).join(LEM26_DATA);
const lines = staged.split("\n");
// Write the template into the job directory.
const writeRes = runCGI(writeBody(fn, lines));
assert.strictEqual(writeRes.ok, true, "write action should succeed");
// Run with msr2data expansion: the CGI calls msr2data to expand the
// template, then musrfit to fit the expanded file, then musrview to plot.
const runParams = new URLSearchParams();
runParams.set("action", "run");
runParams.set("fn", fn);
runParams.set("use_msr2data", "1");
runParams.set("runs", GLOBAL_RUNS.join(" "));
runParams.set("base_run", String(GLOBAL_RUNS[0]));
runParams.set("template_name", templateName);
runParams.set("plot_only", "0");
runParams.set("plot_refresh", "0");
const runRes = runCGI(runParams.toString());
assert.strictEqual(runRes.ok, true, "global run should succeed: " + runRes.message);
assert.strictEqual(runRes.code, 0, "global code should be 0");
assert.ok(runRes.root_url, "global root_url should be set");
assert.ok(runRes.parameter_url, "global parameter_url should be set");
// The fitted file should now be the expanded global MSR with a STATISTIC block.
const fitted = fs.readFileSync(fnAbs, "utf8");
const stat = lib.parseMSRFitStatistic(fitted);
assert.strictEqual(stat.found, true, "global: STATISTIC block not found");
assert.strictEqual(stat.converged, true, "global: fit did not converge");
assert.strictEqual(stat.kind, "chisq", "global: expected a chisq statistic");
// Verify msr2data expanded to the correct number of RUN blocks.
const runCount = (fitted.match(/^RUN\s+/gm) || []).length;
assert.strictEqual(runCount, GLOBAL_RUNS.length,
"global: expected " + GLOBAL_RUNS.length + " RUN blocks (got " + runCount + ")");
// Verify parameter data file.
const parameterAbs = path.join(jobDir, "2452_tmpl_par.dat");
assert.ok(fs.existsSync(parameterAbs), "global: parameter data file not created");
const paramStat = fs.statSync(parameterAbs);
assert.ok(paramStat.size > 0, "global: parameter data file is empty");
// Verify command log .out file contains both msr2data and musrfit.
const globalOutAbs = path.join(jobDir, templateName.replace(/\.msr$/, ".out"));
assert.ok(fs.existsSync(globalOutAbs), "global: .out command log file should exist");
const globalOut = fs.readFileSync(globalOutAbs, "utf8");
assert.ok(globalOut.indexOf("msr2data") !== -1,
"global .out log should mention msr2data");
assert.ok(globalOut.indexOf("musrfit") !== -1,
"global .out log should mention musrfit");
console.log("cgi global fit: chisq/NDF " + stat.value + " OK");
})();
// ---------------------------------------------------------------------------
// 3. Single-run parameter export: verify msr2data produced a usable table
// ---------------------------------------------------------------------------
(function testSingleRunParameterExport() {
const jobName = "cgi-integ-param";
const jobDir = makeJobDir(jobName);
const fn = "/tmp/" + jobName + "/input.msr";
const fnAbs = path.join(jobDir, "input.msr");
const src = fs.readFileSync(
path.join(fixturesDir, "asymmetry.generated.msr"), "utf8"
);
const staged = src.split(LEM24_SRC).join(LEM26_DATA);
const lines = staged.split("\n");
runCGI(writeBody(fn, lines));
const runParams = new URLSearchParams();
runParams.set("action", "run");
runParams.set("fn", fn);
runParams.set("runs", String(SINGLE_RUN));
runParams.set("plot_only", "0");
runParams.set("plot_refresh", "0");
const runRes = runCGI(runParams.toString());
assert.strictEqual(runRes.ok, true, "param run should succeed");
const parameterAbs = path.join(jobDir, "input_par.dat");
const paramContent = fs.readFileSync(parameterAbs, "utf8");
const dataLines = paramContent.split("\n").filter(function(l) {
return l.trim() !== "";
});
assert.ok(dataLines.length >= 1, "parameter file should have at least a header");
console.log("cgi single-run parameter export: " + dataLines.length + " line(s) OK");
})();
// ---------------------------------------------------------------------------
// 4. Error case: invalid fn path outside job directory
// ---------------------------------------------------------------------------
(function testRejectPathTraversal() {
const params = new URLSearchParams();
params.set("action", "run");
params.set("fn", "/tmp/../../etc/passwd");
const res = runCGI(params.toString());
assert.strictEqual(res.ok, false, "path traversal should be rejected");
assert.strictEqual(res.code, 400);
console.log("cgi path traversal rejection OK");
})();
// ---------------------------------------------------------------------------
// Cleanup and summary
// ---------------------------------------------------------------------------
fs.rmSync(testRoot, { recursive: true, force: true });
console.log("cgi-integration tests passed");