From 37a54984bd0b4f79e23489e8724e0e5c028995d2 Mon Sep 17 00:00:00 2001 From: salman Date: Mon, 7 Sep 2026 18:27:05 +0200 Subject: [PATCH] Fix File > Open MSR dropping content through the preload bridge preload.js readFile forwarded only null on success, so the renderer's openFile handler received undefined data and threw on data.toString() after the file dialog closed. Pass the content through (err, data). Add a bridge-contract regression test that settles on the renderer's load callback, plus a source-level guard that the readFile callback forwards data. --- CHANGELOG.md | 7 +++++ preload.js | 4 +-- tests/electron-bridge.test.js | 59 ++++++++++++++++++++++++++++++++++- 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16177a0..a7905f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,13 @@ All notable changes to muSRFlow are recorded here. The project follows the release path documented in [`TODO.md`](TODO.md). +## [Unreleased] + +### Bug fixes +- File > Open MSR... now loads the selected file. The preload bridge's async + `readFile` wrapper was forwarding only the error and dropping the content, so + the renderer's `data.toString()` failed silently after the dialog closed. + ## [0.9.0-beta.5] - 2026-09-07 ### t0 / Background / Data range diff --git a/preload.js b/preload.js index bf547cf..50a0390 100644 --- a/preload.js +++ b/preload.js @@ -121,9 +121,9 @@ const bridge = { if (typeof callback === "function") callback(err ? { message: err.message } : null); }); }, - readFile: function (p, encoding, callback) { +readFile: function (p, encoding, callback) { fs.readFile(p, encoding, function (err, data) { - if (typeof callback === "function") callback(err, data); + if (typeof callback === "function") callback(err ? { message: err.message } : null, data); }); }, constants: { diff --git a/tests/electron-bridge.test.js b/tests/electron-bridge.test.js index 2d9594c..7387b03 100644 --- a/tests/electron-bridge.test.js +++ b/tests/electron-bridge.test.js @@ -190,6 +190,55 @@ function testBridgeChildProcessSurface() { assert.strictEqual(syncRes.status, 0); } +// The preload readFile wrapper must forward the content and not just the error. +// The renderer's Open MSR handler consumes (err, data); dropping data made the +// File > Open MSR menu appear to do nothing while an uncaught data.toString() +// TypeError fired on the renderer side. +function testPreloadReadFileForwardsData() { + const preloadContents = fs.readFileSync(path.join(repoRoot, "preload.js"), "utf8"); + assert.match(preloadContents, /callback\(err \? \{ message: err\.message \} : null, data\)/); + const renderer = fs.readFileSync(path.join(repoRoot, "muSRFitelec.js"), "utf8"); + assert.match(renderer, /readFile\(filePath, "utf8", function read\(err, data\)/); + assert.match(renderer, /loadMSRIntoEditor\(data\.toString\(\), filePath/); +} + +// Run file-menu payloads crossing the bridge contract with the real preload +// readFile semantics: success forwards content to the renderer callback. The +// read is asynchronous, so this test settles on the renderer's load callback. +function testFileMenuOpenForwardsContentThroughBridge() { + return new Promise(function(resolve, reject) { + const bridge = makeBridge(); + bridge.fs.readFile = function (p, encoding, callback) { + fs.readFile(p, encoding, function (err, data) { + if (typeof callback === "function") callback(err ? { message: err.message } : null, data); + }); + }; + const { lib } = load(bridge); + let loaded = false; + lib.loadMSRIntoEditor = function (contents, filePath) { + try { + assert.strictEqual(contents, "TITLE Content forwarded\n"); + loaded = true; + resolve(); + } catch (error) { + reject(error); + } + }; + lib.openTab = function () { + }; + lib.firstLoad(); + + const sourcePath = path.join(testRoot, "bridge-open-content.msr"); + fs.writeFileSync(sourcePath, "TITLE Content forwarded\n", "utf8"); + bridge.ipc.listeners.openFile[0]([sourcePath]); + setTimeout(function() { + if (!loaded) { + reject(new Error("MSR open did not reach loadMSRIntoEditor")); + } + }, 200); + }); +} + // A new working directory must exist before checkDir returns. Existing files // must be rejected immediately rather than failing later during MSR generation. function testSynchronousWorkDirectoryPreparation() { @@ -311,6 +360,8 @@ testBridgeChildProcessSurface(); testSynchronousWorkDirectoryPreparation(); testPreferenceBrowsePayloadsUseBridgeContract(); testFileMenuPayloadsUseBridgeContract(); +const openForwards = testFileMenuOpenForwardsContentThroughBridge(); +testPreloadReadFileForwardsData(); testRunLocalProcessOverBridge(); testHardeningContract(); testPreloadExposesBridgeObject(); @@ -448,4 +499,10 @@ function testParameterPlotElecLoadsLocalPlotly() { testParameterPlotWindowUsesBridge(); testParameterPlotElecAdapterUsesBridge(); testParameterPlotElecLoadsLocalPlotly(); -console.log("Electron preload-bridge tests passed"); + +openForwards.then(function() { + console.log("Electron preload-bridge tests passed"); +}).catch(function(error) { + console.error(error); + process.exitCode = 1; +});