Add missing tests

This commit is contained in:
Cooldude2606 committed 2026-02-05 16:17:34 +00:00
1 parent 372c1bffe1
commit dc3205148e
2 files changed
+83 -13

No files matched your search

+10 -1
View File
@@ -128,7 +128,13 @@ async function downloadAndExtractZip(url: string, targetDir: string) {
await fs.ensureDir(targetDir);
await Promise.all(
Object.values(zip.files).map(async (entry) => {
const entryPath = path.join(targetDir, entry.name);
const parts = entry.name.split("/").filter(Boolean);
const strippedPath = parts.slice(1).join("/");
if (!strippedPath) {
return; // This copies the behaviour of tar --strip-components 1
}
const entryPath = path.join(targetDir, strippedPath);
if (entry.dir) {
await fs.ensureDir(entryPath);
} else {
@@ -156,6 +162,7 @@ async function downloadAndExtractTar(url: string, targetDir: string) {
const tmpFilePath = `${targetDir}.tmp`;
const buffer = Buffer.from(await res.arrayBuffer());
await fs.writeFile(tmpFilePath, buffer);
await fs.ensureDir(targetDir);
// Execute tar CLI to extract
try {
@@ -1560,6 +1567,8 @@ export const _getFactorioVersion = getFactorioVersion;
export const _versionOrder = versionOrder;
export const _findVersion = findVersion;
export const _listFactorioVersions = listFactorioVersions;
export const _downloadAndExtractZip = downloadAndExtractZip;
export const _downloadAndExtractTar = downloadAndExtractTar;
export const _randomDynamicPort = randomDynamicPort;
export const _generatePassword = generatePassword;
export const _parseOutput = parseOutput;
+73 -12
View File
@@ -8,6 +8,7 @@ const hostServer = require("@clusterio/host/dist/node/src/server");
const lib = require("@clusterio/lib");
const { wait } = lib;
const { testLines } = require("../lib/factorio/lines");
const { slowTest } = require("../integration");
describe("host/server", function() {
@@ -57,11 +58,12 @@ describe("host/server", function() {
assert.equal(dir, path.join(installDir, "data"));
assert.equal(version, "0.1.2");
});
it("should search given directory for latest Factorio install", async function() {
const installDir = path.join("test", "file", "factorio");
const [dir, version] = await hostServer._findVersion(installDir, "latest");
assert.equal(dir, path.join(installDir, "0.1.2", "data"));
assert.equal(version, "0.1.2");
it("should reject if the version does not match", async function() {
let installDir = path.join("test", "file", "0.1.1");
await assert.rejects(
hostServer._findVersion(installDir, "0.1.2"),
new Error("Unable to find Factorio version 0.1.2")
);
});
});
describe("mutli install", function() {
@@ -91,13 +93,13 @@ describe("host/server", function() {
new Error("Unable to find Factorio version 0.1.3")
);
});
});
it("should reject if no factorio install was found", async function() {
let installDir = path.join("test", "file");
await assert.rejects(
hostServer._findVersion(installDir, "latest"),
new Error(`Unable to find any Factorio install in ${installDir}`)
);
it("should reject if no factorio install was found", async function() {
let installDir = path.join("test", "file");
await assert.rejects(
hostServer._findVersion(installDir, "0.0.0"),
new Error(`Unable to find any Factorio install in ${installDir}`)
);
});
});
});
@@ -120,6 +122,50 @@ describe("host/server", function() {
});
});
describe("downloadAndExtractZip", function() {
let _fetch;
beforeEach(function() {
_fetch = global.fetch;
});
afterEach(function() {
global.fetch = _fetch;
});
it("works", async function() {
const url = "https://github.com/clusterio/clusterio/archive/refs/tags/v2.0.0-alpha.22.zip";
const downloads = path.join("temp", "test", "downloads");
await fs.emptyDir(downloads);
await hostServer._downloadAndExtractZip(url, path.join(downloads, "zip"));
assert.ok(await fs.exists(path.join(downloads, "zip", "packages", "controller", "package.json")));
});
it("errors and bad status", async function() {
global.fetch = () => ({ ok: false, status: -1, statusText: "Fetch called" });
await assert.rejects(hostServer._downloadAndExtractZip("url does not matter"), /-1 Fetch called/);
});
});
describe("downloadAndExtractTar", function() {
let _fetch;
beforeEach(function() {
_fetch = global.fetch;
});
afterEach(function() {
global.fetch = _fetch;
});
it("works", async function() {
const url = "https://github.com/clusterio/clusterio/archive/refs/tags/v2.0.0-alpha.22.tar.gz";
const downloads = path.join("temp", "test", "downloads");
await fs.emptyDir(downloads);
await hostServer._downloadAndExtractTar(url, path.join(downloads, "tar"));
assert.ok(await fs.exists(path.join(downloads, "tar", "packages", "controller", "package.json")));
});
it("errors and bad status", async function() {
global.fetch = () => ({ ok: false, status: -1, statusText: "Fetch called" });
await assert.rejects(hostServer._downloadAndExtractZip("url does not matter"), /-1 Fetch called/);
});
});
describe("randomDynamicPort()", function() {
it("should return a port number", function() {
let port = hostServer._randomDynamicPort();
@@ -289,6 +335,7 @@ describe("host/server", function() {
});
afterEach(function() {
global.fetch = _fetch;
server._factorioDir = path.join("test", "file", "factorio");
Object.defineProperty(process, "platform", {
value: _platform,
});
@@ -552,6 +599,20 @@ describe("host/server", function() {
assert.ok(logLine.endsWith("starting download..."));
});
});
it("should download a version correctly (live api)", async function() {
slowTest(this);
if (_platform !== "linux") {
this.skip();
}
server._factorioDir = path.join("test", "file", "factorioDownload");
server._targetVersion = "latest";
await server.checkForUpdates([{
stable: true,
version: "2.0.73",
headlessUrl: "https://www.factorio.com/get-download/2.0.73/headless/linux64",
}]);
});
});
});
});