116 lines
4.7 KiB
JavaScript
116 lines
4.7 KiB
JavaScript
// Copyright (C) 2026 Zaher Salman
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
// Electron portable-RUN-naming wiring: the filesystem resolver, the startup-XML
|
|
// discovery/configuration path, and the opt-in into runFileNameAuto via
|
|
// loadLocalPortableRunNaming(). Without a configured/discovered startup XML the
|
|
// absolute-path fallback must remain the default.
|
|
|
|
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, "..");
|
|
const testRoot = fs.mkdtempSync(path.join(os.tmpdir(), "musrflow-elec-portable-"));
|
|
const dataRoot = path.join(testRoot, "data");
|
|
|
|
// A portable file matching the LEM template d%yyyy%/tdc/lem%yy%_his_%rrrr%.root
|
|
// for year 2026, run 514, plus a startup XML that points at this data root.
|
|
const portableFile = path.join(dataRoot, "d2026", "tdc", "lem26_his_0514.root");
|
|
fs.mkdirSync(path.dirname(portableFile), { recursive: true });
|
|
fs.writeFileSync(portableFile, "dummy\n");
|
|
const startupXmlPath = path.join(testRoot, "musrfit_startup.xml");
|
|
fs.writeFileSync(startupXmlPath, `<?xml version="1.0"?>
|
|
<musrfit>
|
|
<data_path>${dataRoot}</data_path>
|
|
<run_name_template inst="lem">d%yyyy%/tdc/lem%yy%_his_%rrrr%.root</run_name_template>
|
|
</musrfit>
|
|
`);
|
|
|
|
function localStoreFactory() {
|
|
const store = {};
|
|
return {
|
|
getItem: function(key) { return Object.prototype.hasOwnProperty.call(store, key) ? store[key] : null; },
|
|
setItem: function(key, value) { store[key] = String(value); },
|
|
removeItem: function(key) { delete store[key]; }
|
|
};
|
|
}
|
|
|
|
function load(cwd) {
|
|
const localStorage = localStoreFactory();
|
|
const sandbox = {
|
|
console: { log: function() {}, error: console.error, warn: console.warn },
|
|
process: { env: {}, cwd: function() { return cwd || repoRoot; } },
|
|
require: require,
|
|
window: {
|
|
MSREditor: {},
|
|
localStorage: localStorage,
|
|
electronIpcRenderer: { on: function() {}, send: function() {} },
|
|
nodeChildProcess: { spawn: function() { throw new Error("no spawn in this test"); } },
|
|
nodeFs: fs,
|
|
nodePath: path
|
|
},
|
|
document: { getElementById: function() { return null; } }
|
|
};
|
|
sandbox.global = sandbox;
|
|
vm.runInNewContext(
|
|
fs.readFileSync(path.join(repoRoot, "muSRFitlib.js"), "utf8"),
|
|
sandbox, { filename: "muSRFitlib.js" });
|
|
vm.runInNewContext(
|
|
fs.readFileSync(path.join(repoRoot, "muSRFitelec.js"), "utf8"),
|
|
sandbox, { filename: "muSRFitelec.js" });
|
|
return { lib: sandbox, localStorage: localStorage };
|
|
}
|
|
|
|
// The filesystem resolver confirms existing relative files and rejects
|
|
// traversal/absolute/missing paths.
|
|
function testResolver() {
|
|
const { lib } = load();
|
|
assert.strictEqual(lib.portableRunResolver("d2026/tdc/lem26_his_0514.root", dataRoot), true);
|
|
assert.strictEqual(lib.portableRunResolver("d2026/tdc/missing.root", dataRoot), false);
|
|
assert.strictEqual(lib.portableRunResolver("../escape.txt", dataRoot), false);
|
|
assert.strictEqual(lib.portableRunResolver("/etc/passwd", dataRoot), false);
|
|
}
|
|
|
|
// With a configured startup XML, loadLocalPortableRunNaming enables portable
|
|
// naming so runFileNameAuto emits a relative RUN line resolved by the fs hook.
|
|
function testPortableEnabledViaConfiguredXml() {
|
|
const { lib, localStorage } = load();
|
|
localStorage.setItem("musrflow.startupXml", startupXmlPath);
|
|
|
|
const found = lib.loadLocalPortableRunNaming();
|
|
assert.strictEqual(found, startupXmlPath);
|
|
assert.strictEqual(
|
|
lib.runFileNameAuto(514, "2026", "LEM", "/psi.ch/data/"),
|
|
"RUN d2026/tdc/lem26_his_0514 MUE4 PSI MUSR-ROOT"
|
|
);
|
|
assert.strictEqual(
|
|
lib.window.MusRFlowPlatform.resolveDataFile("d2026/tdc/lem26_his_0514"),
|
|
portableFile
|
|
);
|
|
// The indicator is null-safe in a document without the status element.
|
|
assert.strictEqual(lib.updateStartupNamingSourceIndicator(), undefined);
|
|
}
|
|
|
|
// With no startup XML configured (and none discoverable), portable naming stays
|
|
// disabled and the absolute path is the default. Use a cwd with no startup XML
|
|
// so discovery does not find the repository copy.
|
|
function testFallbackWhenNoXml() {
|
|
const emptyCwd = path.join(testRoot, "empty-cwd");
|
|
fs.mkdirSync(emptyCwd, { recursive: true });
|
|
const { lib } = load(emptyCwd);
|
|
const found = lib.loadLocalPortableRunNaming();
|
|
assert.strictEqual(found, "");
|
|
assert.strictEqual(
|
|
lib.runFileNameAuto(514, "2026", "LEM", "/psi.ch/data/"),
|
|
"RUN /psi.ch/data/lem/his/2026/lem26_his_0514 MUE4 PSI MUSR-ROOT"
|
|
);
|
|
}
|
|
|
|
testResolver();
|
|
testPortableEnabledViaConfiguredXml();
|
|
testFallbackWhenNoXml();
|
|
console.log("Electron portable-naming tests passed");
|