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: [], runs: [] },
64 });
65 expect(previous).toBe(pending);
66
67 previous = resolvePendingThreadCreation({
68 threadKey,
69 pending,
70 previous,
71 detail: { messages: [prompt], runs: [] },
72 });
73 expect(previous).toBe(pending);
74
75 previous = resolvePendingThreadCreation({
76 threadKey,
77 pending: null,
78 previous,
79 detail: { messages: [prompt], runs: [] },
80 });
81 expect(previous).toBe(pending);
82
83 expect(
84 resolvePendingThreadCreation({
85 threadKey,
86 pending: null,
87 previous,
88 detail: {
89 messages: [prompt],
90 runs: [{ status: "running" }],
91 },
92 }),
93 ).toBeNull();
94 });
95
96 it("keeps the prompt until both the turn and its message have arrived", () => {
97 expect(
98 resolvePendingThreadCreation({
99 threadKey,
100 pending,
101 previous: null,
102 detail: { messages: [], runs: [{ status: "running" }] },
103 }),
104 ).toBe(pending);
105 });
106
107 it("preserves rejected task recovery", () => {
108 const failed: PendingThreadCreation = {
109 message: creation,
110 outcome: { kind: "failed", message: creation, reason: "Checkout failed" },
111 };
112 expect(
113 resolvePendingThreadCreation({
114 threadKey,
115 pending: failed,
116 previous: pending,
117 detail: { messages: [], runs: [] },
118 }),
119 ).toBe(failed);
120 });
121
122 it("does not carry setup into another thread or invent it for existing threads", () => {
123 expect(
124 resolvePendingThreadCreation({
125 threadKey: "another-thread",
126 pending: null,
127 previous: pending,
128 detail: null,
129 }),
130 ).toBeNull();
131 expect(
132 resolvePendingThreadCreation({
133 threadKey,
134 pending: null,
135 previous: null,
136 detail: null,
137 }),
138 ).toBeNull();
139 });
140});
141
142describe("selectThreadCreationDetail", () => {
143 const threadKey = `${creation.environmentId}:${creation.threadId}`;
144 const threadRef = { environmentId: creation.environmentId, threadId: creation.threadId };
145
146 it("withholds the detail ref until the creation is delivered", () => {
147 const queued = selectThreadCreationDetail({
148 threadRef,
149 threadKey,
150 queuedMessagesByThreadKey: { [threadKey]: [creation] },
151 outcomes: {},
152 });
153 expect(queued).toEqual({
154 pendingCreation: { message: creation, outcome: null },
155 detailRef: null,
156 });
157
158 const failed = selectThreadCreationDetail({
159 threadRef,
160 threadKey,
161 queuedMessagesByThreadKey: {},
162 outcomes: { [threadKey]: { kind: "failed", message: creation, reason: "Checkout failed" } },
163 });
164 expect(failed.pendingCreation?.outcome?.kind).toBe("failed");
165 expect(failed.detailRef).toBeNull();
166
167 const delivered = selectThreadCreationDetail({
168 threadRef,
169 threadKey,
170 queuedMessagesByThreadKey: {},
171 outcomes: { [threadKey]: { kind: "delivered", message: creation } },
172 });
173 expect(delivered.pendingCreation?.outcome?.kind).toBe("delivered");
174 expect(delivered.detailRef).toBe(threadRef);
175 });
176
177 it("does not hold back existing threads with only follow-ups queued", () => {
178 const { creation: _creation, ...followUp } = creation;
179 expect(
180 selectThreadCreationDetail({
181 threadRef,
182 threadKey,
183 queuedMessagesByThreadKey: { [threadKey]: [followUp] },
184 outcomes: {},
185 }),
186 ).toEqual({ pendingCreation: null, detailRef: threadRef });
187 });
188});