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