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

queued-run-edit.test.ts

240 lines8.5 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2import { Atom } from "effect/reactivity";
3import {
4 ChatAttachmentId,
5 ComposerContextId,
6 MessageId,
7 RunId,
8 type ChatAttachment,
9 type OrchestrationMessageContext,
10} from "@t3tools/contracts";
11​
12import { appAtomRegistry } from "./atom-registry";
13import {
14 activeComposerDraftKey,
15 beginQueuedRunEdit,
16 getQueuedRunEdit,
17 queuedEditDraftKey,
18 removeQueuedRunEditAttachment,
19 resolveQueuedEditPayload,
20 type QueuedRunEdit,
21} from "./queued-run-edit";
22import { threadComposerErrorsAtom } from "./thread-composer-error";
23import {
24 flushComposerDrafts,
25 getComposerDraftSnapshot,
26 setComposerDraftText,
27 waitForComposerDraftsLoaded,
28} from "./use-composer-drafts";
29​
30vi.mock("expo-file-system", () => ({
31 Directory: class {
32 create() {}
33 },
34 File: class {
35 exists = false;
36 create() {}
37 write() {}
38 moveSync() {}
39 },
40 Paths: { document: { uri: "file:///documents" } },
41}));
42​
43const image = (id: string): ChatAttachment => ({
44 type: "image",
45 id: ChatAttachmentId.make(id),
46 name: `${id}.png`,
47 mimeType: "image/png",
48 sizeBytes: 10,
49});
50​
51function edit(attachments: ReadonlyArray<ChatAttachment>): QueuedRunEdit {
52 return {
53 runId: RunId.make("run-1"),
54 messageId: MessageId.make("message-1"),
55 originalText: "original",
56 existingAttachments: attachments,
57 };
59​
60function context(attachmentIds: ReadonlyArray<string>): OrchestrationMessageContext {
61 return {
62 version: 1,
63 records: attachmentIds.map((attachmentId, index) => ({
64 version: 1,
65 kind: "image",
66 contextId: ComposerContextId.make(`ctx-${index}`),
67 label: `${attachmentId}.png`,
68 attachmentId,
69 name: `${attachmentId}.png`,
70 mimeType: "image/png",
71 sizeBytes: 10,
72 })),
73 };
75​
76describe("resolveQueuedEditPayload", () => {
77 it("resends the kept server attachments ahead of the newly uploaded ones", () => {
78 const payload = resolveQueuedEditPayload({
79 edit: edit([image("kept")]),
80 draftContext: undefined,
81 draftAttachments: [{ id: "draft-1" }],
82 uploaded: [image("uploaded")],
83 });
84​
85 expect(payload.attachments.map((attachment) => attachment.id)).toEqual(["kept", "uploaded"]);
86 });
87​
88 it("rebinds context records from the draft's ids to the uploaded ones", () => {
89 const payload = resolveQueuedEditPayload({
90 edit: edit([]),
91 draftContext: context(["draft-1"]),
92 draftAttachments: [{ id: "draft-1" }],
93 uploaded: [image("uploaded")],
94 });
95​
96 expect(payload.context?.records).toEqual([
97 expect.objectContaining({ attachmentId: "uploaded" }),
98 ]);
99 });
100​
101 it("drops the record of an attachment the user removed", () => {
102 const payload = resolveQueuedEditPayload({
103 edit: edit([image("kept")]),
104 draftContext: context(["kept", "gone"]),
105 draftAttachments: [],
106 uploaded: [],
107 });
108​
109 expect(payload.attachments.map((attachment) => attachment.id)).toEqual(["kept"]);
110 expect(payload.context?.records).toEqual([expect.objectContaining({ attachmentId: "kept" })]);
111 });
112});
113​
114describe("beginQueuedRunEdit", () => {
115 it("silently drops an untouched edit when its run starts", () => {
116 const threadKey = "env-exit:thread-untouched";
117 const runId = RunId.make("run-1");
118 const runs = Atom.make<ReadonlyArray<{ readonly id: RunId; readonly status: string }> | null>([
119 { id: runId, status: "queued" },
120 ]);
121 beginQueuedRunEdit(threadKey, edit([]), runs);
122​
123 appAtomRegistry.set(runs, [{ id: runId, status: "running" }]);
124​
125 expect(getQueuedRunEdit(threadKey)).toBeNull();
126 expect(getComposerDraftSnapshot(threadKey).text).toBe("");
127 expect(getComposerDraftSnapshot(queuedEditDraftKey(threadKey, runId)).text).toBe("");
128 expect(appAtomRegistry.get(threadComposerErrorsAtom)[threadKey]?.message).toBeUndefined();
129 });
130​
131 it("recovers text after a server attachment was removed", async () => {
132 await waitForComposerDraftsLoaded();
133 const threadKey = "env-exit:thread-removed-recover";
134 const runId = RunId.make("run-1");
135 const runs = Atom.make<ReadonlyArray<{ readonly id: RunId; readonly status: string }> | null>([
136 { id: runId, status: "queued" },
137 ]);
138 beginQueuedRunEdit(threadKey, edit([image("removed")]), runs);
139 removeQueuedRunEditAttachment(threadKey, "removed");
140​
141 appAtomRegistry.set(runs, [{ id: runId, status: "running" }]);
142 await flushComposerDrafts();
143​
144 expect(getQueuedRunEdit(threadKey)).toBeNull();
145 expect(getComposerDraftSnapshot(threadKey).text).toBe("original");
146 expect(getComposerDraftSnapshot(queuedEditDraftKey(threadKey, runId)).text).toBe("");
147 expect(appAtomRegistry.get(threadComposerErrorsAtom)[threadKey]?.message).toBe(
148 "That message already started. Your edit is back in the composer.",
149 );
150 });
151​
152 it("recovers changed text into an empty thread draft", async () => {
153 await waitForComposerDraftsLoaded();
154 const threadKey = "env-exit:thread-changed-recover";
155 const runId = RunId.make("run-1");
156 const runs = Atom.make<ReadonlyArray<{ readonly id: RunId; readonly status: string }> | null>([
157 { id: runId, status: "queued" },
158 ]);
159 beginQueuedRunEdit(threadKey, edit([]), runs);
160 setComposerDraftText(queuedEditDraftKey(threadKey, runId), "revised");
161​
162 appAtomRegistry.set(runs, [{ id: runId, status: "running" }]);
163 await flushComposerDrafts();
164​
165 expect(getQueuedRunEdit(threadKey)).toBeNull();
166 expect(getComposerDraftSnapshot(threadKey).text).toBe("revised");
167 expect(getComposerDraftSnapshot(queuedEditDraftKey(threadKey, runId)).text).toBe("");
168 expect(appAtomRegistry.get(threadComposerErrorsAtom)[threadKey]?.message).toBe(
169 "That message already started. Your edit is back in the composer.",
170 );
171 });
172​
173 it("ends a changed edit when its run starts, without the thread's composer mounted", () => {
174 const threadKey = "env-exit:thread-exit";
175 const runId = RunId.make("run-1");
176 const runs = Atom.make<ReadonlyArray<{ readonly id: RunId; readonly status: string }> | null>([
177 { id: runId, status: "queued" },
178 ]);
179 setComposerDraftText(threadKey, "other work");
180 beginQueuedRunEdit(threadKey, edit([]), runs);
181 setComposerDraftText(queuedEditDraftKey(threadKey, runId), "changed");
182​
183 appAtomRegistry.set(runs, [{ id: runId, status: "running" }]);
184​
185 expect(getQueuedRunEdit(threadKey)).toBeNull();
186 expect(getComposerDraftSnapshot(threadKey).text).toBe("other work");
187 expect(appAtomRegistry.get(threadComposerErrorsAtom)[threadKey]?.message).toBe(
188 "That message already started, so the edit was discarded.",
189 );
190 });
191​
192 it("reports a cleared edit as discarded when the run starts", () => {
193 const threadKey = "env-exit:thread-cleared";
194 const runId = RunId.make("run-1");
195 const runs = Atom.make<ReadonlyArray<{ readonly id: RunId; readonly status: string }> | null>([
196 { id: runId, status: "queued" },
197 ]);
198 beginQueuedRunEdit(threadKey, edit([]), runs);
199 setComposerDraftText(queuedEditDraftKey(threadKey, runId), "");
200​
201 appAtomRegistry.set(runs, [{ id: runId, status: "running" }]);
202​
203 expect(getQueuedRunEdit(threadKey)).toBeNull();
204 expect(getComposerDraftSnapshot(threadKey).text).toBe("");
205 expect(appAtomRegistry.get(threadComposerErrorsAtom)[threadKey]?.message).toBe(
206 "That message already started, so the edit was discarded.",
207 );
208 });
209​
210 it("treats a removed server attachment as a change when the run starts", () => {
211 const threadKey = "env-exit:thread-removed";
212 const runId = RunId.make("run-1");
213 const runs = Atom.make<ReadonlyArray<{ readonly id: RunId; readonly status: string }> | null>([
214 { id: runId, status: "queued" },
215 ]);
216 setComposerDraftText(threadKey, "other work");
217 beginQueuedRunEdit(threadKey, edit([image("kept")]), runs);
218 removeQueuedRunEditAttachment(threadKey, "kept");
219​
220 appAtomRegistry.set(runs, [{ id: runId, status: "running" }]);
221​
222 expect(getQueuedRunEdit(threadKey)).toBeNull();
223 expect(appAtomRegistry.get(threadComposerErrorsAtom)[threadKey]?.message).toBe(
224 "That message already started, so the edit was discarded.",
225 );
226 });
227});
228​
229describe("activeComposerDraftKey", () => {
230 const threadKey = "env:thread";
231 const editKey = queuedEditDraftKey(threadKey, RunId.make("run-1"));
232​
233 it("keeps a queued edit's draft while that edit is open", () => {
234 expect(activeComposerDraftKey(threadKey, editKey, edit([]))).toBe(editKey);
235 });
236​
237 it("falls back to the thread's draft once the edit has ended", () => {
238 expect(activeComposerDraftKey(threadKey, editKey, null)).toBe(threadKey);
239 });
240});