181 lines
6.7 KiB
JavaScript
181 lines
6.7 KiB
JavaScript
// Copyright (C) 2026 Zaher Salman
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
// Unit tests for the Windows bundled-runtime (WSL2) import helper. The real
|
|
// import only runs on Windows; the pure helpers and the injected spawner let us
|
|
// exercise every branch here on Linux.
|
|
|
|
const assert = require("assert");
|
|
const fs = require("fs");
|
|
const os = require("os");
|
|
const path = require("path");
|
|
|
|
const wsl = require("../wsl-runtime");
|
|
|
|
// --- decode / parse ------------------------------------------------------
|
|
|
|
function testDecodeWslOutput() {
|
|
const utf16 = Buffer.from("muSRFlow-runtime\r\n", "utf16le");
|
|
assert.strictEqual(wsl.decodeWslOutput(utf16), "muSRFlow-runtime\r\n");
|
|
const utf8 = Buffer.from("Importing... 42%\n", "utf8");
|
|
assert.strictEqual(wsl.decodeWslOutput(utf8), "Importing... 42%\n");
|
|
assert.strictEqual(wsl.decodeWslOutput("plain"), "plain");
|
|
assert.strictEqual(wsl.decodeWslOutput(null), "");
|
|
assert.strictEqual(wsl.decodeWslOutput(Buffer.alloc(0)), "");
|
|
}
|
|
|
|
function testParseDistroList() {
|
|
assert.deepStrictEqual(wsl.parseDistroList("Ubuntu\r\nDebian\r\n"), ["Ubuntu", "Debian"]);
|
|
assert.deepStrictEqual(wsl.parseDistroList(" spaced \n\n"), ["spaced"]);
|
|
// UTF-16LE text that was not decoded first: NULs are stripped.
|
|
assert.deepStrictEqual(wsl.parseDistroList("m\u0000u\u0000S\u0000R\u0000"), ["muSR"]);
|
|
assert.deepStrictEqual(wsl.parseDistroList(""), []);
|
|
}
|
|
|
|
function testDistroIsRegistered() {
|
|
assert.strictEqual(wsl.distroIsRegistered("Ubuntu\r\nmuSRFlow-runtime\r\n", "muSRFlow-runtime"), true);
|
|
assert.strictEqual(wsl.distroIsRegistered("ubuntu\r\n", "Ubuntu"), true);
|
|
assert.strictEqual(wsl.distroIsRegistered("Ubuntu\r\n", "muSRFlow-runtime"), false);
|
|
}
|
|
|
|
function testArgumentBuilders() {
|
|
assert.deepStrictEqual(wsl.listArgs(), ["--list", "--quiet"]);
|
|
assert.deepStrictEqual(
|
|
wsl.importArgs("C:\\res\\rootfs.tar.gz", "C:\\appdata\\muSRFlow\\runtime", "muSRFlow-runtime"),
|
|
["--import", "muSRFlow-runtime", "C:\\appdata\\muSRFlow\\runtime",
|
|
"C:\\res\\rootfs.tar.gz", "--version", "2"]
|
|
);
|
|
assert.strictEqual(
|
|
wsl.distroInstallDir("C:\\appdata", "muSRFlow-runtime"),
|
|
path.join("C:\\appdata", "muSRFlow", "muSRFlow-runtime")
|
|
);
|
|
}
|
|
|
|
// --- rootfs discovery ----------------------------------------------------
|
|
|
|
function testFindRootfsTarball() {
|
|
const resources = fs.mkdtempSync(path.join(os.tmpdir(), "musrflow-wsl-"));
|
|
assert.strictEqual(wsl.findRootfsTarball(resources), "");
|
|
assert.strictEqual(wsl.findRootfsTarball(path.join(resources, "nope")), "");
|
|
|
|
const primary = path.join(resources, "wsl-runtime");
|
|
fs.mkdirSync(primary, { recursive: true });
|
|
const rootfs = path.join(primary, wsl.ROOTFS_BASENAME);
|
|
fs.writeFileSync(rootfs, "x");
|
|
assert.strictEqual(wsl.findRootfsTarball(resources), rootfs);
|
|
assert(wsl.rootfsCandidates(resources)[0].endsWith(wsl.ROOTFS_BASENAME));
|
|
}
|
|
|
|
// --- ensureWSLRuntime(flow) with an injected spawner ---------------------
|
|
|
|
function makeRun(responses) {
|
|
const calls = [];
|
|
const run = function (args, onOutput) {
|
|
calls.push(args);
|
|
const next = responses.shift() || { status: 0, stdout: "", stderr: "" };
|
|
if (typeof onOutput === "function" && next.stderr) onOutput(next.stderr);
|
|
return Promise.resolve(next);
|
|
};
|
|
run.calls = calls;
|
|
return run;
|
|
}
|
|
|
|
function makeRootfs() {
|
|
const rootfs = fs.mkdtempSync(path.join(os.tmpdir(), "musrflow-wsl-"));
|
|
const rootfsFile = path.join(rootfs, wsl.ROOTFS_BASENAME);
|
|
fs.writeFileSync(rootfsFile, "x");
|
|
return rootfsFile;
|
|
}
|
|
|
|
async function testNotWindows() {
|
|
const result = await wsl.ensureWSLRuntime({ platform: "linux", run: makeRun([]) });
|
|
assert.strictEqual(result.ok, true);
|
|
assert.strictEqual(result.status, "skipped");
|
|
assert.strictEqual(result.reason, "not-windows");
|
|
}
|
|
|
|
async function testMissingRootfs() {
|
|
const resources = fs.mkdtempSync(path.join(os.tmpdir(), "musrflow-wsl-"));
|
|
const result = await wsl.ensureWSLRuntime({
|
|
platform: "win32",
|
|
resourcesPath: resources,
|
|
appDataPath: resources,
|
|
run: makeRun([])
|
|
});
|
|
assert.strictEqual(result.ok, false);
|
|
assert.strictEqual(result.status, "missing-rootfs");
|
|
}
|
|
|
|
async function testAlreadyPresentSkipsImport() {
|
|
const rootfsFile = makeRootfs();
|
|
const run = makeRun([{ status: 0, stdout: "Ubuntu\r\nmuSRFlow-runtime\r\n" }]);
|
|
const result = await wsl.ensureWSLRuntime({
|
|
platform: "win32",
|
|
appDataPath: path.dirname(rootfsFile),
|
|
rootfsPath: rootfsFile,
|
|
run: run
|
|
});
|
|
assert.strictEqual(result.ok, true);
|
|
assert.strictEqual(result.status, "already-present");
|
|
assert.strictEqual(run.calls.length, 1, "only the list command should run");
|
|
}
|
|
|
|
async function testImportSucceeds() {
|
|
const appData = fs.mkdtempSync(path.join(os.tmpdir(), "musrflow-appdata-"));
|
|
const rootfsFile = makeRootfs();
|
|
const phases = [];
|
|
const run = makeRun([
|
|
{ status: 0, stdout: "Ubuntu\r\n" },
|
|
{ status: 0, stdout: "", stderr: "Import in progress\n" }
|
|
]);
|
|
const result = await wsl.ensureWSLRuntime({
|
|
platform: "win32",
|
|
appDataPath: appData,
|
|
rootfsPath: rootfsFile,
|
|
run: run,
|
|
onProgress: function (p) { phases.push(p.phase); }
|
|
});
|
|
assert.strictEqual(result.ok, true);
|
|
assert.strictEqual(result.status, "imported");
|
|
assert.strictEqual(result.target, path.join(appData, "muSRFlow", "muSRFlow-runtime"));
|
|
assert.deepStrictEqual(run.calls[1], wsl.importArgs(rootfsFile, result.target, "muSRFlow-runtime"));
|
|
assert(phases.includes("checking") && phases.includes("importing") && phases.includes("ready"));
|
|
}
|
|
|
|
async function testImportFailure() {
|
|
const appData = fs.mkdtempSync(path.join(os.tmpdir(), "musrflow-appdata-"));
|
|
const rootfsFile = makeRootfs();
|
|
const run = makeRun([
|
|
{ status: 0, stdout: "" },
|
|
{ status: 1, stdout: "", stderr: "The system cannot find the file specified." }
|
|
]);
|
|
const result = await wsl.ensureWSLRuntime({
|
|
platform: "win32",
|
|
appDataPath: appData,
|
|
rootfsPath: rootfsFile,
|
|
run: run
|
|
});
|
|
assert.strictEqual(result.ok, false);
|
|
assert.strictEqual(result.status, "import-failed");
|
|
assert(result.error.includes("cannot find the file"));
|
|
}
|
|
|
|
async function main() {
|
|
testDecodeWslOutput();
|
|
testParseDistroList();
|
|
testDistroIsRegistered();
|
|
testArgumentBuilders();
|
|
testFindRootfsTarball();
|
|
await testNotWindows();
|
|
await testMissingRootfs();
|
|
await testAlreadyPresentSkipsImport();
|
|
await testImportSucceeds();
|
|
await testImportFailure();
|
|
console.log("WSL runtime tests passed");
|
|
}
|
|
|
|
main().catch(function (error) {
|
|
console.error(error);
|
|
process.exit(1);
|
|
});
|