Open History to see the last commit
1import {
2 CommandId,
3 ComposerContextId,
4 EnvironmentId,
5 MessageId,
6 ProjectId,
7 ProviderInstanceId,
8 ThreadId,
9} from "@t3tools/contracts";
10import { describe, expect, it } from "vite-plus/test";
11
12import {
13 isPendingThreadCreationVisible,
14 pendingThreadCreationMessage,
15 pendingThreadCreationShell,
16 resolvePendingThreadCreation,
17 type PendingThreadCreation,
18} from "./pending-thread-creation";
19import type { QueuedThreadMessage } from "./thread-outbox-model";
20
21const creation: QueuedThreadMessage = {
22 environmentId: EnvironmentId.make("environment-1"),
23 threadId: ThreadId.make("thread-1"),
24 messageId: MessageId.make("message-1"),
25 commandId: CommandId.make("command-1"),
26 text: "Fix the flaky login test",
27 attachments: [
28 {
29 id: "draft-image",
30 type: "image",
31 name: "screen.png",
32 mimeType: "image/png",
33 sizeBytes: 10,
34 previewUri: "data:image/png;base64,AAAA",
35 },
36 ],
37 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.6-sol" },
38 runtimeMode: "full-access",
39 creation: {
40 projectId: ProjectId.make("project-1"),
41 workspaceMode: "worktree",
42 branch: "main",
43 worktreePath: null,
44 },
45 createdAt: "2026-08-24T12:00:00.000Z",
46};
47
48describe("resolvePendingThreadCreation", () => {
49 const threadKey = `${creation.environmentId}:${creation.threadId}`;
50 const pending: PendingThreadCreation = { message: creation, outcome: null };
51 const prompt = { id: creation.messageId };
52
53 it("keeps setup visible through the prompt echo and shell cleanup until detail has a turn", () => {
54 let previous = resolvePendingThreadCreation({
55 threadKey,
56 pending,
57 previous: null,
58 detail: null,
59 });
60 expect(previous).toBe(pending);
61
62 previous = resolvePendingThreadCreation({
63 threadKey,
64 pending,
65 previous,
66 detail: { messages: [], latestTurn: null, session: null },
67 });
68 expect(previous).toBe(pending);
69
70 previous = resolvePendingThreadCreation({
71 threadKey,
72 pending,
73 previous,
74 detail: { messages: [prompt], latestTurn: null, session: { status: "starting" } },
75 });
76 expect(previous).toBe(pending);
77
78 previous = resolvePendingThreadCreation({
79 threadKey,
80 pending: null,
81 previous,
82 detail: { messages: [prompt], latestTurn: null, session: { status: "starting" } },
83 });
84 expect(previous).toBe(pending);
85
86 expect(
87 resolvePendingThreadCreation({
88 threadKey,
89 pending: null,
90 previous,
91 detail: {
92 messages: [prompt],
93 latestTurn: { turnId: "turn-1" },
94 session: { status: "running" },
95 },
96 }),
97 ).toBeNull();
98 });
99
100 it("keeps the prompt until both the turn and its message have arrived", () => {
101 expect(
102 resolvePendingThreadCreation({
103 threadKey,
104 pending,
105 previous: null,
106 detail: { messages: [], latestTurn: { turnId: "turn-1" }, session: { status: "running" } },
107 }),
108 ).toBe(pending);
109 });
110
111 it.each(["error", "stopped", "interrupted"])("ends setup when startup is %s", (status) => {
112 expect(
113 resolvePendingThreadCreation({
114 threadKey,
115 pending: null,
116 previous: pending,
117 detail: { messages: [prompt], latestTurn: null, session: { status } },
118 }),
119 ).toBeNull();
120 });
121
122 it("preserves rejected task recovery", () => {
123 const failed: PendingThreadCreation = {
124 message: creation,
125 outcome: { kind: "failed", message: creation, reason: "Checkout failed" },
126 };
127 expect(
128 resolvePendingThreadCreation({
129 threadKey,
130 pending: failed,
131 previous: pending,
132 detail: { messages: [], latestTurn: null, session: { status: "error" } },
133 }),
134 ).toBe(failed);
135 });
136
137 it("does not carry setup into another thread or invent it for existing threads", () => {
138 expect(
139 resolvePendingThreadCreation({
140 threadKey: "another-thread",
141 pending: null,
142 previous: pending,
143 detail: null,
144 }),
145 ).toBeNull();
146 expect(
147 resolvePendingThreadCreation({
148 threadKey,
149 pending: null,
150 previous: null,
151 detail: null,
152 }),
153 ).toBeNull();
154 });
155});
156
157describe("pendingThreadCreationShell", () => {
158 it("shapes a queued creation as the thread shell the screen renders before creation", () => {
159 expect(pendingThreadCreationShell(creation)).toMatchObject({
160 environmentId: creation.environmentId,
161 id: creation.threadId,
162 projectId: creation.creation!.projectId,
163 title: "Fix the flaky login test",
164 modelSelection: creation.modelSelection,
165 runtimeMode: "full-access",
166 interactionMode: "default",
167 branch: "main",
168 worktreePath: null,
169 latestRun: null,
170 runtime: null,
171 latestUserMessageAt: creation.createdAt,
172 });
173 });
174
175 it("keeps a local task's explicit worktree path", () => {
176 expect(
177 pendingThreadCreationShell({
178 ...creation,
179 creation: {
180 ...creation.creation!,
181 workspaceMode: "local",
182 worktreePath: "/repo/.worktrees/feature",
183 },
184 })?.worktreePath,
185 ).toBe("/repo/.worktrees/feature");
186 });
187
188 it("returns null for a follow-up message or a creation without a model", () => {
189 expect(pendingThreadCreationShell({ ...creation, creation: undefined })).toBeNull();
190 expect(pendingThreadCreationShell({ ...creation, modelSelection: undefined })).toBeNull();
191 });
192});
193
194describe("isPendingThreadCreationVisible", () => {
195 const creationMessageId = String(creation.messageId);
196
197 it("stands in before any detail has loaded", () => {
198 expect(isPendingThreadCreationVisible({ creationMessageId, loadedMessageIds: null })).toBe(
199 true,
200 );
201 });
202
203 it("keeps standing in while the created thread has no messages yet", () => {
204 expect(isPendingThreadCreationVisible({ creationMessageId, loadedMessageIds: [] })).toBe(true);
205 });
206
207 it("keeps standing in when the thread holds only unrelated messages", () => {
208 expect(
209 isPendingThreadCreationVisible({ creationMessageId, loadedMessageIds: ["someone-else"] }),
210 ).toBe(true);
211 });
212
213 it("stands down once the delivered prompt lands under the same id", () => {
214 expect(
215 isPendingThreadCreationVisible({
216 creationMessageId,
217 loadedMessageIds: ["someone-else", creationMessageId],
218 }),
219 ).toBe(false);
220 });
221});
222
223describe("pendingThreadCreationMessage", () => {
224 it("keeps inline context available while the thread is being created", () => {
225 const record = {
226 version: 1 as const,
227 kind: "mention" as const,
228 contextId: ComposerContextId.make("setup-file"),
229 label: "Checkout.tsx",
230 path: "src/Checkout.tsx",
231 };
232 const context = { version: 1 as const, records: [record] };
233 const text = "[Checkout.tsx](t3-context://v1/mention/setup-file)";
234 const message = pendingThreadCreationMessage({ ...creation, text, context });
235 expect(message.text).toBe(text);
236 expect(message.context).toEqual(context);
237 });
238
239 it("renders the queued prompt as the first user message", () => {
240 expect(pendingThreadCreationMessage(creation)).toEqual({
241 id: creation.messageId,
242 role: "user",
243 text: creation.text,
244 streaming: false,
245 createdAt: creation.createdAt,
246 updatedAt: creation.createdAt,
247 });
248 });
249
250 it("omits the queued attachments rather than passing local draft ids to the feed", () => {
251 expect(pendingThreadCreationMessage(creation)).not.toHaveProperty("attachments");
252 });
253});