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