// Copyright (C) 2026 Zaher Salman // SPDX-License-Identifier: GPL-3.0-or-later const assert = require("assert"); const events = require("events"); const fs = require("fs"); const path = require("path"); const vm = require("vm"); const repoRoot = path.resolve(__dirname, ".."); const spawnedCommands = []; const completions = []; const notices = []; let runnerScenario = "missing-data"; const missingOutput = [ "PRunDataHandler::FileExistsCheck(): **ERROR** Couldn't find " + "'/psi.ch/group/lmu/public/data/flame/his/2026/flame26_his_01285' in any standard path.", "musrfit **ERROR** Couldn't read all data files, will quit ..." ].join("\n"); const failedFitOutput = [ "musrfit **ERROR** minimization did not converge.", "The fitted MSR remains available for inspection." ].join("\n"); const fakeFs = { appendFileSync: function() { }, existsSync: function() { return false; }, writeFileSync: function() { } }; const fakeChildProcess = { spawn: function(command, args) { spawnedCommands.push({ command: command, args: args }); const child = new events.EventEmitter(); child.stdout = new events.EventEmitter(); child.stderr = new events.EventEmitter(); setImmediate(function() { if (command === "musrview") { child.emit("close", 0, null); return; } if (runnerScenario === "missing-data") { child.stderr.emit("data", Buffer.from(missingOutput)); child.emit("close", 1, null); return; } child.stderr.emit("data", Buffer.from(failedFitOutput)); child.emit("close", 2, null); }); return child; } }; const sandbox = { alert: function(message) { throw new Error("Unexpected alert: " + message); }, console: { log: function() {}, error: console.error, warn: console.warn }, document: { getElementById: function() { return null; } }, process: { cwd: function() { return repoRoot; }, env: {}, resourcesPath: "" }, require: require, window: { MSREditor: {}, electronIpcRenderer: { on: function() {}, send: function() {} }, localStorage: (function() { const store = {}; return { getItem: function(k){ return Object.prototype.hasOwnProperty.call(store,k)?store[k]:null; }, setItem: function(k,v){ store[k]=String(v); }, removeItem: function(k){ delete store[k]; } }; })(), nodeChildProcess: fakeChildProcess, nodeFs: fakeFs, nodePath: path } }; 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" } ); if (sandbox.setConfiguredT0FromPromptPeak) { sandbox.setConfiguredT0FromPromptPeak(false, true); } sandbox.appendJobLog = function() { }; sandbox.updateJobStatus = function() { }; sandbox.setFitStatus = function(message, options) { notices.push({ message: message, options: options || {} }); }; sandbox.finishJobStatus = function(ok, code, message, outcome) { completions.push({ ok: ok, code: code, message: message, outcome: outcome }); }; sandbox.runMSRFileLocal("/tmp/1285_asy.msr", { plotOnly: false }).then(function(result) { assert.strictEqual(result, 0); assert.strictEqual(sandbox.window.MusrfitRunner.fitActive, false); assert.strictEqual(spawnedCommands.length, 1, "musrview must not start after missing fit data"); assert.strictEqual(spawnedCommands[0].command, "musrfit"); assert.strictEqual(completions.length, 1); assert.strictEqual(completions[0].outcome, "missing-data"); assert.match(completions[0].message, /Data file not found: flame26_his_01285/); spawnedCommands.length = 0; completions.length = 0; notices.length = 0; runnerScenario = "fit-failure"; return sandbox.runMSRFileLocal("/tmp/1285_asy.msr", { plotOnly: false }); }).then(function(result) { assert.strictEqual(result, 0, "a failed fit must remain a failed action"); assert.strictEqual(spawnedCommands.length, 2, "a generic fit failure should still open one diagnostic musrview"); assert.strictEqual(spawnedCommands[0].command, "musrfit"); assert.strictEqual(spawnedCommands[1].command, "musrview"); // Allow the diagnostic viewer's close handler to run. It must not add a // successful completion or turn the visible notice into a success. return new Promise(function(resolve) { setImmediate(resolve); }); }).then(function() { assert.strictEqual(completions.length, 1, "diagnostic musrview must not own the main job completion status"); assert.strictEqual(completions[0].ok, 0); assert.strictEqual(completions[0].code, 2); assert.strictEqual(notices.some(function(notice) { return notice.options.level === "success"; }), false, "a diagnostic plot must not emit a success notice after a failed fit"); assert.match(notices[0].message, /Fit failed; opening plot/); console.log("Electron missing-data and failed-fit status tests passed"); }).catch(function(error) { console.error(error); process.exitCode = 1; });