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

previewAnnotation.test.ts

74 lines2.3 KB

1import type { PreviewAnnotationPayload } from "@t3tools/contracts";
2import { afterEach, describe, expect, it, vi } from "vite-plus/test";
3​
4import { capturePreviewAnnotationScreenshot } from "./previewAnnotation";
5​
6const annotation: PreviewAnnotationPayload = {
7 id: "annotation_1",
8 pageUrl: "http://localhost:3000",
9 pageTitle: "Example",
10 comment: "Make these cards feel related.",
11 elements: [],
12 regions: [{ id: "region_1", rect: { x: 10, y: 20, width: 100, height: 80 } }],
13 strokes: [
14 {
15 id: "stroke_1",
16 color: "#7c3aed",
17 width: 4,
18 points: [
19 { x: 10, y: 10 },
20 { x: 20, y: 20 },
21 ],
22 bounds: { x: 6, y: 6, width: 18, height: 18 },
23 },
24 ],
25 styleChanges: [
26 {
27 targetId: "element_1",
28 selector: ".card",
29 property: "border-radius",
30 previousValue: "4px",
31 value: "16px",
32 },
33 ],
34 screenshot: {
35 dataUrl: "data:image/png;base64,AA==",
36 width: 100,
37 height: 80,
38 cropRect: { x: 10, y: 20, width: 100, height: 80 },
39 },
40 createdAt: "2026-06-11T00:00:00.000Z",
41};
42​
43describe("preview annotation capture", () => {
44 afterEach(() => {
45 vi.useRealTimers();
46 vi.unstubAllGlobals();
47 });
48​
49 it("returns the crop when the fetch resolves", async () => {
50 vi.stubGlobal("fetch", async () => new Response(new Blob(["png"], { type: "image/png" })));
51 const capture = await capturePreviewAnnotationScreenshot(annotation);
52 expect(capture.status).toBe("captured");
53 });
54​
55 it("reports none when the annotation carries no crop", async () => {
56 const capture = await capturePreviewAnnotationScreenshot({ ...annotation, screenshot: null });
57 expect(capture).toEqual({ status: "none" });
58 });
59​
60 it("fails instead of hanging when the crop never arrives", async () => {
61 vi.useFakeTimers();
62 vi.stubGlobal("fetch", () => new Promise<Response>(() => {}));
63 const capturePromise = capturePreviewAnnotationScreenshot(annotation, 1_000);
64 await vi.advanceTimersByTimeAsync(1_000);
65 expect(await capturePromise).toEqual({ status: "failed" });
66 });
67​
68 it("fails when the crop fetch throws", async () => {
69 vi.stubGlobal("fetch", async () => {
70 throw new Error("data url unreadable");
71 });
72 expect(await capturePreviewAnnotationScreenshot(annotation)).toEqual({ status: "failed" });
73 });
74});