Open History to see the last commit
1import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { scopeThreadRef } from "@t3tools/client-runtime/environment";
3import { beforeEach, describe, expect, it } from "vite-plus/test";
4import {
5 DraftId,
6 useComposerDraftStore,
7 type ComposerFileAttachment,
8 type ComposerImageAttachment,
9} from "../../composerDraftStore";
10import { prepareQueuedEditAttachments, recoverQueuedMessageEdit } from "./queuedMessageEdit";
11
12const environmentId = EnvironmentId.make("remote-environment");
13const threadTarget = scopeThreadRef(environmentId, ThreadId.make("thread:edit"));
14const editTarget = DraftId.make("queued-edit:test");
15const file: ComposerFileAttachment = {
16 type: "file",
17 id: "file:report",
18 name: "report.pdf",
19 mimeType: "application/pdf",
20 sizeBytes: 6,
21 file: new File(["report"], "report.pdf", { type: "application/pdf" }),
22};
23const image: ComposerImageAttachment = {
24 type: "image",
25 id: "image:screen",
26 name: "screen.png",
27 mimeType: "image/png",
28 sizeBytes: 5,
29 file: new File(["image"], "screen.png", { type: "image/png" }),
30 previewUrl: "blob:screen",
31};
32const uploadedFile = {
33 type: "file" as const,
34 id: "upload:report",
35 name: file.name,
36 mimeType: file.mimeType,
37 sizeBytes: file.sizeBytes,
38};
39
40describe("queued message file edits", () => {
41 beforeEach(() => {
42 useComposerDraftStore.setState({ draftsByThreadKey: {}, draftThreadsByThreadKey: {} });
43 });
44
45 for (const images of [[], [image]]) {
46 it(`preserves a generic file in a ${images.length === 0 ? "file-only" : "mixed"} save`, async () => {
47 const attachments = await prepareQueuedEditAttachments({
48 existingAttachments: [],
49 images,
50 files: [file],
51 uploadFiles: async () => [uploadedFile],
52 readImage: async () => "data:image/png;base64,aW1hZ2U=",
53 });
54 expect(attachments.at(-1)).toEqual(uploadedFile);
55 expect(attachments.length).toBe(images.length + 1);
56 if (images.length > 0)
57 expect(attachments[0]).toMatchObject({
58 type: "image",
59 dataUrl: "data:image/png;base64,aW1hZ2U=",
60 });
61 });
62 }
63
64 it("retains saved attachments alongside newly uploaded files", async () => {
65 const saved = { ...uploadedFile, id: "saved:earlier" };
66 const attachments = await prepareQueuedEditAttachments({
67 existingAttachments: [saved],
68 images: [],
69 files: [file],
70 uploadFiles: async () => [uploadedFile],
71 readImage: async () => "unused",
72 });
73 expect(attachments).toEqual([saved, uploadedFile]);
74 });
75
76 it("fails a save instead of dropping a file whose upload is missing", async () => {
77 await expect(
78 prepareQueuedEditAttachments({
79 existingAttachments: [],
80 images: [image],
81 files: [file],
82 uploadFiles: async () => [],
83 readImage: async () => "image",
84 }),
85 ).rejects.toThrow("Retry or remove");
86 });
87
88 it("keeps file-only edits when another client starts the queued run", () => {
89 const store = useComposerDraftStore.getState();
90 store.setPrompt(editTarget, "Original message");
91 store.addFiles(editTarget, [file]);
92 expect(
93 recoverQueuedMessageEdit({ editTarget, threadTarget, originalText: "Original message" }),
94 ).toBe("kept");
95 expect(store.getComposerDraft(threadTarget)?.prompt).toBe("Original message");
96 expect(store.getComposerDraft(threadTarget)?.files).toEqual([file]);
97 expect(store.getComposerDraft(editTarget)).toBeNull();
98 });
99
100 it("preserves uploaded file references when a remote queue advance interrupts the edit", () => {
101 const store = useComposerDraftStore.getState();
102 const uploaded = {
103 ...file,
104 file: null,
105 uploadedAttachmentId: uploadedFile.id,
106 uploadEnvironmentId: environmentId,
107 };
108 store.addFiles(editTarget, [uploaded]);
109 expect(recoverQueuedMessageEdit({ editTarget, threadTarget, originalText: "" })).toBe("kept");
110 expect(store.getComposerDraft(threadTarget)?.files).toEqual([uploaded]);
111 });
112
113 it("does not overwrite a separate draft when the queued run leaves", () => {
114 const store = useComposerDraftStore.getState();
115 store.setPrompt(threadTarget, "Separate draft");
116 store.addFiles(editTarget, [file]);
117 expect(recoverQueuedMessageEdit({ editTarget, threadTarget, originalText: "" })).toBe(
118 "discarded",
119 );
120 expect(store.getComposerDraft(threadTarget)?.prompt).toBe("Separate draft");
121 expect(store.getComposerDraft(editTarget)).toBeNull();
122 });
123});