Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

previewAnnotation.test.ts

87 lines2.8 KB

1import type { PreviewAnnotationPayload } from "@v12/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 appendPreviewAnnotationPrompt,
6 buildPreviewAnnotationPrompt,
7 extractTrailingPreviewAnnotation,
8} from "./previewAnnotation";
9​
10const annotation: PreviewAnnotationPayload = {
11 id: "annotation_1",
12 pageUrl: "http://localhost:3000",
13 pageTitle: "Example",
14 comment: "Make these cards feel related.",
15 elements: [],
16 regions: [{ id: "region_1", rect: { x: 10, y: 20, width: 100, height: 80 } }],
17 strokes: [
18 {
19 id: "stroke_1",
20 color: "#7c3aed",
21 width: 4,
22 points: [
23 { x: 10, y: 10 },
24 { x: 20, y: 20 },
25 ],
26 bounds: { x: 6, y: 6, width: 18, height: 18 },
27 },
28 ],
29 styleChanges: [
30 {
31 targetId: "element_1",
32 selector: ".card",
33 property: "border-radius",
34 previousValue: "4px",
35 value: "16px",
36 },
37 ],
38 screenshot: {
39 dataUrl: "data:image/png;base64,AA==",
40 width: 100,
41 height: 80,
42 cropRect: { x: 10, y: 20, width: 100, height: 80 },
43 },
44 createdAt: "2026-06-11T00:00:00.000Z",
45};
46​
47describe("preview annotations", () => {
48 it("describes regions, drawings, styles, and screenshot context", () => {
49 const result = buildPreviewAnnotationPrompt(annotation);
50 expect(result).toContain("Make these cards feel related.");
51 expect(result).toContain("1 marked region");
52 expect(result).toContain("1 drawing");
53 expect(result).toContain("border-radius: 4px → 16px");
54 expect(result).toContain("attached screenshot");
55 });
56​
57 it("appends to an existing composer prompt", () => {
58 expect(
59 appendPreviewAnnotationPrompt("Fix this", annotation).startsWith(
60 "Fix this\n\n<preview_annotation>",
61 ),
62 ).toBe(true);
63 });
64​
65 it("extracts annotation presentation from a sent prompt", () => {
66 const result = extractTrailingPreviewAnnotation(
67 appendPreviewAnnotationPrompt("Fix this", annotation),
68 );
69 expect(result.promptText).toBe("Fix this");
70 expect(result.annotation).toMatchObject({
71 title: "Example",
72 targetSummary: "1 marked region, 1 drawing.",
73 hasScreenshot: true,
74 });
75 });
76​
77 it("extracts multiple trailing annotations one at a time", () => {
78 const first = appendPreviewAnnotationPrompt("Fix this", annotation);
79 const secondAnnotation = { ...annotation, id: "annotation_2", pageTitle: "Details" };
80 const second = appendPreviewAnnotationPrompt(first, secondAnnotation);
81 const extractedSecond = extractTrailingPreviewAnnotation(second);
82 const extractedFirst = extractTrailingPreviewAnnotation(extractedSecond.promptText);
83 expect(extractedSecond.annotation?.id).toBe("annotation_2");
84 expect(extractedFirst.annotation?.id).toBe("annotation_1");
85 expect(extractedFirst.promptText).toBe("Fix this");
86 });
87});