Files
muSRFlow/tests/run-naming.test.js
salman 62f8eec980 Fix RUN filename round-trip for GPD and HAL 2023+ eras
parseMSRRunFileToken could not reconstruct instrument/year/run for names
runFileNameAuto emits for GPD pre-2008 (deltat_pta_gpd), GPD 2008-2022
(deltat_tdc_gpd), and HAL 2023+ (deltat_tdc_hifi). Keep deltatbc as the
canonical GPS/GPD 2023+ ROOT-era name, restoring the shipped
musrfit_startup.xml templates to match, and pin the full instrument/era
matrix with a forward/reverse round-trip regression test.
2026-09-06 23:07:34 +02:00

497 lines
20 KiB
JavaScript

// Copyright (C) 2026 Zaher Salman
// SPDX-License-Identifier: GPL-3.0-or-later
// Portable RUN naming through musrfit_startup.xml.
//
// Covers the parser, template expansion, instrument-name mapping, candidate
// generation, and the opt-in integration into runFileNameAuto. The absolute-
// path generation path (portable naming disabled) must remain byte-for-byte
// unchanged, so the existing runFileNameAuto contract is also asserted here.
const assert = require("assert");
const fs = require("fs");
const os = require("os");
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: console,
window: { MSREditor: {} },
document: {}
};
sandbox.global = sandbox;
vm.runInNewContext(code, sandbox, { filename: "muSRFitlib.js" });
return sandbox;
}
const SAMPLE_XML = `
<?xml version="1.0" encoding="UTF-8"?>
<musrfit>
<data_path>/psi.ch/data/lem</data_path>
<data_path>/mnt/dmz-data</data_path>
<run_name_template inst="gps">d%yyyy%/delttat_tdc_gps_%yy%_%rrrr%.bin</run_name_template>
<run_name_template inst="gps">d%yyyy%/tdc/root/deltat_tdc_gps_%yyyy%_%rrrr%.root</run_name_template>
<run_name_template inst="lem">d%yyyy%/tdc/lem%yy%_his_%rrrr%.root</run_name_template>
<run_name_template inst="hifi">d%yyyy%/tdc/root/deltat_tdc_hifi_%yyyy%_%rrrr%.mdu</run_name_template>
<some_unrelated_setting>ignored</some_unrelated_setting>
</musrfit>
`;
function testParseStartupXML() {
const lib = loadLibrary();
const parsed = lib.parseMusrfitStartupXML(SAMPLE_XML);
assert.deepStrictEqual(Array.from(parsed.dataPaths), ["/psi.ch/data/lem", "/mnt/dmz-data"]);
assert.strictEqual(parsed.templates.gps.length, 2);
assert.strictEqual(parsed.templates.lem.length, 1);
assert.strictEqual(parsed.templates.hifi.length, 1);
assert.ok(!parsed.templates.gpd);
const empty = lib.parseMusrfitStartupXML("");
assert.deepStrictEqual(Array.from(empty.dataPaths), []);
assert.strictEqual(Object.keys(empty.templates).length, 0);
// A missing inst attribute and a missing value are ignored.
const malformed = lib.parseMusrfitStartupXML(
"<run_name_template>d%yyyy%/x_%rrrr%.bin</run_name_template>" +
'<run_name_template inst="lem"> </run_name_template>'
);
assert.deepStrictEqual(Array.from(malformed.dataPaths), []);
assert.strictEqual(malformed.templates.lem, undefined);
}
function testCommentsAreSkipped() {
const lib = loadLibrary();
// XML comments, even ones whose prose contains literal tag-like text, must
// never be mistaken for <data_path>/<run_name_template> elements.
const commented = lib.parseMusrfitStartupXML(
"<!-- The <data_path> entries are search roots and each <run_name_template>\n"
+ " sets a pattern like d%yyyy%/tdc/... --></musrfit> --!>\n"
+ "<musrfit>\n"
+ " <data_path>/ok</data_path>\n"
+ ' <run_name_template inst="lem">d%yyyy%/t_%rrrr%.bin</run_name_template>\n'
+ "</musrfit>"
);
assert.deepStrictEqual(Array.from(commented.dataPaths), ["/ok"]);
assert.deepStrictEqual(Array.from(commented.templates.lem), ["d%yyyy%/t_%rrrr%.bin"]);
// The shipped default startup XML parses cleanly despite its header comment.
const shippable = lib.parseMusrfitStartupXML(
fs.readFileSync(path.join(repoRoot, "cgi", "musrfit_startup.xml"), "utf8")
);
// Data on the standard LMU machines lives under
// /psi.ch/group/lmu/public/data/<instrument>/his/, so the shipped default
// carries one search root per instrument (plus the legacy NEMU mount).
assert.deepStrictEqual(Array.from(shippable.dataPaths), [
"/psi.ch/group/lmu/public/data/lem/his",
"/psi.ch/group/lmu/public/data/alc/his",
"/psi.ch/group/lmu/public/data/dolly/his",
"/psi.ch/group/lmu/public/data/flame/his",
"/psi.ch/group/lmu/public/data/gpd/his",
"/psi.ch/group/lmu/public/data/gps/his",
"/psi.ch/group/lmu/public/data/hal/his",
"/psi.ch/group/lmu/public/data/ltf/his",
"/psi.ch/group/lmu/public/data/misc/his",
"/psi.ch/group/lmu/public/data/vms/his",
"/mnt/data/nemu/his"
]);
assert.ok(shippable.templates.lem && shippable.templates.lem.length === 4);
assert.ok(shippable.templates.hifi && shippable.templates.hifi.length === 4);
// The whole pointer set: every data_path root names exactly one instrument.
for (const inst of ["lem", "gps", "ltf", "dolly", "vms", "gpd", "hifi"]) {
assert.ok(shippable.templates[inst] && shippable.templates[inst].length >= 1, inst);
}
}
function testTemplateExpansion() {
const lib = loadLibrary();
assert.strictEqual(
lib.expandMusrfitRunTemplate("d%yyyy%/tdc/lem%yy%_his_%rrrr%.root", "2026", 514),
"d2026/tdc/lem26_his_0514.root"
);
assert.strictEqual(
lib.expandMusrfitRunTemplate("d%yyyy%/tdc/root/deltat_tdc_hifi_%yyyy%_%rrrr%.mdu", "2026", "0514"),
"d2026/tdc/root/deltat_tdc_hifi_2026_0514.mdu"
);
// Pad to any number of r's.
assert.strictEqual(
lib.expandMusrfitRunTemplate("run_%rrrrr%.root", "2026", 12),
"run_00012.root"
);
// Same template may repeat both year forms.
assert.strictEqual(
lib.expandMusrfitRunTemplate("%yy%/%yyyy%", "2026", 1),
"26/2026"
);
}
function testInstrumentNameMapping() {
const lib = loadLibrary();
assert.strictEqual(lib.musrfitInstrumentXmlName("HAL"), "hifi");
assert.strictEqual(lib.musrfitInstrumentXmlName("hal"), "hifi");
assert.strictEqual(lib.musrfitInstrumentXmlName("LEM"), "lem");
assert.strictEqual(lib.musrfitInstrumentXmlName("GPS"), "gps");
assert.strictEqual(lib.musrfitInstrumentXmlName("Dolly"), "dolly");
assert.strictEqual(lib.musrfitInstrumentXmlName("FLAME"), "flame");
}
function testCandidateGeneration() {
const lib = loadLibrary();
const startup = lib.parseMusrfitStartupXML(SAMPLE_XML);
const gpsCandidates = lib.portableMusrfitRunCandidates(1234, "2026", "GPS", startup);
assert.strictEqual(gpsCandidates.length, 2);
assert.strictEqual(gpsCandidates[0].subPath, "d2026/delttat_tdc_gps_26_1234.bin");
assert.strictEqual(gpsCandidates[0].file, "d2026/delttat_tdc_gps_26_1234");
assert.strictEqual(gpsCandidates[1].subPath, "d2026/tdc/root/deltat_tdc_gps_2026_1234.root");
const halCandidates = lib.portableMusrfitRunCandidates(514, "2026", "HAL", startup);
assert.strictEqual(halCandidates.length, 1);
assert.strictEqual(halCandidates[0].subPath, "d2026/tdc/root/deltat_tdc_hifi_2026_0514.mdu");
const unknown = lib.portableMusrfitRunCandidates(514, "2026", "VMS", startup);
assert.strictEqual(unknown.length, 0);
const noStartup = lib.portableMusrfitRunCandidates(514, "2026", "LEM", null);
assert.strictEqual(noStartup.length, 0);
}
function testAbsolutePathUnchangedWhenDisabled() {
const lib = loadLibrary();
assert.strictEqual(
lib.runFileNameAuto(514, "2025", "LEM", "/psi.ch/data/"),
"RUN /psi.ch/data/lem/his/2025/lem25_his_0514 MUE4 PSI MUSR-ROOT"
);
// Portable naming enabled but with nothing configured still falls back to
// the absolute path.
lib.setPortableRunNaming(true, null, function() { return true; });
assert.strictEqual(
lib.runFileNameAuto(514, "2025", "LEM", "/psi.ch/data/"),
"RUN /psi.ch/data/lem/his/2025/lem25_his_0514 MUE4 PSI MUSR-ROOT"
);
// Reset for later tests in this module.
lib.setPortableRunNaming(false, null, null);
}
function testPortableNamingIntegration() {
const lib = loadLibrary();
const startup = lib.parseMusrfitStartupXML(SAMPLE_XML);
// First LEM template resolves -> portable relative RUN line.
const resolveTrue = function(subPath) { return true; };
lib.setPortableRunNaming(true, startup, resolveTrue);
assert.strictEqual(
lib.runFileNameAuto(514, "2026", "LEM", "/psi.ch/data/"),
"RUN d2026/tdc/lem26_his_0514 MUE4 PSI MUSR-ROOT"
);
// HAL maps to hifi templates.
assert.strictEqual(
lib.runFileNameAuto(514, "2026", "HAL", "/psi.ch/data/"),
"RUN d2026/tdc/root/deltat_tdc_hifi_2026_0514 PIE3 PSI PSI-MDU"
);
// When no template resolves, fall back to the absolute path.
const resolveFalse = function() { return false; };
lib.setPortableRunNaming(true, startup, resolveFalse);
assert.strictEqual(
lib.runFileNameAuto(514, "2026", "LEM", "/psi.ch/data/"),
"RUN /psi.ch/data/lem/his/2026/lem26_his_0514 MUE4 PSI MUSR-ROOT"
);
// The resolver is called against each data path in order.
let calls = [];
lib.setPortableRunNaming(true, startup, function(subPath, dataPath) {
calls.push([subPath, dataPath]);
return false;
});
lib.runFileNameAuto(514, "2026", "LEM", "/psi.ch/data/");
assert.deepStrictEqual(calls, [
["d2026/tdc/lem26_his_0514.root", "/psi.ch/data/lem"],
["d2026/tdc/lem26_his_0514.root", "/mnt/dmz-data"]
]);
lib.setPortableRunNaming(false, null, null);
}
function testPromiseResolverIsNotAcceptedAsAFileMatch() {
const lib = loadLibrary();
const startup = lib.parseMusrfitStartupXML(SAMPLE_XML);
// Browser fetch() returns a Promise. The synchronous generator must not
// interpret that object as proof that the first candidate exists.
lib.setPortableRunNaming(true, startup, function() {
return Promise.resolve(true);
});
assert.strictEqual(
lib.runFileNameAuto(514, "2026", "LEM", "/psi.ch/data/"),
"RUN /psi.ch/data/lem/his/2026/lem26_his_0514 MUE4 PSI MUSR-ROOT"
);
}
// Historical era transitions and format selection: when multiple templates
// exist for one instrument across years, only the era whose file actually
// resolves is accepted, and its format (BIN vs ROOT vs MDU) is carried through.
function testEraAndFormatSelection() {
const lib = loadLibrary();
const startup = lib.parseMusrfitStartupXML(`
<?xml version="1.0"?>
<musrfit>
<data_path>/era</data_path>
<run_name_template inst="gps">d%yyyy%/deltat_pta_gps_%rrrr%.bin</run_name_template>
<run_name_template inst="gps">d%yyyy%/tdc/deltat_tdc_gps_%rrrr%.bin</run_name_template>
<run_name_template inst="gps">d%yyyy%/tdc/root/deltatbc_tdc_gps_%yyyy%_%rrrr%.root</run_name_template>
<run_name_template inst="hifi">d%yyyy%/tdc_hifi_%yyyy%_%rrrrr%.bin</run_name_template>
<run_name_template inst="hifi">d%yyyy%/tdc/root/deltat_tdc_hifi_%yyyy%_%rrrr%.root</run_name_template>
</musrfit>
`);
// Year 2024 (>=2023): only the ROOT-era GPS template file exists.
let gpsRoot = { "d2024/tdc/root/deltatbc_tdc_gps_2024_0007.root": "/era" };
lib.setPortableRunNaming(true, startup, function(subPath, dataPath) {
return Object.prototype.hasOwnProperty.call(gpsRoot, subPath);
});
assert.strictEqual(
lib.runFileNameAuto(7, "2024", "GPS", "/psi.ch/data/"),
"RUN d2024/tdc/root/deltatbc_tdc_gps_2024_0007 PIM3 PSI MUSR-ROOT"
);
// Year 2010 (2008-2022): only the tdc BIN-era template exists.
let gpsBin = { "d2010/tdc/deltat_tdc_gps_0007.bin": "/era" };
lib.setPortableRunNaming(true, startup, function(subPath) {
return Object.prototype.hasOwnProperty.call(gpsBin, subPath);
});
assert.strictEqual(
lib.runFileNameAuto(7, "2010", "GPS", "/psi.ch/data/"),
"RUN d2010/tdc/deltat_tdc_gps_0007 PIM3 PSI PSI-BIN"
);
// HAL maps to hifi and prefers the pre-2023 MDU-era template (5-digit run).
let hifiMdu = { "d2015/tdc_hifi_2015_00007.bin": "/era" };
lib.setPortableRunNaming(true, startup, function(subPath) {
return Object.prototype.hasOwnProperty.call(hifiMdu, subPath);
});
assert.strictEqual(
lib.runFileNameAuto(7, "2015", "HAL", "/psi.ch/data/"),
"RUN d2015/tdc_hifi_2015_00007 PIE3 PSI PSI-MDU"
);
lib.setPortableRunNaming(false, null, null);
}
// Platform equivalence: the same effective startup XML + run + year +
// instrument must produce the same RUN line whether existence is confirmed
// through the Electron filesystem resolver or the web server-backed resolver.
function testPlatformEquivalence() {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "musrflow-naming-equiv-"));
const dataPath = path.join(tmp, "data");
// Real file that both resolvers will confirm.
const target = path.join(dataPath, "d2026", "tdc", "lem26_his_0514.root");
fs.mkdirSync(path.dirname(target), { recursive: true });
fs.writeFileSync(target, "x\n");
const xml = fs.readFileSync(path.join(repoRoot, "cgi", "musrfit_startup.xml"), "utf8");
function runLineWith(resolverFactory) {
const lib = loadLibrary();
const startup = lib.parseMusrfitStartupXML(xml);
lib.setPortableRunNaming(true, startup, resolverFactory(dataPath));
const line = lib.runFileNameAuto(514, "2026", "LEM", "/psi.ch/data/");
lib.setPortableRunNaming(false, null, null);
return line;
}
// Electron-style: existence is a local filesystem stat.
const electronLine = runLineWith(
function(dataPath) {
return function(subPath) {
return fs.existsSync(path.join(dataPath, subPath));
};
}
);
// Web-style: a resolver receiving the same candidate confirms it exists.
const webLine = runLineWith(
function(dataPath) {
return function(subPath) {
return fs.existsSync(path.join(dataPath, subPath));
};
}
);
assert.strictEqual(electronLine, webLine);
assert.strictEqual(
electronLine,
"RUN d2026/tdc/lem26_his_0514 MUE4 PSI MUSR-ROOT"
);
}
// Every exposed instrument, exercised through the shipped default startup XML:
// the correct era template resolves to a RUN line carrying the right sub-path,
// beam line, and data format.
function testAllInstrumentsRunLines() {
const startup = shippedStartup(loadLibrary());
const cases = [
// [instrument, run, year, accepted sub-path, expected RUN line]
["LEM", 7, "2026", "d2026/tdc/lem26_his_0007.root",
"RUN d2026/tdc/lem26_his_0007 MUE4 PSI MUSR-ROOT"],
["GPS", 7, "2024", "d2024/tdc/root/deltatbc_tdc_gps_2024_0007.root",
"RUN d2024/tdc/root/deltatbc_tdc_gps_2024_0007 PIM3 PSI MUSR-ROOT"],
["LTF", 7, "2026", "d2026/tdc/deltat_tdc_ltf_0007.bin",
"RUN d2026/tdc/deltat_tdc_ltf_0007 PIM3 PSI PSI-BIN"],
["Dolly", 7, "2026", "d2026/tdc/root/deltat_tdc_dolly_2026_0007.root",
"RUN d2026/tdc/root/deltat_tdc_dolly_2026_0007 PIE1 PSI MUSR-ROOT"],
["VMS", 7, "2026", "d2026/tdc/root/deltat_tdc_vms_2026_0007.root",
"RUN d2026/tdc/root/deltat_tdc_vms_2026_0007 PIE1 PSI MUSR-ROOT"],
["GPD", 7, "2010", "d2010/tdc/deltat_tdc_gpd_0007.bin",
"RUN d2010/tdc/deltat_tdc_gpd_0007 PIE1 PSI PSI-BIN"],
["FLAME", 7, "2026", "d2026/tdc/root/deltat_tdc_flame_2026_0007.root",
"RUN d2026/tdc/root/deltat_tdc_flame_2026_0007 PIM3 PSI MUSR-ROOT"],
["HAL", 7, "2026", "d2026/tdc/root/deltat_tdc_hifi_2026_0007.root",
"RUN d2026/tdc/root/deltat_tdc_hifi_2026_0007 PIE3 PSI MUSR-ROOT"]
];
cases.forEach(function(c) {
const lib = libOfShippedXml();
const accepted = {};
accepted[c[3]] = true;
lib.setPortableRunNaming(true, startup, function(subPath) {
return Object.prototype.hasOwnProperty.call(accepted, subPath);
});
assert.strictEqual(
lib.runFileNameAuto(c[1], c[2], c[0], "/psi.ch/data/"),
c[4],
c[0] + " " + c[2]
);
lib.setPortableRunNaming(false, null, null);
});
}
// Lazy loader reused by the run-line tests.
let _shipped;
function shippedStartup(lib) {
if (!_shipped) {
_shipped = lib.parseMusrfitStartupXML(
fs.readFileSync(path.join(repoRoot, "cgi", "musrfit_startup.xml"), "utf8"));
}
return _shipped;
}
function libOfShippedXml() {
const lib = loadLibrary();
shippedStartup(lib);
return lib;
}
// The series inventory is keyed on absolute data-file paths on both the browser
// and the CGI. seriesDataFilePath() must resolve a portable RUN resolution to
// the absolute data_path + sub-path instead of returning a bare relative token,
// and fall back to the legacy absolute path when portable naming is off.
function testSeriesDataFilePathPortableAbsolute() {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "musrflow-series-path-"));
const dataPath = path.join(tmp, "data");
const target = path.join(dataPath, "2026", "lem26_his_0514.root");
fs.mkdirSync(path.dirname(target), { recursive: true });
fs.writeFileSync(target, "x\n");
// A startup XML whose single <data_path> is the on-disk root the resolver
// confirms files under, so portableRunFileName resolves to this root.
const xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<musrfit>\n"
+ " <data_path>" + dataPath + "</data_path>\n"
+ " <run_name_template inst=\"lem\">%yyyy%/lem%yy%_his_%rrrr%.root</run_name_template>\n"
+ "</musrfit>\n";
const lib = loadLibrary();
const startup = lib.parseMusrfitStartupXML(xml);
const all = { selYear: "2026", instrName: "LEM", dataRoot: "/psi.ch/data/" };
lib.setPortableRunNaming(true, startup, function(subPath, dataPathArg) {
return dataPathArg === dataPath && fs.existsSync(path.join(dataPath, subPath));
});
assert.strictEqual(
lib.seriesDataFilePath(514, all), target,
"portable resolution yields the absolute data_path + sub-path"
);
lib.setPortableRunNaming(false, null, null);
// With portable naming disabled, the legacy absolute path is returned
// unchanged (dumped from the RUN directive). This proves the non-portable
// branch keeps the previous behavior.
const legacyAll = { selYear: "2026", instrName: "LEM", dataRoot: tmp };
const legacyDir = path.join(tmp, "lem", "his", "2026");
fs.mkdirSync(legacyDir, { recursive: true });
assert.strictEqual(
lib.seriesDataFilePath(514, legacyAll),
path.join(tmp, "lem", "his", "2026", "lem26_his_0514.root"),
"non-portable resolution returns the legacy absolute path"
);
fs.rmSync(tmp, { recursive: true, force: true });
}
// Every era boundary across the exposed instruments must forward-generate a
// RUN line whose data-file token reverse-parses to the same instrument, year,
// and run number. This guards the GUI/MSR synchronization that reads
// instrument, year, and run data back out of RUN/ADDRUN directives.
function testForwardReverseRoundTrip() {
const cases = [
["LEM", 2001, "2001", 514],
["GPS", 1993, "1993", 514],
["GPS", 2008, "2008", 514],
["GPS", 2023, "2023", 514],
["LTF", 1995, "1995", 514],
["LTF", 2009, "2009", 514],
["Dolly", 1998, "1998", 514],
["Dolly", 2008, "2008", 514],
["Dolly", 2023, "2023", 514],
["VMS", 2025, "2025", 514],
["GPD", 1993, "1993", 514],
["GPD", 2008, "2008", 514],
["GPD", 2023, "2023", 514],
["HAL", 2012, "2012", 514],
["HAL", 2023, "2023", 514],
["FLAME", 2020, "2020", 514]
];
cases.forEach(function(c) {
const lib = loadLibrary();
const line = lib.runFileNameAuto(c[3], c[1], c[0], "/psi.ch/data/");
const token = lib.runFileTokenFromDirective(line);
const parsed = lib.parseMSRRunFileToken(token);
assert.strictEqual(
parsed.instrument, c[0],
c[0] + " " + c[1] + ": " + token
);
assert.strictEqual(
parsed.year, c[2],
c[0] + " " + c[1] + " year: " + token
);
assert.strictEqual(
parsed.run, String(c[3]),
c[0] + " " + c[1] + " run: " + token
);
});
}
testParseStartupXML();
testCommentsAreSkipped();
testTemplateExpansion();
testInstrumentNameMapping();
testCandidateGeneration();
testAbsolutePathUnchangedWhenDisabled();
testPortableNamingIntegration();
testPromiseResolverIsNotAcceptedAsAFileMatch();
testEraAndFormatSelection();
testPlatformEquivalence();
testAllInstrumentsRunLines();
testForwardReverseRoundTrip();
testSeriesDataFilePathPortableAbsolute();
console.log("Portable run-naming tests passed");