Skip to content
Source
Sign inSign up
samwillis/neonbricks
Open History to see the last commit

design.test.ts

217 lines6.9 KB

1import { describe, expect, it } from "vitest";
2import {
3 ASSEMBLIES,
4 expandDesign,
5 applyDesignPatch,
6 type Design,
7} from "../src/shared/design";
8import { compileBlueprint, MAX_AI_PARTS } from "../src/shared/blueprint";
9import { bounds, collision, mask } from "../src/shared/geometry";
10const row = (
11 id: string,
12 part: string,
13 at = [0, 0, 0],
14 turn = 0,
15): Design["pieces"][number] => ({
16 id,
17 part,
18 colour: "jade",
19 turn,
20 at,
21 repeat: null,
22});
23const design = (pieces: Design["pieces"]): Design => ({
24 version: 2,
25 title: "Model",
26 summary: "A model.",
27 pieces,
28});
29describe("compact AI designs", () => {
30 it("applies bounded add/replace/delete patches without mutating the original", () => {
31 const original = design([
32 row("a", "brick-1x1"),
33 row("b", "brick-1x1", [2, 0, 0]),
34 ]);
35 const patched = applyDesignPatch(original, {
36 title: null,
37 summary: null,
38 changes: [
39 { id: "a", row: null },
40 { id: "b", row: row("b", "brick-1x1", [3, 0, 0]) },
41 { id: "c", row: row("c", "brick-1x1", [5, 0, 0]) },
42 ],
43 });
44 expect(patched.pieces.map((p) => p.id)).toEqual(["b", "c"]);
45 expect(original.pieces[1].at).toEqual([2, 0, 0]);
46 for (const changes of [
47 [{ id: "missing", row: null }],
48 [{ id: "a", row: row("b", "brick-1x1") }],
49 [
50 { id: "a", row: null },
51 { id: "a", row: row("a", "brick-1x1") },
52 ],
53 ])
54 expect(() =>
55 applyDesignPatch(original, { title: null, summary: null, changes }),
56 ).toThrow();
57 });
58 it("expands a course with stable IDs and normal editable parts", () => {
59 const d = design([
60 { ...row("tower", "brick-2x2"), repeat: { count: 4, step: [0, 3, 0] } },
61 ]);
62 const b = compileBlueprint(d);
63 expect(b.issues).toEqual([]);
64 expect(b.blueprint.version).toBe(1);
65 expect(b.parts.map((p) => p.position.y)).toEqual([0, 6, 12, 18]);
66 expect(b.parts.map((p) => p.id)).toEqual([
67 "tower_0",
68 "tower_1",
69 "tower_2",
70 "tower_3",
71 ]);
72 expect(b.edges.length).toBe(12);
73 });
74 it("keeps all verified assemblies fitted through four yaws", () => {
75 for (const key of Object.keys(ASSEMBLIES))
76 for (let turn = 0; turn < 4; turn++) {
77 const b = compileBlueprint(
78 design([row("mechanism", key, [-4, 0, -4], turn)]),
79 );
80 expect(b.issues, `${key}/${turn}`).toEqual([]);
81 expect(b.edges.length).toBeGreaterThan(0);
82 const size = ASSEMBLIES[key].size;
83 expect(Math.min(...b.parts.map((p) => bounds(p).minx))).toBe(-24);
84 expect(
85 Math.max(...b.parts.map((p) => bounds(p).maxx)) - -24,
86 ).toBeCloseTo(size[turn % 2 ? 2 : 0] * 6);
87 expect(Math.min(...b.parts.map((p) => bounds(p).minz))).toBe(-24);
88 expect(
89 Math.max(...b.parts.map((p) => bounds(p).maxz)) + 24,
90 ).toBeCloseTo(size[turn % 2 ? 0 : 2] * 6);
91 expect(Math.min(...b.parts.map((p) => bounds(p).miny))).toBe(0);
92 expect(Math.max(...b.parts.map((p) => bounds(p).maxy))).toBeCloseTo(
93 size[1] * 2,
94 );
95 for (const a of b.parts)
96 for (const c of b.parts)
97 if (a.id !== c.id) expect(collision(a, c)).toBe(false);
98 }
99 });
100 it("joins roof slopes at their high edges to form a ridge instead of a valley", () => {
101 const b = compileBlueprint(design([row("roof", "gable-roof")]));
102 const top = b.parts.flatMap((p) => mask(p)).filter((c) => c.y === 7);
103 expect(top.length).toBeGreaterThan(0);
104 expect(top.every((c) => c.z >= 10 && c.z <= 13)).toBe(true);
105 });
106 it("supports a four-wheel chassis on real bearing surfaces without fake stud connections", () => {
107 const b = compileBlueprint(
108 design([
109 {
110 ...row("axles", "wheel-axle", [-3, 0, -4], 1),
111 repeat: { count: 2, step: [0, 0, 5] },
112 },
113 {
114 ...row("decks", "plate-2x4", [-2, 6, -3.5], 1),
115 repeat: { count: 2, step: [0, 0, 5] },
116 },
117 row("bridge", "plate-2x4", [-1, 7, -3]),
118 ]),
119 );
120 expect(b.issues).toEqual([]);
121 expect(
122 b.edges.filter((e) => e.a.includes("mount") && e.b.includes("decks")),
123 ).toEqual([]);
124 });
125 it("finds real bottom support transitively and rejects tiny corner contacts or gaps", () => {
126 const b = compileBlueprint(
127 design([
128 row("floor", "plate-4x4"),
129 row("beam", "beam-3", [0, 1, 0]),
130 row("body", "brick-2x2", [1, 4, 0]),
131 ]),
132 );
133 expect(b.issues).toEqual([]);
134 expect(b.edges.some((e) => e.a === "beam" || e.b === "beam")).toBe(false);
135 expect(
136 compileBlueprint(
137 design([
138 row("base", "brick-1x1"),
139 row("top", "brick-1x1", [5 / 6, 3, 5 / 6]),
140 ]),
141 ).issues.some((i) => i.code === "unattached"),
142 ).toBe(true);
143 expect(
144 compileBlueprint(
145 design([
146 row("base", "brick-1x1"),
147 row("top", "brick-1x1", [0, 3.5, 0]),
148 ]),
149 ).issues.some((i) => i.code === "unattached"),
150 ).toBe(true);
151 });
152 it("rejects expanded overflows, duplicate row and expanded IDs, and colliding repeats", () => {
153 expect(() =>
154 expandDesign(
155 design(
156 Array.from({ length: 3 }, (_, i) => ({
157 ...row(`row${i}`, "wheel-axle"),
158 repeat: { count: 32, step: [4, 0, 0] },
159 })),
160 ),
161 ),
162 ).toThrow(`${MAX_AI_PARTS}`);
163 expect(() =>
164 expandDesign(
165 design([row("same", "brick-1x1"), row("same", "brick-1x1")]),
166 ),
167 ).toThrow("Duplicate");
168 expect(() =>
169 expandDesign(
170 design([
171 { ...row("a", "brick-1x1"), repeat: { count: 2, step: [2, 0, 0] } },
172 row("a_0", "brick-1x1"),
173 ]),
174 ),
175 ).toThrow("IDs collide");
176 expect(
177 compileBlueprint(
178 design([
179 { ...row("a", "brick-1x1"), repeat: { count: 2, step: [0, 0, 0] } },
180 ]),
181 ).issues.some((i) => i.code === "overlap"),
182 ).toBe(true);
183 });
184 it("checks the expanded rotated bounds rather than only an assembly origin", () => {
185 expect(
186 compileBlueprint(
187 design([row("axle", "wheel-axle", [12, 0, 0], 1)]),
188 ).issues.some((i) => i.code === "bounds"),
189 ).toBe(true);
190 });
191 it("presents an explicitly declared doorway front toward the default camera", () => {
192 for (const front of ["+z", "-z", "+x", "-x"] as const) {
193 const at =
194 front === "+z"
195 ? [-2, 1, 1]
196 : front === "-z"
197 ? [-2, 1, -2]
198 : front === "+x"
199 ? [1, 1, -2]
200 : [-2, 1, -2];
201 const b = compileBlueprint({
202 ...design([
203 row("floor", "plate-4x4", [-2, 0, -2]),
204 row("door", "arch", at, front.endsWith("x") ? 1 : 0),
205 ]),
206 front,
207 });
208 expect(b.issues).toEqual([]);
209 expect(b.parts.find((p) => p.id === "door")!.position).toEqual({
210 x: -12,
211 y: 2,
212 z: 6,
213 });
214 expect(b.edges.length).toBeGreaterThan(0);
215 }
216 });
217});