import test from 'node:test'; import assert from 'node:assert/strict'; import { mkdtemp, readFile, writeFile, rm } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import path from 'node:path'; import { spawnSync } from 'node:child_process'; import { existsSync } from 'node:fs'; import { releaseVersion, nextHotfix, compareReleases } from './releaseVersion.mjs'; const ordering = ['0.1.22z', '0.1.23', '0.1.23a', '0.1.23b', '0.1.23c', '0.1.23z', '0.1.23aa', '0.1.23ab', '0.1.23zz', '0.1.23aaa', '0.1.24']; test('hotfix versions retain their release identity and advance in letter order', () => { assert.deepEqual(releaseVersion('0.1.23a'), { version: '0.1.23a', base: '0.1.23', suffix: 'a', revision: 1, toolchain: '0.1.23+1', windows: '0.1.23.1' }); for (const [before, after] of [['0.1.23', '0.1.23a'], ['0.1.23a', '0.1.23b'], ['0.1.23b', '0.1.23c'], ['0.1.23z', '0.1.23aa'], ['0.1.23aa', '0.1.23ab'], ['0.1.23zz', '0.1.23aaa']]) assert.equal(nextHotfix(before), after); for (const version of ['0.1.23A', '0.1.23a1', '0.1.23-a', '0.1.23+1', '0.1', '0.1.23aaaaa', '0.1.65536']) assert.throws(() => releaseVersion(version)); for (let i = 0; i < ordering.length; i++) for (let j = 0; j < ordering.length; j++) assert.equal(compareReleases(ordering[i], ordering[j]), Math.sign(i - j)); }); const makensis = path.join(process.env.LOCALAPPDATA ?? '', 'tauri/NSIS/makensis.exe'); const nsisSkip = process.platform !== 'win32' ? 'Windows only' : existsSync(makensis) ? false : 'the NSIS compiler is missing; run pnpm build:installer once to download it'; test('the native NSIS comparator blocks older hotfixes and permits reinstall', { skip: nsisSkip }, async () => { const root = await mkdtemp(path.join(tmpdir(), 'runestone-installer-version-')); try { const checks: string[] = []; checks.push('StrCpy $0 "keep0"\nStrCpy $1 "keep1"\nPush "0.1.23a"\nPush "0.1.23"\nCall RuneStoneCompareVersions\nPop $2\nStrCmp $0 "keep0" saved0\nSetErrorLevel 1\nQuit\nsaved0:\nStrCmp $1 "keep1" saved1\nSetErrorLevel 1\nQuit\nsaved1:'); let index = 0; for (let i = 0; i < ordering.length; i++) for (let j = 0; j < ordering.length; j++) { checks.push(`Push "${ordering[i]}"\nPush "${ordering[j]}"\nCall RuneStoneCompareVersions\nPop $0\nStrCmp $0 ${Math.sign(i - j)} check_${index}\nSetErrorLevel 1\nQuit\ncheck_${index++}:`); } for (const version of ['0.1.23A', '0.1.23a1', '0.1.a', '0.1.23-rc', '0.1.23aaaaa', '4294967296.0.0']) { checks.push(`Push "0.1.23a"\nPush "${version}"\nCall RuneStoneCompareVersions\nPop $0\nStrCmp $0 -1 check_${index}\nSetErrorLevel 1\nQuit\ncheck_${index++}:`); } const executable = path.join(root, 'compare.exe'); const source = `Unicode true\nSilentInstall silent\nRequestExecutionLevel user\nName "RuneStone version check"\nOutFile "${executable}"\n!include LogicLib.nsh\n!include WordFunc.nsh\n!include "${path.resolve('src-tauri/windows/versionCompare.nsh')}"\nSection\n${checks.join('\n')}\nSetErrorLevel 0\nSectionEnd\n`; const script = path.join(root, 'compare.nsi'); await writeFile(script, source); const compiler = makensis; const built = spawnSync(compiler, ['/V2', script], { encoding: 'utf8', windowsHide: true }); assert.equal(built.status, 0, built.stdout + built.stderr); const checked = spawnSync(executable, ['/S'], { encoding: 'utf8', windowsHide: true, timeout: 30000 }); assert.equal(checked.status, 0, checked.stdout + checked.stderr); const patched = spawnSync('powershell.exe', ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', path.resolve('scripts/setWindowsVersion.ps1'), '-Path', executable, '-Version', '0.1.23a', '-NumericVersion', '0.1.23.1'], { encoding: 'utf8', windowsHide: true }); assert.equal(patched.status, 0, patched.stdout + patched.stderr); assert.match(patched.stdout, /0\.1\.23a/); // Git can check the template out with CRLF line endings on Windows. const template = (await readFile('src-tauri/windows/installer.nsi', 'utf8')).replaceAll('\r\n', '\n'); assert.ok(template.includes('Call RuneStoneCompareVersions')); assert.ok(!template.includes('nsis_tauri_utils::SemverCompare')); assert.match(template, /SetErrorLevel 2\n\s+Quit/); const guard = / !if "\$\{ALLOWDOWNGRADES\}" == "false"\n \$\{If\} \$R0 = -1[\s\S]+? !endif/.exec(template)?.[0]; assert.ok(guard, 'The downgrade guard must precede reinstall and update actions.'); const blockExe = path.join(root, 'block.exe'); const blockScript = path.join(root, 'block.nsi'); await writeFile(blockScript, `Unicode true\nSilentInstall silent\nRequestExecutionLevel user\nName "RuneStone downgrade check"\nOutFile "${blockExe}"\n!include LogicLib.nsh\n!define ALLOWDOWNGRADES "false"\nLangString dontUninstallDowngrade 1033 "Older versions cannot replace this installation."\nSection\nStrCpy $R0 -1\n${guard}\nSetErrorLevel 1\nSectionEnd\n`); const blockBuilt = spawnSync(compiler, ['/V2', blockScript], { encoding: 'utf8', windowsHide: true }); assert.equal(blockBuilt.status, 0, blockBuilt.stdout + blockBuilt.stderr); for (const flags of [['/S'], ['/S', '/P'], ['/S', '/UPDATE']]) { const blocked = spawnSync(blockExe, flags, { encoding: 'utf8', windowsHide: true, timeout: 30000 }); assert.equal(blocked.status, 2, 'The installer must block a downgrade with ' + flags.join(' ')); } assert.match(template, /\$\{ElseIf\} \$R0 = 1 ; Upgrading\n\s+\$\{If\} \$R1 = 1\n\s+Goto reinst_done/); assert.ok(template.includes('"RuneStoneReleaseVersion" "${VERSION}"')); assert.ok(template.includes('"DisplayVersion" "${TOOLCHAINVERSION}"')); const legacyExe = path.join(root, 'legacy.exe'); const legacyScript = path.join(root, 'legacy.nsi'); const pluginPath = path.join(process.env.LOCALAPPDATA!, 'tauri/NSIS/Plugins/x86-unicode/additional'); await writeFile(legacyScript, `Unicode true\nSilentInstall silent\nRequestExecutionLevel user\nName "RuneStone legacy version check"\nOutFile "${legacyExe}"\n!addplugindir "${pluginPath}"\nSection\nnsis_tauri_utils::SemverCompare "0.1.23" "0.1.23+1"\nPop $0\nStrCmp $0 -1 legacy_ok\nSetErrorLevel 1\nQuit\nlegacy_ok:\nnsis_tauri_utils::SemverCompare "0.1.22" "0.1.23+1"\nPop $0\nStrCmp $0 -1 legacy_older_ok\nSetErrorLevel 1\nQuit\nlegacy_older_ok:\nnsis_tauri_utils::SemverCompare "0.1.24" "0.1.23+1"\nPop $0\nStrCmp $0 1 legacy_newer_ok\nSetErrorLevel 1\nQuit\nlegacy_newer_ok:\nSetErrorLevel 0\nSectionEnd\n`); const legacyBuilt = spawnSync(compiler, ['/V2', legacyScript], { encoding: 'utf8', windowsHide: true }); assert.equal(legacyBuilt.status, 0, legacyBuilt.stdout + legacyBuilt.stderr); const legacyChecked = spawnSync(legacyExe, ['/S'], { encoding: 'utf8', windowsHide: true, timeout: 30000 }); assert.equal(legacyChecked.status, 0, 'Older installers must detect the encoded hotfix as newer.'); } finally { assert.equal(path.isAbsolute(root) && root.startsWith(path.join(tmpdir(), 'runestone-installer-version-')), true); await rm(root, { recursive: true, force: true }); } });