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

blueprint.test.ts

148 lines4.5 KB

1import { describe, expect, it } from "vitest";
2import {
3 blueprintSchema,
4 compileBlueprint,
5 MAX_AI_PARTS,
6 type Blueprint,
7} from "../src/shared/blueprint";
8import { CATALOGUE_VERSION } from "../src/shared/types";
9const part = (
10 id: string,
11 key: string,
12 at: [number, number, number],
13): Blueprint["parts"][number] => ({
14 id,
15 part: key,
16 at,
17 colour: "jade",
18 turn: 0,
19 attach: null,
20});
21const blueprint = (parts: Blueprint["parts"]): Blueprint => ({
22 version: 1,
23 catalogue: CATALOGUE_VERSION,
24 title: "Test build",
25 summary: "A test build.",
26 parts,
27});
28describe("AI blueprint compiler", () => {
29 it("converts studs and plate heights and derives a connected stack", () => {
30 const result = compileBlueprint(
31 blueprint([
32 part("base", "plate-4x4", [-2, 0, -2]),
33 part("body", "brick-2x2", [-1, 1, -1]),
34 part("roof", "slope-2x2", [-1, 4, -1]),
35 ]),
36 );
37 expect(result.issues).toEqual([]);
38 expect(result.parts[1].position).toEqual({ x: -6, y: 2, z: -6 });
39 expect(result.edges.length).toBeGreaterThan(1);
40 });
41 it("solves connector placements including forward references", () => {
42 const attached = {
43 ...part("top", "brick-1x1", [0, 0, 0]),
44 at: null,
45 attach: { to: "base", connector: "s0:0", ownConnector: "b0:0" },
46 };
47 const result = compileBlueprint(
48 blueprint([attached, part("base", "brick-2x2", [0, 0, 0])]),
49 );
50 expect(result.issues).toEqual([]);
51 expect(result.parts.find((p) => p.id === "top")!.position.y).toBe(6);
52 });
53 it("fits a wheel onto an axle through their authored channels", () => {
54 const wheel = part("wheel", "wheel", [0, 0, 0]);
55 const axle = {
56 ...part("axle", "axle-4", [0, 0, 0]),
57 at: null,
58 attach: { to: "wheel", connector: "h0", ownConnector: "a0" },
59 };
60 expect(compileBlueprint(blueprint([wheel, axle])).issues).toEqual([]);
61 });
62 it("reports named collision pairs", () => {
63 const result = compileBlueprint(
64 blueprint([
65 part("a", "brick-2x2", [0, 0, 0]),
66 part("b", "brick-1x1", [1, 0, 1]),
67 ]),
68 );
69 expect(result.issues).toContainEqual({
70 code: "overlap",
71 parts: ["a", "b"],
72 message: expect.any(String),
73 });
74 });
75 it("allows separate grounded objects but rejects floating assemblies", () => {
76 const result = compileBlueprint(
77 blueprint([
78 part("a", "brick-1x1", [0, 0, 0]),
79 part("b", "brick-1x1", [3, 0, 0]),
80 part("floating", "brick-1x1", [5, 3, 0]),
81 ]),
82 );
83 expect(result.issues.map((i) => i.parts)).toEqual([["floating"]]);
84 });
85 it("rejects cycles, duplicate IDs, and ambiguous placements", () => {
86 const a = {
87 ...part("a", "brick-1x1", [0, 0, 0]),
88 at: null,
89 attach: { to: "b", connector: "s0:0", ownConnector: "b0:0" },
90 };
91 const b = { ...a, id: "b", attach: { ...a.attach, to: "a" } };
92 expect(
93 compileBlueprint(blueprint([a, b])).issues.some(
94 (i) => i.code === "attachment_cycle",
95 ),
96 ).toBe(true);
97 expect(
98 compileBlueprint(
99 blueprint([
100 part("same", "brick-1x1", [0, 0, 0]),
101 part("same", "brick-1x1", [2, 0, 0]),
102 ]),
103 ).issues.some((i) => i.code === "duplicate_id"),
104 ).toBe(true);
105 expect(
106 compileBlueprint(
107 blueprint([{ ...a, at: [0, 0, 0] }, part("b", "brick-1x1", [0, 0, 0])]),
108 ).issues.some((i) => i.code === "placement"),
109 ).toBe(true);
110 });
111 it("rejects unknown parts, colours and unsupported format versions", () => {
112 for (const change of [
113 { version: 2 },
114 { catalogue: "future" },
115 { parts: [part("x", "imaginary-motor", [0, 0, 0])] },
116 {
117 parts: [{ ...part("x", "brick-1x1", [0, 0, 0]), colour: "invisible" }],
118 },
119 ])
120 expect(
121 blueprintSchema.safeParse({
122 ...blueprint([part("x", "brick-1x1", [0, 0, 0])]),
123 ...change,
124 }).success,
125 ).toBe(false);
126 });
127 it("checks rotated bounds, exact lattice alignment and expanded part count", () => {
128 expect(
129 compileBlueprint(
130 blueprint([{ ...part("x", "brick-1x4", [15, 0, 0]), turn: 1 }]),
131 ).issues.some((i) => i.code === "bounds"),
132 ).toBe(true);
133 expect(
134 compileBlueprint(
135 blueprint([part("x", "brick-1x1", [0.1, 0, 0])]),
136 ).issues.some((i) => i.code === "grid"),
137 ).toBe(true);
138 expect(
139 blueprintSchema.safeParse(
140 blueprint(
141 Array.from({ length: MAX_AI_PARTS + 1 }, (_, i) =>
142 part(`p${i}`, "brick-1x1", [0, 0, 0]),
143 ),
144 ),
145 ).success,
146 ).toBe(false);
147 });
148});