Open History to see the last commit
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("reads the latest selector state synchronously for send", () => {
123 const draftKey = "environment-1:thread-1";
124 const selectedDraft: ComposerDraft = {
125 text: "send this",
126 attachments: [],
127 modelSelection: {
128 instanceId: ProviderInstanceId.make("codex"),
129 model: "gpt-5.4",
130 options: [{ id: "reasoningEffort", value: "xhigh" }],
131 },
132 };
133 appAtomRegistry.set(composerDraftsAtom, { [draftKey]: selectedDraft });
134
135 expect(getComposerDraftSnapshot(draftKey)).toEqual(selectedDraft);
136 });
137
138 it("merges shared content into a project draft without duplicating retries", () => {
139 const draftKey = "new-task:environment-1:project-1";
140 const sharedAttachment = {
141 id: "share-1:image:0",
142 type: "image" as const,
143 name: "Screenshot.png",
144 mimeType: "image/png",
145 sizeBytes: 3,
146 dataUrl: "data:image/png;base64,YWJj",
147 previewUri: "data:image/png;base64,YWJj",
148 };
149 const existing: Record<string, ComposerDraft> = {
150 [draftKey]: { text: "Existing context", attachments: [] },
151 };
152 const content = {
153 text: "Shared note",
154 attachments: [sharedAttachment],
155 sourceShareId: "share-1",
156 };
157
158 const merged = mergeComposerDraftContentState(existing, draftKey, content);
159 expect(merged[draftKey]).toMatchObject({
160 text: "Existing context\n\nShared note",
161 attachments: [sharedAttachment],
162 importedShareIds: ["share-1"],
163 });
164 expect(mergeComposerDraftContentState(merged, draftKey, content)).toBe(merged);
165
166 const edited = {
167 ...merged,
168 [draftKey]: { ...merged[draftKey]!, text: "User edited the imported context" },
169 };
170 expect(mergeComposerDraftContentState(edited, draftKey, content)).toBe(edited);
171 });
172
173 it("preserves existing images when shared content exceeds the draft attachment limit", () => {
174 const draftKey = "new-task:environment-1:project-1";
175 const image = (id: string) => ({
176 id,
177 type: "image" as const,
178 name: `${id}.png`,
179 mimeType: "image/png",
180 sizeBytes: 3,
181 dataUrl: "data:image/png;base64,YWJj",
182 previewUri: "data:image/png;base64,YWJj",
183 });
184 const existingImage = image("existing");
185 const sharedImages = Array.from({ length: 8 }, (_, index) => image(`shared-${index}`));
186
187 const merged = mergeComposerDraftContentState(
188 { [draftKey]: { text: "", attachments: [existingImage] } },
189 draftKey,
190 { text: "", attachments: sharedImages },
191 );
192
193 expect(merged[draftKey]?.attachments).toHaveLength(8);
194 expect(merged[draftKey]?.attachments[0]).toEqual(existingImage);
195 expect(merged[draftKey]?.attachments.at(-1)?.id).toBe("shared-6");
196 });
197
198 it("restores the exact draft captured before an interrupted share import", () => {
199 const draftKey = "new-task:environment-1:project-1";
200 const beforeImport: ComposerDraft = {
201 text: "Existing context",
202 attachments: [],
203 runtimeMode: "approval-required",
204 };
205 const imported: ComposerDraft = {
206 ...beforeImport,
207 text: "Existing context\n\nShared note",
208 importedShareIds: ["share-1"],
209 };
210
211 expect(
212 restoreComposerDraftSnapshotState({ [draftKey]: imported }, draftKey, beforeImport),
213 ).toEqual({ [draftKey]: beforeImport });
214 expect(
215 restoreComposerDraftSnapshotState({ [draftKey]: imported }, draftKey, {
216 text: "",
217 attachments: [],
218 }),
219 ).toEqual({});
220 });
221
222 it("removes only drafts owned by the selected environment", () => {
223 const environmentId = EnvironmentId.make("environment-cloud");
224 const retainedEnvironmentId = EnvironmentId.make("environment-local");
225
226 expect(
227 removeComposerDraftsForEnvironment(
228 {
229 [`${environmentId}:thread-cloud`]: DRAFT,
230 [`new-task:${environmentId}:project-cloud`]: DRAFT,
231 [`${retainedEnvironmentId}:thread-local`]: DRAFT,
232 [`new-task:${retainedEnvironmentId}:project-local`]: DRAFT,
233 },
234 environmentId,
235 ),
236 ).toEqual({
237 [`${retainedEnvironmentId}:thread-local`]: DRAFT,
238 [`new-task:${retainedEnvironmentId}:project-local`]: DRAFT,
239 });
240 });
241});