mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-09-21 17:04:00 +00:00
Clusterio #984 converted its packages to ESM, so a CommonJS plugin can no longer import @clusterio/lib and plugin entrypoints must name the full file. Every package gets "type": "module", entrypoints and relative imports carry the .js extension, and the webpack configs become .cjs as webpack-cli loads them with require. tsconfig.node.json moves from node16 to nodenext to match clusterio, which needs it for the JSON import attributes in its sources. The tap tests and the shared test helpers use import and import.meta.dirname. The test scripts pass --type-strip-only so tap leaves lib's .ts subpath imports to Node's own type stripping, as its TypeScript loader compiled them without a default export. The lua runner resolves fengari from the test file it is given, since a bare import from the shared test directory does not reach the plugin's devDependencies. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
55 lines
1.5 KiB
JavaScript
55 lines
1.5 KiB
JavaScript
import { compile } from "@clusterio/lib";
|
|
|
|
/**
|
|
* Generate a flat array of tests from a matrix of inputs.
|
|
*
|
|
* @template {any[][]} T
|
|
* @param {T} arrays - A list of arrays representing the different values for each argument
|
|
* @returns {Array<{ [K in keyof T]: T[K][number] }>}
|
|
* An array of tuples, each containing one value from each input array.
|
|
*/
|
|
function testMatrix(...arrays) {
|
|
return arrays.reduce((acc, curr) => acc.flatMap(a => curr.map(b => [...a, b])), [[]]);
|
|
}
|
|
|
|
/**
|
|
* Test that a class is round trip JSON serialisable across multiple test cases.
|
|
*
|
|
* @template {any[]} T - Constructor arguments
|
|
* @param {import("tap").Test} t - Parent test.
|
|
* @param {{new(...args: T): object}} Class - The class which has toJSON and fromJSON methods.
|
|
* @param {T[]} tests - The test inputs to pass to the class constructor.
|
|
*/
|
|
function testRoundTripJsonSerialisable(t, Class, tests) {
|
|
const validate = compile(Class.jsonSchema);
|
|
|
|
for (const test of tests) {
|
|
const name = JSON.stringify(test);
|
|
|
|
try {
|
|
const original = new Class(...test);
|
|
const jsonObject = JSON.parse(JSON.stringify(original));
|
|
const reconstructed = Class.fromJSON(jsonObject);
|
|
|
|
if (!validate(jsonObject)) {
|
|
t.fail(`schema validation: ${name}`, {
|
|
diagnostic: {
|
|
json: jsonObject,
|
|
errors: validate.errors,
|
|
},
|
|
});
|
|
continue;
|
|
}
|
|
|
|
t.strictSame(reconstructed, original, `round trip: ${name}`);
|
|
} catch (error) {
|
|
t.fail(`round trip: ${name}`, { error });
|
|
}
|
|
}
|
|
}
|
|
|
|
export {
|
|
testMatrix,
|
|
testRoundTripJsonSerialisable,
|
|
};
|