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

composerDraftStore.test.ts

350 lines11.7 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 createdAt: "2026-01-01T00:00:00.000Z",
188 });
189 expect(useComposerDraftStore.getState().getDraftThread(threadId)).toEqual({
190 projectId,
191 branch: "feature/test",
192 worktreePath: "/tmp/worktree-test",
193 envMode: "worktree",
194 createdAt: "2026-01-01T00:00:00.000Z",
195 });
196 });
197​
198 it("clears only matching project draft mapping entries", () => {
199 const store = useComposerDraftStore.getState();
200 store.setProjectDraftThreadId(projectId, threadId);
201 store.setPrompt(threadId, "hello");
202​
203 store.clearProjectDraftThreadById(projectId, otherThreadId);
204 expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)?.threadId).toBe(
205 threadId,
206 );
207​
208 store.clearProjectDraftThreadById(projectId, threadId);
209 expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)).toBeNull();
210 expect(useComposerDraftStore.getState().getDraftThread(threadId)).toBeNull();
211 expect(useComposerDraftStore.getState().draftsByThreadId[threadId]).toBeUndefined();
212 });
213​
214 it("clears project draft mapping by project id", () => {
215 const store = useComposerDraftStore.getState();
216 store.setProjectDraftThreadId(projectId, threadId);
217 store.setPrompt(threadId, "hello");
218 store.clearProjectDraftThreadId(projectId);
219 expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)).toBeNull();
220 expect(useComposerDraftStore.getState().getDraftThread(threadId)).toBeNull();
221 expect(useComposerDraftStore.getState().draftsByThreadId[threadId]).toBeUndefined();
222 });
223​
224 it("clears orphaned composer drafts when remapping a project to a new draft thread", () => {
225 const store = useComposerDraftStore.getState();
226 store.setProjectDraftThreadId(projectId, threadId);
227 store.setPrompt(threadId, "orphan me");
228​
229 store.setProjectDraftThreadId(projectId, otherThreadId);
230​
231 expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)?.threadId).toBe(
232 otherThreadId,
233 );
234 expect(useComposerDraftStore.getState().getDraftThread(threadId)).toBeNull();
235 expect(useComposerDraftStore.getState().draftsByThreadId[threadId]).toBeUndefined();
236 });
237​
238 it("keeps composer drafts when the thread is still mapped by another project", () => {
239 const store = useComposerDraftStore.getState();
240 store.setProjectDraftThreadId(projectId, threadId);
241 store.setProjectDraftThreadId(otherProjectId, threadId);
242 store.setPrompt(threadId, "keep me");
243​
244 store.clearProjectDraftThreadId(projectId);
245​
246 expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)).toBeNull();
247 expect(useComposerDraftStore.getState().getDraftThreadByProjectId(otherProjectId)?.threadId).toBe(
248 threadId,
249 );
250 expect(useComposerDraftStore.getState().draftsByThreadId[threadId]?.prompt).toBe("keep me");
251 });
252​
253 it("clears draft registration independently", () => {
254 const store = useComposerDraftStore.getState();
255 store.setProjectDraftThreadId(projectId, threadId);
256 store.clearDraftThread(threadId);
257 expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)).toBeNull();
258 expect(useComposerDraftStore.getState().getDraftThread(threadId)).toBeNull();
259 });
260​
261 it("updates branch context on an existing draft thread", () => {
262 const store = useComposerDraftStore.getState();
263 store.setProjectDraftThreadId(projectId, threadId, {
264 branch: "main",
265 worktreePath: null,
266 });
267 store.setDraftThreadContext(threadId, {
268 branch: "feature/next",
269 worktreePath: "/tmp/feature-next",
270 });
271 expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)?.threadId).toBe(
272 threadId,
273 );
274 expect(useComposerDraftStore.getState().getDraftThread(threadId)).toMatchObject({
275 projectId,
276 branch: "feature/next",
277 worktreePath: "/tmp/feature-next",
278 envMode: "worktree",
279 });
280 });
281​
282 it("preserves existing branch and worktree when setProjectDraftThreadId receives undefined", () => {
283 const store = useComposerDraftStore.getState();
284 store.setProjectDraftThreadId(projectId, threadId, {
285 branch: "main",
286 worktreePath: "/tmp/main-worktree",
287 });
288 const runtimeUndefinedOptions = {
289 branch: undefined,
290 worktreePath: undefined,
291 } as unknown as {
292 branch?: string | null;
293 worktreePath?: string | null;
294 };
295 store.setProjectDraftThreadId(projectId, threadId, runtimeUndefinedOptions);
296​
297 expect(useComposerDraftStore.getState().getDraftThread(threadId)).toMatchObject({
298 projectId,
299 branch: "main",
300 worktreePath: "/tmp/main-worktree",
301 envMode: "worktree",
302 });
303 });
304​
305 it("preserves worktree env mode without a worktree path", () => {
306 const store = useComposerDraftStore.getState();
307 store.setProjectDraftThreadId(projectId, threadId, {
308 branch: "feature/base",
309 worktreePath: null,
310 envMode: "worktree",
311 });
312 const runtimeUndefinedOptions = {
313 branch: undefined,
314 worktreePath: undefined,
315 envMode: undefined,
316 } as unknown as {
317 branch?: string | null;
318 worktreePath?: string | null;
319 envMode?: "local" | "worktree";
320 };
321 store.setProjectDraftThreadId(projectId, threadId, runtimeUndefinedOptions);
322​
323 expect(useComposerDraftStore.getState().getDraftThread(threadId)).toMatchObject({
324 projectId,
325 branch: "feature/base",
326 worktreePath: null,
327 envMode: "worktree",
328 });
329 });
330});
331​
332describe("composerDraftStore setModel", () => {
333 const threadId = ThreadId.makeUnsafe("thread-model");
334​
335 beforeEach(() => {
336 useComposerDraftStore.setState({
337 draftsByThreadId: {},
338 draftThreadsByThreadId: {},
339 projectDraftThreadIdByProjectId: {},
340 });
341 });
342​
343 it("keeps explicit DEFAULT_MODEL overrides instead of coercing to null", () => {
344 const store = useComposerDraftStore.getState();
345​
346 store.setModel(threadId, "gpt-5.3-codex");
347​
348 expect(useComposerDraftStore.getState().draftsByThreadId[threadId]?.model).toBe("gpt-5.3-codex");
349 });
350});