Open History to see the last commit
1import { describe, expect, it } 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 { getComposerDraftSnapshot, setComposerDraftText } from "./use-composer-drafts";
24
25const image = (id: string): ChatAttachment => ({
26 type: "image",
27 id: ChatAttachmentId.make(id),
28 name: `${id}.png`,
29 mimeType: "image/png",
30 sizeBytes: 10,
31});
32
33function edit(attachments: ReadonlyArray<ChatAttachment>): QueuedRunEdit {
34 return {
35 runId: RunId.make("run-1"),
36 messageId: MessageId.make("message-1"),
37 originalText: "original",
38 existingAttachments: attachments,
39 };
40}
41
42function context(attachmentIds: ReadonlyArray<string>): OrchestrationMessageContext {
43 return {
44 version: 1,
45 records: attachmentIds.map((attachmentId, index) => ({
46 version: 1,
47 kind: "image",
48 contextId: ComposerContextId.make(`ctx-${index}`),
49 label: `${attachmentId}.png`,
50 attachmentId,
51 name: `${attachmentId}.png`,
52 mimeType: "image/png",
53 sizeBytes: 10,
54 })),
55 };
56}
57
58describe("resolveQueuedEditPayload", () => {
59 it("resends the kept server attachments ahead of the newly uploaded ones", () => {
60 const payload = resolveQueuedEditPayload({
61 edit: edit([image("kept")]),
62 draftContext: undefined,
63 draftAttachments: [{ id: "draft-1" }],
64 uploaded: [image("uploaded")],
65 });
66
67 expect(payload.attachments.map((attachment) => attachment.id)).toEqual(["kept", "uploaded"]);
68 });
69
70 it("rebinds context records from the draft's ids to the uploaded ones", () => {
71 const payload = resolveQueuedEditPayload({
72 edit: edit([]),
73 draftContext: context(["draft-1"]),
74 draftAttachments: [{ id: "draft-1" }],
75 uploaded: [image("uploaded")],
76 });
77
78 expect(payload.context?.records).toEqual([
79 expect.objectContaining({ attachmentId: "uploaded" }),
80 ]);
81 });
82
83 it("drops the record of an attachment the user removed", () => {
84 const payload = resolveQueuedEditPayload({
85 edit: edit([image("kept")]),
86 draftContext: context(["kept", "gone"]),
87 draftAttachments: [],
88 uploaded: [],
89 });
90
91 expect(payload.attachments.map((attachment) => attachment.id)).toEqual(["kept"]);
92 expect(payload.context?.records).toEqual([expect.objectContaining({ attachmentId: "kept" })]);
93 });
94});
95
96describe("beginQueuedRunEdit", () => {
97 it("ends a changed edit when its run starts, without the thread's composer mounted", () => {
98 const threadKey = "env-exit:thread-exit";
99 const runId = RunId.make("run-1");
100 const runs = Atom.make<ReadonlyArray<{ readonly id: RunId; readonly status: string }> | null>([
101 { id: runId, status: "queued" },
102 ]);
103 setComposerDraftText(threadKey, "other work");
104 beginQueuedRunEdit(threadKey, edit([]), runs);
105 setComposerDraftText(queuedEditDraftKey(threadKey, runId), "changed");
106
107 appAtomRegistry.set(runs, [{ id: runId, status: "running" }]);
108
109 expect(getQueuedRunEdit(threadKey)).toBeNull();
110 expect(getComposerDraftSnapshot(threadKey).text).toBe("other work");
111 expect(appAtomRegistry.get(threadComposerErrorsAtom)[threadKey]?.message).toBe(
112 "That message already started, so the edit was discarded.",
113 );
114 });
115
116 it("reports a cleared edit as discarded when the run starts", () => {
117 const threadKey = "env-exit:thread-cleared";
118 const runId = RunId.make("run-1");
119 const runs = Atom.make<ReadonlyArray<{ readonly id: RunId; readonly status: string }> | null>([
120 { id: runId, status: "queued" },
121 ]);
122 beginQueuedRunEdit(threadKey, edit([]), runs);
123 setComposerDraftText(queuedEditDraftKey(threadKey, runId), "");
124
125 appAtomRegistry.set(runs, [{ id: runId, status: "running" }]);
126
127 expect(getQueuedRunEdit(threadKey)).toBeNull();
128 expect(getComposerDraftSnapshot(threadKey).text).toBe("");
129 expect(appAtomRegistry.get(threadComposerErrorsAtom)[threadKey]?.message).toBe(
130 "That message already started, so the edit was discarded.",
131 );
132 });
133
134 it("treats a removed server attachment as a change when the run starts", () => {
135 const threadKey = "env-exit:thread-removed";
136 const runId = RunId.make("run-1");
137 const runs = Atom.make<ReadonlyArray<{ readonly id: RunId; readonly status: string }> | null>([
138 { id: runId, status: "queued" },
139 ]);
140 setComposerDraftText(threadKey, "other work");
141 beginQueuedRunEdit(threadKey, edit([image("kept")]), runs);
142 removeQueuedRunEditAttachment(threadKey, "kept");
143
144 appAtomRegistry.set(runs, [{ id: runId, status: "running" }]);
145
146 expect(getQueuedRunEdit(threadKey)).toBeNull();
147 expect(appAtomRegistry.get(threadComposerErrorsAtom)[threadKey]?.message).toBe(
148 "That message already started, so the edit was discarded.",
149 );
150 });
151});
152
153describe("activeComposerDraftKey", () => {
154 const threadKey = "env:thread";
155 const editKey = queuedEditDraftKey(threadKey, RunId.make("run-1"));
156
157 it("keeps a queued edit's draft while that edit is open", () => {
158 expect(activeComposerDraftKey(threadKey, editKey, edit([]))).toBe(editKey);
159 });
160
161 it("falls back to the thread's draft once the edit has ended", () => {
162 expect(activeComposerDraftKey(threadKey, editKey, null)).toBe(threadKey);
163 });
164});