|
1 | 1 | import { test, expect, suite } from "vitest"; |
2 | | -import { Component } from "../../../src/core/ecs/Component"; |
3 | | -import { World } from "../../../src/core/ecs/World"; |
4 | | -import { Query } from "../../../src/core/ecs/Query"; |
5 | | -import { ServiceRegistry } from "../../../src/core/service/ServiceRegistry"; |
6 | | -import { EventSystem } from "../../../src/core/events/EventSystem"; |
| 2 | +import { Component } from "@/core/ecs/Component"; |
| 3 | +import { World } from "@/core/ecs/World"; |
| 4 | +import { Query } from "@/core/ecs/Query"; |
| 5 | +import { ServiceRegistry } from "@/core/service/ServiceRegistry"; |
| 6 | +import { EventSystem } from "@/core/events/EventSystem"; |
7 | 7 |
|
8 | 8 | suite("Query Test Suite", () => { |
9 | 9 | class PointComponent extends Component<{ |
@@ -59,4 +59,101 @@ suite("Query Test Suite", () => { |
59 | 59 | expect(results[0].getID()).toEqual("1337"); |
60 | 60 | expect(results[1].getID()).toEqual("7331"); |
61 | 61 | }); |
| 62 | + |
| 63 | + test("Query removes entity when component is added that is in blocklist", () => { |
| 64 | + const query = new Query({ |
| 65 | + allowlist: ["PointComponent"], |
| 66 | + blocklist: ["MoveComponent"] |
| 67 | + }); |
| 68 | + |
| 69 | + const entity = world.createEntity("remove-test-1"); |
| 70 | + entity.addComponent(PointComponent, { x: 5, y: 5 }); |
| 71 | + eventSystem.processQueue(); |
| 72 | + |
| 73 | + let results = query.getResult(); |
| 74 | + expect(results.some(e => e.getID() === "remove-test-1")).toBeTruthy(); |
| 75 | + |
| 76 | + // Add a component from the blocklist - entity should be removed from query |
| 77 | + entity.addComponent(MoveComponent, { dx: 1, dy: 1 }); |
| 78 | + eventSystem.processQueue(); |
| 79 | + |
| 80 | + results = query.getResult(); |
| 81 | + expect(results.some(e => e.getID() === "remove-test-1")).toBeFalsy(); |
| 82 | + |
| 83 | + query.dispose(); |
| 84 | + }); |
| 85 | + |
| 86 | + test("Query removes entity when entity is removed from world", () => { |
| 87 | + const query = new Query({ |
| 88 | + allowlist: ["PointComponent"] |
| 89 | + }); |
| 90 | + |
| 91 | + const entity = world.createEntity("remove-test-2"); |
| 92 | + entity.addComponent(PointComponent, { x: 3, y: 3 }); |
| 93 | + eventSystem.processQueue(); |
| 94 | + |
| 95 | + let results = query.getResult(); |
| 96 | + expect(results.some(e => e.getID() === "remove-test-2")).toBeTruthy(); |
| 97 | + |
| 98 | + // Remove entity from world - should trigger onEntityRemoved |
| 99 | + world.unregisterEntity(entity); |
| 100 | + eventSystem.processQueue(); |
| 101 | + |
| 102 | + results = query.getResult(); |
| 103 | + expect(results.some(e => e.getID() === "remove-test-2")).toBeFalsy(); |
| 104 | + |
| 105 | + query.dispose(); |
| 106 | + }); |
| 107 | + |
| 108 | + test("Query getSingleResult returns first entity or null", () => { |
| 109 | + const query = new Query({ |
| 110 | + allowlist: ["PointComponent"], |
| 111 | + blocklist: ["MoveComponent"] |
| 112 | + }); |
| 113 | + |
| 114 | + eventSystem.processQueue(); |
| 115 | + |
| 116 | + // Should return first entity |
| 117 | + const singleResult = query.getSingleResult(); |
| 118 | + expect(singleResult).not.toBeNull(); |
| 119 | + expect(singleResult?.getID()).toEqual("1337"); |
| 120 | + |
| 121 | + // Test with empty query |
| 122 | + const emptyQuery = new Query({ |
| 123 | + allowlist: ["NonExistentComponent"] |
| 124 | + }); |
| 125 | + |
| 126 | + const noResult = emptyQuery.getSingleResult(); |
| 127 | + expect(noResult).toBeNull(); |
| 128 | + |
| 129 | + query.dispose(); |
| 130 | + emptyQuery.dispose(); |
| 131 | + }); |
| 132 | + |
| 133 | + test("Query onEntityRemoved when entity is not in query (else branch)", () => { |
| 134 | + // Create a query that only matches PointComponent |
| 135 | + const query = new Query({ |
| 136 | + allowlist: ["PointComponent"] |
| 137 | + }); |
| 138 | + |
| 139 | + // Create an entity that DOESN'T match the query |
| 140 | + const entity = world.createEntity("non-matching-entity"); |
| 141 | + entity.addComponent(MoveComponent, { dx: 5, dy: 5 }); |
| 142 | + eventSystem.processQueue(); |
| 143 | + |
| 144 | + // Verify entity is NOT in the query |
| 145 | + let results = query.getResult(); |
| 146 | + expect(results.some(e => e.getID() === "non-matching-entity")).toBeFalsy(); |
| 147 | + |
| 148 | + // Now remove the entity - this triggers onEntityRemoved |
| 149 | + // Since entity is not in query, exists will be false |
| 150 | + world.unregisterEntity(entity); |
| 151 | + eventSystem.processQueue(); |
| 152 | + |
| 153 | + // Query should still not contain it (nothing should change) |
| 154 | + results = query.getResult(); |
| 155 | + expect(results.some(e => e.getID() === "non-matching-entity")).toBeFalsy(); |
| 156 | + |
| 157 | + query.dispose(); |
| 158 | + }); |
62 | 159 | }); |
0 commit comments