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