Open History to see the last commit
1import { describe, it, expect } from "vitest";
2import { randomUUID } from "node:crypto";
3import { CATALOGUE } from "../src/shared/catalogue";
4import {
5 bounds,
6 collision,
7 validPart,
8 connectorList,
9 mating,
10 bucketKeys,
11 mask,
12} from "../src/shared/geometry";
13import { starter } from "../src/shared/starter";
14import type { Part } from "../src/shared/types";
15const part = (key: string, x = 0, y = 0, z = 0, rotation = 0): Part => ({
16 id: randomUUID(),
17 key,
18 color: "#289b74",
19 position: { x, y, z },
20 rotation,
21 revision: 1,
22});
23const connects = (a: Part, b: Part) =>
24 connectorList(a).some((c) => connectorList(b).some((d) => mating(c, d)));
25describe("catalogue collision and fit contract", () => {
26 it("has 36 valid nonempty proxies in all four supported poses", () => {
27 expect(CATALOGUE).toHaveLength(36);
28 for (const d of CATALOGUE)
29 for (let r = 0; r < 4; r++) {
30 const p = part(d.key, 0, 0, 0, r);
31 expect(validPart(p), `${d.key}/${r}`).toBe(true);
32 expect(mask(p).length).toBeGreaterThan(0);
33 expect(collision(p, { ...p, id: randomUUID() })).toBe(true);
34 }
35 });
36 it("mates studs without overlapping bodies", () => {
37 const a = part("brick-1x1"),
38 b = part("brick-1x1", 0, 6);
39 expect(collision(a, b)).toBe(false);
40 expect(connects(a, b)).toBe(true);
41 expect(connects(a, part("brick-1x1", 6, 6))).toBe(false);
42 });
43 it("fits shafts through beams, bearings, gears and wheels in each pose", () => {
44 for (let r = 0; r < 4; r++)
45 for (const key of [
46 "beam-3",
47 "bearing",
48 "gear-12",
49 "gear-20",
50 "wheel",
51 "hub",
52 "crank",
53 ]) {
54 const b = part(key, 12, 12, 12, r),
55 hole = connectorList(b).find((c) => c.type === "hole")!;
56 const a = part("axle-4", 0, 0, 0, r),
57 shaft = connectorList(a).find((c) => c.type === "shaft")!;
58 a.position = {
59 x: hole.at.x - shaft.at.x,
60 y: hole.at.y - shaft.at.y,
61 z: hole.at.z - shaft.at.z,
62 };
63 expect(collision(a, b), `${key}/${r}`).toBe(false);
64 expect(connects(a, b)).toBe(true);
65 a.position.y += 2;
66 expect(collision(a, b)).toBe(true);
67 }
68 });
69 it("rejects incompatible connectors", () => {
70 const a = connectorList(part("brick-1x1")).find((c) => c.type === "stud")!;
71 expect(mating(a, a)).toBe(false);
72 });
73 it("mates the authored static gear pose and paired hinge leaves", () => {
74 const a = part("gear-12", 0, 3),
75 b = part("gear-20", 10, 0);
76 expect(collision(a, b)).toBe(false);
77 expect(connects(a, b)).toBe(true);
78 expect(connects(a, part("gear-20", 11, 0))).toBe(false);
79 const h = part("hinge-a"),
80 leaf = part("hinge-b", 0, 0, 12);
81 expect(collision(h, leaf)).toBe(false);
82 expect(connects(h, leaf)).toBe(true);
83 expect(connects(h, part("hinge-a", 0, 0, 12))).toBe(false);
84 const turn = (p: Part): Part => {
85 const b = bounds(p);
86 return {
87 ...p,
88 rotation: (p.rotation + 1) % 4,
89 position: { x: -b.maxz, y: p.position.y, z: b.minx },
90 };
91 };
92 for (const [left, right] of [
93 [a, b],
94 [h, leaf],
95 [part("gear-12"), part("gear-12", 10)],
96 [part("gear-20"), part("gear-20", 16)],
97 ]) {
98 let one = left,
99 two = right;
100 for (let r = 0; r < 4; r++) {
101 expect(collision(one, two)).toBe(false);
102 expect(connects(one, two)).toBe(true);
103 one = turn(one);
104 two = turn(two);
105 }
106 }
107 });
108 it("covers touching and intersecting neighbours across bucket boundaries", () => {
109 const a = part("brick-1x1", 23),
110 b = part("brick-1x1", 29);
111 expect(bucketKeys([a]).some((k) => bucketKeys([b]).includes(k))).toBe(true);
112 const c = part("brick-1x1", -60, 0, -60);
113 expect(bucketKeys([a]).some((k) => bucketKeys([c]).includes(k))).toBe(
114 false,
115 );
116 });
117 it("validates the complete starter and every pair", () => {
118 const ps = starter(randomUUID);
119 for (let i = 0; i < ps.length; i++) {
120 expect(validPart(ps[i])).toBe(true);
121 for (let j = 0; j < i; j++)
122 expect(collision(ps[i], ps[j]), `${ps[i].key}/${ps[j].key}`).toBe(
123 false,
124 );
125 }
126 });
127 it("enforces integer coordinates and board limits", () => {
128 expect(validPart(part("brick-1x1", 95))).toBe(false);
129 expect(validPart(part("brick-1x1", 0.5))).toBe(false);
130 expect(bounds(part("brick-1x2", 0, 0, 0, 1)).maxx).toBe(12);
131 });
132});