mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-18 15:33:12 +09:00
Parse mod versions leniently like the game does
The mod portal "ApiVersion" enum was validated strictly when loading a mod's info.json, which rejected versions the game itself accepts. Versions are now normalised the same lenient way the game reads them: leading whitespace is allowed, the major and minor are required, the patch is optional, any trailing content is ignored, and numbers are read as integers. This applies to both a mod's version and its factorio_version, validated by a regex schema rather than the fixed enum. The enum is no longer used to validate versions at all - the mod portal is the source of truth, so its error is now surfaced when a version is unsupported. The known-version list kept for the web interface fallback gains 2.1. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
ce8bddad8f
commit
97a6c32838
10 files changed
+141
-81
No files matched your search
@@ -358,7 +358,7 @@ describe("lib/ModStore", function () {
|
||||
|
||||
await assert.rejects(
|
||||
modStore.downloadMods(modsToDownload, username, token, factorioVersion),
|
||||
/Fetch: https:\/\/mods\.factorio\.com\/api\/mods.* returned 503 Service Unavailable/
|
||||
/Mod portal request to https:\/\/mods\.factorio\.com\/api\/mods.* failed: 503 Service Unavailable/
|
||||
);
|
||||
assert.equal(modStore.files.size, 0, "No mods should be added on API error");
|
||||
});
|
||||
|
||||
@@ -341,6 +341,21 @@ describe("lib/data/ModInfo", function() {
|
||||
dependencies: ["base", ""],
|
||||
}), /my-mod/); // check the mod name is included in the error message
|
||||
});
|
||||
it("should normalise lenient version fields the way the game reads them", function() {
|
||||
const mod = ModInfo.fromJSON({
|
||||
name: "my-mod",
|
||||
version: " 1.2.3-extra",
|
||||
factorio_version: "1.1.0",
|
||||
});
|
||||
assert.equal(mod.version, "1.2.3");
|
||||
assert.equal(mod.factorioVersion, "1.1");
|
||||
});
|
||||
it("should throw for an unparseable mod version", function() {
|
||||
assert.throws(
|
||||
() => ModInfo.fromJSON({ name: "my-mod", version: "not-a-version" }),
|
||||
/Invalid mod version/
|
||||
);
|
||||
});
|
||||
it("should sort integer mod versions lexicographically", function() {
|
||||
let unsortedVersions = ["1.0.0", "1.1.0", "0.1.0", "3.0.0", "1.2.0", "0.3.1", "0.3.3", "2.1.1", "0.0.1"];
|
||||
let sortedVersions = ["0.0.1", "0.1.0", "0.3.1", "0.3.3", "1.0.0", "1.1.0", "1.2.0", "2.1.1", "3.0.0"];
|
||||
|
||||
@@ -17,36 +17,33 @@ describe("lib/data/version", function() {
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("isApiVersion()", function() {
|
||||
it("should correctly validate input strings", function() {
|
||||
const valid = ["0.13", "0.14", "0.15", "0.16", "0.17", "0.18", "1.0", "1.1", "2.0"];
|
||||
for (const test of valid) {
|
||||
assert.equal(lib.isApiVersion(test), true, test);
|
||||
}
|
||||
const invalid = ["0.12", "0.19", "1.2", "2.1", "1.0.0", "1.0.0.0", "0", "", "latest"];
|
||||
describe("normaliseGameVersion()", function() {
|
||||
it("should normalise lenient version strings the way the game reads them", function() {
|
||||
// Major and minor required, patch optional and defaulting to 0.
|
||||
assert.equal(lib.normaliseGameVersion("1.1"), "1.1.0");
|
||||
assert.equal(lib.normaliseGameVersion("1.1.50"), "1.1.50");
|
||||
assert.equal(lib.normaliseGameVersion("0.18"), "0.18.0");
|
||||
// Leading whitespace and zeros are dropped.
|
||||
assert.equal(lib.normaliseGameVersion(" 1.1"), "1.1.0");
|
||||
assert.equal(lib.normaliseGameVersion("01.02.03"), "1.2.3");
|
||||
assert.equal(lib.normaliseGameVersion("1. 1. 50"), "1.1.50");
|
||||
// Anything after the patch (or a non-numeric patch) is ignored.
|
||||
assert.equal(lib.normaliseGameVersion("1.1.50-beta"), "1.1.50");
|
||||
assert.equal(lib.normaliseGameVersion("1.1.x"), "1.1.0");
|
||||
assert.equal(lib.normaliseGameVersion("1.1.50.60"), "1.1.50");
|
||||
});
|
||||
it("should return undefined when no major.minor can be read", function() {
|
||||
const invalid = ["1", "", "latest", "abc", ".1.1", "x.y"];
|
||||
for (const test of invalid) {
|
||||
assert.equal(lib.isApiVersion(test), false, test);
|
||||
assert.equal(lib.normaliseGameVersion(test), undefined, test);
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("normaliseApiVersion()", function() {
|
||||
it("should normalise accepted versions", function() {
|
||||
assert.equal(lib.normaliseApiVersion("0.13"), "0.13");
|
||||
assert.equal(lib.normaliseApiVersion("0.13.0"), "0.13");
|
||||
assert.equal(lib.normaliseApiVersion("0.13.1"), "0.13");
|
||||
assert.equal(lib.normaliseApiVersion("1.1"), "1.1");
|
||||
assert.equal(lib.normaliseApiVersion("1.1.0"), "1.1");
|
||||
assert.equal(lib.normaliseApiVersion("1.1.1"), "1.1");
|
||||
assert.equal(lib.normaliseApiVersion("2.0"), "2.0");
|
||||
assert.equal(lib.normaliseApiVersion("2.0.0"), "2.0");
|
||||
assert.equal(lib.normaliseApiVersion("2.0.1"), "2.0");
|
||||
assert.equal(lib.normaliseApiVersion("1.0.0.0"), "1.0");
|
||||
});
|
||||
it("should throw for invalid versions", function() {
|
||||
const invalid = ["0.12", "0", "", "latest"];
|
||||
for (const test of invalid) {
|
||||
assert.throws(() => lib.normaliseApiVersion(test), undefined, test);
|
||||
}
|
||||
describe("majorMinorVersion()", function() {
|
||||
it("should return the major.minor", function() {
|
||||
assert.equal(lib.majorMinorVersion("1.1"), "1.1");
|
||||
assert.equal(lib.majorMinorVersion("1.1.50"), "1.1");
|
||||
assert.equal(lib.majorMinorVersion("0.18.0"), "0.18");
|
||||
});
|
||||
});
|
||||
describe("isFullVersion()", function() {
|
||||
|
||||
Reference in new issue
Block a user