Open History to see the last commit
1import { describe, it, expect } from "vitest";
2import { snapToSurface } from "../src/client/snapping";
3import { fitsScene } from "../src/client/movement";
4import { connectorList, mating } from "../src/shared/geometry";
5import type { Part } from "../src/shared/types";
6
7const part = (
8 id: string,
9 key = "brick-1x1",
10 x = 0,
11 y = 0,
12 z = 0,
13 rotation = 0,
14): Part => ({
15 id,
16 key,
17 color: "#289b74",
18 position: { x, y, z },
19 rotation,
20 revision: 1,
21});
22const connects = (a: Part, b: Part) =>
23 connectorList(a).some((c) => connectorList(b).some((d) => mating(c, d)));
24const up = { x: 0, y: 1, z: 0 };
25
26describe("surface and attachment snapping", () => {
27 it("catches top studs while dragging over the upper side of a brick", () => {
28 const source = part("a", "brick-1x1", -30),
29 target = part("target", "brick-2x2", 0, 6);
30 const snap = snapToSurface(
31 [source],
32 {
33 part: target,
34 point: { x: 12, y: 10, z: 3 },
35 normal: { x: 1, y: 0, z: 0 },
36 },
37 { x: 36, y: 6, z: 0 },
38 [source, target],
39 { excluded: [source.id] },
40 );
41 expect(snap.parts[0].position).toEqual({ x: 6, y: 12, z: 0 });
42 expect(connects(snap.parts[0], target)).toBe(true);
43 expect(fitsScene(snap.parts, [target])).toBe(true);
44 });
45
46 it("keeps lower-side placement beside the brick instead of lifting it", () => {
47 const source = part("a", "brick-1x1", -30),
48 target = part("target", "brick-2x2", 0, 6);
49 const snap = snapToSurface(
50 [source],
51 {
52 part: target,
53 point: { x: 12, y: 7, z: 3 },
54 normal: { x: 1, y: 0, z: 0 },
55 },
56 { x: 36, y: 6, z: 0 },
57 [target],
58 );
59 expect(snap.parts[0].position).toEqual({ x: 12, y: 6, z: 0 });
60 expect(connects(snap.parts[0], target)).toBe(false);
61 });
62
63 it("leaves fine placement free of the upper-edge stud magnet", () => {
64 const source = part("a", "brick-1x1", -30),
65 target = part("target", "brick-2x2", 0, 6);
66 const snap = snapToSurface(
67 [source],
68 {
69 part: target,
70 point: { x: 12, y: 10, z: 3 },
71 normal: { x: 1, y: 0, z: 0 },
72 },
73 { x: 36, y: 6, z: 0 },
74 [target],
75 { fine: true },
76 );
77 expect(snap.parts[0].position).toEqual({ x: 12, y: 6, z: 0 });
78 });
79
80 it("keeps an occupied upper-edge attachment invalid at the intended height", () => {
81 const source = part("a", "brick-1x1", -30),
82 target = part("target", "brick-2x2", 0, 6),
83 blocker = part("blocker", "brick-2x2", 0, 12);
84 const snap = snapToSurface(
85 [source],
86 {
87 part: target,
88 point: { x: 12, y: 10, z: 3 },
89 normal: { x: 1, y: 0, z: 0 },
90 },
91 { x: 36, y: 6, z: 0 },
92 [target, blocker],
93 );
94 expect(snap.parts[0].position).toEqual({ x: 6, y: 12, z: 0 });
95 expect(fitsScene(snap.parts, [target, blocker])).toBe(false);
96 });
97
98 it("snaps onto the logical stud height and an off-grid target, preserving the grabbed socket", () => {
99 const source = part("a", "brick-1x1", -30),
100 target = part("target", "brick-2x2", 1, 6, 5);
101 const snap = snapToSurface(
102 [source],
103 { part: target, point: { x: 4, y: 13.1, z: 8 }, normal: up },
104 { x: 30, y: 12, z: 6 },
105 [source, target],
106 { excluded: [source.id] },
107 );
108 expect(snap.parts[0].position).toEqual({ x: 1, y: 12, z: 5 });
109 expect(connects(snap.parts[0], target)).toBe(true);
110 expect(fitsScene(snap.parts, [target])).toBe(true);
111 });
112
113 it("translates a group onto a target without changing its offsets or rotations", () => {
114 const sources = [
115 part("a", "brick-1x1", -30),
116 part("b", "brick-1x1", -30, 6, 0, 1),
117 ];
118 const target = part("target", "brick-2x2");
119 const snap = snapToSurface(
120 sources,
121 { part: target, point: { x: 3, y: 6.5, z: 3 }, normal: up },
122 { x: 30, y: 0, z: 0 },
123 [target, ...sources],
124 { excluded: sources.map((p) => p.id) },
125 );
126 expect(snap.parts.map((p) => p.position)).toEqual([
127 { x: 0, y: 6, z: 0 },
128 { x: 0, y: 12, z: 0 },
129 ]);
130 expect(snap.parts.map((p) => p.rotation)).toEqual([0, 1]);
131 expect(connects(snap.parts[0], target)).toBe(true);
132 expect(fitsScene(snap.parts, [target])).toBe(true);
133 });
134
135 it("keeps an occupied attachment at its target height so it remains invalid", () => {
136 const source = part("a"),
137 target = part("target", "brick-2x2"),
138 blocker = part("blocker", "brick-2x2", 0, 6);
139 const snap = snapToSurface(
140 [source],
141 { part: target, point: { x: 3, y: 6, z: 3 }, normal: up },
142 { x: 0, y: 0, z: 0 },
143 [target, blocker],
144 );
145 expect(snap.parts[0].position).toEqual({ x: 0, y: 6, z: 0 });
146 expect(fitsScene(snap.parts, [target, blocker])).toBe(false);
147 });
148
149 it.each([0, 1, 2, 3])(
150 "finds an axle/beam fit with automatic orientation at yaw %s",
151 (rotation) => {
152 const target = part("target", "beam-3", 12, 0, 12, rotation);
153 const hole = connectorList(target).find((c) => c.type === "hole")!;
154 const source = part("a", "axle-4", 0, 0, 0, (rotation + 1) % 4);
155 const oriented = { ...source, rotation };
156 const shaft = connectorList(oriented).find((c) => c.type === "shaft")!;
157 const desired = {
158 x: hole.at.x - shaft.at.x,
159 y: hole.at.y - shaft.at.y + 2,
160 z: hole.at.z - shaft.at.z,
161 };
162 const snap = snapToSurface(
163 [source],
164 { part: target, point: hole.at, normal: { x: 0, y: 0, z: -1 } },
165 desired,
166 [target],
167 { rotate: true },
168 );
169 expect(connects(snap.parts[0], target)).toBe(true);
170 expect(fitsScene(snap.parts, [target])).toBe(true);
171 expect(snap.parts[0].rotation % 2).toBe(rotation % 2);
172 },
173 );
174
175 it("snaps a moved receiver onto an existing shaft", () => {
176 const source = part("a", "bearing", -30),
177 target = part("target", "axle-4", 12, 0, 12);
178 const snap = snapToSurface(
179 [source],
180 { part: target, point: { x: 15, y: 3, z: 15 }, normal: up },
181 { x: 42, y: 0, z: 12 },
182 [source, target],
183 { excluded: [source.id] },
184 );
185 expect(snap.parts[0].position).toEqual({ x: 12, y: 0, z: 12 });
186 expect(connects(snap.parts[0], target)).toBe(true);
187 expect(fitsScene(snap.parts, [target])).toBe(true);
188 });
189
190 it("fits gear and hinge connectors without relying on grid-height rounding", () => {
191 const gear = part("target", "gear-20", 10),
192 source = part("a", "gear-12");
193 const snap = snapToSurface(
194 [source],
195 {
196 part: gear,
197 point: { x: 11, y: 9, z: 3 },
198 normal: { x: 0, y: 0, z: -1 },
199 },
200 { x: 0, y: 2, z: 0 },
201 [gear],
202 );
203 expect(snap.parts[0].position).toEqual({ x: 0, y: 3, z: 0 });
204 expect(connects(snap.parts[0], gear)).toBe(true);
205 expect(fitsScene(snap.parts, [gear])).toBe(true);
206 const base = part("base", "hinge-a"),
207 leaf = part("leaf", "hinge-b");
208 const hinge = snapToSurface(
209 [leaf],
210 {
211 part: base,
212 point: { x: 6, y: 1, z: 12 },
213 normal: { x: 0, y: 0, z: 1 },
214 },
215 { x: 0, y: 0, z: 12 },
216 [base],
217 );
218 expect(connects(hinge.parts[0], base)).toBe(true);
219 expect(fitsScene(hinge.parts, [base])).toBe(true);
220 });
221
222 it("uses rotated bounds for flush side placement and respects an explicit build layer", () => {
223 const source = part("a", "brick-1x2", 0, 0, 0, 1),
224 target = part("target", "brick-2x2", 18, 12);
225 const side = snapToSurface(
226 [source],
227 {
228 part: target,
229 point: { x: 18, y: 15, z: 3 },
230 normal: { x: -1, y: 0, z: 0 },
231 },
232 { x: 12, y: 12, z: 0 },
233 [target],
234 );
235 expect(side.parts[0].position.x).toBe(6);
236 const manual = snapToSurface(
237 [source],
238 { part: target, point: { x: 21, y: 18, z: 3 }, normal: up },
239 { x: 18, y: 6, z: 0 },
240 [target],
241 { autoHeight: false },
242 );
243 expect(manual.parts[0].position.y).toBe(6);
244 });
245});