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