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

questionAttachments.test.ts

138 lines5.9 KB

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