Open History to see the last commit
1import { ThreadId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import { buildMessageContext } from "~/lib/composerContextRecords";
5import {
6 composerContextRecordsFromDraft,
7 uploadedContextRecordFromDraft,
8} from "./composerContextPresentation";
9
10describe("composerContextRecordsFromDraft", () => {
11 it("pairs a picked element with its crop, which the draft keeps as an image", () => {
12 const annotation = {
13 id: "pick-1",
14 pageUrl: "http://localhost:3000/",
15 pageTitle: "Example",
16 comment: "",
17 elements: [],
18 regions: [],
19 strokes: [],
20 styleChanges: [],
21 screenshot: {
22 dataUrl: "",
23 width: 10,
24 height: 10,
25 cropRect: { x: 0, y: 0, width: 10, height: 10 },
26 },
27 createdAt: "2026-10-08T00:00:00.000Z",
28 };
29 const crop = {
30 type: "image" as const,
31 id: "pick-1",
32 name: "preview-annotation-pick-1.png",
33 mimeType: "image/png",
34 sizeBytes: 1,
35 previewUrl: "blob:crop",
36 file: new File(["x"], "preview-annotation-pick-1.png"),
37 };
38
39 expect(
40 composerContextRecordsFromDraft({
41 terminalContexts: [],
42 previewAnnotations: [annotation],
43 images: [crop],
44 }).get("preview-annotation_pick-1"),
45 ).toMatchObject({ kind: "preview-annotation", screenshot: { previewUrl: "blob:crop" } });
46 });
47
48 it("recovers the uploaded record when clipboard data points at an attachment already in the draft", () => {
49 const file = {
50 type: "file" as const,
51 id: "file-1",
52 name: "file.txt",
53 mimeType: "text/plain",
54 sizeBytes: 4,
55 file: new File(["test"], "file.txt"),
56 uploadedAttachmentId: "attachment-1",
57 };
58 const draftRecord = composerContextRecordsFromDraft({
59 terminalContexts: [],
60 files: [file],
61 }).get("file_file-1");
62
63 expect(draftRecord && uploadedContextRecordFromDraft(draftRecord)).toMatchObject({
64 kind: "file",
65 contextId: "file_file-1",
66 attachmentId: "attachment-1",
67 });
68 });
69
70 it("resolves each wire reference to its own backing draft even when producer ids collide", () => {
71 const id = "same.id:1";
72 const terminal = {
73 id,
74 threadId: ThreadId.make("t1"),
75 terminalId: "default",
76 terminalLabel: "Terminal",
77 lineStart: 1,
78 lineEnd: 1,
79 text: "output",
80 createdAt: "2026-01-01T00:00:00.000Z",
81 };
82 const image = {
83 type: "image" as const,
84 id,
85 name: "shot.png",
86 mimeType: "image/png",
87 sizeBytes: 1,
88 file: new File(["x"], "shot.png"),
89 previewUrl: "blob:shot",
90 };
91 const file = {
92 type: "file" as const,
93 id,
94 name: "file.txt",
95 mimeType: "text/plain",
96 sizeBytes: 1,
97 file: null,
98 };
99 const draftRecords = composerContextRecordsFromDraft({
100 terminalContexts: [terminal],
101 images: [image],
102 files: [file],
103 });
104 const message = buildMessageContext({
105 terminalContexts: [terminal],
106 reviewComments: [],
107 previewAnnotations: [],
108 attachments: [
109 { attachment: image, attachmentId: "uploaded-image" },
110 { attachment: file, attachmentId: "uploaded-file" },
111 ],
112 })!;
113 expect(draftRecords.size).toBe(3);
114 for (const record of message.records) {
115 expect(draftRecords.get(record.contextId)?.kind).toBe(record.kind);
116 }
117 });
118});