The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { PreviewAnnotationPayload } from "@t3tools/contracts"; |
| 2 | import { describe, expect, it } from "vite-plus/test"; |
| 3 | |
| 4 | import type { ComposerImageAttachment } from "../../composerDraftStore"; |
| 5 | import { buildMessageContext, previewAnnotationContextId } from "../../lib/composerContextRecords"; |
| 6 | import { |
| 7 | reconcileAttachmentContextReferences, |
| 8 | type RetainedAttachmentContextPayloads, |
| 9 | } from "./composerContextUndo"; |
| 10 | |
| 11 | const binary = new File(["binary"], "context.png", { type: "image/png" }); |
| 12 | const image = { |
| 13 | type: "image", |
| 14 | id: "annotation-1", |
| 15 | name: "annotation.png", |
| 16 | mimeType: "image/png", |
| 17 | sizeBytes: binary.size, |
| 18 | previewUrl: "blob:annotation", |
| 19 | file: binary, |
| 20 | } satisfies ComposerImageAttachment; |
| 21 | const annotation = { |
| 22 | id: "annotation-1", |
| 23 | pageUrl: "https://example.com", |
| 24 | pageTitle: "Example", |
| 25 | comment: "Fix this", |
| 26 | elements: [], |
| 27 | regions: [], |
| 28 | strokes: [], |
| 29 | styleChanges: [], |
| 30 | screenshot: { |
| 31 | dataUrl: "data:image/png;base64,YmluYXJ5", |
| 32 | width: 1, |
| 33 | height: 1, |
| 34 | cropRect: { x: 0, y: 0, width: 1, height: 1 }, |
| 35 | }, |
| 36 | createdAt: "2026-01-01T00:00:00.000Z", |
| 37 | } satisfies PreviewAnnotationPayload; |
| 38 | |
| 39 | function retention(): RetainedAttachmentContextPayloads { |
| 40 | return { files: new Map(), previewAnnotations: new Map() }; |
| 41 | } |
| 42 | |
| 43 | describe("reconcileAttachmentContextReferences", () => { |
| 44 | it("restores an annotation and its screenshot attachment after undo", () => { |
| 45 | const retained = retention(); |
| 46 | const removed = reconcileAttachmentContextReferences({ |
| 47 | referencedContextIds: new Set(), |
| 48 | files: [], |
| 49 | images: [image], |
| 50 | previewAnnotations: [annotation], |
| 51 | retained, |
| 52 | }); |
| 53 | expect(removed.annotationIdsToRemove).toEqual(["annotation-1"]); |
| 54 | |
| 55 | const restored = reconcileAttachmentContextReferences({ |
| 56 | referencedContextIds: new Set([previewAnnotationContextId(annotation.id)]), |
| 57 | files: [], |
| 58 | images: [], |
| 59 | previewAnnotations: [], |
| 60 | retained, |
| 61 | }); |
| 62 | expect(restored.annotationsToRestore).toEqual([{ annotation, image }]); |
| 63 | expect(restored.annotationsToRestore[0]?.annotation.screenshot?.dataUrl).toBe( |
| 64 | "data:image/png;base64,YmluYXJ5", |
| 65 | ); |
| 66 | expect(restored.annotationsToRestore[0]?.image?.file).toBe(binary); |
| 67 | expect( |
| 68 | buildMessageContext({ |
| 69 | terminalContexts: [], |
| 70 | reviewComments: [], |
| 71 | previewAnnotations: restored.annotationsToRestore.map((entry) => entry.annotation), |
| 72 | attachments: [ |
| 73 | { |
| 74 | attachment: restored.annotationsToRestore[0]!.image!, |
| 75 | attachmentId: "uploaded-screenshot", |
| 76 | }, |
| 77 | ], |
| 78 | })?.records, |
| 79 | ).toEqual([ |
| 80 | expect.objectContaining({ |
| 81 | kind: "preview-annotation", |
| 82 | screenshotContextId: "image_annotation-1", |
| 83 | }), |
| 84 | expect.objectContaining({ |
| 85 | kind: "image", |
| 86 | contextId: "image_annotation-1", |
| 87 | attachmentId: "uploaded-screenshot", |
| 88 | }), |
| 89 | ]); |
| 90 | }); |
| 91 | }); |