// Copyright (C) 2026 Zaher Salman // SPDX-License-Identifier: GPL-3.0-or-later const assert = require("assert"); const fs = require("fs"); const path = require("path"); const os = require("os"); const { spawnSync } = require("child_process"); const repoRoot = path.resolve(__dirname, ".."); function testSharedHelpWindowInfrastructure() { const main = fs.readFileSync(path.join(repoRoot, "main.js"), "utf8"); const lib = fs.readFileSync(path.join(repoRoot, "muSRFitlib.js"), "utf8"); const elec = fs.readFileSync(path.join(repoRoot, "muSRFitelec.js"), "utf8"); const html = fs.readFileSync(path.join(repoRoot, "index.html"), "utf8"); const help = fs.readFileSync(path.join(repoRoot, "quick-start.html"), "utf8"); const deploy = fs.readFileSync(path.join(repoRoot, "scripts", "deploy-web.sh"), "utf8"); const remoteDeploy = fs.readFileSync( path.join(repoRoot, "scripts", "deploy-remote-web.sh"), "utf8" ); const preload = fs.readFileSync(path.join(repoRoot, "preload.js"), "utf8"); assert.match(main, /function createAuxiliaryWindow/); assert.match(main, /createParameterPlotWindow[\s\S]*createAuxiliaryWindow/); assert.match(main, /ipcMain\.on\("openQuickStartWindow"/); assert.match(main, /file: "quick-start\.html"/); assert.match(elec, /function openElectronAuxiliaryWindow/); assert.match(elec, /parameterPlot: "openParameterPlotWindow"/); assert.match(elec, /quickStart: "openQuickStartWindow"/); assert.match(lib, /openElectronAuxiliaryWindow\("quickStart"/); assert.match(lib, /dialog\.show\(\)/); assert.doesNotMatch(lib, /quickStartDialog[\s\S]{0,500}showModal/); assert.match(html, /class="web-plot-window quick-start-dialog"/); assert.match(html, /id="quickStartFrame"/); assert.match(help, /id="helpRuns"/); assert.match(help, /Suggest fit<\/b>/); assert.match(help, /nearest-context score/); assert.match(help, /id="helpParams"/); assert.match(help, /quickStartSection/); assert.match(help, /class="quick-start-footer"/); assert.match(help, /zaher\.salman@psi\.ch/); assert.match(help, /https:\/\/musruser\.psi\.ch\/muSRFlow\/docs\//); assert.match(help, /id="helpAbout"/); assert.match(help, /About muSRFlow/); assert.match(help, /id="musrflowVersion"/); assert.match(help, /id="musrflowRevision"/); assert.match(help, /id="musrflowRuntime"/); assert.match(help, /setBuildInfo\(payload && payload\.buildInfo\)/); assert.match(help, /window\.electronAPI\.ipc\.on\("quickStartSection"/); assert.match(preload, /"quickStartSection"/); assert.match(main, /version: app\.getVersion\(\)/); assert.match(main, /Electron\/.*platformNames/); assert.match(main, /build-info\.json/); assert.match(elec, /electronQuickStartRuntimeInfo/); assert.match(elec, /musrfit version:/); assert.match(elec, /ROOT version:/); assert.match(main, /shell\.openExternal/); assert.match(main, /setWindowOpenHandler/); assert.match(deploy, /quick-start\.html/); assert.match(deploy, /__MUSRFLOW_VERSION__/); assert.match(deploy, /__MUSRFLOW_REVISION__/); assert.match(deploy, /git -C "\$project_root" rev-parse --short=12 HEAD/); assert.match(deploy, /node -p 'require\(process\.argv\[1\]\)\.version'/); assert.match(remoteDeploy, /assets\/model-recommender\.js/); assert.match(remoteDeploy, /Fit recommendation index missing/); assert.doesNotMatch(lib, /musrfit-gui|bitbucket\.org\/zaher-salman\/musrfit-gui/); } function testDocumentationDeployment() { // The Apache deployment script targets Linux; no real web directory is used. if (process.platform !== "linux") return; const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "musrflow web deploy ")); try { const source = path.join(temporary, "source"); const target = path.join(temporary, "installed"); for (const directory of ["scripts", "assets", "cgi"]) { fs.mkdirSync(path.join(source, directory), { recursive: true }); } fs.mkdirSync(target); fs.copyFileSync(path.join(repoRoot, "scripts/deploy-web.sh"), path.join(source, "scripts/deploy-web.sh")); const assets = ["index.html", "quick-start.html", "ZGUI.css", "muSRFitweb.js", "muSRFitRoot.js", "muSRFitParameterPlot.js", "muSRFitlib.js", "assets/musrflow-favicon.png", "assets/musrflow-icon.png", "assets/model-recommender.js", "cgi/single_musrfit.cgi", "cgi/musrfit_startup.xml"]; for (const file of assets) fs.writeFileSync(path.join(source, file), "fixture\n"); fs.copyFileSync(path.join(repoRoot, "quick-start.html"), path.join(source, "quick-start.html")); fs.writeFileSync(path.join(source, "build-revision.txt"), "muSRFlow=" + "b".repeat(40) + "\n"); fs.writeFileSync(path.join(source, "package.json"), JSON.stringify({ version: "1.2.3" })); const options = { encoding: "utf8", env: { ...process.env, MUSRFLOW_WEB_TARGET: path.join(target, "web"), MUSRFLOW_CGI_TARGET: path.join(target, "runner.cgi"), MUSRFLOW_STARTUP_XML_TARGET: path.join(target, "startup.xml"), MUSRFLOW_WEB_OWNER: "", MUSRFLOW_WEB_GROUP: "", MUSRFLOW_DATA_ROOT: "", MUSRFIT_BIN: path.join(source, "missing-musrfit") } }; const script = path.join(source, "scripts/deploy-web.sh"); const missing = spawnSync("bash", [script], options); assert.strictEqual(missing.status, 1, missing.stderr); assert.match(missing.stderr, /HTML manual missing.*make -C/); assert.deepStrictEqual(fs.readdirSync(target), [], "missing docs must not partially deploy the app"); const html = path.join(source, "docs/_build/html"); for (const directory of ["_static", "_sources"]) { fs.mkdirSync(path.join(html, directory), { recursive: true, mode: 0o700 }); } const pages = ["index.html", "searchindex.js", "_static/theme.css", "_sources/index.rst.txt"]; for (const file of pages) fs.writeFileSync(path.join(html, file), file, { mode: 0o600 }); const deployed = spawnSync("bash", [script], options); assert.strictEqual(deployed.status, 0, deployed.stderr); assert.match(deployed.stdout, /Installed muSRFlow HTML manual/); const installedHelp = fs.readFileSync(path.join(target, "web/quick-start.html"), "utf8"); assert.match(installedHelp, /id="musrflowVersion">1\.2\.3Webbbbbbbbbbbbb