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

questionAttachments.test.ts

116 lines4.6 KB

1import { ApprovalRequestId, EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { beforeEach, expect, it, vi } from "vite-plus/test";
3import { useComposerDraftStore } from "./composerDraftStore";
4import {
5 questionAttachmentDraftId,
6 countQuestionAttachments,
7 questionAttachmentDraftPrefix,
8 changeQuestionAttachmentPreparation,
9 clearQuestionAttachmentDraft,
10 useQuestionAttachmentPreparation,
11} from "./questionAttachments";
12​
13const release = vi.hoisted(() => vi.fn());
14vi.mock("./lib/attachmentUploadQueue", () => ({ releaseDraftAttachments: release }));
15const environmentId = EnvironmentId.make("environment-1");
16const threadId = ThreadId.make("thread-1");
17const requestId = ApprovalRequestId.make("request-1");
18beforeEach(() => {
19 useComposerDraftStore.setState({ draftsByThreadKey: {}, draftThreadsByThreadKey: {} });
20 useQuestionAttachmentPreparation.setState({ counts: {} });
21 release.mockClear();
22});
23it("keeps question files separate from the normal draft and other questions", () => {
24 const revoke = vi.spyOn(URL, "revokeObjectURL");
25 const store = useComposerDraftStore.getState();
26 const first = questionAttachmentDraftId(environmentId, threadId, requestId, "first");
27 const second = questionAttachmentDraftId(environmentId, threadId, requestId, "second");
28 store.setPrompt({ environmentId, threadId }, "Unsent prompt");
29 const image = {
30 type: "image" as const,
31 id: "image-1",
32 name: "image.png",
33 mimeType: "image/png",
34 sizeBytes: 5,
35 previewUrl: "blob:question-image",
36 file: new File(["image"], "image.png", { type: "image/png" }),
37 };
38 store.addImages(first, [image]);
39 store.addFiles(second, [
40 {
41 type: "file",
42 id: "file-1",
43 name: "spec.txt",
44 mimeType: "text/plain",
45 sizeBytes: 4,
46 file: new File(["spec"], "spec.txt", { type: "text/plain" }),
47 },
48 ]);
49 changeQuestionAttachmentPreparation(first, 1);
50 expect(store.getComposerDraft(first)?.images).toHaveLength(1);
51 expect(store.getComposerDraft(second)?.files).toHaveLength(1);
52 clearQuestionAttachmentDraft(first);
53 expect(store.getComposerDraft(first)).toBeNull();
54 expect(store.getComposerDraft(second)?.files).toHaveLength(1);
55 expect(store.getComposerDraft({ environmentId, threadId })?.prompt).toBe("Unsent prompt");
56 expect(useQuestionAttachmentPreparation.getState().counts[first]).toBeUndefined();
57 expect(release).toHaveBeenCalledWith([image]);
58 expect(revoke).toHaveBeenCalledWith(image.previewUrl);
59 revoke.mockRestore();
60});
61it("does not clear another environment whose id contains a question prefix", () => {
62 const otherEnvironment = EnvironmentId.make(
63 `${environmentId}:question-${encodeURIComponent(JSON.stringify(threadId))}-nested`,
64 );
65 const otherKey = questionAttachmentDraftId(otherEnvironment, threadId, requestId, "q");
66 const ownKey = questionAttachmentDraftId(environmentId, threadId, requestId, "q");
67 const store = useComposerDraftStore.getState();
68 store.setPrompt(otherKey, "Keep this answer");
69 store.setPrompt(ownKey, "Discard this answer");
70 const prefix = questionAttachmentDraftPrefix(environmentId, threadId);
71 for (const key of [ownKey, otherKey]) {
72 if (key.startsWith(prefix)) clearQuestionAttachmentDraft(key);
73 }
74 expect(store.getComposerDraft(ownKey)).toBeNull();
75 expect(store.getComposerDraft(otherKey)?.prompt).toBe("Keep this answer");
76});
77​
78it("counts files, images, and pending preparation across the shared question budget", () => {
79 const first = questionAttachmentDraftId(environmentId, threadId, requestId, "first");
80 const second = questionAttachmentDraftId(environmentId, threadId, requestId, "second");
81 const other = questionAttachmentDraftId(
82 EnvironmentId.make("other"),
83 threadId,
84 requestId,
85 "first",
86 );
87 const store = useComposerDraftStore.getState();
88 store.addFiles(
89 second,
90 Array.from({ length: 6 }, (_, index) => ({
91 type: "file" as const,
92 id: `file-${index}`,
93 name: `spec-${index}.txt`,
94 mimeType: "text/plain",
95 sizeBytes: 4,
96 file: new File(["spec"], `spec-${index}.txt`),
97 })),
98 );
99 store.addImages(first, [
100 {
101 type: "image",
102 id: "image",
103 name: "image.png",
104 mimeType: "image/png",
105 sizeBytes: 5,
106 previewUrl: "blob:count-test",
107 file: new File(["image"], "image.png"),
108 },
109 ]);
110 changeQuestionAttachmentPreparation(second, 1);
111 changeQuestionAttachmentPreparation(other, 8);
112 expect(countQuestionAttachments([first, second])).toBe(8);
113 expect(countQuestionAttachments([second])).toBe(7);
114 changeQuestionAttachmentPreparation(second, -1);
115 expect(countQuestionAttachments([first, second])).toBe(7);
116});