LaMaison / floorplan /frontend /floorplan.test.ts
valcore's picture
feat: add FloorPlan Svelte component with SVG rendering and drag interaction
575e6bd
import { describe, it, expect } from "vitest";
// Logic mirror test for clampOffset — tests the algorithm, not the component import
function clampOffset(
dx: number,
dy: number,
xmin: number,
ymin: number,
xmax: number,
ymax: number,
roomMinX: number,
roomMinY: number,
roomMaxX: number,
roomMaxY: number
): [number, number] {
const clampedDx = Math.max(roomMinX - xmin, Math.min(roomMaxX - xmax, dx));
const clampedDy = Math.max(roomMinY - ymin, Math.min(roomMaxY - ymax, dy));
return [clampedDx, clampedDy];
}
describe("clampOffset", () => {
const room = { minX: 50, minY: 50, maxX: 550, maxY: 450 };
it("allows movement within room", () => {
const [dx, dy] = clampOffset(50, 0, 100, 100, 300, 200, room.minX, room.minY, room.maxX, room.maxY);
expect(dx).toBe(50);
expect(dy).toBe(0);
});
it("clamps at right wall", () => {
const [dx] = clampOffset(300, 0, 100, 100, 300, 200, room.minX, room.minY, room.maxX, room.maxY);
expect(dx).toBe(250);
});
it("clamps at left wall", () => {
const [dx] = clampOffset(-200, 0, 100, 100, 300, 200, room.minX, room.minY, room.maxX, room.maxY);
expect(dx).toBe(-50);
});
it("clamps at bottom wall", () => {
const [, dy] = clampOffset(0, 400, 100, 100, 300, 200, room.minX, room.minY, room.maxX, room.maxY);
expect(dy).toBe(250);
});
it("clamps at top wall", () => {
const [, dy] = clampOffset(0, -200, 100, 100, 300, 200, room.minX, room.minY, room.maxX, room.maxY);
expect(dy).toBe(-50);
});
});