Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

Sidebar.logic.test.ts

232 lines7.1 KB

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