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

use-composer-drafts.test.ts

271 lines8.0 KB

1import { afterEach, describe, expect, it } from "@effect/vitest";
2import { EnvironmentId, ProviderInstanceId } from "@t3tools/contracts";
3​
4import { appAtomRegistry } from "./atom-registry";
5import {
6 clearComposerDraftContentState,
7 composerDraftsAtom,
8 decodePersistedComposerDrafts,
9 type ComposerDraft,
10 getComposerDraftSnapshot,
11 mergeComposerDraftContentState,
12 removeComposerDraftsForEnvironment,
13 restoreComposerDraftSnapshotState,
14} from "./use-composer-drafts";
15​
16const DRAFT: ComposerDraft = {
17 text: "hello",
18 attachments: [],
19};
20​
21afterEach(() => {
22 appAtomRegistry.set(composerDraftsAtom, {});
23});
24​
25describe("mobile composer drafts", () => {
26 it("hydrates selector state even when the message content is empty", () => {
27 expect(
28 decodePersistedComposerDrafts({
29 schemaVersion: 1,
30 drafts: {
31 "new-task:environment-1:project-1": {
32 text: "",
33 attachments: [],
34 modelSelection: {
35 instanceId: "codex",
36 model: "gpt-5.4",
37 options: [{ id: "reasoningEffort", value: "xhigh" }],
38 },
39 runtimeMode: "approval-required",
40 interactionMode: "plan",
41 workspaceSelection: {
42 mode: "worktree",
43 branch: "main",
44 worktreePath: null,
45 },
46 },
47 },
48 }),
49 ).toEqual({
50 "new-task:environment-1:project-1": {
51 text: "",
52 attachments: [],
53 modelSelection: {
54 instanceId: "codex",
55 model: "gpt-5.4",
56 options: [{ id: "reasoningEffort", value: "xhigh" }],
57 },
58 runtimeMode: "approval-required",
59 interactionMode: "plan",
60 workspaceSelection: {
61 mode: "worktree",
62 branch: "main",
63 worktreePath: null,
64 },
65 },
66 });
67 });
68​
69 it("keeps legacy content-only drafts and rejects invalid selector state", () => {
70 expect(
71 decodePersistedComposerDrafts({
72 schemaVersion: 1,
73 drafts: {
74 "environment-1:thread-1": DRAFT,
75 },
76 }),
77 ).toEqual({
78 "environment-1:thread-1": DRAFT,
79 });
80​
81 expect(() =>
82 decodePersistedComposerDrafts({
83 schemaVersion: 1,
84 drafts: {
85 "environment-1:thread-1": {
86 ...DRAFT,
87 runtimeMode: "sometimes-safe",
88 },
89 },
90 }),
91 ).toThrow();
92 });
93​
94 it("clears sent content without clearing the selected model or workspace", () => {
95 const draftKey = "environment-1:thread-1";
96 const draft: ComposerDraft = {
97 text: "send this",
98 attachments: [],
99 importedShareIds: ["share-1"],
100 modelSelection: {
101 instanceId: ProviderInstanceId.make("codex"),
102 model: "gpt-5.4",
103 options: [{ id: "reasoningEffort", value: "xhigh" }],
104 },
105 workspaceSelection: {
106 mode: "worktree",
107 branch: "main",
108 worktreePath: null,
109 },
110 };
111​
112 expect(clearComposerDraftContentState({ [draftKey]: draft }, draftKey)).toEqual({
113 [draftKey]: {
114 modelSelection: draft.modelSelection,
115 workspaceSelection: draft.workspaceSelection,
116 text: "",
117 attachments: [],
118 },
119 });
120 });
121​
122 it("drops the workspace selection when clearing a sent new-task draft", () => {
123 const draftKey = "new-task:environment-1:project-1";
124 const draft: ComposerDraft = {
125 text: "send this",
126 attachments: [],
127 modelSelection: {
128 instanceId: ProviderInstanceId.make("codex"),
129 model: "gpt-5.4",
130 },
131 workspaceSelection: {
132 mode: "worktree",
133 branch: "main",
134 worktreePath: null,
135 startFromOrigin: false,
136 },
137 };
138​
139 expect(
140 clearComposerDraftContentState({ [draftKey]: draft }, draftKey, {
141 clearWorkspaceSelection: true,
142 }),
143 ).toEqual({
144 [draftKey]: {
145 modelSelection: draft.modelSelection,
146 text: "",
147 attachments: [],
148 },
149 });
150 });
151​
152 it("reads the latest selector state synchronously for send", () => {
153 const draftKey = "environment-1:thread-1";
154 const selectedDraft: ComposerDraft = {
155 text: "send this",
156 attachments: [],
157 modelSelection: {
158 instanceId: ProviderInstanceId.make("codex"),
159 model: "gpt-5.4",
160 options: [{ id: "reasoningEffort", value: "xhigh" }],
161 },
162 };
163 appAtomRegistry.set(composerDraftsAtom, { [draftKey]: selectedDraft });
164​
165 expect(getComposerDraftSnapshot(draftKey)).toEqual(selectedDraft);
166 });
167​
168 it("merges shared content into a project draft without duplicating retries", () => {
169 const draftKey = "new-task:environment-1:project-1";
170 const sharedAttachment = {
171 id: "share-1:image:0",
172 type: "image" as const,
173 name: "Screenshot.png",
174 mimeType: "image/png",
175 sizeBytes: 3,
176 dataUrl: "data:image/png;base64,YWJj",
177 previewUri: "data:image/png;base64,YWJj",
178 };
179 const existing: Record<string, ComposerDraft> = {
180 [draftKey]: { text: "Existing context", attachments: [] },
181 };
182 const content = {
183 text: "Shared note",
184 attachments: [sharedAttachment],
185 sourceShareId: "share-1",
186 };
187​
188 const merged = mergeComposerDraftContentState(existing, draftKey, content);
189 expect(merged[draftKey]).toMatchObject({
190 text: "Existing context\n\nShared note",
191 attachments: [sharedAttachment],
192 importedShareIds: ["share-1"],
193 });
194 expect(mergeComposerDraftContentState(merged, draftKey, content)).toBe(merged);
195​
196 const edited = {
197 ...merged,
198 [draftKey]: { ...merged[draftKey]!, text: "User edited the imported context" },
199 };
200 expect(mergeComposerDraftContentState(edited, draftKey, content)).toBe(edited);
201 });
202​
203 it("preserves existing images when shared content exceeds the draft attachment limit", () => {
204 const draftKey = "new-task:environment-1:project-1";
205 const image = (id: string) => ({
206 id,
207 type: "image" as const,
208 name: `${id}.png`,
209 mimeType: "image/png",
210 sizeBytes: 3,
211 dataUrl: "data:image/png;base64,YWJj",
212 previewUri: "data:image/png;base64,YWJj",
213 });
214 const existingImage = image("existing");
215 const sharedImages = Array.from({ length: 8 }, (_, index) => image(`shared-${index}`));
216​
217 const merged = mergeComposerDraftContentState(
218 { [draftKey]: { text: "", attachments: [existingImage] } },
219 draftKey,
220 { text: "", attachments: sharedImages },
221 );
222​
223 expect(merged[draftKey]?.attachments).toHaveLength(8);
224 expect(merged[draftKey]?.attachments[0]).toEqual(existingImage);
225 expect(merged[draftKey]?.attachments.at(-1)?.id).toBe("shared-6");
226 });
227​
228 it("restores the exact draft captured before an interrupted share import", () => {
229 const draftKey = "new-task:environment-1:project-1";
230 const beforeImport: ComposerDraft = {
231 text: "Existing context",
232 attachments: [],
233 runtimeMode: "approval-required",
234 };
235 const imported: ComposerDraft = {
236 ...beforeImport,
237 text: "Existing context\n\nShared note",
238 importedShareIds: ["share-1"],
239 };
240​
241 expect(
242 restoreComposerDraftSnapshotState({ [draftKey]: imported }, draftKey, beforeImport),
243 ).toEqual({ [draftKey]: beforeImport });
244 expect(
245 restoreComposerDraftSnapshotState({ [draftKey]: imported }, draftKey, {
246 text: "",
247 attachments: [],
248 }),
249 ).toEqual({});
250 });
251​
252 it("removes only drafts owned by the selected environment", () => {
253 const environmentId = EnvironmentId.make("environment-cloud");
254 const retainedEnvironmentId = EnvironmentId.make("environment-local");
255​
256 expect(
257 removeComposerDraftsForEnvironment(
258 {
259 [`${environmentId}:thread-cloud`]: DRAFT,
260 [`new-task:${environmentId}:project-cloud`]: DRAFT,
261 [`${retainedEnvironmentId}:thread-local`]: DRAFT,
262 [`new-task:${retainedEnvironmentId}:project-local`]: DRAFT,
263 },
264 environmentId,
265 ),
266 ).toEqual({
267 [`${retainedEnvironmentId}:thread-local`]: DRAFT,
268 [`new-task:${retainedEnvironmentId}:project-local`]: DRAFT,
269 });
270 });
271});