world.test.ts 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. import { describe, expect, it } from "vitest";
  2. import { World, type Entity } from "../src/index.js";
  3. describe("World", () => {
  4. it("create/get/query/remove/snapshot roundtrip", () => {
  5. const world = new World();
  6. world.create({ id: "player", type: "player", x: 1, y: 2 });
  7. world.create({ id: "farm", type: "map", width: 12 });
  8. world.create({ id: "plot-1", type: "plot", x: 3, y: 3 });
  9. expect(world.get("player")).toEqual({ id: "player", type: "player", x: 1, y: 2 });
  10. expect(world.get("missing")).toBeUndefined();
  11. const players = world.query({ type: "player" });
  12. expect(players).toHaveLength(1);
  13. expect(players[0]?.id).toBe("player");
  14. const all = world.query();
  15. expect(all).toHaveLength(3);
  16. expect(world.snapshot()).toHaveLength(3);
  17. expect(world.remove("plot-1")).toBe(true);
  18. expect(world.remove("plot-1")).toBe(false);
  19. expect(world.get("plot-1")).toBeUndefined();
  20. expect(world.snapshot()).toHaveLength(2);
  21. const entity: Entity = world.create({ id: "plot-2", type: "plot", x: 0 });
  22. expect(entity).toBe(world.get("plot-2"));
  23. });
  24. });