|
| 1 | +import { describe, expect, it } from "bun:test"; |
| 2 | +import { component, relation } from "./entity"; |
| 3 | +import { World } from "./world"; |
| 4 | + |
| 5 | +describe("DontFragment Query Notification Issue", () => { |
| 6 | + it("should notify queries when new archetypes with dontFragment relations are created", () => { |
| 7 | + const world = new World(); |
| 8 | + |
| 9 | + const PositionId = component(); |
| 10 | + const ChildOf = component({ dontFragment: true }); |
| 11 | + |
| 12 | + // Create a query BEFORE any entities with ChildOf relations exist |
| 13 | + const wildcardChildOf = relation(ChildOf, "*"); |
| 14 | + const query = world.createQuery([wildcardChildOf, PositionId]); |
| 15 | + |
| 16 | + // Initially, no entities match |
| 17 | + expect(query.getEntities().length).toBe(0); |
| 18 | + |
| 19 | + // Now create entities with ChildOf relations |
| 20 | + const parent1 = world.new(); |
| 21 | + const parent2 = world.new(); |
| 22 | + |
| 23 | + const child1 = world.new(); |
| 24 | + world.set(child1, PositionId); |
| 25 | + world.set(child1, relation(ChildOf, parent1)); |
| 26 | + |
| 27 | + const child2 = world.new(); |
| 28 | + world.set(child2, PositionId); |
| 29 | + world.set(child2, relation(ChildOf, parent2)); |
| 30 | + |
| 31 | + world.sync(); |
| 32 | + |
| 33 | + // The query should now find both children |
| 34 | + // This is the key test: the query was created before the archetype existed |
| 35 | + const entities = query.getEntities(); |
| 36 | + expect(entities.length).toBe(2); |
| 37 | + expect(entities).toContain(child1); |
| 38 | + expect(entities).toContain(child2); |
| 39 | + }); |
| 40 | + |
| 41 | + it("should separate archetypes with and without wildcard markers", () => { |
| 42 | + const world = new World(); |
| 43 | + |
| 44 | + const PositionId = component(); |
| 45 | + const VelocityId = component(); |
| 46 | + const ChildOf = component({ dontFragment: true }); |
| 47 | + |
| 48 | + // Create entities without ChildOf relation |
| 49 | + const entity1 = world.new(); |
| 50 | + world.set(entity1, PositionId); |
| 51 | + world.set(entity1, VelocityId); |
| 52 | + |
| 53 | + world.sync(); |
| 54 | + |
| 55 | + // Create a query for entities with ChildOf relation |
| 56 | + const wildcardChildOf = relation(ChildOf, "*"); |
| 57 | + const query = world.createQuery([wildcardChildOf, PositionId]); |
| 58 | + |
| 59 | + // Entity1 should NOT match because it has no ChildOf relation |
| 60 | + expect(query.getEntities()).not.toContain(entity1); |
| 61 | + |
| 62 | + // Create entities with ChildOf relation |
| 63 | + const parent = world.new(); |
| 64 | + const entity2 = world.new(); |
| 65 | + world.set(entity2, PositionId); |
| 66 | + world.set(entity2, VelocityId); |
| 67 | + world.set(entity2, relation(ChildOf, parent)); |
| 68 | + |
| 69 | + world.sync(); |
| 70 | + |
| 71 | + // Now entity2 should match |
| 72 | + const entities = query.getEntities(); |
| 73 | + expect(entities.length).toBe(1); |
| 74 | + expect(entities).toContain(entity2); |
| 75 | + expect(entities).not.toContain(entity1); |
| 76 | + |
| 77 | + // Verify they're in different archetypes |
| 78 | + const archetypes = (world as any).archetypes; |
| 79 | + const archetypesWithPosition = archetypes.filter((arch: any) => { |
| 80 | + return arch.componentTypes.includes(PositionId) && arch.componentTypes.includes(VelocityId); |
| 81 | + }); |
| 82 | + |
| 83 | + // Should have 2 archetypes: one without wildcard marker, one with |
| 84 | + expect(archetypesWithPosition.length).toBe(2); |
| 85 | + |
| 86 | + // One archetype should have the wildcard marker |
| 87 | + const withMarker = archetypesWithPosition.filter((arch: any) => arch.componentTypes.includes(wildcardChildOf)); |
| 88 | + expect(withMarker.length).toBe(1); |
| 89 | + |
| 90 | + // The other should not |
| 91 | + const withoutMarker = archetypesWithPosition.filter((arch: any) => !arch.componentTypes.includes(wildcardChildOf)); |
| 92 | + expect(withoutMarker.length).toBe(1); |
| 93 | + }); |
| 94 | + |
| 95 | + it("should handle adding dontFragment relations to existing entities", () => { |
| 96 | + const world = new World(); |
| 97 | + |
| 98 | + const PositionId = component(); |
| 99 | + const ChildOf = component({ dontFragment: true }); |
| 100 | + |
| 101 | + // Create entity without ChildOf |
| 102 | + const entity = world.new(); |
| 103 | + world.set(entity, PositionId); |
| 104 | + world.sync(); |
| 105 | + |
| 106 | + // Create query for entities with ChildOf |
| 107 | + const wildcardChildOf = relation(ChildOf, "*"); |
| 108 | + const query = world.createQuery([wildcardChildOf, PositionId]); |
| 109 | + |
| 110 | + // Entity should not match initially |
| 111 | + expect(query.getEntities()).not.toContain(entity); |
| 112 | + |
| 113 | + // Add ChildOf relation |
| 114 | + const parent = world.new(); |
| 115 | + world.set(entity, relation(ChildOf, parent)); |
| 116 | + world.sync(); |
| 117 | + |
| 118 | + // Entity should now match |
| 119 | + expect(query.getEntities()).toContain(entity); |
| 120 | + }); |
| 121 | + |
| 122 | + it("should handle removing last dontFragment relation", () => { |
| 123 | + const world = new World(); |
| 124 | + |
| 125 | + const PositionId = component(); |
| 126 | + const ChildOf = component({ dontFragment: true }); |
| 127 | + |
| 128 | + const parent = world.new(); |
| 129 | + const entity = world.new(); |
| 130 | + world.set(entity, PositionId); |
| 131 | + world.set(entity, relation(ChildOf, parent)); |
| 132 | + world.sync(); |
| 133 | + |
| 134 | + // Create query |
| 135 | + const wildcardChildOf = relation(ChildOf, "*"); |
| 136 | + const query = world.createQuery([wildcardChildOf, PositionId]); |
| 137 | + |
| 138 | + // Entity should match |
| 139 | + expect(query.getEntities()).toContain(entity); |
| 140 | + |
| 141 | + // Remove the relation |
| 142 | + world.remove(entity, relation(ChildOf, parent)); |
| 143 | + world.sync(); |
| 144 | + |
| 145 | + // Entity should no longer match |
| 146 | + expect(query.getEntities()).not.toContain(entity); |
| 147 | + |
| 148 | + // Verify entity moved to archetype without wildcard marker |
| 149 | + const archetype = (world as any).entityToArchetype.get(entity); |
| 150 | + expect(archetype.componentTypes).not.toContain(wildcardChildOf); |
| 151 | + }); |
| 152 | + |
| 153 | + it("should handle multiple dontFragment relations on same entity", () => { |
| 154 | + const world = new World(); |
| 155 | + |
| 156 | + const PositionId = component(); |
| 157 | + const ChildOf = component({ dontFragment: true }); |
| 158 | + |
| 159 | + const parent1 = world.new(); |
| 160 | + const parent2 = world.new(); |
| 161 | + const entity = world.new(); |
| 162 | + |
| 163 | + world.set(entity, PositionId); |
| 164 | + world.set(entity, relation(ChildOf, parent1)); |
| 165 | + world.set(entity, relation(ChildOf, parent2)); |
| 166 | + world.sync(); |
| 167 | + |
| 168 | + // Create query |
| 169 | + const wildcardChildOf = relation(ChildOf, "*"); |
| 170 | + const query = world.createQuery([wildcardChildOf, PositionId]); |
| 171 | + |
| 172 | + // Entity should match |
| 173 | + expect(query.getEntities()).toContain(entity); |
| 174 | + |
| 175 | + // Remove one relation |
| 176 | + world.remove(entity, relation(ChildOf, parent1)); |
| 177 | + world.sync(); |
| 178 | + |
| 179 | + // Entity should still match (still has one relation) |
| 180 | + expect(query.getEntities()).toContain(entity); |
| 181 | + |
| 182 | + // Wildcard marker should still be present |
| 183 | + const archetype = (world as any).entityToArchetype.get(entity); |
| 184 | + expect(archetype.componentTypes).toContain(wildcardChildOf); |
| 185 | + |
| 186 | + // Remove the last relation |
| 187 | + world.remove(entity, relation(ChildOf, parent2)); |
| 188 | + world.sync(); |
| 189 | + |
| 190 | + // Entity should no longer match |
| 191 | + expect(query.getEntities()).not.toContain(entity); |
| 192 | + |
| 193 | + // Wildcard marker should be removed |
| 194 | + const newArchetype = (world as any).entityToArchetype.get(entity); |
| 195 | + expect(newArchetype.componentTypes).not.toContain(wildcardChildOf); |
| 196 | + }); |
| 197 | + |
| 198 | + it("should allow false positives but filter correctly during iteration", () => { |
| 199 | + const world = new World(); |
| 200 | + |
| 201 | + const PositionId = component(); |
| 202 | + const TagA = component({ dontFragment: true }); |
| 203 | + const TagB = component({ dontFragment: true }); |
| 204 | + |
| 205 | + // Create entities with different dontFragment relations |
| 206 | + const parent1 = world.new(); |
| 207 | + const parent2 = world.new(); |
| 208 | + |
| 209 | + const entity1 = world.new(); |
| 210 | + world.set(entity1, PositionId); |
| 211 | + world.set(entity1, relation(TagA, parent1)); |
| 212 | + |
| 213 | + const entity2 = world.new(); |
| 214 | + world.set(entity2, PositionId); |
| 215 | + world.set(entity2, relation(TagB, parent2)); |
| 216 | + |
| 217 | + world.sync(); |
| 218 | + |
| 219 | + // Both entities should be in the same archetype (Position + wildcard for TagA/TagB) |
| 220 | + // But queries should still work correctly |
| 221 | + const wildcardTagA = relation(TagA, "*"); |
| 222 | + const queryA = world.createQuery([wildcardTagA, PositionId]); |
| 223 | + |
| 224 | + const wildcardTagB = relation(TagB, "*"); |
| 225 | + const queryB = world.createQuery([wildcardTagB, PositionId]); |
| 226 | + |
| 227 | + // QueryA should only find entity1 |
| 228 | + expect(queryA.getEntities()).toContain(entity1); |
| 229 | + expect(queryA.getEntities()).not.toContain(entity2); |
| 230 | + |
| 231 | + // QueryB should only find entity2 |
| 232 | + expect(queryB.getEntities()).not.toContain(entity1); |
| 233 | + expect(queryB.getEntities()).toContain(entity2); |
| 234 | + }); |
| 235 | +}); |
0 commit comments