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

ai-workshop.test.ts

171 lines5.0 KB

1import { describe, expect, it } from "vitest";
2import {
3 aiWorkshopEdit,
4 streamingPreview,
5 workshopReference,
6 sameAISnapshot,
7 type AISnapshot,
8} from "../src/shared/ai-workshop";
9import { compileBlueprint } from "../src/shared/blueprint";
10import { designSchema } from "../src/shared/design";
11const row = {
12 id: "base",
13 part: "brick-1x1",
14 colour: "jade",
15 turn: 0,
16 at: [0, 0, 0],
17 repeat: null,
18};
19const snapshot: AISnapshot = {
20 generation: 2,
21 title: "My model",
22 parts: [
23 {
24 id: "real-base",
25 key: "brick-1x1",
26 color: "#289b74",
27 position: { x: 0, y: 0, z: 0 },
28 rotation: 0,
29 revision: 4,
30 },
31 {
32 id: "real-top",
33 key: "brick-1x1",
34 color: "#289b74",
35 position: { x: 0, y: 6, z: 0 },
36 rotation: 0,
37 revision: 7,
38 },
39 ],
40 connections: [
41 {
42 id: "edge",
43 a: "real-base",
44 b: "real-top",
45 ca: "top",
46 cb: "bottom",
47 revision: 2,
48 },
49 ],
50};
51describe("Workshop AI drafts", () => {
52 it("compares Postgres JSON snapshots independently of object key and row order", () => {
53 const stored = {
54 ...snapshot,
55 parts: snapshot.parts
56 .map((p) => ({
57 revision: p.revision,
58 rotation: p.rotation,
59 position: { z: p.position.z, y: p.position.y, x: p.position.x },
60 color: p.color,
61 key: p.key,
62 id: p.id,
63 }))
64 .reverse(),
65 };
66 expect(sameAISnapshot(snapshot, stored)).toBe(true);
67 expect(sameAISnapshot(snapshot, { ...stored, generation: 3 })).toBe(false);
68 expect(
69 sameAISnapshot(snapshot, {
70 ...stored,
71 parts: stored.parts.map((p, i) =>
72 i ? p : { ...p, revision: p.revision + 1 },
73 ),
74 }),
75 ).toBe(false);
76 expect(sameAISnapshot(snapshot, { ...stored, connections: [] })).toBe(
77 false,
78 );
79 });
80 it("draws complete rows from a partial stream and omits unfinished pieces", () => {
81 const drawing = streamingPreview({
82 title: "Tower",
83 front: "+z",
84 pieces: [row, { id: "cap", part: "brick-1x1" }],
85 });
86 expect(drawing?.parts).toHaveLength(1);
87 expect(drawing?.parts[0].position).toEqual({ x: 0, y: 0, z: 0 });
88 expect(streamingPreview({ pieces: [{ ...row, turn: 9 }] })).toBeNull();
89 expect(
90 streamingPreview({
91 pieces: [{ ...row, repeat: { count: 32, step: [1, 0, 0] } }],
92 }),
93 ).toBeNull();
94 });
95 it("streams patches without dropping unaffected rows or applying incomplete changes", () => {
96 const previous = designSchema.parse(workshopReference(snapshot));
97 const patch = {
98 changes: [
99 { id: "p1", row: { ...previous.pieces[1], colour: "cobalt" } },
100 { id: "p0", row: { id: "p0" } },
101 ],
102 };
103 const drawing = streamingPreview(patch, previous);
104 expect(drawing?.parts).toHaveLength(2);
105 expect(drawing?.parts.find((p) => p.id === "p0")?.color).toBe("#289b74");
106 expect(drawing?.parts.find((p) => p.id === "p1")?.color).toBe("#486fa4");
107 expect(
108 streamingPreview({ changes: [{ id: "p0", row: {} }] }, previous),
109 ).toBeNull();
110 });
111 it("captures exact current placements and retains real part IDs for edits", () => {
112 const reference = designSchema.parse(workshopReference(snapshot));
113 const compiled = compileBlueprint(reference);
114 expect(compiled.issues).toEqual([]);
115 const parts = compiled.parts.map((p) =>
116 p.id === "p1" ? { ...p, color: "#486fa4" } : p,
117 );
118 const edit = aiWorkshopEdit(snapshot, parts, "operation", () => "new-id");
119 expect(edit.generation).toBe(2);
120 expect(edit.changes).toEqual([
121 {
122 id: "real-top",
123 expected: 7,
124 key: "brick-1x1",
125 color: "#486fa4",
126 position: { x: 0, y: 6, z: 0 },
127 rotation: 0,
128 },
129 ]);
130 expect(edit.connections).toEqual([{ id: "edge", revision: 2 }]);
131 expect(
132 aiWorkshopEdit(snapshot, compiled.parts, "operation", () => "new")
133 .changes,
134 ).toEqual([]);
135 });
136 it("includes deletion revisions and incident connectors in a replacement", () => {
137 const replacement = compileBlueprint({
138 version: 2,
139 title: "New",
140 summary: "New",
141 pieces: [{ ...row, id: "new" }],
142 }).parts;
143 const edit = aiWorkshopEdit(
144 snapshot,
145 replacement,
146 "operation",
147 () => "replacement",
148 );
149 expect(edit.changes?.map((c) => c.id)).toEqual([
150 "replacement",
151 "real-base",
152 "real-top",
153 ]);
154 expect(edit.changes?.slice(1)).toEqual([
155 { id: "real-base", expected: 4, deleted: true },
156 { id: "real-top", expected: 7, deleted: true },
157 ]);
158 });
159 it("handles empty and larger workshop references without truncating the model", () => {
160 expect(workshopReference({ ...snapshot, parts: [] })).toBeNull();
161 const ref = workshopReference({
162 ...snapshot,
163 parts: Array.from({ length: 101 }, (_, i) => ({
164 ...snapshot.parts[0],
165 id: `part-${i}`,
166 })),
167 }) as { version: number; parts: unknown[] };
168 expect(ref.version).toBe(1);
169 expect(ref.parts).toHaveLength(101);
170 });
171});