Files
muSRFlow/tests/session-persistence.test.js

140 lines
6.1 KiB
JavaScript

// Copyright (C) 2026 Zaher Salman
// SPDX-License-Identifier: GPL-3.0-or-later
// Session persistence is an Electron-only convenience: a "Save Session" button
// appends a timestamped, labeled configuration fragment to a plain journal in
// the working directory, and a "Restore Session..." menu re-applies one of the
// recorded entries through the same decode/restore path as a pasted share link.
const assert = require("assert");
const fs = require("fs");
const path = require("path");
const vm = require("vm");
const repoRoot = path.resolve(__dirname, "..");
function loadPlatformSandbox() {
const ipcListeners = {};
const electron = {
clipboard: {
readText: function() { return ""; },
writeText: function() {}
},
ipcRenderer: {
on: function(channel, handler) { ipcListeners[channel] = handler; },
send: function() {}
}
};
const elements = {};
const sandbox = {
URL: URL,
console: { log: function() {}, error: console.error, warn: console.warn },
confirm: function() { return true; },
process: process,
setTimeout: function(callback) { callback(); },
window: { MSREditor: {} },
document: {
getElementById: function(id) { return elements[id] || null; },
querySelectorAll: function() { return []; },
createElement: function() { return {}; }
},
require: function(name) {
if (name === "electron") return electron;
return require(name);
}
};
sandbox.window.window = sandbox.window;
sandbox.window.nodePath = path;
sandbox.global = sandbox;
sandbox.window.nodeFs = require("fs");
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 { elements: elements, ipcListeners: ipcListeners, sandbox: sandbox };
}
function testSessionJournalFormat(lib) {
// Blank lines, comments and malformed rows are ignored; fragments may
// contain tabs only after being sanitized by the writer, never in practice.
const text = [
"# muSRFlow sessions",
"",
"2026-09-22T10:15:00.000Z\tFe calibration, run 514\t#c=payload-1",
"2026-09-22T11:30:00.000Z\tTwo-component fit\t#cfg=payload-2",
"not-a-session-line"
].join("\n");
const entries = lib.parseSessionHistoryFile(text);
assert.strictEqual(entries.length, 2);
assert.strictEqual(entries[0].timestamp, "2026-09-22T10:15:00.000Z");
assert.strictEqual(entries[0].label, "Fe calibration, run 514");
assert.strictEqual(entries[0].fragment, "#c=payload-1");
assert.strictEqual(entries[1].label, "Two-component fit");
assert.strictEqual(entries[1].fragment, "#cfg=payload-2");
// The restore path is fed the exact fragment recorded in the journal.
assert.ok([ "#c=", "#cfg=" ].some(function(prefix) {
return entries[0].fragment.indexOf(prefix) === 0;
}));
assert.strictEqual(lib.parseSessionHistoryFile("").length, 0);
assert.strictEqual(lib.parseSessionHistoryFile(null).length, 0);
}
function testSessionJournalLivesInWorkDirectory(lib, elements) {
elements.workPath = { value: "/tmp/musrflow-work" };
const filename = lib.electronSessionFilename();
assert.ok(filename.endsWith("musrflow-sessions.txt"));
assert.ok(filename.indexOf("/tmp/musrflow-work") === 0);
// The configured working directory is the single persistence location, so
// sessions move together with the data folder they describe.
assert.strictEqual(lib.MUSRFLOW_SESSION_FILENAME, "musrflow-sessions.txt");
}
function testSessionUIAndMenuAreWired() {
const main = fs.readFileSync(path.join(repoRoot, "main.js"), "utf8");
const html = fs.readFileSync(path.join(repoRoot, "index.html"), "utf8");
const preload = fs.readFileSync(path.join(repoRoot, "preload.js"), "utf8");
const elec = fs.readFileSync(path.join(repoRoot, "muSRFitelec.js"), "utf8");
const css = fs.readFileSync(path.join(repoRoot, "ZGUI.css"), "utf8");
// Menu entries actively request a session save/restore; no startup prompt.
assert.match(main, /label: 'Save Session\.\.\.'/);
assert.match(main, /label: 'Restore Session\.\.\.'/);
assert.match(main, /webContents\.send\('saveSession'\)/);
assert.match(main, /webContents\.send\('openSessionHistory'\)/);
// The hardened renderer only receives channels the preload bridge allows.
assert.match(preload, /"saveSession"/);
assert.match(preload, /"openSessionHistory"/);
// The label-carrying save dialog and the history dialog exist.
assert.match(html, /id="saveSessionDialog"/);
assert.match(html, /id="sessionLabel"/);
assert.match(html, /writeElectronSession\(\)/);
assert.match(html, /id="sessionHistoryDialog"/);
assert.match(html, /id="sessionHistoryList"/);
assert.match(html, /restoreSelectedSession\(\)/);
assert.match(html, /id="saveSessionButton"/);
// The Save Session button only appears in the Electron shell.
assert.match(html, /saveSessionButton\.hidden = false/);
// The button starts hidden and the flex-normalized help-button style must
// not defeat the hidden attribute on the web deployment.
assert.match(html, /id="saveSessionButton"[\s\S]*hidden/);
assert.match(css, /\.tab \.help-button\[hidden\]\s*\{\s*display:\s*none;\s*\}/);
// The renderer routes the menu channels to the session dialogs.
assert.match(elec, /saveElectronSession\(\)/);
assert.match(elec, /openElectronSessionHistory\(\)/);
assert.match(elec, /electronSessionFilename\(\)/);
assert.match(elec, /writeAsciiFile\(filename, joined \+ "\\n"\)/);
assert.match(elec, /restoreSharedConfigurationFromSource\(entry\.fragment, null\)/);
}
const env = loadPlatformSandbox();
testSessionJournalFormat(env.sandbox);
testSessionJournalLivesInWorkDirectory(env.sandbox, env.elements);
testSessionUIAndMenuAreWired();
console.log("session persistence tests passed");