Open History to see the last commit
1import { EnvironmentId, MessageId, ThreadId, TurnItemId } from "@t3tools/contracts";
2import { scopeThreadRef } from "@t3tools/client-runtime/environment";
3import { threadContextRecord } from "@t3tools/shared/composerContextReferences";
4import { beforeEach, describe, expect, it } from "vite-plus/test";
5import {
6 DraftId,
7 useComposerDraftStore,
8 type ComposerFileAttachment,
9 type ComposerImageAttachment,
10} from "../../composerDraftStore";
11import { environmentContextRecord, responseContextRecord } from "../../lib/composerContextRecords";
12import { prepareQueuedEditAttachments, recoverQueuedMessageEdit } from "./queuedMessageEdit";
13
14const environmentId = EnvironmentId.make("remote-environment");
15const threadTarget = scopeThreadRef(environmentId, ThreadId.make("thread:edit"));
16const editTarget = DraftId.make("queued-edit:test");
17const file: ComposerFileAttachment = {
18 type: "file",
19 id: "file:report",
20 name: "report.pdf",
21 mimeType: "application/pdf",
22 sizeBytes: 6,
23 file: new File(["report"], "report.pdf", { type: "application/pdf" }),
24};
25const image: ComposerImageAttachment = {
26 type: "image",
27 id: "image:screen",
28 name: "screen.png",
29 mimeType: "image/png",
30 sizeBytes: 5,
31 file: new File(["image"], "screen.png", { type: "image/png" }),
32 previewUrl: "blob:screen",
33};
34const uploadedFile = {
35 type: "file" as const,
36 id: "upload:report",
37 name: file.name,
38 mimeType: file.mimeType,
39 sizeBytes: file.sizeBytes,
40};
41
42describe("queued message file edits", () => {
43 beforeEach(() => {
44 useComposerDraftStore.setState({ draftsByThreadKey: {}, draftThreadsByThreadKey: {} });
45 });
46
47 it.each([
48 { images: [], label: "file-only" },
49 { images: [image], label: "mixed" },
50 ])("preserves a generic file in a $label save", async ({ images }) => {
51 const attachments = await prepareQueuedEditAttachments({
52 existingAttachments: [],
53 images,
54 files: [file],
55 uploadFiles: async () => [uploadedFile],
56 readImage: async () => "data:image/png;base64,aW1hZ2U=",
57 });
58 expect(attachments.at(-1)).toEqual(uploadedFile);
59 expect(attachments.length).toBe(images.length + 1);
60 if (images.length > 0)
61 expect(attachments[0]).toMatchObject({
62 type: "image",
63 dataUrl: "data:image/png;base64,aW1hZ2U=",
64 });
65 });
66
67 it("fails a save instead of dropping a file whose upload is missing", async () => {
68 await expect(
69 prepareQueuedEditAttachments({
70 existingAttachments: [],
71 images: [image],
72 files: [file],
73 uploadFiles: async () => [],
74 readImage: async () => "image",
75 }),
76 ).rejects.toThrow("Retry or remove");
77 });
78
79 it("keeps file-only edits when another client starts the queued run", () => {
80 const store = useComposerDraftStore.getState();
81 store.setPrompt(editTarget, "Original message");
82 const elsewhere: ComposerFileAttachment = {
83 ...uploadedFile,
84 id: "file:elsewhere",
85 name: "elsewhere.pdf",
86 file: null,
87 uploadedAttachmentId: "upload:elsewhere",
88 uploadEnvironmentId: EnvironmentId.make("other-environment"),
89 };
90 store.addFiles(editTarget, [file, elsewhere]);
91 expect(
92 recoverQueuedMessageEdit({ editTarget, threadTarget, originalText: "Original message" }),
93 ).toBe("kept");
94 expect(store.getComposerDraft(threadTarget)?.prompt).toBe("Original message");
95 // An upload this environment cannot reach comes back as a file to reattach, not a dropped chip.
96 const { uploadedAttachmentId: _id, uploadEnvironmentId: _env, ...reattach } = elsewhere;
97 expect(store.getComposerDraft(threadTarget)?.files).toEqual([file, reattach]);
98 expect(store.getComposerDraft(editTarget)).toBeNull();
99 });
100
101 it("keeps the mentions and reply of a kept edit in the thread draft", () => {
102 const store = useComposerDraftStore.getState();
103 store.setPrompt(editTarget, "Edited message");
104 const thread = threadContextRecord(
105 scopeThreadRef(environmentId, ThreadId.make("thread:other")),
106 "Other",
107 );
108 const machine = environmentContextRecord(EnvironmentId.make("environment-vps"), "vps");
109 const reply = responseContextRecord({
110 citation: {
111 version: 1,
112 environmentId,
113 threadId: ThreadId.make("thread:edit"),
114 messageId: MessageId.make("message:answer"),
115 text: "Done.",
116 start: 0,
117 end: 5,
118 prefix: "",
119 suffix: "",
120 },
121 itemId: TurnItemId.make("item:answer"),
122 subagents: [],
123 });
124 store.addThreadContexts(editTarget, [thread]);
125 store.addEnvironmentContexts(editTarget, [machine]);
126 store.setReplyTo(editTarget, reply);
127 const editedPrompt = store.getComposerDraft(editTarget)!.prompt;
128 expect(
129 recoverQueuedMessageEdit({ editTarget, threadTarget, originalText: "Original message" }),
130 ).toBe("kept");
131 expect(store.getComposerDraft(editTarget)).toBeNull();
132 expect(store.getComposerDraft(threadTarget)).toMatchObject({
133 prompt: editedPrompt,
134 threadContexts: [thread],
135 environmentContexts: [machine],
136 replyTo: reply,
137 });
138 });
139
140 it("reports a removed queued attachment as an edit when the run leaves", () => {
141 const store = useComposerDraftStore.getState();
142 store.setPrompt(editTarget, "Original message");
143 expect(
144 recoverQueuedMessageEdit({
145 editTarget,
146 threadTarget,
147 originalText: "Original message",
148 removedExistingAttachment: true,
149 }),
150 ).toBe("kept");
151 expect(store.getComposerDraft(threadTarget)?.prompt).toBe("Original message");
152 store.clearComposerContent(threadTarget);
153 // An attachment-only message leaves nothing to put back in the composer.
154 expect(
155 recoverQueuedMessageEdit({
156 editTarget,
157 threadTarget,
158 originalText: "",
159 removedExistingAttachment: true,
160 }),
161 ).toBe("discarded");
162 expect(store.getComposerDraft(threadTarget)).toBeNull();
163 });
164
165 it("does not overwrite a separate draft when the queued run leaves", () => {
166 const store = useComposerDraftStore.getState();
167 store.setPrompt(threadTarget, "Separate draft");
168 store.addFiles(editTarget, [file]);
169 expect(recoverQueuedMessageEdit({ editTarget, threadTarget, originalText: "" })).toBe(
170 "discarded",
171 );
172 expect(store.getComposerDraft(threadTarget)?.prompt).toBe("Separate draft");
173 expect(store.getComposerDraft(editTarget)).toBeNull();
174 });
175});