Files
muSRFlow/tests/cgi-run-inline.test.js
T

174 lines
6.4 KiB
JavaScript

// Copyright (C) 2026 Zaher Salman
// SPDX-License-Identifier: GPL-3.0-or-later
// Exercise the real Perl run action with stub executables. A normal Fit/Plot
// must return the fitted MSR and command log inline so the browser does not
// need follow-up artifact GETs.
const assert = require("assert");
const childProcess = require("child_process");
const fs = require("fs");
const os = require("os");
const path = require("path");
const repoRoot = path.resolve(__dirname, "..");
const testRoot = fs.mkdtempSync(path.join(os.tmpdir(), "musrflow-run-inline-"));
const webRoot = path.join(testRoot, "web");
const tmpRoot = path.join(webRoot, "tmp");
const jobDir = path.join(tmpRoot, "job1");
const stubDir = path.join(testRoot, "stubs");
fs.mkdirSync(jobDir, { recursive: true });
fs.mkdirSync(stubDir, { recursive: true });
const msrPath = path.join(jobDir, "1014_asy.msr");
fs.writeFileSync(msrPath, [
"FITPARAMETER",
"1 Asy 0.2 0.01 none 0 0",
"2 Lam 1.0 0.01 none 0 0",
"RUN /tmp/does-not-matter.root MUE4 PSI MUSR-ROOT",
"PLOT 2",
""
].join("\n"));
function writeStub(name, content) {
const target = path.join(stubDir, name);
fs.writeFileSync(target, content);
fs.chmodSync(target, 0o755);
return target;
}
const musrfit = writeStub("musrfit", `#!/bin/sh
for last; do :; done
printf '\\n# fitted by stub\\n' >> "$last"
echo "stub musrfit ran on $last"
exit 0
`);
const musrview = writeStub("musrview", `#!/bin/sh
for last; do :; done
base="\${last%.msr}"
touch "\${base}_1.root"
echo "stub musrview ran on $last"
exit 0
`);
const msr2data = writeStub("msr2data", `#!/bin/sh
echo "stub msr2data $*"
out=""
prev=""
for a in "$@"; do
if [ "$prev" = "-o" ]; then out="$a"; fi
prev="$a"
done
if [ -n "$out" ]; then printf 'Asy Lam\\n0.15 0.52\\n' > "$out"; fi
exit 0
`);
const body = new URLSearchParams({
action: "run",
fn: "/tmp/job1/1014_asy.msr",
use_msr2data: "0",
runs: "1014",
base_run: "1014",
fftv: "n",
plot_only: "0"
}).toString();
const result = childProcess.spawnSync("perl", [path.join(repoRoot, "cgi", "single_musrfit.cgi")], {
cwd: repoRoot,
input: body,
encoding: "utf8",
env: Object.assign({}, process.env, {
REQUEST_METHOD: "POST",
CONTENT_TYPE: "application/x-www-form-urlencoded",
CONTENT_LENGTH: String(Buffer.byteLength(body)),
PERL5LIB: path.join(repoRoot, "tests", "perl"),
MUSRFIT_WEBROOT: webRoot,
MUSRFIT_TMPROOT: tmpRoot,
MUSRFIT_TMP_URL: "/tmp",
MUSRFIT_BIN: musrfit,
MUSRVIEW_BIN: musrview,
MSR2DATA_BIN: msr2data,
MUSRFIT_CMD_TIMEOUT: "30"
})
});
assert.ifError(result.error);
assert.strictEqual(result.status, 0, result.stderr);
const separator = result.stdout.indexOf("\n\n");
assert.ok(separator > 0, "CGI headers precede the response body");
const response = JSON.parse(result.stdout.slice(separator + 2));
assert.strictEqual(response.ok, true, JSON.stringify(response.stderr));
assert.strictEqual(response.code, 0);
assert.strictEqual(response.message, "Fit completed");
assert.strictEqual(response.msr_url, "/tmp/job1/1014_asy.msr");
assert.strictEqual(response.out_url, "/tmp/job1/1014_asy.out");
assert.strictEqual(response.command_log, "", "normal runs do not use the append-only log delta");
assert.ok(Array.isArray(response.root_urls) && response.root_urls.length === 1);
assert.match(response.root_urls[0],
/^\/tmp\/job1\/1014_asy_spectrum_[0-9a-f]{16}_1\.root$/,
"normal spectrum uses an immutable mode-specific artifact name");
const originalRoot = path.join(jobDir, path.basename(response.root_urls[0]));
assert.ok(fs.existsSync(originalRoot), "returned immutable ROOT artifact exists");
assert.ok(!fs.existsSync(path.join(jobDir, "1014_asy_1.root")),
"canonical musrview output was moved out of the overwrite path");
assert.strictEqual(typeof response.msr_text, "string");
assert.strictEqual(response.msr_text, fs.readFileSync(msrPath, "utf8"),
"msr_text is the fitted MSR that musrfit wrote");
assert.match(response.msr_text, /# fitted by stub/);
assert.strictEqual(typeof response.log_text, "string");
assert.strictEqual(response.log_text,
fs.readFileSync(path.join(jobDir, "1014_asy.out"), "utf8"),
"log_text is the complete command log");
assert.match(response.log_text, /stub musrfit ran on 1014_asy\.msr/);
assert.match(response.log_text, /stub musrview ran on 1014_asy\.msr/);
assert.match(response.log_text, /stub msr2data/);
// Generate another view in the same web job. Its URL must identify the new
// mode while the first spectrum remains available to its open plot window.
const averageBody = new URLSearchParams({
action: "run",
fn: "/tmp/job1/1014_asy.msr",
use_msr2data: "0",
runs: "1014",
base_run: "1014",
fftv: "n",
avgv: "y",
plot_only: "1",
plot_refresh: "1"
}).toString();
const averageResult = childProcess.spawnSync(
"perl", [path.join(repoRoot, "cgi", "single_musrfit.cgi")], {
cwd: repoRoot,
input: averageBody,
encoding: "utf8",
env: Object.assign({}, process.env, {
REQUEST_METHOD: "POST",
CONTENT_TYPE: "application/x-www-form-urlencoded",
CONTENT_LENGTH: String(Buffer.byteLength(averageBody)),
PERL5LIB: path.join(repoRoot, "tests", "perl"),
MUSRFIT_WEBROOT: webRoot,
MUSRFIT_TMPROOT: tmpRoot,
MUSRFIT_TMP_URL: "/tmp",
MUSRFIT_BIN: musrfit,
MUSRVIEW_BIN: musrview,
MSR2DATA_BIN: msr2data,
MUSRFIT_CMD_TIMEOUT: "30"
})
}
);
assert.ifError(averageResult.error);
assert.strictEqual(averageResult.status, 0, averageResult.stderr);
const averageSeparator = averageResult.stdout.indexOf("\n\n");
const averageResponse = JSON.parse(averageResult.stdout.slice(averageSeparator + 2));
assert.strictEqual(averageResponse.ok, true, JSON.stringify(averageResponse.stderr));
assert.match(averageResponse.root_urls[0],
/^\/tmp\/job1\/1014_asy_spectrum-average_[0-9a-f]{16}_1\.root$/,
"average spectrum receives a separate immutable artifact name");
assert.notStrictEqual(averageResponse.root_urls[0], response.root_urls[0]);
assert.ok(fs.existsSync(originalRoot),
"generating an average view does not replace the original spectrum");
assert.ok(fs.existsSync(path.join(jobDir, path.basename(averageResponse.root_urls[0]))),
"average ROOT artifact exists beside the original spectrum");
console.log("CGI inline run response tests passed");