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

previewAnnotation.test.ts

121 lines4.1 KB

1import type { PreviewAnnotationPayload } from "@t3tools/contracts";
2import { afterEach, describe, expect, it, vi } from "vite-plus/test";
3​
4import {
5 appendPreviewAnnotationPrompt,
6 buildPreviewAnnotationPrompt,
7 capturePreviewAnnotationScreenshot,
8 extractTrailingPreviewAnnotation,
9} from "./previewAnnotation";
10​
11const annotation: PreviewAnnotationPayload = {
12 id: "annotation_1",
13 pageUrl: "http://localhost:3000",
14 pageTitle: "Example",
15 comment: "Make these cards feel related.",
16 elements: [],
17 regions: [{ id: "region_1", rect: { x: 10, y: 20, width: 100, height: 80 } }],
18 strokes: [
19 {
20 id: "stroke_1",
21 color: "#7c3aed",
22 width: 4,
23 points: [
24 { x: 10, y: 10 },
25 { x: 20, y: 20 },
26 ],
27 bounds: { x: 6, y: 6, width: 18, height: 18 },
28 },
29 ],
30 styleChanges: [
31 {
32 targetId: "element_1",
33 selector: ".card",
34 property: "border-radius",
35 previousValue: "4px",
36 value: "16px",
37 },
38 ],
39 screenshot: {
40 dataUrl: "data:image/png;base64,AA==",
41 width: 100,
42 height: 80,
43 cropRect: { x: 10, y: 20, width: 100, height: 80 },
44 },
45 createdAt: "2026-06-11T00:00:00.000Z",
46};
47​
48describe("preview annotations", () => {
49 it("describes regions, drawings, styles, and screenshot context", () => {
50 const result = buildPreviewAnnotationPrompt(annotation);
51 expect(result).toContain("Make these cards feel related.");
52 expect(result).toContain("1 marked region");
53 expect(result).toContain("1 drawing");
54 expect(result).toContain("border-radius: 4px → 16px");
55 expect(result).toContain("attached screenshot");
56 });
57​
58 it("appends to an existing composer prompt", () => {
59 expect(
60 appendPreviewAnnotationPrompt("Fix this", annotation).startsWith(
61 "Fix this\n\n<preview_annotation>",
62 ),
63 ).toBe(true);
64 });
65​
66 it("extracts annotation presentation from a sent prompt", () => {
67 const result = extractTrailingPreviewAnnotation(
68 appendPreviewAnnotationPrompt("Fix this", annotation),
69 );
70 expect(result.promptText).toBe("Fix this");
71 expect(result.annotation).toMatchObject({
72 title: "Example",
73 targetSummary: "1 marked region, 1 drawing.",
74 hasScreenshot: true,
75 });
76 });
77​
78 it("extracts multiple trailing annotations one at a time", () => {
79 const first = appendPreviewAnnotationPrompt("Fix this", annotation);
80 const secondAnnotation = { ...annotation, id: "annotation_2", pageTitle: "Details" };
81 const second = appendPreviewAnnotationPrompt(first, secondAnnotation);
82 const extractedSecond = extractTrailingPreviewAnnotation(second);
83 const extractedFirst = extractTrailingPreviewAnnotation(extractedSecond.promptText);
84 expect(extractedSecond.annotation?.id).toBe("annotation_2");
85 expect(extractedFirst.annotation?.id).toBe("annotation_1");
86 expect(extractedFirst.promptText).toBe("Fix this");
87 });
88});
89​
90describe("preview annotation capture", () => {
91 afterEach(() => {
92 vi.useRealTimers();
93 vi.unstubAllGlobals();
94 });
95​
96 it("returns the crop when the fetch resolves", async () => {
97 vi.stubGlobal("fetch", async () => new Response(new Blob(["png"], { type: "image/png" })));
98 const capture = await capturePreviewAnnotationScreenshot(annotation);
99 expect(capture.status).toBe("captured");
100 });
101​
102 it("reports none when the annotation carries no crop", async () => {
103 const capture = await capturePreviewAnnotationScreenshot({ ...annotation, screenshot: null });
104 expect(capture).toEqual({ status: "none" });
105 });
106​
107 it("fails instead of hanging when the crop never arrives", async () => {
108 vi.useFakeTimers();
109 vi.stubGlobal("fetch", () => new Promise<Response>(() => {}));
110 const capturePromise = capturePreviewAnnotationScreenshot(annotation, 1_000);
111 await vi.advanceTimersByTimeAsync(1_000);
112 expect(await capturePromise).toEqual({ status: "failed" });
113 });
114​
115 it("fails when the crop fetch throws", async () => {
116 vi.stubGlobal("fetch", async () => {
117 throw new Error("data url unreadable");
118 });
119 expect(await capturePreviewAnnotationScreenshot(annotation)).toEqual({ status: "failed" });
120 });
121});