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

use-composer-drafts.test.ts

153 lines4.2 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 removeComposerDraftsForEnvironment,
12} from "./use-composer-drafts";
13​
14const DRAFT: ComposerDraft = {
15 text: "hello",
16 attachments: [],
17};
18​
19afterEach(() => {
20 appAtomRegistry.set(composerDraftsAtom, {});
21});
22​
23describe("mobile composer drafts", () => {
24 it("hydrates selector state even when the message content is empty", () => {
25 expect(
26 decodePersistedComposerDrafts({
27 schemaVersion: 1,
28 drafts: {
29 "new-task:environment-1:project-1": {
30 text: "",
31 attachments: [],
32 modelSelection: {
33 instanceId: "codex",
34 model: "gpt-5.4",
35 options: [{ id: "reasoningEffort", value: "xhigh" }],
36 },
37 runtimeMode: "approval-required",
38 interactionMode: "plan",
39 workspaceSelection: {
40 mode: "worktree",
41 branch: "main",
42 worktreePath: null,
43 },
44 },
45 },
46 }),
47 ).toEqual({
48 "new-task:environment-1:project-1": {
49 text: "",
50 attachments: [],
51 modelSelection: {
52 instanceId: "codex",
53 model: "gpt-5.4",
54 options: [{ id: "reasoningEffort", value: "xhigh" }],
55 },
56 runtimeMode: "approval-required",
57 interactionMode: "plan",
58 workspaceSelection: {
59 mode: "worktree",
60 branch: "main",
61 worktreePath: null,
62 },
63 },
64 });
65 });
66​
67 it("keeps legacy content-only drafts and rejects invalid selector state", () => {
68 expect(
69 decodePersistedComposerDrafts({
70 schemaVersion: 1,
71 drafts: {
72 "environment-1:thread-1": DRAFT,
73 },
74 }),
75 ).toEqual({
76 "environment-1:thread-1": DRAFT,
77 });
78​
79 expect(() =>
80 decodePersistedComposerDrafts({
81 schemaVersion: 1,
82 drafts: {
83 "environment-1:thread-1": {
84 ...DRAFT,
85 runtimeMode: "sometimes-safe",
86 },
87 },
88 }),
89 ).toThrow();
90 });
91​
92 it("clears sent content without clearing the selected model or workspace", () => {
93 const draftKey = "environment-1:thread-1";
94 const draft: ComposerDraft = {
95 text: "send this",
96 attachments: [],
97 modelSelection: {
98 instanceId: ProviderInstanceId.make("codex"),
99 model: "gpt-5.4",
100 options: [{ id: "reasoningEffort", value: "xhigh" }],
101 },
102 workspaceSelection: {
103 mode: "worktree",
104 branch: "main",
105 worktreePath: null,
106 },
107 };
108​
109 expect(clearComposerDraftContentState({ [draftKey]: draft }, draftKey)).toEqual({
110 [draftKey]: {
111 ...draft,
112 text: "",
113 attachments: [],
114 },
115 });
116 });
117​
118 it("reads the latest selector state synchronously for send", () => {
119 const draftKey = "environment-1:thread-1";
120 const selectedDraft: ComposerDraft = {
121 text: "send this",
122 attachments: [],
123 modelSelection: {
124 instanceId: ProviderInstanceId.make("codex"),
125 model: "gpt-5.4",
126 options: [{ id: "reasoningEffort", value: "xhigh" }],
127 },
128 };
129 appAtomRegistry.set(composerDraftsAtom, { [draftKey]: selectedDraft });
130​
131 expect(getComposerDraftSnapshot(draftKey)).toEqual(selectedDraft);
132 });
133​
134 it("removes only drafts owned by the selected environment", () => {
135 const environmentId = EnvironmentId.make("environment-cloud");
136 const retainedEnvironmentId = EnvironmentId.make("environment-local");
137​
138 expect(
139 removeComposerDraftsForEnvironment(
140 {
141 [`${environmentId}:thread-cloud`]: DRAFT,
142 [`new-task:${environmentId}:project-cloud`]: DRAFT,
143 [`${retainedEnvironmentId}:thread-local`]: DRAFT,
144 [`new-task:${retainedEnvironmentId}:project-local`]: DRAFT,
145 },
146 environmentId,
147 ),
148 ).toEqual({
149 [`${retainedEnvironmentId}:thread-local`]: DRAFT,
150 [`new-task:${retainedEnvironmentId}:project-local`]: DRAFT,
151 });
152 });
153});