Open History to see the last commit
1import { readFileSync } from "node:fs";
2import { describe, expect, it } from "vitest";
3import { snapDesign } from "../src/shared/design-fit";
4import { compileBlueprint } from "../src/shared/blueprint";
5import { designSchema, type Design } from "../src/shared/design";
6
7const rover = designSchema.parse(
8 JSON.parse(
9 readFileSync(
10 new URL("./fixtures/ai-rover-missed-antenna.json", import.meta.url),
11 "utf8",
12 ),
13 ),
14);
15describe("bounded AI stud snapping", () => {
16 it("grounds the observed lookout's bushes while leaving its collision repairs to the model", () => {
17 const original = designSchema.parse(
18 JSON.parse(
19 readFileSync(
20 new URL("./fixtures/ai-lookout-fit-errors.json", import.meta.url),
21 "utf8",
22 ),
23 ),
24 );
25 for (const front of ["+z", "-z", "+x", "-x"] as const) {
26 const design = { ...original, front };
27 expect(compileBlueprint(design).issueCounts).toEqual({
28 overlap: 12,
29 unattached: 3,
30 });
31 const fit = snapDesign(design);
32 expect(fit.compiled.issueCounts).toEqual({ overlap: 12 });
33 expect(fit.adjustments).toHaveLength(3);
34 expect(fit.design.pieces.filter((p) => !p.id.startsWith("bush"))).toEqual(
35 design.pieces.filter((p) => !p.id.startsWith("bush")),
36 );
37 expect(
38 fit.design.pieces
39 .filter((p) => p.id.startsWith("bush"))
40 .map((p) => p.at[1]),
41 ).toEqual([0, 0, 0]);
42 expect(fit.compiled.parts).toHaveLength(95);
43 }
44 });
45 it("does not swap an existing overlap for a new one while grounding scenery", () => {
46 const original = designSchema.parse(
47 JSON.parse(
48 readFileSync(
49 new URL("./fixtures/ai-lookout-fit-errors.json", import.meta.url),
50 "utf8",
51 ),
52 ),
53 );
54 const d = {
55 ...original,
56 pieces: original.pieces.map((p) =>
57 p.id === "bush1" ? { ...p, at: [-6, 0.5, 0] } : p,
58 ),
59 };
60 const fit = snapDesign(d);
61 expect(fit.design.pieces.find((p) => p.id === "bush1")).toEqual(
62 d.pieces.find((p) => p.id === "bush1"),
63 );
64 expect(fit.compiled.issueCounts.overlap).toBe(13);
65 });
66 it("fixes the observed three-piece antenna without changing the rover", () => {
67 for (const front of [undefined, "+z", "-z", "+x", "-x"] as const) {
68 const original = { ...rover, front };
69 expect(compileBlueprint(original).issues).toHaveLength(3);
70 const fit = snapDesign(original);
71 expect(fit.compiled.issues).toEqual([]);
72 expect(fit.adjustments).toHaveLength(1);
73 expect(fit.adjustments[0].rows).toEqual(["ant1", "ant2"]);
74 expect(fit.adjustments[0].shift.slice(0, 2)).toEqual([0, 0]);
75 expect(Math.abs(fit.adjustments[0].shift[2])).toBe(1);
76 expect(fit.design.pieces.filter((p) => !p.id.startsWith("ant"))).toEqual(
77 original.pieces.filter((p) => !p.id.startsWith("ant")),
78 );
79 expect(original.pieces.find((p) => p.id === "ant1")!.at).toEqual([
80 1, 17, 0,
81 ]);
82 }
83 });
84 it("does not delete details, move parts far away, or fix overlaps by weakening checks", () => {
85 const row: Design["pieces"][number] = {
86 id: "base",
87 part: "brick-1x1",
88 colour: "jade",
89 turn: 0,
90 at: [0, 0, 0],
91 repeat: null,
92 };
93 for (const at of [
94 [3, 3, 0],
95 [0, 0, 0],
96 [0, 6, 0],
97 ]) {
98 const d: Design = {
99 version: 2,
100 title: "Fixture",
101 summary: "",
102 pieces: [row, { ...row, id: "detail", at }],
103 };
104 const fit = snapDesign(d);
105 expect(fit.adjustments).toEqual([]);
106 expect(fit.compiled.issues.length).toBeGreaterThan(0);
107 expect(fit.design).toEqual(d);
108 }
109 });
110 it("leaves partially grounded repeat rows in place", () => {
111 const d: Design = {
112 version: 2,
113 title: "Fixture",
114 summary: "",
115 pieces: [
116 {
117 id: "base",
118 part: "brick-1x1",
119 colour: "jade",
120 turn: 0,
121 at: [0, 0, 0],
122 repeat: null,
123 },
124 {
125 id: "caps",
126 part: "brick-1x1",
127 colour: "jade",
128 turn: 0,
129 at: [0, 3, 0],
130 repeat: { count: 2, step: [1, 0, 0] },
131 },
132 ],
133 };
134 const fit = snapDesign(d);
135 expect(fit.adjustments).toEqual([]);
136 expect(fit.compiled.issues).toHaveLength(1);
137 });
138});