Skip to content

Commit 8713d7c

Browse files
committed
Refactor tests to use updated geometry structure and enhance randomization tests
- Updated import paths in Polygon, Rectangle, and Vector tests to reflect new geometry structure. - Added comprehensive tests for random number generation, including integers, decimals, booleans, and UUIDs. - Removed outdated Maths utility tests as they are no longer relevant.
1 parent 2bf2681 commit 8713d7c

File tree

13 files changed

+1339
-17
lines changed

13 files changed

+1339
-17
lines changed

src/database.schema.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
/* eslint-disable @typescript-eslint/no-unused-vars */import { LocalDatabaseSchema } from "core/database/DatabaseSchema";declare module "core/database/DatabaseSchema" { interface LocalDatabaseSchema {}}
1+
/* eslint-disable @typescript-eslint/no-unused-vars */
2+
3+
import { LocalDatabaseSchema } from "@/core/database/DatabaseSchema";
4+
5+
declare module "@/core/database/DatabaseSchema" {
6+
interface LocalDatabaseSchema {}
7+
}

src/game.events.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
/* eslint-disable @typescript-eslint/no-unused-vars */import { GameEvents } from "core/events/GameEvents";declare module "core/events/GameEvents" { interface GameEvents {}}
1+
/* eslint-disable @typescript-eslint/no-unused-vars */
2+
3+
import { GameEvents } from "@/core/events/GameEvents";
4+
5+
declare module "@/core/events/GameEvents" {
6+
interface GameEvents {}
7+
}

test/core/ecs/Query.spec.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Component } from "../../../src/core/ecs/Component";
33
import { World } from "../../../src/core/ecs/World";
44
import { Query } from "../../../src/core/ecs/Query";
55
import { ServiceRegistry } from "../../../src/core/service/ServiceRegistry";
6+
import { EventSystem } from "../../../src/core/events/EventSystem";
67

78
suite("Query Test Suite", () => {
89
class PointComponent extends Component<{
@@ -16,22 +17,25 @@ suite("Query Test Suite", () => {
1617
}> {}
1718

1819
const world = ServiceRegistry.get<World>(World.name);
20+
const eventSystem = ServiceRegistry.get<EventSystem>(EventSystem.name);
1921
world.registerComponent(PointComponent);
2022
world.registerComponent(MoveComponent);
2123

22-
const query = new Query({
23-
allowlist: ["PointComponent"],
24-
blocklist: ["MoveComponent"]
25-
});
26-
2724
test("Query updates when entity changes", () => {
25+
const query = new Query({
26+
allowlist: ["PointComponent"],
27+
blocklist: ["MoveComponent"]
28+
});
29+
2830
const entity = world.createEntity("1337");
2931
entity.addComponent(PointComponent, { x: 10, y: 20 });
3032

3133
const other = world.createEntity("4711");
3234
other.addComponent(PointComponent, { x: 10, y: 20 });
3335
other.addComponent(MoveComponent, { dx: 1, dy: 2 });
3436

37+
eventSystem.processQueue();
38+
3539
const results = query.getResult();
3640
expect(results[0].getID()).toEqual("1337");
3741
});
@@ -49,6 +53,8 @@ suite("Query Test Suite", () => {
4953
blocklist: ["MoveComponent"]
5054
});
5155

56+
eventSystem.processQueue();
57+
5258
const results = otherQuery.getResult();
5359
expect(results[0].getID()).toEqual("1337");
5460
expect(results[1].getID()).toEqual("7331");

test/core/events/EventSystem.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ suite("EventSystem Test Suite", () => {
1616
eventSystem.subscribe("bundleLoaded", handler);
1717

1818
const subscribers = eventSystem.getSubscribers("bundleLoaded");
19-
expect(subscribers[0]).toBe(handler);
19+
expect(subscribers[0].handler).toBe(handler);
2020
expect(subscribers).toHaveLength(1);
21+
22+
// Cleanup: unsubscribe to not affect other tests
23+
eventSystem.unsubscribe("bundleLoaded", handler);
2124
});
2225

2326
test("Unsubscribe event handler from an event", () => {
@@ -28,7 +31,7 @@ suite("EventSystem Test Suite", () => {
2831

2932
let subscribers = eventSystem.getSubscribers("bundleLoaded");
3033
expect(subscribers).toHaveLength(0);
31-
eventSystem.unsubscribe("bundleLoaded", handler);
34+
3235

3336
eventSystem.subscribe("bundleLoaded", handler);
3437
eventSystem.unsubscribe("bundleLoaded", handler);

test/core/math/Circle.spec.ts

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,96 @@
1-
import { test, expect } from "vitest";import { Vector } from "../../../src/core/math/Vector";import { Circle } from "../../../src/core/math/Circle";import { Rectangle } from "../../../src/core/math/Rectangle";import { Polygon } from "../../../src/core/math/Polygon";test("Create circle from three points", () => { const start = new Vector(0, 6); const center = new Vector(6, 0); const end = new Vector(2, 2); const result = Circle.ofPoints(start, center, end); const position = result.getPosition(); const radius = result.getRadius(); expect(position.x).toBe(7); expect(position.y).toBe(7); expect(position.z).toBe(0); expect(radius).approximately(7.071, 0.1);});test("Circle intersects with circle", () => { const value = new Circle(6, 6, 6); const positiveValue = new Circle(0, 0, 3); const negativeValue = new Circle(100, 100, 6); let result = value.intersects(positiveValue); expect(result).toBeTruthy(); result = value.intersects(negativeValue); expect(result).toBeFalsy();});test("Circle intersects with rectangle", () => { const value = new Circle(0, 0, 4); const positiveValue = new Rectangle(-2, -2, 10, 5); const negativeValue = new Rectangle(100, 100, 10, 5); let result = value.intersects(positiveValue); expect(result).toBeTruthy(); result = value.intersects(negativeValue); expect(result).toBeFalsy();});test("Circle intersects with polygon", () => { const value = new Circle(0, 0, 4); const positiveValue = new Polygon([new Vector(1, -2), new Vector(1, 2), new Vector(0, 8)]); const negativeValue = new Polygon([new Vector(5, 5), new Vector(10, 10), new Vector(5, 15)]); let result = value.intersects(positiveValue); expect(result).toBeTruthy(); result = value.intersects(negativeValue); expect(result).toBeFalsy();});test("Circle intersects with null", () => { const value = new Circle(0, 0, 4); const errorValue = null; const result = value.intersects(errorValue); expect(result).toBeFalsy();});test("Get border point of circle", () => { const value = new Circle(6, 6, 6); const result = value.getBorderPoint(90); expect(result.x).approximately(6, 0.1); expect(result.y).toBe(0); expect(result.z).toBe(0);});test("Get area of circle", () => { const value = new Circle(0, 0, 6); const result = value.getArea(); expect(result).approximately(113.0973, 0.1);});test("Get perimeter of circle", () => { const value = new Circle(0, 0, 6); const result = value.getPerimeter(); expect(result).approximately(37.6991, 0.1);});test("Get diameter of circle", () => { const value = new Circle(0, 0, 6); const result = value.getDiameter(); expect(result).toBe(12);});
1+
import { test, expect } from "vitest";
2+
import { Vector } from "../../../src/core/math/geometry/Vector";
3+
import { Circle } from "../../../src/core/math/geometry/Circle";
4+
import { Rectangle } from "../../../src/core/math/geometry/Rectangle";
5+
import { Polygon } from "../../../src/core/math/geometry/Polygon";
6+
7+
test("Create circle from three points", () => {
8+
const start = new Vector(0, 6);
9+
const center = new Vector(6, 0);
10+
const end = new Vector(2, 2);
11+
12+
const result = Circle.ofPoints(start, center, end);
13+
const position = result.getPosition();
14+
const radius = result.getRadius();
15+
16+
expect(position.x).toBe(7);
17+
expect(position.y).toBe(7);
18+
expect(position.z).toBe(0);
19+
expect(radius).approximately(7.071, 0.1);
20+
});
21+
22+
test("Circle intersects with circle", () => {
23+
const value = new Circle(6, 6, 6);
24+
const positiveValue = new Circle(0, 0, 3);
25+
const negativeValue = new Circle(100, 100, 6);
26+
27+
let result = value.intersects(positiveValue);
28+
expect(result).toBeTruthy();
29+
30+
result = value.intersects(negativeValue);
31+
expect(result).toBeFalsy();
32+
});
33+
34+
test("Circle intersects with rectangle", () => {
35+
const value = new Circle(0, 0, 4);
36+
const positiveValue = new Rectangle(-2, -2, 10, 5);
37+
const negativeValue = new Rectangle(100, 100, 10, 5);
38+
39+
let result = value.intersects(positiveValue);
40+
expect(result).toBeTruthy();
41+
42+
result = value.intersects(negativeValue);
43+
expect(result).toBeFalsy();
44+
});
45+
46+
test("Circle intersects with polygon", () => {
47+
const value = new Circle(0, 0, 4);
48+
49+
const positiveValue = new Polygon([new Vector(1, -2), new Vector(1, 2), new Vector(0, 8)]);
50+
51+
const negativeValue = new Polygon([new Vector(5, 5), new Vector(10, 10), new Vector(5, 15)]);
52+
53+
let result = value.intersects(positiveValue);
54+
expect(result).toBeTruthy();
55+
56+
result = value.intersects(negativeValue);
57+
expect(result).toBeFalsy();
58+
});
59+
60+
test("Circle intersects with null", () => {
61+
const value = new Circle(0, 0, 4);
62+
const errorValue = null;
63+
64+
const result = value.intersects(errorValue);
65+
expect(result).toBeFalsy();
66+
});
67+
68+
test("Get border point of circle", () => {
69+
const value = new Circle(6, 6, 6);
70+
const result = value.getBorderPoint(90);
71+
72+
expect(result.x).approximately(6, 0.1);
73+
expect(result.y).toBe(0);
74+
expect(result.z).toBe(0);
75+
});
76+
77+
test("Get area of circle", () => {
78+
const value = new Circle(0, 0, 6);
79+
const result = value.getArea();
80+
81+
expect(result).approximately(113.0973, 0.1);
82+
});
83+
84+
test("Get perimeter of circle", () => {
85+
const value = new Circle(0, 0, 6);
86+
const result = value.getPerimeter();
87+
88+
expect(result).approximately(37.6991, 0.1);
89+
});
90+
91+
test("Get diameter of circle", () => {
92+
const value = new Circle(0, 0, 6);
93+
const result = value.getDiameter();
94+
95+
expect(result).toBe(12);
96+
});

test/core/math/Line.spec.ts

Lines changed: 145 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,145 @@
1-
import { test, expect } from "vitest";import { Line } from "../../../src/core/math/Line";import { Vector } from "../../../src/core/math/Vector";import { Circle } from "../../../src/core/math/Circle";import { Rectangle } from "../../../src/core/math/Rectangle";import { Polygon } from "../../../src/core/math/Polygon";test("Create line from two points", () => { const start = new Vector(0, 0); const end = new Vector(6, 0); const result = Line.ofPoints(start, end); const lineStart = result.getStart(); expect(lineStart.x).toBe(0); expect(lineStart.y).toBe(0); expect(lineStart.z).toBe(0); const lineEnd = result.getEnd(); expect(lineEnd.x).toBe(6); expect(lineEnd.y).toBe(0); expect(lineEnd.z).toBe(0);});test("Line contains point", () => { const value = new Line(0, 0, 6, 0); const positiveValue = new Vector(3, 0); const negativeValue = new Vector(9, 0); let result = value.contains(positiveValue); expect(result).toBeTruthy(); result = value.contains(negativeValue); expect(result).toBeFalsy();});test("Line intersects with line", () => { const value = new Line(0, 0, 6, 0); const positiveValue = new Line(3, 3, 3, -3); const negativeValue = new Line(0, 3, 3, 3); let result = value.intersects(positiveValue); expect(result).toBeTruthy(); result = value.intersects(negativeValue); expect(result).toBeFalsy();});test("Line intersects with circle", () => { const value = new Line(0, 0, 6, 0); const positiveValue = new Circle(0, 3, 3); const negativeValue = new Circle(5, 5, 2); let result = value.intersects(positiveValue); expect(result).toBeTruthy(); result = value.intersects(negativeValue); expect(result).toBeFalsy();});test("Line intersects with rectangle", () => { const value = new Line(0, 0, 6, 0); const positiveValue = new Rectangle(1, -2, 3, 4); const negativeValue = new Rectangle(100, 100, 1, 1); let result = value.intersects(positiveValue); expect(result).toBeTruthy(); result = value.intersects(negativeValue); expect(result).toBeFalsy();});test("Line intersects with polygon", () => { const value = new Line(0, 0, 6, 0); const positiveValue = new Polygon([new Vector(1, -2), new Vector(1, 2), new Vector(0, 8)]); const negativeValue = new Polygon([new Vector(5, 5), new Vector(10, 10), new Vector(5, 15)]); let result = value.intersects(positiveValue); expect(result).toBeTruthy(); result = value.intersects(negativeValue); expect(result).toBeFalsy();});test("Line intersects with null", () => { const value = new Line(0, 0, 6, 0); const errorValue = null; const result = value.intersects(errorValue); expect(result).toBeFalsy();});test("Reflect vector with line", () => { const value = new Line(0, 0, 0, 6); const other = new Vector(-6, 3); const result = value.reflect(other); expect(result.x).toBe(6); expect(result.y).toBe(3); expect(result.z).toBe(0);});test("Get vertical bisector of line", () => { const value = new Line(0, 0, 0, 6); const result = value.getVerticalBisector(); expect(result.A).toBe(-0); expect(result.B).toBe(6); expect(result.C).toBe(18);});test("Two lines are parallel", () => { const value = new Line(0, 0, 6, 0); const positiveValue = new Line(0, 6, 6, 6); const negativeValue = new Line(3, -3, 3, 3); let result = value.isParallel(positiveValue); expect(result).toBeTruthy(); result = value.isParallel(negativeValue); expect(result).toBeFalsy();});test("Get angle of line", () => { const value = new Line(0, 0, 1, 1); const result = value.getAngle(); expect(result).toBe(45);});test("Get slope of line", () => { const value = new Line(0, 0, 1, 1); const result = value.getSlope(); expect(result).toBe(1);});test("Get center of line", () => { const value = new Line(0, 0, 6, 0); const result = value.getCenter(); expect(result.x).toBe(3); expect(result.y).toBe(0); expect(result.z).toBe(0);});
1+
import { test, expect } from "vitest";
2+
import { Line } from "../../../src/core/math/geometry/Line";
3+
import { Vector } from "../../../src/core/math/geometry/Vector";
4+
import { Circle } from "../../../src/core/math/geometry/Circle";
5+
import { Rectangle } from "../../../src/core/math/geometry/Rectangle";
6+
import { Polygon } from "../../../src/core/math/geometry/Polygon";
7+
8+
test("Create line from two points", () => {
9+
const start = new Vector(0, 0);
10+
const end = new Vector(6, 0);
11+
12+
const result = Line.ofPoints(start, end);
13+
14+
const lineStart = result.getStart();
15+
expect(lineStart.x).toBe(0);
16+
expect(lineStart.y).toBe(0);
17+
expect(lineStart.z).toBe(0);
18+
19+
const lineEnd = result.getEnd();
20+
expect(lineEnd.x).toBe(6);
21+
expect(lineEnd.y).toBe(0);
22+
expect(lineEnd.z).toBe(0);
23+
});
24+
25+
test("Line contains point", () => {
26+
const value = new Line(0, 0, 6, 0);
27+
const positiveValue = new Vector(3, 0);
28+
const negativeValue = new Vector(9, 0);
29+
30+
let result = value.contains(positiveValue);
31+
expect(result).toBeTruthy();
32+
33+
result = value.contains(negativeValue);
34+
expect(result).toBeFalsy();
35+
});
36+
37+
test("Line intersects with line", () => {
38+
const value = new Line(0, 0, 6, 0);
39+
const positiveValue = new Line(3, 3, 3, -3);
40+
const negativeValue = new Line(0, 3, 3, 3);
41+
42+
let result = value.intersects(positiveValue);
43+
expect(result).toBeTruthy();
44+
45+
result = value.intersects(negativeValue);
46+
expect(result).toBeFalsy();
47+
});
48+
49+
test("Line intersects with circle", () => {
50+
const value = new Line(0, 0, 6, 0);
51+
const positiveValue = new Circle(0, 3, 3);
52+
const negativeValue = new Circle(5, 5, 2);
53+
54+
let result = value.intersects(positiveValue);
55+
expect(result).toBeTruthy();
56+
57+
result = value.intersects(negativeValue);
58+
expect(result).toBeFalsy();
59+
});
60+
61+
test("Line intersects with rectangle", () => {
62+
const value = new Line(0, 0, 6, 0);
63+
const positiveValue = new Rectangle(1, -2, 3, 4);
64+
const negativeValue = new Rectangle(100, 100, 1, 1);
65+
66+
let result = value.intersects(positiveValue);
67+
expect(result).toBeTruthy();
68+
69+
result = value.intersects(negativeValue);
70+
expect(result).toBeFalsy();
71+
});
72+
73+
test("Line intersects with polygon", () => {
74+
const value = new Line(0, 0, 6, 0);
75+
76+
const positiveValue = new Polygon([new Vector(1, -2), new Vector(1, 2), new Vector(0, 8)]);
77+
78+
const negativeValue = new Polygon([new Vector(5, 5), new Vector(10, 10), new Vector(5, 15)]);
79+
80+
let result = value.intersects(positiveValue);
81+
expect(result).toBeTruthy();
82+
83+
result = value.intersects(negativeValue);
84+
expect(result).toBeFalsy();
85+
});
86+
87+
test("Line intersects with null", () => {
88+
const value = new Line(0, 0, 6, 0);
89+
const errorValue = null;
90+
91+
const result = value.intersects(errorValue);
92+
expect(result).toBeFalsy();
93+
});
94+
95+
test("Reflect vector with line", () => {
96+
const value = new Line(0, 0, 0, 6);
97+
const other = new Vector(-6, 3);
98+
99+
const result = value.reflect(other);
100+
expect(result.x).toBe(6);
101+
expect(result.y).toBe(3);
102+
expect(result.z).toBe(0);
103+
});
104+
105+
test("Get vertical bisector of line", () => {
106+
const value = new Line(0, 0, 0, 6);
107+
const result = value.getVerticalBisector();
108+
109+
expect(result.A).toBe(-0);
110+
expect(result.B).toBe(6);
111+
expect(result.C).toBe(18);
112+
});
113+
114+
test("Two lines are parallel", () => {
115+
const value = new Line(0, 0, 6, 0);
116+
const positiveValue = new Line(0, 6, 6, 6);
117+
const negativeValue = new Line(3, -3, 3, 3);
118+
119+
let result = value.isParallel(positiveValue);
120+
expect(result).toBeTruthy();
121+
122+
result = value.isParallel(negativeValue);
123+
expect(result).toBeFalsy();
124+
});
125+
126+
test("Get angle of line", () => {
127+
const value = new Line(0, 0, 1, 1);
128+
const result = value.getAngle();
129+
expect(result).toBe(45);
130+
});
131+
132+
test("Get slope of line", () => {
133+
const value = new Line(0, 0, 1, 1);
134+
const result = value.getSlope();
135+
expect(result).toBe(1);
136+
});
137+
138+
test("Get center of line", () => {
139+
const value = new Line(0, 0, 6, 0);
140+
const result = value.getCenter();
141+
142+
expect(result.x).toBe(3);
143+
expect(result.y).toBe(0);
144+
expect(result.z).toBe(0);
145+
});

0 commit comments

Comments
 (0)