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

composerDraftStore.test.ts

453 lines14.8 KB

1import { ProjectId, ThreadId } from "@t3tools/contracts";
2import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
3​
4import { type ComposerImageAttachment, useComposerDraftStore } from "./composerDraftStore";
5​
6function makeImage(input: {
7 id: string;
8 previewUrl: string;
9 name?: string;
10 mimeType?: string;
11 sizeBytes?: number;
12 lastModified?: number;
13}): ComposerImageAttachment {
14 const name = input.name ?? "image.png";
15 const mimeType = input.mimeType ?? "image/png";
16 const sizeBytes = input.sizeBytes ?? 4;
17 const lastModified = input.lastModified ?? 1_700_000_000_000;
18 const file = new File([new Uint8Array(sizeBytes).fill(1)], name, {
19 type: mimeType,
20 lastModified,
21 });
22 return {
23 type: "image",
24 id: input.id,
25 name,
26 mimeType,
27 sizeBytes: file.size,
28 previewUrl: input.previewUrl,
29 file,
30 };
32​
33describe("composerDraftStore addImages", () => {
34 const threadId = ThreadId.makeUnsafe("thread-dedupe");
35 let originalRevokeObjectUrl: typeof URL.revokeObjectURL;
36 let revokeSpy: ReturnType<typeof vi.fn<(url: string) => void>>;
37​
38 beforeEach(() => {
39 useComposerDraftStore.setState({
40 draftsByThreadId: {},
41 draftThreadsByThreadId: {},
42 projectDraftThreadIdByProjectId: {},
43 });
44 originalRevokeObjectUrl = URL.revokeObjectURL;
45 revokeSpy = vi.fn();
46 URL.revokeObjectURL = revokeSpy;
47 });
48​
49 afterEach(() => {
50 URL.revokeObjectURL = originalRevokeObjectUrl;
51 });
52​
53 it("deduplicates identical images in one batch by file signature", () => {
54 const first = makeImage({
55 id: "img-1",
56 previewUrl: "blob:first",
57 name: "same.png",
58 mimeType: "image/png",
59 sizeBytes: 12,
60 lastModified: 12345,
61 });
62 const duplicate = makeImage({
63 id: "img-2",
64 previewUrl: "blob:duplicate",
65 name: "same.png",
66 mimeType: "image/png",
67 sizeBytes: 12,
68 lastModified: 12345,
69 });
70​
71 useComposerDraftStore.getState().addImages(threadId, [first, duplicate]);
72​
73 const draft = useComposerDraftStore.getState().draftsByThreadId[threadId];
74 expect(draft?.images.map((image) => image.id)).toEqual(["img-1"]);
75 expect(revokeSpy).toHaveBeenCalledWith("blob:duplicate");
76 });
77​
78 it("deduplicates against existing images across calls by file signature", () => {
79 const first = makeImage({
80 id: "img-a",
81 previewUrl: "blob:a",
82 name: "same.png",
83 mimeType: "image/png",
84 sizeBytes: 9,
85 lastModified: 777,
86 });
87 const duplicateLater = makeImage({
88 id: "img-b",
89 previewUrl: "blob:b",
90 name: "same.png",
91 mimeType: "image/png",
92 sizeBytes: 9,
93 lastModified: 999,
94 });
95​
96 useComposerDraftStore.getState().addImage(threadId, first);
97 useComposerDraftStore.getState().addImage(threadId, duplicateLater);
98​
99 const draft = useComposerDraftStore.getState().draftsByThreadId[threadId];
100 expect(draft?.images.map((image) => image.id)).toEqual(["img-a"]);
101 expect(revokeSpy).toHaveBeenCalledWith("blob:b");
102 });
103​
104 it("does not revoke blob URLs that are still used by an accepted duplicate image", () => {
105 const first = makeImage({
106 id: "img-shared",
107 previewUrl: "blob:shared",
108 });
109 const duplicateSameUrl = makeImage({
110 id: "img-shared",
111 previewUrl: "blob:shared",
112 });
113​
114 useComposerDraftStore.getState().addImages(threadId, [first, duplicateSameUrl]);
115​
116 const draft = useComposerDraftStore.getState().draftsByThreadId[threadId];
117 expect(draft?.images.map((image) => image.id)).toEqual(["img-shared"]);
118 expect(revokeSpy).not.toHaveBeenCalledWith("blob:shared");
119 });
120});
121​
122describe("composerDraftStore clearComposerContent", () => {
123 const threadId = ThreadId.makeUnsafe("thread-clear");
124 let originalRevokeObjectUrl: typeof URL.revokeObjectURL;
125 let revokeSpy: ReturnType<typeof vi.fn<(url: string) => void>>;
126​
127 beforeEach(() => {
128 useComposerDraftStore.setState({
129 draftsByThreadId: {},
130 draftThreadsByThreadId: {},
131 projectDraftThreadIdByProjectId: {},
132 });
133 originalRevokeObjectUrl = URL.revokeObjectURL;
134 revokeSpy = vi.fn();
135 URL.revokeObjectURL = revokeSpy;
136 });
137​
138 afterEach(() => {
139 URL.revokeObjectURL = originalRevokeObjectUrl;
140 });
141​
142 it("does not revoke blob preview URLs when clearing composer content", () => {
143 const first = makeImage({
144 id: "img-optimistic",
145 previewUrl: "blob:optimistic",
146 });
147 useComposerDraftStore.getState().addImage(threadId, first);
148​
149 useComposerDraftStore.getState().clearComposerContent(threadId);
150​
151 const draft = useComposerDraftStore.getState().draftsByThreadId[threadId];
152 expect(draft).toBeUndefined();
153 expect(revokeSpy).not.toHaveBeenCalledWith("blob:optimistic");
154 });
155});
156​
157describe("composerDraftStore project draft thread mapping", () => {
158 const projectId = ProjectId.makeUnsafe("project-a");
159 const otherProjectId = ProjectId.makeUnsafe("project-b");
160 const threadId = ThreadId.makeUnsafe("thread-a");
161 const otherThreadId = ThreadId.makeUnsafe("thread-b");
162​
163 beforeEach(() => {
164 useComposerDraftStore.setState({
165 draftsByThreadId: {},
166 draftThreadsByThreadId: {},
167 projectDraftThreadIdByProjectId: {},
168 });
169 });
170​
171 it("stores and reads project draft thread ids via actions", () => {
172 const store = useComposerDraftStore.getState();
173 expect(store.getDraftThreadByProjectId(projectId)).toBeNull();
174 expect(store.getDraftThread(threadId)).toBeNull();
175​
176 store.setProjectDraftThreadId(projectId, threadId, {
177 branch: "feature/test",
178 worktreePath: "/tmp/worktree-test",
179 createdAt: "2026-01-01T00:00:00.000Z",
180 });
181 expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)).toEqual({
182 threadId,
183 projectId,
184 branch: "feature/test",
185 worktreePath: "/tmp/worktree-test",
186 envMode: "worktree",
187 runtimeMode: "full-access",
188 interactionMode: "default",
189 createdAt: "2026-01-01T00:00:00.000Z",
190 });
191 expect(useComposerDraftStore.getState().getDraftThread(threadId)).toEqual({
192 projectId,
193 branch: "feature/test",
194 worktreePath: "/tmp/worktree-test",
195 envMode: "worktree",
196 runtimeMode: "full-access",
197 interactionMode: "default",
198 createdAt: "2026-01-01T00:00:00.000Z",
199 });
200 });
201​
202 it("clears only matching project draft mapping entries", () => {
203 const store = useComposerDraftStore.getState();
204 store.setProjectDraftThreadId(projectId, threadId);
205 store.setPrompt(threadId, "hello");
206​
207 store.clearProjectDraftThreadById(projectId, otherThreadId);
208 expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)?.threadId).toBe(
209 threadId,
210 );
211​
212 store.clearProjectDraftThreadById(projectId, threadId);
213 expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)).toBeNull();
214 expect(useComposerDraftStore.getState().getDraftThread(threadId)).toBeNull();
215 expect(useComposerDraftStore.getState().draftsByThreadId[threadId]).toBeUndefined();
216 });
217​
218 it("clears project draft mapping by project id", () => {
219 const store = useComposerDraftStore.getState();
220 store.setProjectDraftThreadId(projectId, threadId);
221 store.setPrompt(threadId, "hello");
222 store.clearProjectDraftThreadId(projectId);
223 expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)).toBeNull();
224 expect(useComposerDraftStore.getState().getDraftThread(threadId)).toBeNull();
225 expect(useComposerDraftStore.getState().draftsByThreadId[threadId]).toBeUndefined();
226 });
227​
228 it("clears orphaned composer drafts when remapping a project to a new draft thread", () => {
229 const store = useComposerDraftStore.getState();
230 store.setProjectDraftThreadId(projectId, threadId);
231 store.setPrompt(threadId, "orphan me");
232​
233 store.setProjectDraftThreadId(projectId, otherThreadId);
234​
235 expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)?.threadId).toBe(
236 otherThreadId,
237 );
238 expect(useComposerDraftStore.getState().getDraftThread(threadId)).toBeNull();
239 expect(useComposerDraftStore.getState().draftsByThreadId[threadId]).toBeUndefined();
240 });
241​
242 it("keeps composer drafts when the thread is still mapped by another project", () => {
243 const store = useComposerDraftStore.getState();
244 store.setProjectDraftThreadId(projectId, threadId);
245 store.setProjectDraftThreadId(otherProjectId, threadId);
246 store.setPrompt(threadId, "keep me");
247​
248 store.clearProjectDraftThreadId(projectId);
249​
250 expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)).toBeNull();
251 expect(useComposerDraftStore.getState().getDraftThreadByProjectId(otherProjectId)?.threadId).toBe(
252 threadId,
253 );
254 expect(useComposerDraftStore.getState().draftsByThreadId[threadId]?.prompt).toBe("keep me");
255 });
256​
257 it("clears draft registration independently", () => {
258 const store = useComposerDraftStore.getState();
259 store.setProjectDraftThreadId(projectId, threadId);
260 store.clearDraftThread(threadId);
261 expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)).toBeNull();
262 expect(useComposerDraftStore.getState().getDraftThread(threadId)).toBeNull();
263 });
264​
265 it("updates branch context on an existing draft thread", () => {
266 const store = useComposerDraftStore.getState();
267 store.setProjectDraftThreadId(projectId, threadId, {
268 branch: "main",
269 worktreePath: null,
270 });
271 store.setDraftThreadContext(threadId, {
272 branch: "feature/next",
273 worktreePath: "/tmp/feature-next",
274 });
275 expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)?.threadId).toBe(
276 threadId,
277 );
278 expect(useComposerDraftStore.getState().getDraftThread(threadId)).toMatchObject({
279 projectId,
280 branch: "feature/next",
281 worktreePath: "/tmp/feature-next",
282 envMode: "worktree",
283 });
284 });
285​
286 it("preserves existing branch and worktree when setProjectDraftThreadId receives undefined", () => {
287 const store = useComposerDraftStore.getState();
288 store.setProjectDraftThreadId(projectId, threadId, {
289 branch: "main",
290 worktreePath: "/tmp/main-worktree",
291 });
292 const runtimeUndefinedOptions = {
293 branch: undefined,
294 worktreePath: undefined,
295 } as unknown as {
296 branch?: string | null;
297 worktreePath?: string | null;
298 };
299 store.setProjectDraftThreadId(projectId, threadId, runtimeUndefinedOptions);
300​
301 expect(useComposerDraftStore.getState().getDraftThread(threadId)).toMatchObject({
302 projectId,
303 branch: "main",
304 worktreePath: "/tmp/main-worktree",
305 envMode: "worktree",
306 });
307 });
308​
309 it("preserves worktree env mode without a worktree path", () => {
310 const store = useComposerDraftStore.getState();
311 store.setProjectDraftThreadId(projectId, threadId, {
312 branch: "feature/base",
313 worktreePath: null,
314 envMode: "worktree",
315 });
316 const runtimeUndefinedOptions = {
317 branch: undefined,
318 worktreePath: undefined,
319 envMode: undefined,
320 } as unknown as {
321 branch?: string | null;
322 worktreePath?: string | null;
323 envMode?: "local" | "worktree";
324 };
325 store.setProjectDraftThreadId(projectId, threadId, runtimeUndefinedOptions);
326​
327 expect(useComposerDraftStore.getState().getDraftThread(threadId)).toMatchObject({
328 projectId,
329 branch: "feature/base",
330 worktreePath: null,
331 envMode: "worktree",
332 });
333 });
334});
335​
336describe("composerDraftStore codex fast mode", () => {
337 const threadId = ThreadId.makeUnsafe("thread-service-tier");
338​
339 beforeEach(() => {
340 useComposerDraftStore.setState({
341 draftsByThreadId: {},
342 draftThreadsByThreadId: {},
343 projectDraftThreadIdByProjectId: {},
344 });
345 });
346​
347 it("stores codex fast mode in the draft", () => {
348 const store = useComposerDraftStore.getState();
349 store.setCodexFastMode(threadId, true);
350​
351 expect(useComposerDraftStore.getState().draftsByThreadId[threadId]?.codexFastMode).toBe(true);
352 });
353​
354 it("clears codex fast mode when reset to the default", () => {
355 const store = useComposerDraftStore.getState();
356 store.setCodexFastMode(threadId, true);
357 store.setCodexFastMode(threadId, false);
358​
359 expect(useComposerDraftStore.getState().draftsByThreadId[threadId]).toBeUndefined();
360 });
361});
362​
363describe("composerDraftStore setModel", () => {
364 const threadId = ThreadId.makeUnsafe("thread-model");
365​
366 beforeEach(() => {
367 useComposerDraftStore.setState({
368 draftsByThreadId: {},
369 draftThreadsByThreadId: {},
370 projectDraftThreadIdByProjectId: {},
371 });
372 });
373​
374 it("keeps explicit DEFAULT_MODEL overrides instead of coercing to null", () => {
375 const store = useComposerDraftStore.getState();
376​
377 store.setModel(threadId, "gpt-5.3-codex");
378​
379 expect(useComposerDraftStore.getState().draftsByThreadId[threadId]?.model).toBe("gpt-5.3-codex");
380 });
381});
382​
383describe("composerDraftStore setProvider", () => {
384 const threadId = ThreadId.makeUnsafe("thread-provider");
385​
386 beforeEach(() => {
387 useComposerDraftStore.setState({
388 draftsByThreadId: {},
389 draftThreadsByThreadId: {},
390 projectDraftThreadIdByProjectId: {},
391 });
392 });
393​
394 it("persists provider-only selection even when prompt/model are empty", () => {
395 const store = useComposerDraftStore.getState();
396​
397 store.setProvider(threadId, "codex");
398​
399 expect(useComposerDraftStore.getState().draftsByThreadId[threadId]?.provider).toBe("codex");
400 });
401​
402 it("removes empty provider-only draft when provider is reset", () => {
403 const store = useComposerDraftStore.getState();
404​
405 store.setProvider(threadId, "codex");
406 store.setProvider(threadId, null);
407​
408 expect(useComposerDraftStore.getState().draftsByThreadId[threadId]).toBeUndefined();
409 });
410});
411​
412describe("composerDraftStore runtime and interaction settings", () => {
413 const threadId = ThreadId.makeUnsafe("thread-settings");
414​
415 beforeEach(() => {
416 useComposerDraftStore.setState({
417 draftsByThreadId: {},
418 draftThreadsByThreadId: {},
419 projectDraftThreadIdByProjectId: {},
420 });
421 });
422​
423 it("stores runtime mode overrides in the composer draft", () => {
424 const store = useComposerDraftStore.getState();
425​
426 store.setRuntimeMode(threadId, "approval-required");
427​
428 expect(useComposerDraftStore.getState().draftsByThreadId[threadId]?.runtimeMode).toBe(
429 "approval-required",
430 );
431 });
432​
433 it("stores interaction mode overrides in the composer draft", () => {
434 const store = useComposerDraftStore.getState();
435​
436 store.setInteractionMode(threadId, "plan");
437​
438 expect(useComposerDraftStore.getState().draftsByThreadId[threadId]?.interactionMode).toBe(
439 "plan",
440 );
441 });
442​
443 it("removes empty settings-only drafts when overrides are cleared", () => {
444 const store = useComposerDraftStore.getState();
445​
446 store.setRuntimeMode(threadId, "approval-required");
447 store.setInteractionMode(threadId, "plan");
448 store.setRuntimeMode(threadId, null);
449 store.setInteractionMode(threadId, null);
450​
451 expect(useComposerDraftStore.getState().draftsByThreadId[threadId]).toBeUndefined();
452 });
453});