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

ai-conversation.test.ts

89 lines3.0 KB

1import { describe, expect, it } from "vitest";
2import {
3 conversationRows,
4 conversationState,
5} from "../src/client/ai-conversation";
6import type { AIConversationRow } from "../src/shared/types";
7​
8const row = (
9 id: string,
10 status: AIConversationRow["status"],
11 time: string,
12 author = "alice",
13): AIConversationRow => ({
14 id,
15 status,
16 created_at: time,
17 author,
18 author_name: author,
19 attempt: 1,
20 error: null,
21 world: null,
22 target: "workshop",
23 prompt: "Change the model",
24 model: "fixture",
25 blueprint: null,
26});
27describe("Shared AI conversation snapshots", () => {
28 it("discovers another author's prompt and selects its active build regardless of query order", () => {
29 const jobs = conversationRows([
30 row("bob-build", "designing", "2026-10-13T10:01:00Z", "bob"),
31 row("alice-build", "accepted", "2026-10-13T10:00:00Z"),
32 ]);
33 expect(jobs.map((r) => r.id)).toEqual(["alice-build", "bob-build"]);
34 expect(conversationState(jobs).job?.author).toBe("bob");
35 expect(conversationState(jobs).preview).toBeNull();
36 });
37 it("keeps the shared valid draft during a refinement and after failure", () => {
38 for (const status of ["designing", "failed", "cancelled"] as const) {
39 const jobs = conversationRows([
40 row("draft", "ready", "2026-10-13T10:00:00Z"),
41 row("refinement", status, "2026-10-13T10:01:00Z", "bob"),
42 ]);
43 expect(conversationState(jobs).preview?.id).toBe("draft");
44 }
45 });
46 it("clears an earlier preview on peer application and all state on membership withdrawal", () => {
47 const jobs = conversationRows([
48 row("draft", "ready", "2026-10-13T10:00:00Z"),
49 row("refinement", "accepted", "2026-10-13T10:01:00Z", "bob"),
50 ]);
51 expect(conversationState(jobs).preview).toBeNull();
52 expect(conversationState(conversationRows([]))).toEqual({
53 job: null,
54 preview: null,
55 });
56 });
57 it("uses the ready preview and keeps partial drawings separate from validated parts", () => {
58 const piece = {
59 id: "p0",
60 key: "brick-2x2",
61 color: "#289b74",
62 rotation: 0,
63 position: { x: 0, y: 0, z: 0 },
64 revision: 1,
65 };
66 for (const status of ["checking", "ready"] as const) {
67 const jobs = conversationRows([
68 {
69 ...row("draft", status, "2026-10-13T10:00:00Z"),
70 preview: { parts: [piece], title: "Tower" },
71 },
72 ]);
73 expect(jobs[0].parts).toHaveLength(status === "ready" ? 1 : 0);
74 expect(jobs[0].preview?.parts).toHaveLength(1);
75 }
76 });
77});
78​
79it("reuses historic exchange objects while an active drawing changes", () => {
80 const history = row("applied", "accepted", "2026-10-13T10:00:00Z");
81 const busy = row("drawing", "designing", "2026-10-13T10:01:00Z");
82 const previous = conversationRows([history, busy]);
83 const next = conversationRows(
84 [{ ...history }, { ...busy, activity: "Drawing 20 pieces" }],
85 previous,
86 );
87 expect(next[0]).toBe(previous[0]);
88 expect(next[1]).not.toBe(previous[1]);
89});