0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-27 09:50:52 -05:00
liabru-matter-js/test/Examples.spec.js

114 lines
3.7 KiB
JavaScript
Raw Normal View History

2019-09-28 10:15:54 -04:00
/* eslint-env es6 */
2019-09-14 14:31:31 -04:00
"use strict";
2021-12-26 16:57:51 -05:00
jest.setTimeout(2 * 60 * 1000);
2019-09-14 14:31:31 -04:00
const fs = require('fs');
const {
requireUncached,
2021-01-28 19:09:27 -05:00
comparisonReport,
logReport,
toMatchExtrinsics,
toMatchIntrinsics,
getArg
2021-01-28 19:09:27 -05:00
} = require('./TestTools');
2019-09-14 14:31:31 -04:00
const Example = requireUncached('../examples/index');
const MatterBuild = requireUncached('../build/matter');
const { versionSatisfies } = requireUncached('../src/core/Plugin');
2024-03-03 16:01:45 -05:00
const Worker = require('jest-worker').Worker;
2019-09-14 14:31:31 -04:00
const testComparison = getArg('compare', null) === 'true';
const saveComparison = getArg('save', null) === 'true';
const specificExamples = getArg('examples', null, (val) => val.split(','));
const repeats = getArg('repeats', 1, parseFloat);
const updates = getArg('updates', 150, parseFloat);
2024-03-03 16:01:45 -05:00
const benchmark = getArg('benchmark', null) === 'true';
2021-12-15 04:58:31 -05:00
2020-03-09 16:56:06 -04:00
const excludeExamples = ['svg', 'terrain'];
const excludeJitter = ['stack', 'circleStack', 'restitution', 'staticFriction', 'friction', 'newtonsCradle', 'catapult'];
2021-12-15 04:58:31 -05:00
const examples = (specificExamples || Object.keys(Example)).filter(key => {
const excluded = excludeExamples.includes(key);
const buildVersion = MatterBuild.version;
const exampleFor = Example[key].for;
const supported = versionSatisfies(buildVersion, exampleFor);
return !excluded && supported;
});
2019-09-14 14:31:31 -04:00
const captureExamples = async useDev => {
2024-03-03 16:01:45 -05:00
const worker = new Worker(require.resolve('./ExampleWorker'), {
enableWorkerThreads: true,
2024-03-03 16:01:45 -05:00
numWorkers: benchmark ? 1 : undefined
});
2019-09-30 18:28:17 -04:00
2024-03-03 16:01:45 -05:00
const completeRuns = await Promise.all(examples.map(name => worker.runExample({
name,
useDev,
updates: updates,
2024-03-03 16:01:45 -05:00
repeats: benchmark ? Math.max(repeats, 3) : repeats,
stableSort: false,
2020-03-09 16:56:06 -04:00
jitter: excludeJitter.includes(name) ? 0 : 1e-10
})));
2019-09-30 18:28:17 -04:00
2024-03-03 16:01:45 -05:00
await worker.end();
const capture = {};
for (const completeRun of completeRuns) {
2024-03-03 16:01:45 -05:00
capture[completeRun.name] = completeRun;
}
2019-09-30 18:28:17 -04:00
return capture;
2019-09-30 18:28:17 -04:00
};
const capturesDev = captureExamples(true);
const capturesBuild = captureExamples(false);
afterAll(async () => {
// Report experimental capture comparison.
const dev = await capturesDev;
const build = await capturesBuild;
2021-01-28 19:09:27 -05:00
const buildSize = fs.statSync('./build/matter.min.js').size;
const devSize = fs.statSync('./build/matter.dev.min.js').size;
2021-01-28 19:09:27 -05:00
console.log(
'Examples ran against previous release and current build\n\n'
2021-01-31 12:34:38 -05:00
+ logReport(build, `release`) + '\n'
+ logReport(dev, `current`) + '\n'
2024-03-03 16:01:45 -05:00
+ comparisonReport(dev, build, devSize, buildSize, MatterBuild.version, saveComparison, benchmark)
2021-01-28 19:09:27 -05:00
);
});
2019-09-30 18:28:17 -04:00
describe(`Integration checks (${examples.length})`, () => {
test(`Examples run without throwing`, async () => {
const dev = await capturesDev;
const build = await capturesBuild;
expect(Object.keys(dev)).toEqual(examples);
expect(Object.keys(build)).toEqual(examples);
2019-09-30 18:28:17 -04:00
});
});
// Experimental regression comparison checks.
if (testComparison) {
describe(`Regression checks (${examples.length})`, () => {
2019-09-30 18:28:17 -04:00
expect.extend(toMatchExtrinsics);
expect.extend(toMatchIntrinsics);
test(`Examples match intrinsic properties with release build`, async () => {
const dev = await capturesDev;
const build = await capturesBuild;
// compare mass, inertia, friction etc.
expect(dev).toMatchIntrinsics(build);
2019-09-30 18:28:17 -04:00
});
test(`Examples match extrinsic positions and velocities with release build`, async () => {
const dev = await capturesDev;
const build = await capturesBuild;
// compare position, linear and angular velocity
expect(dev).toMatchExtrinsics(build);
2019-09-30 18:28:17 -04:00
});
});
}