mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-12 07:04:52 +09:00
These are used outside of the integration tests, but including the integration index file causes the very slow setup of the contorller and host. Avoid the integration setup costs when running commands such as `pnpm mocha test/lib` by moving these utility functions to the common test functions.
58 lines
1.7 KiB
JavaScript
58 lines
1.7 KiB
JavaScript
import assert from "node:assert/strict";
|
|
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.
|
|
*/
|
|
export 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 {{new(...args: T): object}} Class - The class which has toJSON and fromJSON methods.
|
|
* @param {T[]} tests - The tests inputs to pass to the class constructor.
|
|
*/
|
|
export function testRoundTripJsonSerialisable(Class, tests) {
|
|
const validate = compile(Class.jsonSchema);
|
|
for (const test of tests) {
|
|
const original = new Class(...test);
|
|
const serialised = JSON.stringify(original);
|
|
const jsonObject = JSON.parse(serialised);
|
|
const reconstructed = Class.fromJSON(jsonObject);
|
|
assert.deepEqual(reconstructed, original, JSON.stringify(test));
|
|
if (!validate(jsonObject)) {
|
|
throw validate.errors;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Mark that this test takes a lot of time, or depends on a test that takes a
|
|
* lot of time.
|
|
*/
|
|
export function slowTest(test) {
|
|
if (process.env.FAST_TEST) {
|
|
test.skip();
|
|
}
|
|
test.timeout(30000);
|
|
}
|
|
|
|
/**
|
|
* Mark that this test depends on an external API and may be slow or flaky.
|
|
*/
|
|
export function externalTest(test) {
|
|
if (process.env.NO_EXTERNAL_TEST) {
|
|
test.skip();
|
|
}
|
|
test.timeout(60000);
|
|
}
|