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 resolvePendingThreadCreation,
13 selectThreadCreationDetail,
14 type PendingThreadCreation,
15} from "./pending-thread-creation";
16import type { QueuedThreadMessage } from "./thread-outbox-model";
17
18const creation: QueuedThreadMessage = {
19 environmentId: EnvironmentId.make("environment-1"),
20 threadId: ThreadId.make("thread-1"),
21 messageId: MessageId.make("message-1"),
22 commandId: CommandId.make("command-1"),
23 text: "Fix the flaky login test",
24 attachments: [
25 {
26 id: "draft-image",
27 type: "image",
28 name: "screen.png",
29 mimeType: "image/png",
30 sizeBytes: 10,
31 previewUri: "data:image/png;base64,AAAA",
32 },
33 ],
34 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.6-sol" },
35 runtimeMode: "full-access",
36 creation: {
37 projectId: ProjectId.make("project-1"),
38 workspaceMode: "worktree",
39 branch: "main",
40 worktreePath: null,
41 },
42 createdAt: "2026-08-24T12:00:00.000Z",
43};
44
45describe("resolvePendingThreadCreation", () => {
46 const threadKey = `${creation.environmentId}:${creation.threadId}`;
47 const pending: PendingThreadCreation = { message: creation, outcome: null };
48 const prompt = { id: creation.messageId };
49
50 it("keeps setup visible through the prompt echo and shell cleanup until detail has a turn", () => {
51 let previous = resolvePendingThreadCreation({
52 threadKey,
53 pending,
54 previous: null,
55 detail: null,
56 });
57 expect(previous).toBe(pending);
58
59 previous = resolvePendingThreadCreation({
60 threadKey,
61 pending,
62 previous,
63 detail: { messages: [], latestTurn: null, session: null },
64 });
65 expect(previous).toBe(pending);
66
67 previous = resolvePendingThreadCreation({
68 threadKey,
69 pending,
70 previous,
71 detail: { messages: [prompt], latestTurn: null, session: { status: "starting" } },
72 });
73 expect(previous).toBe(pending);
74
75 previous = resolvePendingThreadCreation({
76 threadKey,
77 pending: null,
78 previous,
79 detail: { messages: [prompt], latestTurn: null, session: { status: "starting" } },
80 });
81 expect(previous).toBe(pending);
82
83 expect(
84 resolvePendingThreadCreation({
85 threadKey,
86 pending: null,
87 previous,
88 detail: {
89 messages: [prompt],
90 latestTurn: { turnId: "turn-1" },
91 session: { status: "running" },
92 },
93 }),
94 ).toBeNull();
95 });
96
97 it("keeps the prompt until both the turn and its message have arrived", () => {
98 expect(
99 resolvePendingThreadCreation({
100 threadKey,
101 pending,
102 previous: null,
103 detail: { messages: [], latestTurn: { turnId: "turn-1" }, session: { status: "running" } },
104 }),
105 ).toBe(pending);
106 });
107
108 it("preserves rejected task recovery", () => {
109 const failed: PendingThreadCreation = {
110 message: creation,
111 outcome: { kind: "failed", message: creation, reason: "Checkout failed" },
112 };
113 expect(
114 resolvePendingThreadCreation({
115 threadKey,
116 pending: failed,
117 previous: pending,
118 detail: { messages: [], latestTurn: null, session: { status: "error" } },
119 }),
120 ).toBe(failed);
121 });
122
123 it("does not carry setup into another thread or invent it for existing threads", () => {
124 expect(
125 resolvePendingThreadCreation({
126 threadKey: "another-thread",
127 pending: null,
128 previous: pending,
129 detail: null,
130 }),
131 ).toBeNull();
132 expect(
133 resolvePendingThreadCreation({
134 threadKey,
135 pending: null,
136 previous: null,
137 detail: null,
138 }),
139 ).toBeNull();
140 });
141});
142
143describe("selectThreadCreationDetail", () => {
144 const threadKey = `${creation.environmentId}:${creation.threadId}`;
145 const threadRef = { environmentId: creation.environmentId, threadId: creation.threadId };
146
147 it("withholds the detail ref until the creation is delivered", () => {
148 const queued = selectThreadCreationDetail({
149 threadRef,
150 threadKey,
151 queuedMessagesByThreadKey: { [threadKey]: [creation] },
152 outcomes: {},
153 });
154 expect(queued).toEqual({
155 pendingCreation: { message: creation, outcome: null },
156 detailRef: null,
157 });
158
159 const failed = selectThreadCreationDetail({
160 threadRef,
161 threadKey,
162 queuedMessagesByThreadKey: {},
163 outcomes: { [threadKey]: { kind: "failed", message: creation, reason: "Checkout failed" } },
164 });
165 expect(failed.pendingCreation?.outcome?.kind).toBe("failed");
166 expect(failed.detailRef).toBeNull();
167
168 const delivered = selectThreadCreationDetail({
169 threadRef,
170 threadKey,
171 queuedMessagesByThreadKey: {},
172 outcomes: { [threadKey]: { kind: "delivered", message: creation } },
173 });
174 expect(delivered.pendingCreation?.outcome?.kind).toBe("delivered");
175 expect(delivered.detailRef).toBe(threadRef);
176 });
177
178 it("does not hold back existing threads with only follow-ups queued", () => {
179 const { creation: _creation, ...followUp } = creation;
180 expect(
181 selectThreadCreationDetail({
182 threadRef,
183 threadKey,
184 queuedMessagesByThreadKey: { [threadKey]: [followUp] },
185 outcomes: {},
186 }),
187 ).toEqual({ pendingCreation: null, detailRef: threadRef });
188 });
189});