Open History to see the last commit
1import { describe, it, expect } from "vitest";
2import {
3 captureMove,
4 fitsScene,
5 movedParts,
6 moveRequest,
7 snapMovement,
8} from "../src/client/movement";
9import type { Part } from "../src/shared/types";
10
11const part = (id: string, x = 0, y = 0, z = 0): Part => ({
12 id,
13 key: "brick-1x1",
14 color: "#289b74",
15 rotation: 0,
16 revision: 7,
17 position: { x, y, z },
18});
19
20describe("movement gesture contract", () => {
21 it("preserves off-grid origins and group offsets with a snapped planar displacement", () => {
22 const source = [part("a", 1, 3, 5), part("b", 13, 9, 5)];
23 const move = captureMove(source, [], 4);
24 move.delta = snapMovement({ x: -8.7, y: 22, z: 8.9 });
25 expect(movedParts(move).map((p) => p.position)).toEqual([
26 { x: -5, y: 3, z: 11 },
27 { x: 7, y: 9, z: 11 },
28 ]);
29 expect(source[0].position).toEqual({ x: 1, y: 3, z: 5 });
30 });
31
32 it("constrains elevation to plates and permits sixth-stud precision", () => {
33 expect(snapMovement({ x: 23, y: 3.2, z: 17 }, "y")).toEqual({
34 x: 0,
35 y: 4,
36 z: 0,
37 });
38 expect(snapMovement({ x: 23, y: 3.2, z: 17 }, "y", true)).toEqual({
39 x: 0,
40 y: 3,
41 z: 0,
42 });
43 expect(snapMovement({ x: 2.7, y: 33, z: 8.2 }, "x", true)).toEqual({
44 x: 3,
45 y: 0,
46 z: 0,
47 });
48 });
49
50 it("keeps pickup preconditions after another viewer updates the scene", () => {
51 const source = part("a");
52 const connections = [
53 { id: "joint", a: "a", b: "b", ca: "0", cb: "1", revision: 5 },
54 ];
55 const move = captureMove([source], connections, 4);
56 source.revision = 8;
57 source.position.x = 30;
58 connections[0].revision = 6;
59 move.delta = { x: 6, y: 0, z: 0 };
60 expect(moveRequest(move)).toEqual({
61 action: "edit",
62 connections: [{ id: "joint", revision: 5 }],
63 changes: [
64 { id: "a", expected: 7, rotation: 0, position: { x: 6, y: 0, z: 0 } },
65 ],
66 });
67 expect(captureMove([source], [], 4).connections).toEqual([]);
68 });
69
70 it("ignores only the moving sources and rejects occupied space and board limits", () => {
71 const source = part("a"),
72 neighbour = part("b", 6);
73 expect(fitsScene([source], [source, neighbour], ["a"])).toBe(true);
74 expect(
75 fitsScene(
76 [{ ...source, position: neighbour.position }],
77 [source, neighbour],
78 ["a"],
79 ),
80 ).toBe(false);
81 expect(fitsScene([part("a", 96)], [source], ["a"])).toBe(false);
82 expect(fitsScene([source, part("b")], [], [])).toBe(false);
83 });
84});