Open History to see the last commit
1import { describe, expect, it } from "vitest";
2
3import {
4 hasUnseenCompletion,
5 resolveProjectStatusIndicator,
6 resolveSidebarNewThreadEnvMode,
7 resolveThreadRowClassName,
8 resolveThreadStatusPill,
9 shouldClearThreadSelectionOnMouseDown,
10} from "./Sidebar.logic";
11
12function makeLatestTurn(overrides?: {
13 completedAt?: string | null;
14 startedAt?: string | null;
15}): Parameters<typeof hasUnseenCompletion>[0]["latestTurn"] {
16 return {
17 turnId: "turn-1" as never,
18 state: "completed",
19 assistantMessageId: null,
20 requestedAt: "2026-03-09T10:00:00.000Z",
21 startedAt: overrides?.startedAt ?? "2026-03-09T10:00:00.000Z",
22 completedAt: overrides?.completedAt ?? "2026-03-09T10:05:00.000Z",
23 };
24}
25
26describe("hasUnseenCompletion", () => {
27 it("returns true when a thread completed after its last visit", () => {
28 expect(
29 hasUnseenCompletion({
30 interactionMode: "default",
31 latestTurn: makeLatestTurn(),
32 lastVisitedAt: "2026-03-09T10:04:00.000Z",
33 proposedPlans: [],
34 session: null,
35 }),
36 ).toBe(true);
37 });
38});
39
40describe("shouldClearThreadSelectionOnMouseDown", () => {
41 it("preserves selection for thread items", () => {
42 const child = {
43 closest: (selector: string) =>
44 selector.includes("[data-thread-item]") ? ({} as Element) : null,
45 } as unknown as HTMLElement;
46
47 expect(shouldClearThreadSelectionOnMouseDown(child)).toBe(false);
48 });
49
50 it("preserves selection for thread list toggle controls", () => {
51 const selectionSafe = {
52 closest: (selector: string) =>
53 selector.includes("[data-thread-selection-safe]") ? ({} as Element) : null,
54 } as unknown as HTMLElement;
55
56 expect(shouldClearThreadSelectionOnMouseDown(selectionSafe)).toBe(false);
57 });
58
59 it("clears selection for unrelated sidebar clicks", () => {
60 const unrelated = {
61 closest: () => null,
62 } as unknown as HTMLElement;
63
64 expect(shouldClearThreadSelectionOnMouseDown(unrelated)).toBe(true);
65 });
66});
67
68describe("resolveSidebarNewThreadEnvMode", () => {
69 it("uses the app default when the caller does not request a specific mode", () => {
70 expect(
71 resolveSidebarNewThreadEnvMode({
72 defaultEnvMode: "worktree",
73 }),
74 ).toBe("worktree");
75 });
76
77 it("preserves an explicit requested mode over the app default", () => {
78 expect(
79 resolveSidebarNewThreadEnvMode({
80 requestedEnvMode: "local",
81 defaultEnvMode: "worktree",
82 }),
83 ).toBe("local");
84 });
85});
86
87describe("resolveThreadStatusPill", () => {
88 const baseThread = {
89 interactionMode: "plan" as const,
90 latestTurn: null,
91 lastVisitedAt: undefined,
92 proposedPlans: [],
93 session: {
94 provider: "codex" as const,
95 status: "running" as const,
96 createdAt: "2026-03-09T10:00:00.000Z",
97 updatedAt: "2026-03-09T10:00:00.000Z",
98 orchestrationStatus: "running" as const,
99 },
100 };
101
102 it("shows pending approval before all other statuses", () => {
103 expect(
104 resolveThreadStatusPill({
105 thread: baseThread,
106 hasPendingApprovals: true,
107 hasPendingUserInput: true,
108 }),
109 ).toMatchObject({ label: "Pending Approval", pulse: false });
110 });
111
112 it("shows awaiting input when plan mode is blocked on user answers", () => {
113 expect(
114 resolveThreadStatusPill({
115 thread: baseThread,
116 hasPendingApprovals: false,
117 hasPendingUserInput: true,
118 }),
119 ).toMatchObject({ label: "Awaiting Input", pulse: false });
120 });
121
122 it("falls back to working when the thread is actively running without blockers", () => {
123 expect(
124 resolveThreadStatusPill({
125 thread: baseThread,
126 hasPendingApprovals: false,
127 hasPendingUserInput: false,
128 }),
129 ).toMatchObject({ label: "Working", pulse: true });
130 });
131
132 it("shows plan ready when a settled plan turn has a proposed plan ready for follow-up", () => {
133 expect(
134 resolveThreadStatusPill({
135 thread: {
136 ...baseThread,
137 latestTurn: makeLatestTurn(),
138 proposedPlans: [
139 {
140 id: "plan-1" as never,
141 turnId: "turn-1" as never,
142 createdAt: "2026-03-09T10:00:00.000Z",
143 updatedAt: "2026-03-09T10:05:00.000Z",
144 planMarkdown: "# Plan",
145 implementedAt: null,
146 implementationThreadId: null,
147 },
148 ],
149 session: {
150 ...baseThread.session,
151 status: "ready",
152 orchestrationStatus: "ready",
153 },
154 },
155 hasPendingApprovals: false,
156 hasPendingUserInput: false,
157 }),
158 ).toMatchObject({ label: "Plan Ready", pulse: false });
159 });
160
161 it("does not show plan ready after the proposed plan was implemented elsewhere", () => {
162 expect(
163 resolveThreadStatusPill({
164 thread: {
165 ...baseThread,
166 latestTurn: makeLatestTurn(),
167 proposedPlans: [
168 {
169 id: "plan-1" as never,
170 turnId: "turn-1" as never,
171 createdAt: "2026-03-09T10:00:00.000Z",
172 updatedAt: "2026-03-09T10:05:00.000Z",
173 planMarkdown: "# Plan",
174 implementedAt: "2026-03-09T10:06:00.000Z",
175 implementationThreadId: "thread-implement" as never,
176 },
177 ],
178 session: {
179 ...baseThread.session,
180 status: "ready",
181 orchestrationStatus: "ready",
182 },
183 },
184 hasPendingApprovals: false,
185 hasPendingUserInput: false,
186 }),
187 ).toMatchObject({ label: "Completed", pulse: false });
188 });
189
190 it("shows completed when there is an unseen completion and no active blocker", () => {
191 expect(
192 resolveThreadStatusPill({
193 thread: {
194 ...baseThread,
195 interactionMode: "default",
196 latestTurn: makeLatestTurn(),
197 lastVisitedAt: "2026-03-09T10:04:00.000Z",
198 session: {
199 ...baseThread.session,
200 status: "ready",
201 orchestrationStatus: "ready",
202 },
203 },
204 hasPendingApprovals: false,
205 hasPendingUserInput: false,
206 }),
207 ).toMatchObject({ label: "Completed", pulse: false });
208 });
209});
210
211describe("resolveThreadRowClassName", () => {
212 it("uses the darker selected palette when a thread is both selected and active", () => {
213 const className = resolveThreadRowClassName({ isActive: true, isSelected: true });
214 expect(className).toContain("bg-primary/22");
215 expect(className).toContain("hover:bg-primary/26");
216 expect(className).toContain("dark:bg-primary/30");
217 expect(className).not.toContain("bg-accent/85");
218 });
219
220 it("uses selected hover colors for selected threads", () => {
221 const className = resolveThreadRowClassName({ isActive: false, isSelected: true });
222 expect(className).toContain("bg-primary/15");
223 expect(className).toContain("hover:bg-primary/19");
224 expect(className).toContain("dark:bg-primary/22");
225 expect(className).not.toContain("hover:bg-accent");
226 });
227
228 it("keeps the accent palette for active-only threads", () => {
229 const className = resolveThreadRowClassName({ isActive: true, isSelected: false });
230 expect(className).toContain("bg-accent/85");
231 expect(className).toContain("hover:bg-accent");
232 });
233});
234
235describe("resolveProjectStatusIndicator", () => {
236 it("returns null when no threads have a notable status", () => {
237 expect(resolveProjectStatusIndicator([null, null])).toBeNull();
238 });
239
240 it("surfaces the highest-priority actionable state across project threads", () => {
241 expect(
242 resolveProjectStatusIndicator([
243 {
244 label: "Completed",
245 colorClass: "text-emerald-600",
246 dotClass: "bg-emerald-500",
247 pulse: false,
248 },
249 {
250 label: "Pending Approval",
251 colorClass: "text-amber-600",
252 dotClass: "bg-amber-500",
253 pulse: false,
254 },
255 {
256 label: "Working",
257 colorClass: "text-sky-600",
258 dotClass: "bg-sky-500",
259 pulse: true,
260 },
261 ]),
262 ).toMatchObject({ label: "Pending Approval", dotClass: "bg-amber-500" });
263 });
264
265 it("prefers plan-ready over completed when no stronger action is needed", () => {
266 expect(
267 resolveProjectStatusIndicator([
268 {
269 label: "Completed",
270 colorClass: "text-emerald-600",
271 dotClass: "bg-emerald-500",
272 pulse: false,
273 },
274 {
275 label: "Plan Ready",
276 colorClass: "text-violet-600",
277 dotClass: "bg-violet-500",
278 pulse: false,
279 },
280 ]),
281 ).toMatchObject({ label: "Plan Ready", dotClass: "bg-violet-500" });
282 });
283});