Add more tests

This commit is contained in:
Peter Stockings
2026-01-06 10:01:59 +11:00
parent cb1dfea33b
commit a2a1d0cc58
5 changed files with 331 additions and 10 deletions

View File

@@ -90,4 +90,43 @@ describe('EntityManager', () => {
expect(entityManager.getActorsAt(1, 1).map(a => a.id)).toEqual([2]);
});
it('should handle removing non-existent actor gracefully', () => {
// Should not throw
entityManager.removeActor(999);
});
it('should handle moving non-existent actor gracefully', () => {
// Should not throw
entityManager.moveActor(999, { x: 0, y: 0 }, { x: 1, y: 1 });
});
it('should handle moving an actor that is not in the grid at expected position (inconsistent state)', () => {
const actor: Actor = { id: 1, pos: { x: 0, y: 0 } } as any;
// Add to actors map but NOT to grid (simulating desync)
mockWorld.actors.set(1, actor);
// Attempt move
entityManager.moveActor(1, { x: 0, y: 0 }, { x: 1, y: 1 });
expect(actor.pos.x).toBe(1);
expect(actor.pos.y).toBe(1);
// Should be added to new position in grid
expect(entityManager.getActorsAt(1, 1).map(a => a.id)).toContain(1);
});
it('should handle moving an actor that is in grid but ID not found in list (very rare edge case)', () => {
// Manually pollute grid with empty array for old pos
// This forces `ids` to exist but `indexOf` to return -1
const idx = 0; // 0,0
// @ts-ignore
entityManager.grid.set(idx, [999]); // occupied by someone else
const actor: Actor = { id: 1, pos: { x: 0, y:0 } } as any;
mockWorld.actors.set(1, actor);
entityManager.moveActor(1, { x: 0, y: 0 }, { x: 1, y: 1 });
expect(actor.pos).toEqual({ x: 1, y: 1 });
});
});