labels.test.ts 946 B

12345678910111213141516171819202122
  1. import { describe, expect, it } from "vitest";
  2. import type { Entity } from "@nengine/core";
  3. import { labelPosition, LABEL_ABOVE_OFFSET } from "../src/labels.js";
  4. const entity = (overrides: Partial<Entity> = {}): Entity => ({ id: "npc:mayor", type: "npc", x: 6, y: 3, ...overrides });
  5. describe("labelPosition (pure entity → px label anchor)", () => {
  6. it("anchors at the tile's horizontal center, offset above its top edge", () => {
  7. const pos = labelPosition(entity(), 32);
  8. expect(pos).toEqual({ x: 6 * 32 + 16, y: 3 * 32 - LABEL_ABOVE_OFFSET });
  9. });
  10. it("follows the tile size", () => {
  11. const pos = labelPosition(entity(), 48);
  12. expect(pos).toEqual({ x: 6 * 48 + 24, y: 3 * 48 - LABEL_ABOVE_OFFSET });
  13. });
  14. it("falls back to 0,0 for an entity without coordinates", () => {
  15. const pos = labelPosition(entity({ x: undefined, y: undefined }), 32);
  16. expect(pos).toEqual({ x: 16, y: -LABEL_ABOVE_OFFSET });
  17. });
  18. });