Open History to see the last commit
1import {
2 CommandId,
3 EnvironmentId,
4 MessageId,
5 ThreadId,
6 type WorktreeSetupSnapshot,
7} from "@t3tools/contracts";
8import { resolveVisibleWorktreeSetup } from "@t3tools/client-runtime/worktree-setup";
9import { describe, expect, it } from "vite-plus/test";
10import * as DateTime from "effect/DateTime";
11import { appendPendingThreadMessages } from "./pending-thread-feed";
12import {
13 resolveWorktreeSetupAnchorIndex,
14 resolveWorktreeSetupSnapshot,
15 worktreeSetupCardMode,
16} from "./worktree-setup-state";
17
18const threadId = ThreadId.make("setup-thread");
19const hold = (snapshot: WorktreeSetupSnapshot) => ({ snapshot, released: false });
20const running: WorktreeSetupSnapshot = {
21 threadId,
22 sequence: 1,
23 phase: "running",
24 startedAt: "2026-09-17T00:00:00.000Z",
25 endedAt: null,
26 branch: "feature",
27 baseRef: "main",
28 worktreePath: null,
29 setupScript: null,
30 stages: [],
31 error: null,
32};
33
34const run = (
35 userMessageId: string,
36 requestedAt: string,
37 startedAt: string | null,
38 status: "running" | "queued" | "completed" = "completed",
39) => ({
40 userMessageId: MessageId.make(userMessageId),
41 status,
42 requestedAt: DateTime.makeUnsafe(requestedAt),
43 startedAt: startedAt === null ? null : DateTime.makeUnsafe(startedAt),
44});
45const firstMessage = { id: "first", createdAt: running.startedAt };
46const firstRun = run("first", running.startedAt, running.startedAt);
47
48describe("mobile worktree setup handoff", () => {
49 it("keeps the setup identity after its card retires so the working header can take over", () => {
50 const done = { ...running, phase: "done" as const, sequence: 2 };
51 const held = resolveWorktreeSetupSnapshot(threadId, done, hold(running));
52 expect(held?.snapshot).toBe(done);
53 expect(
54 resolveVisibleWorktreeSetup({
55 live: done,
56 recorded: null,
57 runs: [],
58 userMessages: [firstMessage],
59 }),
60 ).toBe(done);
61 expect(
62 resolveVisibleWorktreeSetup({
63 live: done,
64 recorded: null,
65 runs: [firstRun],
66 userMessages: [firstMessage],
67 }),
68 ).toBeNull();
69 const afterFirstMessageEdit = {
70 live: done,
71 recorded: null,
72 userMessages: [firstMessage],
73 runs: [firstRun, run("first", running.startedAt, null)],
74 };
75 expect(resolveVisibleWorktreeSetup(afterFirstMessageEdit)).toBeNull();
76 expect(resolveWorktreeSetupSnapshot(threadId, undefined, held)).toBe(held);
77 });
78
79 it.each(["failed", "cancelled"] as const)(
80 "retains %s details after the stream closes",
81 (phase) => {
82 const settled = { ...running, phase, sequence: 3, endedAt: "2026-09-17T00:00:30.000Z" };
83 const held = resolveWorktreeSetupSnapshot(threadId, settled, hold(running));
84 expect(resolveWorktreeSetupSnapshot(threadId, null, held)?.snapshot).toBe(settled);
85 expect(resolveWorktreeSetupSnapshot(threadId, running, held)).toBe(held);
86 expect(
87 resolveVisibleWorktreeSetup({
88 live: settled,
89 recorded: null,
90 runs: [],
91 userMessages: [firstMessage],
92 }),
93 ).toBe(settled);
94 expect(
95 resolveVisibleWorktreeSetup({
96 live: settled,
97 recorded: null,
98 runs: [],
99 userMessages: [firstMessage, { id: "follow-up", createdAt: "2026-09-17T00:01:00.000Z" }],
100 }),
101 ).toBeNull();
102 },
103 );
104
105 it("keeps a setup failure that settles after the latest follow-up was sent", () => {
106 const failedScript: WorktreeSetupSnapshot = {
107 ...running,
108 phase: "done",
109 sequence: 4,
110 endedAt: "2026-09-17T00:02:00.000Z",
111 stages: [
112 {
113 id: "setup-script",
114 status: "failed",
115 startedAt: null,
116 endedAt: null,
117 percent: null,
118 detail: null,
119 tail: [],
120 },
121 ],
122 };
123 const visible = (
124 snapshot: WorktreeSetupSnapshot,
125 followUp: { sentAt: string; startedAt: string | null; status?: "running" | "queued" },
126 shown = true,
127 ) =>
128 resolveVisibleWorktreeSetup({
129 live: snapshot,
130 recorded: null,
131 runs: [firstRun, run("follow-up", followUp.sentAt, followUp.startedAt, followUp.status)],
132 userMessages: shown
133 ? [firstMessage, { id: "follow-up", createdAt: followUp.sentAt }]
134 : [firstMessage],
135 });
136 // Sent before the failure, its run started only afterwards.
137 expect(
138 visible(failedScript, {
139 sentAt: "2026-09-17T00:01:00.000Z",
140 startedAt: "2026-09-17T00:03:00.000Z",
141 }),
142 ).toBe(failedScript);
143 // Still queued and only shown in the queue, not the transcript.
144 expect(
145 visible(
146 failedScript,
147 { sentAt: "2026-09-17T00:01:00.000Z", startedAt: null, status: "queued" },
148 false,
149 ),
150 ).toBe(failedScript);
151 // A follow-up sent after the failure, such as a steer into the running turn, retires it.
152 expect(
153 visible(failedScript, {
154 sentAt: "2026-09-17T00:03:00.000Z",
155 startedAt: "2026-09-17T00:03:00.000Z",
156 }),
157 ).toBeNull();
158 expect(
159 visible(
160 failedScript,
161 { sentAt: "2026-09-17T00:03:00.000Z", startedAt: null, status: "queued" },
162 false,
163 ),
164 ).toBeNull();
165 expect(
166 visible(
167 { ...failedScript, stages: [] },
168 { sentAt: "2026-09-17T00:01:00.000Z", startedAt: "2026-09-17T00:01:00.000Z" },
169 ),
170 ).toBeNull();
171 });
172
173 it("anchors a setup that settles after a follow-up at the end of that turn", () => {
174 const queued = (id: string, createdAt: string) => ({
175 environmentId: EnvironmentId.make("env"),
176 threadId,
177 messageId: MessageId.make(id),
178 commandId: CommandId.make(id),
179 text: id,
180 attachments: [],
181 createdAt,
182 });
183 const [first, followUp] = appendPendingThreadMessages(
184 [],
185 [],
186 [queued("first", running.startedAt), queued("follow-up", "2026-09-17T00:01:00.000Z")],
187 );
188 if (first?.type !== "message" || followUp?.type !== "message") {
189 throw new Error("Expected message rows");
190 }
191 const reply = (id: string) => ({
192 ...first,
193 id,
194 message: { ...first.message, id: MessageId.make(id), role: "assistant" as const },
195 });
196 const feed = [first, reply("reply-1"), followUp, reply("reply-2")];
197 const settled = (endedAt: string) => ({ ...running, phase: "done" as const, endedAt });
198 expect(resolveWorktreeSetupAnchorIndex(feed, settled("2026-09-17T00:02:00.000Z"))).toBe(3);
199 expect(resolveWorktreeSetupAnchorIndex(feed, settled("2026-09-17T00:00:30.000Z"))).toBe(0);
200 expect(resolveWorktreeSetupAnchorIndex(feed, null)).toBe(0);
201 });
202
203 it("keeps handed-off running setup in view when no Working fold carries its chip", () => {
204 expect(worktreeSetupCardMode(running, true, true)).toBeNull();
205 expect(worktreeSetupCardMode(running, true, false)).toBe("card");
206 expect(worktreeSetupCardMode({ ...running, phase: "done" }, true, false)).toBeNull();
207 });
208
209 it("does not carry a previous thread's progress across navigation", () => {
210 const otherId = ThreadId.make("other-thread");
211 expect(resolveWorktreeSetupSnapshot(otherId, running, hold(running))).toBeNull();
212 const other = { ...running, threadId: otherId };
213 expect(resolveWorktreeSetupSnapshot(otherId, other, hold(running))?.snapshot).toBe(other);
214 });
215
216 it("lets a retry replace a failure the tracker dropped after its sequence reset", () => {
217 const failed = { ...running, phase: "failed" as const, sequence: 9 };
218 const retry = { ...running, sequence: 0 };
219 const dropped = resolveWorktreeSetupSnapshot(threadId, null, hold(failed));
220 expect(dropped?.snapshot).toBe(failed);
221 expect(resolveWorktreeSetupSnapshot(threadId, retry, dropped)?.snapshot).toBe(retry);
222 });
223});