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

composerContextUndo.test.ts

156 lines4.9 KB

1import {
2 ChatAttachmentId,
3 ComposerContextId,
4 type PreviewAnnotationPayload,
5} from "@t3tools/contracts";
6import { describe, expect, it } from "vite-plus/test";
7​
8import type { ComposerImageAttachment } from "../../composerDraftStore";
9import { buildMessageContext, previewAnnotationContextId } from "../../lib/composerContextRecords";
10import {
11 reconcileAttachmentContextReferences,
12 type RetainedAttachmentContextPayloads,
13} from "./composerContextUndo";
14​
15const binary = new File(["binary"], "context.png", { type: "image/png" });
16const image = {
17 type: "image",
18 id: "annotation-1",
19 name: "annotation.png",
20 mimeType: "image/png",
21 sizeBytes: binary.size,
22 previewUrl: "blob:annotation",
23 file: binary,
24} satisfies ComposerImageAttachment;
25const annotation = {
26 id: "annotation-1",
27 pageUrl: "https://example.com",
28 pageTitle: "Example",
29 comment: "Fix this",
30 elements: [],
31 regions: [],
32 strokes: [],
33 styleChanges: [],
34 screenshot: {
35 dataUrl: "data:image/png;base64,YmluYXJ5",
36 width: 1,
37 height: 1,
38 cropRect: { x: 0, y: 0, width: 1, height: 1 },
39 },
40 createdAt: "2026-01-01T00:00:00.000Z",
41} satisfies PreviewAnnotationPayload;
42​
43function retention(): RetainedAttachmentContextPayloads {
44 return { files: new Map(), previewAnnotations: new Map(), restoredQueuedImageIds: new Set() };
46​
47describe("reconcileAttachmentContextReferences", () => {
48 it("restores an annotation and its screenshot attachment after undo", () => {
49 const retained = retention();
50 const removed = reconcileAttachmentContextReferences({
51 referencedContextIds: new Set(),
52 files: [],
53 images: [image],
54 previewAnnotations: [annotation],
55 retained,
56 });
57 expect(removed.annotationIdsToRemove).toEqual(["annotation-1"]);
58​
59 const restored = reconcileAttachmentContextReferences({
60 referencedContextIds: new Set([previewAnnotationContextId(annotation.id)]),
61 files: [],
62 images: [],
63 previewAnnotations: [],
64 retained,
65 });
66 expect(restored.annotationsToRestore).toEqual([{ annotation, image }]);
67 expect(restored.annotationsToRestore[0]?.annotation.screenshot?.dataUrl).toBe(
68 "data:image/png;base64,YmluYXJ5",
69 );
70 expect(restored.annotationsToRestore[0]?.image?.file).toBe(binary);
71 expect(
72 buildMessageContext({
73 terminalContexts: [],
74 reviewComments: [],
75 previewAnnotations: restored.annotationsToRestore.map((entry) => entry.annotation),
76 attachments: [
77 {
78 attachment: restored.annotationsToRestore[0]!.image!,
79 attachmentId: "uploaded-screenshot",
80 },
81 ],
82 })?.records,
83 ).toEqual([
84 expect.objectContaining({
85 kind: "preview-annotation",
86 screenshotContextId: "image_annotation-1",
87 }),
88 expect.objectContaining({
89 kind: "image",
90 contextId: "image_annotation-1",
91 attachmentId: "uploaded-screenshot",
92 }),
93 ]);
94 });
95​
96 it("drops a queued file with its chip, keeps a queued image, and lets undo and redo restore and remove either", () => {
97 const file = {
98 attachment: {
99 type: "file" as const,
100 id: ChatAttachmentId.make("server-file"),
101 name: "notes.txt",
102 mimeType: "text/plain",
103 sizeBytes: 12,
104 },
105 contextIds: [ComposerContextId.make("file_local")],
106 };
107 const queuedImage = {
108 attachment: {
109 type: "image" as const,
110 id: ChatAttachmentId.make("server-image"),
111 name: "image.png",
112 mimeType: "image/png",
113 sizeBytes: 67,
114 },
115 contextIds: [ComposerContextId.make("image_local")],
116 };
117 const base = { files: [], images: [], previewAnnotations: [], retained: retention() };
118​
119 const deleted = reconcileAttachmentContextReferences({
120 ...base,
121 referencedContextIds: new Set(),
122 queuedAttachments: [file, queuedImage],
123 });
124 expect(deleted.queuedAttachmentIdsToRemove).toEqual([file.attachment.id]);
125​
126 const undone = reconcileAttachmentContextReferences({
127 ...base,
128 referencedContextIds: new Set(["file_local", "image_local"]),
129 removedQueuedAttachments: [file, queuedImage],
130 });
131 expect(undone.queuedAttachmentsToRestore).toEqual([file.attachment, queuedImage.attachment]);
132​
133 const redone = reconcileAttachmentContextReferences({
134 ...base,
135 referencedContextIds: new Set(),
136 queuedAttachments: [file, queuedImage],
137 fromHistory: true,
138 });
139 expect(redone.queuedAttachmentIdsToRemove).toEqual([
140 file.attachment.id,
141 queuedImage.attachment.id,
142 ]);
143​
144 reconcileAttachmentContextReferences({
145 ...base,
146 referencedContextIds: new Set(["image_local"]),
147 removedQueuedAttachments: [queuedImage],
148 });
149 const backspaced = reconcileAttachmentContextReferences({
150 ...base,
151 referencedContextIds: new Set(),
152 queuedAttachments: [queuedImage],
153 });
154 expect(backspaced.queuedAttachmentIdsToRemove).toEqual([]);
155 });
156});