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

Sidebar.logic.test.ts

201 lines6.2 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 },
145 ],
146 session: {
147 ...baseThread.session,
148 status: "ready",
149 orchestrationStatus: "ready",
150 },
151 },
152 hasPendingApprovals: false,
153 hasPendingUserInput: false,
154 }),
155 ).toMatchObject({ label: "Plan Ready", pulse: false });
156 });
157​
158 it("shows completed when there is an unseen completion and no active blocker", () => {
159 expect(
160 resolveThreadStatusPill({
161 thread: {
162 ...baseThread,
163 interactionMode: "default",
164 latestTurn: makeLatestTurn(),
165 lastVisitedAt: "2026-03-09T10:04:00.000Z",
166 session: {
167 ...baseThread.session,
168 status: "ready",
169 orchestrationStatus: "ready",
170 },
171 },
172 hasPendingApprovals: false,
173 hasPendingUserInput: false,
174 }),
175 ).toMatchObject({ label: "Completed", pulse: false });
176 });
177});
178​
179describe("resolveThreadRowClassName", () => {
180 it("uses the darker selected palette when a thread is both selected and active", () => {
181 const className = resolveThreadRowClassName({ isActive: true, isSelected: true });
182 expect(className).toContain("bg-primary/22");
183 expect(className).toContain("hover:bg-primary/26");
184 expect(className).toContain("dark:bg-primary/30");
185 expect(className).not.toContain("bg-accent/85");
186 });
187​
188 it("uses selected hover colors for selected threads", () => {
189 const className = resolveThreadRowClassName({ isActive: false, isSelected: true });
190 expect(className).toContain("bg-primary/15");
191 expect(className).toContain("hover:bg-primary/19");
192 expect(className).toContain("dark:bg-primary/22");
193 expect(className).not.toContain("hover:bg-accent");
194 });
195​
196 it("keeps the accent palette for active-only threads", () => {
197 const className = resolveThreadRowClassName({ isActive: true, isSelected: false });
198 expect(className).toContain("bg-accent/85");
199 expect(className).toContain("hover:bg-accent");
200 });
201});