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

composerContextPresentation.test.ts

148 lines4.3 KB

1import { ChatAttachmentId, ComposerContextId, EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import { composerFileNeedsReattach } from "~/composerDraftStore";
5import {
6 attachmentContextRecord,
7 buildMessageContext,
8 isSameComposerContextPayload,
9} from "~/lib/composerContextRecords";
10import {
11 composerContextRecordsFromDraft,
12 uploadedContextRecordFromDraft,
13} from "./composerContextPresentation";
14​
15describe("composerContextRecordsFromDraft", () => {
16 it("resolves each wire reference to its own backing draft even when producer ids collide", () => {
17 const id = "same.id:1";
18 const terminal = {
19 id,
20 threadId: ThreadId.make("t1"),
21 terminalId: "default",
22 terminalLabel: "Terminal",
23 lineStart: 1,
24 lineEnd: 1,
25 text: "output",
26 createdAt: "2026-01-01T00:00:00.000Z",
27 };
28 const image = {
29 type: "image" as const,
30 id,
31 name: "shot.png",
32 mimeType: "image/png",
33 sizeBytes: 1,
34 file: new File(["x"], "shot.png"),
35 previewUrl: "blob:shot",
36 };
37 const file = {
38 type: "file" as const,
39 id,
40 name: "file.txt",
41 mimeType: "text/plain",
42 sizeBytes: 1,
43 file: null,
44 };
45 const draftRecords = composerContextRecordsFromDraft({
46 terminalContexts: [terminal],
47 images: [image],
48 files: [file],
49 });
50 const message = buildMessageContext({
51 terminalContexts: [terminal],
52 reviewComments: [],
53 previewAnnotations: [],
54 attachments: [
55 { attachment: image, attachmentId: "uploaded-image" },
56 { attachment: file, attachmentId: "uploaded-file" },
57 ],
58 })!;
59 expect(draftRecords.size).toBe(3);
60 for (const record of message.records) {
61 expect(draftRecords.get(record.contextId)?.kind).toBe(record.kind);
62 }
63 });
64​
65 it("resolves existing queued attachments under their original wire context ids", () => {
66 const environmentId = EnvironmentId.make("env-1");
67 const imageContextId = ComposerContextId.make("image_local-shot");
68 const fileContextId = ComposerContextId.make("file_local-notes");
69 const imageId = ChatAttachmentId.make("server-shot");
70 const fileId = ChatAttachmentId.make("server-notes");
71 const source = {
72 kind: "snap-shot" as const,
73 capturedAt: "2026-11-22T14:00:00.000Z",
74 appName: "Safari",
75 windowTitle: "Example",
76 };
77 const imageAttachment = {
78 type: "image" as const,
79 id: imageId,
80 name: "image.png",
81 mimeType: "image/png",
82 sizeBytes: 67,
83 source,
84 };
85 const draftRecords = composerContextRecordsFromDraft({
86 terminalContexts: [],
87 environmentId,
88 existingAttachments: [
89 {
90 attachment: imageAttachment,
91 url: "http://127.0.0.1/assets/server-shot",
92 contextIds: [imageContextId],
93 },
94 {
95 attachment: {
96 type: "file",
97 id: fileId,
98 name: "notes.txt",
99 mimeType: "text/plain",
100 sizeBytes: 12,
101 },
102 url: null,
103 contextIds: [fileContextId],
104 },
105 ],
106 });
107​
108 const imageEntry = draftRecords.get(imageContextId);
109 expect(imageEntry).toMatchObject({
110 kind: "image",
111 record: {
112 id: imageId,
113 name: "image.png",
114 previewUrl: "http://127.0.0.1/assets/server-shot",
115 source,
116 },
117 });
118 const uploadedImageRecord = uploadedContextRecordFromDraft(imageEntry!);
119 expect(uploadedImageRecord).toMatchObject({
120 kind: "image",
121 contextId: imageContextId,
122 attachmentId: imageId,
123 source,
124 });
125 expect(
126 isSameComposerContextPayload(
127 uploadedImageRecord!,
128 attachmentContextRecord({
129 attachment: imageAttachment,
130 attachmentId: imageId,
131 contextId: imageContextId,
132 }),
133 ),
134 ).toBe(true);
135​
136 const fileEntry = draftRecords.get(fileContextId);
137 expect(fileEntry?.kind).toBe("file");
138 if (fileEntry?.kind === "file") {
139 expect(composerFileNeedsReattach(fileEntry.record)).toBe(false);
140 expect(fileEntry.record.uploadEnvironmentId).toBe(environmentId);
141 }
142 expect(uploadedContextRecordFromDraft(fileEntry!)).toMatchObject({
143 kind: "file",
144 contextId: fileContextId,
145 attachmentId: fileId,
146 });
147 });
148});