Open History to see the last commit
1import { describe, expect, it, vi } from "vitest";
2vi.mock("../src/server/db", () => ({ pool: {}, transaction: vi.fn() }));
3import { buildCandidate } from "../src/server/ai";
4const good = {
5 version: 1,
6 catalogue: "workshop-1",
7 title: "Tower",
8 summary: "A tower.",
9 parts: [
10 {
11 id: "base",
12 part: "brick-1x1",
13 colour: "jade",
14 at: [0, 0, 0],
15 turn: 0,
16 attach: null,
17 },
18 ],
19};
20const candidate = (blueprint: unknown) => ({
21 blueprint,
22 inputTokens: 10,
23 outputTokens: 10,
24});
25describe("AI generation repair loop", () => {
26 it("patches only affected rows and accounts for each inference exactly once", async () => {
27 const design = {
28 version: 2,
29 title: "Tower",
30 summary: "A tower",
31 pieces: [
32 {
33 id: "tower",
34 part: "brick-2x2",
35 colour: "jade",
36 turn: 0,
37 at: [0, 3, 0],
38 repeat: null,
39 },
40 ],
41 };
42 const generate = vi
43 .fn()
44 .mockResolvedValueOnce(candidate(design))
45 .mockResolvedValueOnce(
46 candidate({
47 title: null,
48 summary: null,
49 changes: [
50 { id: "tower", row: { ...design.pieces[0], at: [0, 0, 0] } },
51 ],
52 }),
53 );
54 const report = vi.fn();
55 const b = await buildCandidate(
56 "Tower",
57 "test",
58 new AbortController().signal,
59 generate,
60 report,
61 );
62 expect(b.issues).toEqual([]);
63 expect(generate.mock.calls[1][4]).toEqual(design);
64 expect(generate.mock.calls[1][1]).toContain("only a patch");
65 expect(
66 report.mock.calls.reduce((n, c) => n + (c[2]?.inputTokens || 0), 0),
67 ).toBe(20);
68 });
69 it("refines a saved compact design without regenerating unaffected pieces", async () => {
70 const design = {
71 version: 2,
72 title: "Tower",
73 summary: "A tower",
74 pieces: [
75 {
76 id: "tower",
77 part: "brick-2x2",
78 colour: "jade",
79 turn: 0,
80 at: [0, 0, 0],
81 repeat: { count: 3, step: [0, 3, 0] },
82 },
83 {
84 id: "roof",
85 part: "slope-2x2",
86 colour: "poppy",
87 turn: 0,
88 at: [0, 9, 0],
89 repeat: null,
90 },
91 ],
92 };
93 const generate = vi.fn().mockResolvedValue(
94 candidate({
95 title: null,
96 summary: "A blue-roofed tower",
97 changes: [
98 { id: "roof", row: { ...design.pieces[1], colour: "cobalt" } },
99 ],
100 }),
101 );
102 const b = await buildCandidate(
103 "Make its roof blue",
104 "test",
105 new AbortController().signal,
106 generate,
107 vi.fn(),
108 design,
109 );
110 expect(b.issues).toEqual([]);
111 expect(
112 b.parts.filter((p) => p.id.startsWith("tower")).map((p) => p.position.y),
113 ).toEqual([0, 6, 12]);
114 expect(b.parts.find((p) => p.id === "roof")!.color).toBe("#486fa4");
115 expect(generate.mock.calls[0][4]).toEqual(design);
116 });
117 it("accepts a valid candidate on the first call", async () => {
118 const generate = vi.fn().mockResolvedValue(candidate(good));
119 const result = await buildCandidate(
120 "Tower",
121 "test",
122 new AbortController().signal,
123 generate,
124 vi.fn(),
125 );
126 expect(result.issues).toEqual([]);
127 expect(generate).toHaveBeenCalledTimes(1);
128 });
129 it("returns specific validation errors and repairs the complete model", async () => {
130 const bad = { ...good, parts: [{ ...good.parts[0], at: [0, 3, 0] }] };
131 const generate = vi
132 .fn()
133 .mockResolvedValueOnce(candidate(bad))
134 .mockResolvedValueOnce(candidate(good));
135 await buildCandidate(
136 "Tower",
137 "test",
138 new AbortController().signal,
139 generate,
140 vi.fn(),
141 );
142 expect(generate.mock.calls[1][1]).toContain("unattached");
143 expect(generate.mock.calls[1][1]).toContain("base");
144 });
145 it("stops after three candidates", async () => {
146 const generate = vi
147 .fn()
148 .mockResolvedValue(
149 candidate({ ...good, parts: [{ ...good.parts[0], at: [0, 3, 0] }] }),
150 );
151 await expect(
152 buildCandidate(
153 "Tower",
154 "test",
155 new AbortController().signal,
156 generate,
157 vi.fn(),
158 ),
159 ).rejects.toThrow("1 piece without support after three design passes");
160 expect(generate).toHaveBeenCalledTimes(3);
161 });
162 it("rejects a worse patch and repairs the best design, while recording all usage", async () => {
163 const design = {
164 version: 2,
165 title: "Tower",
166 summary: "A tower",
167 front: "+z",
168 pieces: [
169 {
170 id: "base",
171 part: "brick-2x2",
172 colour: "jade",
173 turn: 0,
174 at: [0, 0, 0],
175 repeat: null,
176 },
177 {
178 id: "cap",
179 part: "brick-2x2",
180 colour: "poppy",
181 turn: 0,
182 at: [0, 6, 0],
183 repeat: null,
184 },
185 ],
186 };
187 const generate = vi
188 .fn()
189 .mockResolvedValueOnce(candidate(design))
190 .mockResolvedValueOnce(
191 candidate({
192 title: null,
193 summary: null,
194 changes: [
195 { id: "base", row: { ...design.pieces[0], at: [0, 6, 0] } },
196 ],
197 }),
198 )
199 .mockResolvedValueOnce(
200 candidate({
201 title: null,
202 summary: null,
203 changes: [{ id: "cap", row: { ...design.pieces[1], at: [0, 3, 0] } }],
204 }),
205 );
206 const report = vi.fn();
207 const built = await buildCandidate(
208 "Tower",
209 "test",
210 new AbortController().signal,
211 generate,
212 report,
213 );
214 expect(built.issues).toEqual([]);
215 expect(generate.mock.calls[2][4]).toEqual(design);
216 expect(generate.mock.calls[2][1]).toContain("was discarded");
217 const rejected = report.mock.calls.find((c) => c[0] === 2 && c[2]);
218 expect(rejected?.[2].retainedPrevious).toBe(true);
219 expect(rejected?.[2].issueCounts.overlap).toBe(1);
220 expect(rejected?.[2].bestIssueCounts).toEqual({ unattached: 1 });
221 expect(
222 report.mock.calls.reduce((n, c) => n + (c[2]?.inputTokens || 0), 0),
223 ).toBe(30);
224 });
225 it("keeps the repair baseline when a patch fails expansion", async () => {
226 const design = {
227 version: 2,
228 title: "Tower",
229 summary: "",
230 pieces: [
231 {
232 id: "tower",
233 part: "brick-2x2",
234 colour: "jade",
235 turn: 0,
236 at: [0, 6, 0],
237 repeat: null,
238 },
239 ],
240 };
241 const generate = vi
242 .fn()
243 .mockResolvedValueOnce(candidate(design))
244 .mockResolvedValueOnce(
245 candidate({
246 title: null,
247 summary: null,
248 changes: [
249 {
250 id: "tooBig",
251 row: {
252 ...design.pieces[0],
253 id: "tooBig",
254 part: "pine-tree",
255 repeat: { count: 32, step: [1, 0, 0] },
256 },
257 },
258 ],
259 }),
260 )
261 .mockResolvedValueOnce(
262 candidate({
263 title: null,
264 summary: null,
265 changes: [
266 { id: "tower", row: { ...design.pieces[0], at: [0, 0, 0] } },
267 ],
268 }),
269 );
270 const built = await buildCandidate(
271 "Tower",
272 "test",
273 new AbortController().signal,
274 generate,
275 vi.fn(),
276 );
277 expect(built.issues).toEqual([]);
278 expect(generate.mock.calls[2][4]).toEqual(design);
279 expect(built.parts).toHaveLength(1);
280 });
281 it("repairs schema errors and honours cancellation before more inference", async () => {
282 const generate = vi
283 .fn()
284 .mockResolvedValueOnce(candidate({ version: 2 }))
285 .mockResolvedValueOnce(candidate(good));
286 await buildCandidate(
287 "Tower",
288 "test",
289 new AbortController().signal,
290 generate,
291 vi.fn(),
292 );
293 expect(generate.mock.calls[1][1]).toContain("validation");
294 const controller = new AbortController();
295 controller.abort();
296 await expect(
297 buildCandidate("Tower", "test", controller.signal, generate, vi.fn()),
298 ).rejects.toThrow();
299 expect(generate).toHaveBeenCalledTimes(2);
300 });
301});