mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 07:24:53 +09:00
Flatten lib namespace
Move all functions and classes currently available through lib/name and libName to be under a single flat namespace in lib. This make reorganising the lib namespace much simpler as the location of symbols can move without breaking the consumers of lib. The multi-entrypoint design was already problematic for the web plugin system which couldn't effectively use it due to how webpack's shared module system is awkward to set up with multiple entrypoints per package.
This commit is contained in:
1 parent
bb58670597
commit
bfe119eaa3
143 files changed
+1798
-1918
No files matched your search
+34
-34
@@ -3,24 +3,24 @@ const assert = require("assert").strict;
|
||||
const fs = require("fs-extra");
|
||||
const path = require("path");
|
||||
|
||||
const libDatabase = require("@clusterio/lib/database");
|
||||
const lib = require("@clusterio/lib");
|
||||
|
||||
describe("lib/database", function() {
|
||||
describe("mapToObject()", function() {
|
||||
it("should throw on non-string key", function() {
|
||||
assert.throws(
|
||||
() => libDatabase.mapToObject(new Map([[1, 1]])),
|
||||
() => lib.mapToObject(new Map([[1, 1]])),
|
||||
new Error("Expected all keys to be string but got number")
|
||||
);
|
||||
assert.throws(
|
||||
() => libDatabase.mapToObject(new Map([[Symbol("test"), 1]])),
|
||||
() => lib.mapToObject(new Map([[Symbol("test"), 1]])),
|
||||
new Error("Expected all keys to be string but got symbol")
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw convert a map to an object", function() {
|
||||
assert.deepEqual(
|
||||
libDatabase.mapToObject(new Map([["a", 1], ["b", true]])),
|
||||
lib.mapToObject(new Map([["a", 1], ["b", true]])),
|
||||
{ "a": 1, "b": true }
|
||||
);
|
||||
});
|
||||
@@ -32,7 +32,7 @@ describe("lib/database", function() {
|
||||
for (let type of badTypes) {
|
||||
it(`should reject on ${type} JSON`, async function() {
|
||||
await assert.rejects(
|
||||
libDatabase.loadJsonAsMap(path.join("test", "file", "json", `${type}.json`)),
|
||||
lib.loadJsonAsMap(path.join("test", "file", "json", `${type}.json`)),
|
||||
new Error(`Expected object but got ${type}`)
|
||||
);
|
||||
});
|
||||
@@ -40,21 +40,21 @@ describe("lib/database", function() {
|
||||
|
||||
it("should work on empty object JSON", async function() {
|
||||
assert.deepEqual(
|
||||
await libDatabase.loadJsonAsMap(path.join("test", "file", "json", "object.json")),
|
||||
await lib.loadJsonAsMap(path.join("test", "file", "json", "object.json")),
|
||||
new Map()
|
||||
);
|
||||
});
|
||||
|
||||
it("should work on object JSON", async function() {
|
||||
assert.deepEqual(
|
||||
await libDatabase.loadJsonAsMap(path.join("test", "file", "json", "load_map.json")),
|
||||
await lib.loadJsonAsMap(path.join("test", "file", "json", "load_map.json")),
|
||||
new Map([["a", 1], ["b", true]])
|
||||
);
|
||||
});
|
||||
|
||||
it("should give an empty Map for non-existant file", async function() {
|
||||
assert.deepEqual(
|
||||
await libDatabase.loadJsonAsMap(path.join("test", "file", "json", "does-not-exist")),
|
||||
await lib.loadJsonAsMap(path.join("test", "file", "json", "does-not-exist")),
|
||||
new Map()
|
||||
);
|
||||
});
|
||||
@@ -75,7 +75,7 @@ describe("lib/database", function() {
|
||||
}
|
||||
|
||||
await deleteTestFile();
|
||||
await libDatabase.saveMapAsJson(
|
||||
await lib.saveMapAsJson(
|
||||
testFile, new Map([["c", {}], ["d", "foo"]])
|
||||
);
|
||||
|
||||
@@ -92,7 +92,7 @@ describe("lib/database", function() {
|
||||
for (let type of badTypes) {
|
||||
it(`should reject on ${type} JSON`, async function() {
|
||||
await assert.rejects(
|
||||
libDatabase.loadJsonArrayAsMap(path.join("test", "file", "json", `${type}.json`)),
|
||||
lib.loadJsonArrayAsMap(path.join("test", "file", "json", `${type}.json`)),
|
||||
new Error(`Expected array but got ${type}`)
|
||||
);
|
||||
});
|
||||
@@ -100,35 +100,35 @@ describe("lib/database", function() {
|
||||
|
||||
it("should reject on null element", async function() {
|
||||
await assert.rejects(
|
||||
libDatabase.loadJsonArrayAsMap(path.join("test", "file", "json", "array_null.json")),
|
||||
lib.loadJsonArrayAsMap(path.join("test", "file", "json", "array_null.json")),
|
||||
new Error("Expected all elements to be objects")
|
||||
);
|
||||
});
|
||||
|
||||
it("should reject on element missing id", async function() {
|
||||
await assert.rejects(
|
||||
libDatabase.loadJsonArrayAsMap(path.join("test", "file", "json", "array_object.json")),
|
||||
lib.loadJsonArrayAsMap(path.join("test", "file", "json", "array_object.json")),
|
||||
new Error("Expected all elements to have an id property")
|
||||
);
|
||||
});
|
||||
|
||||
it("should work on empty array JSON", async function() {
|
||||
assert.deepEqual(
|
||||
await libDatabase.loadJsonArrayAsMap(path.join("test", "file", "json", "array.json")),
|
||||
await lib.loadJsonArrayAsMap(path.join("test", "file", "json", "array.json")),
|
||||
new Map()
|
||||
);
|
||||
});
|
||||
|
||||
it("should work on array JSON", async function() {
|
||||
assert.deepEqual(
|
||||
await libDatabase.loadJsonArrayAsMap(path.join("test", "file", "json", "load_array_map.json")),
|
||||
await lib.loadJsonArrayAsMap(path.join("test", "file", "json", "load_array_map.json")),
|
||||
new Map([["a", { id: "a" }], ["b", { id: "b" }]])
|
||||
);
|
||||
});
|
||||
|
||||
it("should give an empty Map for non-existant file", async function() {
|
||||
assert.deepEqual(
|
||||
await libDatabase.loadJsonArrayAsMap(path.join("test", "file", "json", "does-not-exist")),
|
||||
await lib.loadJsonArrayAsMap(path.join("test", "file", "json", "does-not-exist")),
|
||||
new Map()
|
||||
);
|
||||
});
|
||||
@@ -149,7 +149,7 @@ describe("lib/database", function() {
|
||||
}
|
||||
|
||||
await deleteTestFile();
|
||||
await libDatabase.saveMapAsJsonArray(
|
||||
await lib.saveMapAsJsonArray(
|
||||
testFile, new Map([["c", { id: "c" }], ["d", { id: "d" }]])
|
||||
);
|
||||
|
||||
@@ -163,23 +163,23 @@ describe("lib/database", function() {
|
||||
describe("class ItemDatabase", function() {
|
||||
describe("constructor()", function() {
|
||||
it("should create an empty database with no args", function() {
|
||||
let items = new libDatabase.ItemDatabase();
|
||||
let items = new lib.ItemDatabase();
|
||||
assert.deepEqual(items._items, new Map());
|
||||
});
|
||||
|
||||
it("should restore the passed serialized database", function() {
|
||||
let items = new libDatabase.ItemDatabase({ "a": 1, "b": 2 });
|
||||
let items = new lib.ItemDatabase({ "a": 1, "b": 2 });
|
||||
assert.deepEqual(items._items, new Map([["a", 1], ["b", 2]]));
|
||||
});
|
||||
|
||||
it("should throw on invalid serialized database", function() {
|
||||
assert.throws(
|
||||
() => new libDatabase.ItemDatabase({ "a": NaN }),
|
||||
() => new lib.ItemDatabase({ "a": NaN }),
|
||||
new Error("count must be a number")
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => new libDatabase.ItemDatabase({ "a": "a" }),
|
||||
() => new lib.ItemDatabase({ "a": "a" }),
|
||||
new Error("count must be a number")
|
||||
);
|
||||
});
|
||||
@@ -187,36 +187,36 @@ describe("lib/database", function() {
|
||||
|
||||
describe(".serialize()", function() {
|
||||
it("should return a serialized database", function() {
|
||||
let items = new libDatabase.ItemDatabase({ "a": 10 });
|
||||
let items = new lib.ItemDatabase({ "a": 10 });
|
||||
assert.deepEqual(items.serialize(), { "a": 10 });
|
||||
});
|
||||
|
||||
it("should remove zero count entries", function() {
|
||||
let items = new libDatabase.ItemDatabase({ "a": 0 });
|
||||
let items = new lib.ItemDatabase({ "a": 0 });
|
||||
assert.deepEqual(items.serialize(), {});
|
||||
});
|
||||
});
|
||||
|
||||
describe(".size", function() {
|
||||
it("should give an approximate size of the database", function() {
|
||||
let items = new libDatabase.ItemDatabase({ "a": 10 });
|
||||
let items = new lib.ItemDatabase({ "a": 10 });
|
||||
assert.equal(items.size, 1);
|
||||
});
|
||||
});
|
||||
|
||||
describe(".getItemCount()", function() {
|
||||
it("should return the count of the given item", function() {
|
||||
let items = new libDatabase.ItemDatabase({ "a": 10 });
|
||||
let items = new lib.ItemDatabase({ "a": 10 });
|
||||
assert.equal(items.getItemCount("a"), 10);
|
||||
});
|
||||
|
||||
it("should return zero if item does not exist", function() {
|
||||
let items = new libDatabase.ItemDatabase();
|
||||
let items = new lib.ItemDatabase();
|
||||
assert.equal(items.getItemCount("b"), 0);
|
||||
});
|
||||
|
||||
it("should throw on invalid name", function() {
|
||||
let items = new libDatabase.ItemDatabase();
|
||||
let items = new lib.ItemDatabase();
|
||||
assert.throws(
|
||||
() => items.getItemCount(2),
|
||||
new Error("name must be a string")
|
||||
@@ -226,19 +226,19 @@ describe("lib/database", function() {
|
||||
|
||||
describe(".addItem()", function() {
|
||||
it("should add a new item", function() {
|
||||
let items = new libDatabase.ItemDatabase();
|
||||
let items = new lib.ItemDatabase();
|
||||
items.addItem("a", 10);
|
||||
assert.deepEqual(items._items, new Map([["a", 10]]));
|
||||
});
|
||||
|
||||
it("should add an existing item", function() {
|
||||
let items = new libDatabase.ItemDatabase({ "a": 10 });
|
||||
let items = new lib.ItemDatabase({ "a": 10 });
|
||||
items.addItem("a", 10);
|
||||
assert.deepEqual(items._items, new Map([["a", 20]]));
|
||||
});
|
||||
|
||||
it("should throw on invalid name", function() {
|
||||
let items = new libDatabase.ItemDatabase();
|
||||
let items = new lib.ItemDatabase();
|
||||
assert.throws(
|
||||
() => items.addItem(2, 10),
|
||||
new Error("name must be a string")
|
||||
@@ -246,7 +246,7 @@ describe("lib/database", function() {
|
||||
});
|
||||
|
||||
it("should throw on invalid count", function() {
|
||||
let items = new libDatabase.ItemDatabase();
|
||||
let items = new lib.ItemDatabase();
|
||||
assert.throws(
|
||||
() => items.addItem("a", NaN),
|
||||
new Error("count must be a number")
|
||||
@@ -260,19 +260,19 @@ describe("lib/database", function() {
|
||||
|
||||
describe(".removeItem()", function() {
|
||||
it("should remove an existing item", function() {
|
||||
let items = new libDatabase.ItemDatabase({ "a": 20 });
|
||||
let items = new lib.ItemDatabase({ "a": 20 });
|
||||
items.removeItem("a", 10);
|
||||
assert.deepEqual(items._items, new Map([["a", 10]]));
|
||||
});
|
||||
|
||||
it("should turn a non-existing item negative", function() {
|
||||
let items = new libDatabase.ItemDatabase();
|
||||
let items = new lib.ItemDatabase();
|
||||
items.removeItem("a", 10);
|
||||
assert.deepEqual(items._items, new Map([["a", -10]]));
|
||||
});
|
||||
|
||||
it("should throw on invalid name", function() {
|
||||
let items = new libDatabase.ItemDatabase();
|
||||
let items = new lib.ItemDatabase();
|
||||
assert.throws(
|
||||
() => items.removeItem(2, 10),
|
||||
new Error("name must be a string")
|
||||
@@ -280,7 +280,7 @@ describe("lib/database", function() {
|
||||
});
|
||||
|
||||
it("should throw on invalid count", function() {
|
||||
let items = new libDatabase.ItemDatabase();
|
||||
let items = new lib.ItemDatabase();
|
||||
assert.throws(
|
||||
() => items.removeItem("a", "b"),
|
||||
new Error("count must be a number")
|
||||
|
||||
Reference in new issue
Block a user