Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { createDefaultPreset } = require("ts-jest");

const tsJestTransformCfg = createDefaultPreset().transform;

/** @type {import("jest").Config} **/
module.exports = {
testEnvironment: "node",
transform: {
...tsJestTransformCfg,
},
};
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"scripts": {
"postinstall": "electron-builder install-app-deps",
"lint": "eslint src",
"test": "jest",
"dev": "electron-webpack dev",
"compile": "electron-webpack",
"dist": "yarn compile && electron-builder --win --x64 --ia32",
Expand Down Expand Up @@ -70,6 +71,7 @@
"@types/cross-spawn": "^6.0.2",
"@types/deep-equal": "^1.0.1",
"@types/i18next": "^13.0.0",
"@types/jest": "29",
"@types/js-yaml": "^3.12.5",
"@types/node": "12",
"@types/node-fetch": "2",
Expand All @@ -91,8 +93,10 @@
"eslint-config-prettier": "^7.1.0",
"eslint-plugin-prettier": "^3.3.0",
"eslint-plugin-react": "^7.21.5",
"jest": "29",
"prettier": "^2.2.1",
"prettier-eslint": "^12.0.0",
"ts-jest": "29",
"typescript": "4.6.x",
"webpack": "~4.42.1",
"webpack-dev-server": "^3.11.0"
Expand Down
7 changes: 4 additions & 3 deletions src/common/ColliderMap.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { MapType, Vector2 } from './AmongusMap';
import intersect from 'path-intersection';
import intersect_module = require('path-intersection');
const intersect: any = (intersect_module as any).default || intersect_module;

export const colliderMaps: { [key in MapType]: string[] | undefined } = {
[MapType.THE_SKELD]: [
'M 33.65 35.32 V 37.57 H 25.3 V 36.35 H 21.71 L 20.41 37.46 V 41.75 H 22.2 V 44.05 H 20.97 V 42.62 H 19.4 V 40.96 H 18.27 L 16.43 42.12 V 48.04 L 18.25 49.14 H 19.4 V 47.49 H 20.97 V 46.18 H 22.2 V 48.66 H 20.36 V 53.04 L 21.69 54.06 H 25.3 V 52.41 H 27.05 V 55.28 H 34.79 L 36.95 57.58 H 41.08 V 52.97 H 44.18 V 53.54 H 41.39 V 56.52 L 42.47 57.54 H 45.52 L 46.63 56.52 V 53.54 H 46.02 V 52.97 H 46.94 V 55.02 H 49.48 L 51.75 52.82 V 49.58 H 50.39 V 47.14 H 52.75 V 45.44 H 55.46 V 46.79 H 57.45 L 59.18 45.39 V 43.45 L 57.47 41.96 H 55.46 V 43.31 H 52.75 V 41.96 H 50.39 V 41.01 H 51.75 V 37.76 L 49.51 35.61 H 46.94 V 37.57 H 45.06 V 36.2 L 42.04 33.26 H 35.75 Z',
'M 48.07 41.01 H 48.6 V 41.96 H 47.86 V 41.57 H 45.54 V 41.77 L 43.97 43.61 V 45.23 H 47.86 V 44.09 H 50.96 V 45.01 H 48.6 V 49.58 H 48.03 L 46.95 50.84 H 41.08 V 48.23 H 40.21 V 47.88 H 41.87 V 50.45 H 46.13 L 47.07 49.51 V 45.75 H 40.21 V 44.62 H 42.46 L 45.06 42.03 V 39.74 H 46.94 V 39.84 Z',
'M 29.98 39.79 V 40.22 H 28.75 V 44.85 L 29.61 45.97 H 34.96 V 44.53 L 33.21 42.82 V 40.22 H 31.77 V 39.79 H 33.65 V 42.07 L 36.21 44.62 H 38.37 V 48.23 H 36.38 L 34.79 49.8 V 53.1 H 31.33 V 52.54 H 32.61 L 33.69 51.52 V 49.45 L 34.96 48.25 V 46.84 H 29.63 V 53.1 H 28.8 V 50.27 H 25.3 V 48.66 H 23.99 V 46.18 H 25.39 V 47.45 H 28.23 V 42.73 L 27.47 41.96 H 26.37 L 25.39 42.92 V 44.05 H 23.99 V 41.75 H 25.3 V 39.79 Z'
'M 29.98 39.79 V 40.22 H 28.75 V 44.85 L 29.61 45.97 H 34.96 V 44.53 L 33.21 42.82 V 40.22 H 31.77 V 39.79 H 33.65 V 42.07 L 36.21 44.62 H 38.37 V 48.23 H 36.38 L 34.79 49.8 V 53.1 H 31.33 V 52.54 H 32.61 L 33.69 51.52 V 49.45 L 34.96 48.25 V 46.84 H 29.63 V 53.1 H 28.8 V 50.27 H 25.3 V 48.66 H 23.99 V 46.18 H 25.39 V 47.45 H 28.23 V 42.73 L 27.47 41.96 H 26.37 L 25.39 42.92 V 44.05 H 23.99 V 41.75 H 25.3 V 39.79 Z',
],
[MapType.MIRA_HQ]: [
'M 39.79 31.723 H 28.373 V 40.223 H 33.731 V 42.698 H 43.717 V 42.213 H 53.633 V 40.463 V 42.213 H 57.119 V 37.556 H 53.594 V 36.64 V 38.64 V 37.556 H 57.119 V 33.806 H 53.584 V 34.733 V 33.757 L 57.805 29.561 L 62 33.802 H 61.098 V 38.437 H 60.118 H 61.118 V 33.802 H 57.868 V 40.74 H 60.783 H 57.868 V 43.907 H 60.366 V 42.74 H 69.283 V 40.574 H 66.783 H 69.283 V 33.74 H 64.449 V 31.49 H 62.949 L 58.989 27.53 V 21.66 V 23.235 H 62.822 V 18.142 H 58.405 H 59.072 V 19.975 V 18.142 H 62.905 V 14.058 H 52.822 V 18.219 H 57.17 H 56.586 V 21.552 V 18.213 H 52.83 H 52.747 V 23.297 H 56.664 V 27.63 L 52.614 31.513 H 51.197 V 34.68 V 33.753 H 48.27 V 37.086 H 47.27 V 26.678 V 30.345 H 52.227 V 24.54 H 44.879 V 28.54 V 23.209 H 39.977 V 30.499 H 44.942 V 37.083 H 43.713 V 39.916 H 51.188 V 36.621 V 39.955 H 43.713 V 40.332 H 37.386 V 40.082 H 39.803 Z M 63.203 40.64 H 64.369',
Expand Down Expand Up @@ -64,7 +65,7 @@ export const colliderMaps: { [key in MapType]: string[] | undefined } = {
'M 63.05 28.06 H 59.68 L 58.52 27.31 H 58.85 L 60.02 27.43 H 60.91 V 26.97 H 61.68 V 27.72 H 63.03 Z',
'M 58.56 25.95 V 25.22 H 58.43 V 25.94 Z',
'M 41.57 32.55 H 38.45 V 34.64 H 38.23 V 31.61 H 41.53 Z',
'M 40.42 34.3 H 43.96 V 37.64 H 38.72 L 38.22 36.5 H 43.02 V 34.73 H 40.52 Z'
'M 40.42 34.3 H 43.96 V 37.64 H 38.72 L 38.22 36.5 H 43.02 V 34.73 H 40.52 Z',
],
[MapType.THE_SKELD_APRIL]: undefined,
[MapType.UNKNOWN]: undefined,
Expand Down
101 changes: 101 additions & 0 deletions tests/ColliderMap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { poseCollide } from '../src/common/ColliderMap';
import { MapType } from '../src/common/AmongusMap';

jest.mock('path-intersection', () => {
return jest.fn((path1, path2) => {
// We will mock path-intersection behavior for testing logic
// This avoids issues with import/export defaults and isolates our tests

// Mock specific intersections for tests
// For general "no intersection" tests
if (path1.includes('THE_SKELD') || path1 === 'no-intersect' || path2.includes('-2 -2')) {
return [];
}

// Return a fake intersection for specific paths
// 'M 33.65 35.32' logic check
if (path2.includes('M 33 36') || path2.includes('L 35 36')) {
return [{ x: 33.65, y: 36 }];
}

// Doors mock
if (path1 === 'M 45.059 37.568 V 39.744 ' || path1 === 'M 45.059 37.568 V 39.744') {
if (path2.includes('44 38') && path2.includes('46 38')) {
return [{ x: 45.059, y: 38 }];
}
}
return [];
});
});

// We want to test path-intersection using mocked/verified values,
// since the method relies heavily on intersections.
// We also want to verify MapType.THE_SKELD_APRIL handling, UNKNOWN, doors and basic paths.

describe('poseCollide', () => {
it('should return false for MapType.UNKNOWN', () => {
const result = poseCollide({ x: 0, y: 0 }, { x: 10, y: 10 }, MapType.UNKNOWN, []);
expect(result).toBe(false);
});

it('should return false if there are no intersections on the skeld map without doors', () => {
// Just a general sanity check on coordinates where we know there's no collision
const result = poseCollide({ x: 1, y: 1 }, { x: 2, y: 2 }, MapType.THE_SKELD, []);
expect(result).toBe(false);
});

it('should correctly handle MapType.THE_SKELD_APRIL', () => {
// THE_SKELD_APRIL flips the x coordinates and acts as THE_SKELD.
// We can test this by checking that if we pass coordinates, they are multiplied by -1.
const p1 = { x: -1, y: 1 };
const p2 = { x: -2, y: 2 };

poseCollide(p1, p2, MapType.THE_SKELD_APRIL, []);

// The original coordinates are modified in-place
expect(p1.x).toBe(1);
expect(p2.x).toBe(2);
});

it('should return true if intersecting a path', () => {
// Based on the Skeld collider: 'M 33.65 35.32 V 37.57...'
// If we intersect it, path-intersection will return an array with items.
// We'll mock the `path-intersection` since it's an external library
// or pass coords that are known to intersect it based on logic.
// For coordinate conversion: intersect uses M {x+40} {40-y}.
// If we want {x+40}=33.65 => x = -6.35
// If we want {40-y}=36 => y = 4
const p1 = { x: -7, y: 4 };
const p2 = { x: -5, y: 4 };

// This horizontal line should cross the vertical line 'V 37.57' around x = 33.65 (which is -6.35)
const result = poseCollide(p1, p2, MapType.THE_SKELD, []);
expect(result).toBe(true);
});

it('should return false if doors are open (not provided in closedDoors)', () => {
// Test cafetaria -> weapons door: 'M 45.059 37.568 V 39.744'
// x = 5.059, y = 40 - 38 = 2
const p1 = { x: 4, y: 2 };
const p2 = { x: 6, y: 2 };

const result = poseCollide(p1, p2, MapType.THE_SKELD, []);
expect(result).toBe(false);
});

it('should return true if doors are closed and intersected', () => {
// Test cafetaria -> weapons door: doorId = 0
// 'M 45.059 37.568 V 39.744 '
const p1 = { x: 4, y: 2 }; // {x:44, y:38}
const p2 = { x: 6, y: 2 }; // {x:46, y:38}

const result = poseCollide(p1, p2, MapType.THE_SKELD, [0]);
expect(result).toBe(true);
});

it('should gracefully return false if map does not exist in doorMaps or colliderMaps', () => {
// If a map like MapType.SUBMERGED is currently undefined in colliderMaps, we can test it
const result = poseCollide({ x: 0, y: 0 }, { x: 10, y: 10 }, MapType.SUBMERGED, []);
expect(result).toBe(false);
});
});
Loading