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