import assert from "node:assert/strict"; import { readFile } from "node:fs/promises"; import { factorioHeadlessUrl, fetchFactorioVersions, hasFactorioHeadless } from "@clusterio/lib"; import { slowTest, externalTest } from "../../common.js"; describe("FactorioVersions", function() { describe("hasFactorioHeadless", function() { for (const [version, platform, arch, expected] of [ ["0.17.79", "linux", "x64", true], ["2.1.17", "linux", "arm64", false], ["2.1.18", "linux", "arm64", true], ["2.1.24", "win32", "x64", false], ["2.1.24", "win32", "arm64", false], ]) { it(`should return ${expected} for ${version} ${platform} ${arch}`, function() { assert.equal(hasFactorioHeadless(version, platform, arch), expected); }); } }); describe("factorioHeadlessUrl", function() { it("should return the linux64 build for x64", function() { assert.equal( factorioHeadlessUrl("2.1.24", "linux", "x64"), "https://www.factorio.com/get-download/2.1.24/headless/linux64", ); }); it("should return the linux-arm64 build for arm64", function() { assert.equal( factorioHeadlessUrl("2.1.24", "linux", "arm64"), "https://www.factorio.com/get-download/2.1.24/headless/linux-arm64", ); }); it("should throw for Windows", function() { assert.throws( () => factorioHeadlessUrl("2.1.24", "win32", "x64"), new Error("Factorio 2.1.24 has no headless build for win32 x64"), ); }); it("should throw for linux arm64 before 2.1.18", function() { assert.throws( () => factorioHeadlessUrl("2.1.17", "linux", "arm64"), new Error("Factorio 2.1.17 has no headless build for linux arm64"), ); }); }); describe("fetchFactorioVersions", function() { const originalFetch = global.fetch; beforeEach(() => { global.fetch = undefined; }); afterEach(() => { global.fetch = originalFetch; }); function mockFetch(html) { const mockResponse = { ok: true, status: 200, statusText: "OK", text: async () => html, }; global.fetch = async () => mockResponse; } it("fetches and parsed versions with stable / experimental flags", async function() { mockFetch(`
2.0.73 2.0.72 `); const versions = await fetchFactorioVersions(); assert.deepEqual(versions, [ { stable: false, version: "2.0.73", headlessUrl: "https://www.factorio.com/get-download/2.0.73/headless/linux64", }, { stable: true, version: "2.0.72", headlessUrl: "https://www.factorio.com/get-download/2.0.72/headless/linux64", }, ]); }); it("deduplicates versions by last occurrence", async function() { mockFetch(` 2.0.73 2.0.73 `); const versions = await fetchFactorioVersions(); assert.deepEqual(versions, [ { stable: true, version: "2.0.73", headlessUrl: "https://www.factorio.com/get-download/2.0.73/headless/linux64", }, ]); }); it("thorws on HTTP error status", async function() { const mockResponse = { ok: false, status: 500, statusText: "Server Error", text: async () => "", }; global.fetch = async () => mockResponse; await assert.rejects(fetchFactorioVersions(), /HTTP 500 Server Error/); }); it("thorws on network error status", async function() { global.fetch = async () => { throw new Error("Network Error"); }; await assert.rejects(fetchFactorioVersions(), /Network Error/); }); it("parses versions from saved html file", async function() { mockFetch(await readFile("test/file/html/factorio-download-archive.html")); const versions = await fetchFactorioVersions(); assert.equal(versions.length, 105); assert.equal(versions.filter(v => v.stable).length, 48); assert.equal(versions.filter(v => !v.stable).length, 57); }); it("parses versions from empty html file", async function() { mockFetch(""); const versions = await fetchFactorioVersions(); assert.equal(versions.length, 0); }); it("parses versions from live api", async function() { slowTest(this); externalTest(this); global.fetch = originalFetch; const versions = await fetchFactorioVersions(); assert.ok(versions.length >= 105); assert.ok(versions.filter(v => v.stable).length >= 48); assert.ok(versions.filter(v => !v.stable).length >= 57); }); }); });