Open History to see the last commit
1import { PROVIDER_SEND_TURN_MAX_ATTACHMENTS } from "@t3tools/contracts";
2import { describe, expect, it, vi } from "vite-plus/test";
3
4import { DraftId, useComposerDraftStore } from "../../composerDraftStore";
5import {
6 importComposerThreadAttachment,
7 remainingComposerAttachmentSlots,
8} from "./composerThreadImport";
9
10function deferredFile() {
11 let resolve!: (file: File) => void;
12 let reject!: (error: Error) => void;
13 const promise = new Promise<File>((resolveFile, rejectFile) => {
14 resolve = resolveFile;
15 reject = rejectFile;
16 });
17 return { promise, resolve, reject };
18}
19
20describe("importComposerThreadAttachment", () => {
21 it("rejects the next drop before loading when the draft takes the final slot", async () => {
22 const targetKey = DraftId.make("thread-import-synchronous-admission");
23 const store = useComposerDraftStore.getState();
24 const pendingImports = new Map<string, number>();
25 const files = Array.from({ length: PROVIDER_SEND_TURN_MAX_ATTACHMENTS - 1 }, (_, index) => ({
26 type: "file" as const,
27 id: `existing-${index}`,
28 name: `existing-${index}.txt`,
29 mimeType: "text/plain",
30 sizeBytes: 1,
31 file: new File(["x"], `existing-${index}.txt`, { type: "text/plain" }),
32 }));
33 store.addFiles(targetKey, files);
34 const load = vi.fn(async () => new File(["transcript"], "thread.jsonl"));
35 const onLimitReached = vi.fn();
36 const input = {
37 targetKey,
38 pendingImports,
39 countReservedAttachments: () =>
40 (store.getComposerDraft(targetKey)?.files.length ?? 0) +
41 (pendingImports.get(targetKey) ?? 0),
42 load,
43 isActive: () => true,
44 attach: async (file: File) =>
45 store.addFiles(targetKey, [
46 {
47 type: "file",
48 id: "transcript",
49 name: file.name,
50 mimeType: file.type,
51 sizeBytes: file.size,
52 file,
53 },
54 ]).length > 0,
55 onLimitReached,
56 };
57
58 await importComposerThreadAttachment(input);
59 await importComposerThreadAttachment(input);
60
61 expect(load).toHaveBeenCalledTimes(1);
62 expect(onLimitReached).toHaveBeenCalledTimes(1);
63 expect(store.getComposerDraft(targetKey)?.files.at(-1)?.name).toBe("thread.jsonl");
64 expect(pendingImports.size).toBe(0);
65 store.clearComposerContent(targetKey);
66 });
67
68 it.each(["file", "image"])(
69 "reserves the final slot while restoring a stashed %s",
70 async (kind) => {
71 const pendingImports = new Map<string, number>();
72 const targetKey = "draft";
73 const attached = Array.from(
74 { length: PROVIDER_SEND_TURN_MAX_ATTACHMENTS - 1 },
75 (_, index) => `existing-${index}`,
76 );
77 const countReservedAttachments = () => attached.length + (pendingImports.get(targetKey) ?? 0);
78 const attach = async (file: File) => {
79 if (
80 remainingComposerAttachmentSlots(attached.length, pendingImports.get(targetKey) ?? 0) ===
81 0
82 ) {
83 return false;
84 }
85 attached.push(file.name);
86 return true;
87 };
88 const transcript = deferredFile();
89 const importing = importComposerThreadAttachment({
90 targetKey,
91 pendingImports,
92 countReservedAttachments,
93 load: () => transcript.promise,
94 isActive: () => true,
95 attach,
96 onLimitReached: () => {
97 throw new Error("The final slot should be available");
98 },
99 });
100
101 expect(await attach(new File(["unrelated"], `unrelated.${kind}`))).toBe(false);
102 transcript.resolve(new File(["transcript"], "thread.txt"));
103 await importing;
104
105 expect(attached.at(-1)).toBe("thread.txt");
106 expect(attached).toHaveLength(PROVIDER_SEND_TURN_MAX_ATTACHMENTS);
107 expect(pendingImports.size).toBe(0);
108 },
109 );
110
111 it("rejects a repeated drop until the reserved slot is released", async () => {
112 const pendingImports = new Map<string, number>();
113 const transcript = deferredFile();
114 const load = vi.fn(() => transcript.promise);
115 const onLimitReached = vi.fn();
116 const attach = vi.fn(async (_file: File) => true);
117 const input = {
118 targetKey: "draft",
119 pendingImports,
120 countReservedAttachments: () =>
121 PROVIDER_SEND_TURN_MAX_ATTACHMENTS - 1 + (pendingImports.get("draft") ?? 0),
122 load,
123 isActive: () => true,
124 attach,
125 onLimitReached,
126 };
127 const first = importComposerThreadAttachment(input);
128 await importComposerThreadAttachment(input);
129 expect(load).toHaveBeenCalledTimes(1);
130 expect(onLimitReached).toHaveBeenCalledTimes(1);
131
132 transcript.resolve(new File(["transcript"], "thread.txt"));
133 await first;
134 await importComposerThreadAttachment(input);
135 expect(attach).toHaveBeenCalledTimes(2);
136 expect(pendingImports.size).toBe(0);
137 });
138
139 it("keeps the second drop's slot reserved when the first drop finishes", async () => {
140 const pendingImports = new Map<string, number>();
141 const first = deferredFile();
142 const second = deferredFile();
143 const attached: string[] = [];
144 const countReservedAttachments = () =>
145 PROVIDER_SEND_TURN_MAX_ATTACHMENTS - 2 + attached.length + (pendingImports.get("draft") ?? 0);
146 const attach = async (file: File) => {
147 if (countReservedAttachments() >= PROVIDER_SEND_TURN_MAX_ATTACHMENTS) return false;
148 attached.push(file.name);
149 return true;
150 };
151 const input = {
152 targetKey: "draft",
153 pendingImports,
154 countReservedAttachments,
155 isActive: () => true,
156 attach,
157 onLimitReached: () => {
158 throw new Error("Both slots should be available");
159 },
160 };
161 const firstImport = importComposerThreadAttachment({ ...input, load: () => first.promise });
162 const secondImport = importComposerThreadAttachment({ ...input, load: () => second.promise });
163 first.resolve(new File(["first"], "first-thread.txt"));
164 await firstImport;
165 expect(await attach(new File(["unrelated"], "unrelated.txt"))).toBe(false);
166
167 second.resolve(new File(["second"], "second-thread.txt"));
168 await secondImport;
169 expect(attached).toEqual(["first-thread.txt", "second-thread.txt"]);
170 expect(pendingImports.size).toBe(0);
171 });
172
173 it.each(["failure", "inactive"])(
174 "releases the original draft's slot after %s",
175 async (outcome) => {
176 const pendingImports = new Map<string, number>();
177 const transcript = deferredFile();
178 const attach = vi.fn(async (_file: File) => true);
179 let activeTargetKey = "original-draft";
180 const input = {
181 targetKey: "original-draft",
182 pendingImports,
183 countReservedAttachments: () =>
184 PROVIDER_SEND_TURN_MAX_ATTACHMENTS - 1 + (pendingImports.get("original-draft") ?? 0),
185 load: () => transcript.promise,
186 isActive: () => activeTargetKey === "original-draft",
187 attach,
188 onLimitReached: () => {
189 throw new Error("The slot should have been released");
190 },
191 };
192 const importing = importComposerThreadAttachment(input);
193 if (outcome === "inactive") activeTargetKey = "new-draft";
194 expect(pendingImports.get("new-draft") ?? 0).toBe(0);
195 if (outcome === "failure") {
196 const failed = expect(importing).rejects.toThrow("Failed to load");
197 transcript.reject(new Error("Failed to load"));
198 await failed;
199 } else {
200 transcript.resolve(new File(["transcript"], "stale-thread.txt"));
201 await importing;
202 }
203 expect(attach).not.toHaveBeenCalled();
204 expect(pendingImports.size).toBe(0);
205
206 await importComposerThreadAttachment({
207 ...input,
208 load: async () => new File(["transcript"], "retry-thread.txt"),
209 isActive: () => true,
210 });
211 expect(attach.mock.calls[0]?.[0]?.name).toBe("retry-thread.txt");
212 expect(pendingImports.size).toBe(0);
213 },
214 );
215});