Open History to see the last commit
1import { describe, expect, it, vi } from "vite-plus/test";
2import { EnvironmentId, ProjectId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
3import type { Thread } from "../types";
4import {
5 buildBrowseGroups,
6 buildThreadActionItems,
7 enumerateCommandPaletteItems,
8 filterCommandPaletteGroups,
9 reduceCommandPaletteUiState,
10 type CommandPaletteGroup,
11} from "./CommandPalette.logic";
12
13describe("reduceCommandPaletteUiState", () => {
14 const closedState = { open: false, mode: "command", openIntent: null } as const;
15
16 it("toggles each overlay mode open and closed", () => {
17 const filesOpen = reduceCommandPaletteUiState(closedState, {
18 _tag: "ToggleMode",
19 mode: "files",
20 });
21 expect(filesOpen).toEqual({ open: true, mode: "files", openIntent: null });
22
23 const contentOpen = reduceCommandPaletteUiState(filesOpen, {
24 _tag: "ToggleMode",
25 mode: "content",
26 });
27 expect(contentOpen).toEqual({ open: true, mode: "content", openIntent: null });
28
29 expect(
30 reduceCommandPaletteUiState(contentOpen, { _tag: "ToggleMode", mode: "content" }),
31 ).toEqual({ open: false, mode: "command", openIntent: null });
32 });
33
34 it("switches between open modes without closing", () => {
35 const filesOpen = reduceCommandPaletteUiState(closedState, {
36 _tag: "ToggleMode",
37 mode: "files",
38 });
39 expect(reduceCommandPaletteUiState(filesOpen, { _tag: "ToggleMode", mode: "command" })).toEqual(
40 {
41 open: true,
42 mode: "command",
43 openIntent: null,
44 },
45 );
46 });
47
48 it("routes open intents to command mode", () => {
49 const filesOpen = reduceCommandPaletteUiState(closedState, {
50 _tag: "ToggleMode",
51 mode: "files",
52 });
53 expect(reduceCommandPaletteUiState(filesOpen, { _tag: "OpenAddProject" })).toEqual({
54 open: true,
55 mode: "command",
56 openIntent: { kind: "add-project" },
57 });
58 expect(reduceCommandPaletteUiState(filesOpen, { _tag: "OpenNewThreadIn" })).toEqual({
59 open: true,
60 mode: "command",
61 openIntent: { kind: "new-thread-in" },
62 });
63 });
64
65 it("resets to command mode for dialog-driven opens and closes", () => {
66 const filesOpen = reduceCommandPaletteUiState(closedState, {
67 _tag: "ToggleMode",
68 mode: "files",
69 });
70
71 expect(reduceCommandPaletteUiState(filesOpen, { _tag: "SetOpen", open: false })).toEqual({
72 open: false,
73 mode: "command",
74 openIntent: null,
75 });
76 expect(reduceCommandPaletteUiState(filesOpen, { _tag: "SetOpen", open: true })).toEqual({
77 open: true,
78 mode: "command",
79 openIntent: null,
80 });
81 });
82});
83
84describe("enumerateCommandPaletteItems", () => {
85 it("assigns positional jump shortcuts to the first nine displayed items", () => {
86 const items = Array.from({ length: 10 }, (_, index) => ({
87 kind: "action" as const,
88 value: `project-${index + 1}`,
89 searchTerms: [],
90 title: `Project ${index + 1}`,
91 icon: null,
92 shortcutCommand: "chat.new" as const,
93 run: async () => undefined,
94 }));
95
96 expect(enumerateCommandPaletteItems(items).map((item) => item.shortcutCommand)).toEqual([
97 "thread.jump.1",
98 "thread.jump.2",
99 "thread.jump.3",
100 "thread.jump.4",
101 "thread.jump.5",
102 "thread.jump.6",
103 "thread.jump.7",
104 "thread.jump.8",
105 "thread.jump.9",
106 undefined,
107 ]);
108 });
109});
110
111const LOCAL_ENVIRONMENT_ID = EnvironmentId.make("environment-local");
112const PROJECT_ID = ProjectId.make("project-1");
113
114function makeThread(overrides: Partial<Thread> = {}): Thread {
115 return {
116 id: ThreadId.make("thread-1"),
117 environmentId: LOCAL_ENVIRONMENT_ID,
118 projectId: PROJECT_ID,
119 title: "Thread",
120 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },
121 runtimeMode: "full-access",
122 interactionMode: "default",
123 session: null,
124 messages: [],
125 proposedPlans: [],
126 createdAt: "2026-03-01T00:00:00.000Z",
127 archivedAt: null,
128 settledOverride: null,
129 settledAt: null,
130 deletedAt: null,
131 updatedAt: "2026-03-01T00:00:00.000Z",
132 latestTurn: null,
133 branch: null,
134 worktreePath: null,
135 checkpoints: [],
136 activities: [],
137 ...overrides,
138 };
139}
140
141describe("buildThreadActionItems", () => {
142 it("orders threads by most recent activity and formats timestamps from updatedAt", () => {
143 vi.useFakeTimers();
144 vi.setSystemTime(new Date("2026-03-25T12:00:00.000Z"));
145
146 try {
147 const items = buildThreadActionItems({
148 threads: [
149 makeThread({
150 id: ThreadId.make("thread-older"),
151 title: "Older thread",
152 updatedAt: "2026-03-24T12:00:00.000Z",
153 }),
154 makeThread({
155 id: ThreadId.make("thread-newer"),
156 title: "Newer thread",
157 createdAt: "2026-03-20T00:00:00.000Z",
158 updatedAt: "2026-03-20T00:00:00.000Z",
159 }),
160 ],
161 projectTitleById: new Map([[PROJECT_ID, "Project"]]),
162 sortOrder: "updated_at",
163 icon: null,
164 runThread: async (_thread) => undefined,
165 });
166
167 expect(items.map((item) => item.value)).toEqual([
168 "thread:thread-older",
169 "thread:thread-newer",
170 ]);
171 expect(items[0]?.timestamp).toBe("1d ago");
172 expect(items[1]?.timestamp).toBe("5d ago");
173 } finally {
174 vi.useRealTimers();
175 }
176 });
177
178 it("ranks thread title matches ahead of contextual project-name matches", () => {
179 const threadItems = buildThreadActionItems({
180 threads: [
181 makeThread({
182 id: ThreadId.make("thread-context-match"),
183 title: "Fix navbar spacing",
184 updatedAt: "2026-03-20T00:00:00.000Z",
185 }),
186 makeThread({
187 id: ThreadId.make("thread-title-match"),
188 title: "Project kickoff notes",
189 createdAt: "2026-03-02T00:00:00.000Z",
190 updatedAt: "2026-03-19T00:00:00.000Z",
191 }),
192 ],
193 projectTitleById: new Map([[PROJECT_ID, "Project"]]),
194 sortOrder: "updated_at",
195 icon: null,
196 runThread: async (_thread) => undefined,
197 });
198
199 const groups = filterCommandPaletteGroups({
200 activeGroups: [],
201 query: "project",
202 isInSubmenu: false,
203 projectSearchItems: [],
204 threadSearchItems: threadItems,
205 });
206
207 expect(groups).toHaveLength(1);
208 expect(groups[0]?.value).toBe("threads-search");
209 expect(groups[0]?.items.map((item) => item.value)).toEqual([
210 "thread:thread-title-match",
211 "thread:thread-context-match",
212 ]);
213 });
214
215 it("preserves thread project-name matches when there is no stronger title match", () => {
216 const group: CommandPaletteGroup = {
217 value: "threads-search",
218 label: "Threads",
219 items: [
220 {
221 kind: "action",
222 value: "thread:project-context-only",
223 searchTerms: ["Fix navbar spacing", "Project"],
224 title: "Fix navbar spacing",
225 description: "Project",
226 icon: null,
227 run: async () => undefined,
228 },
229 ],
230 };
231
232 const groups = filterCommandPaletteGroups({
233 activeGroups: [group],
234 query: "project",
235 isInSubmenu: false,
236 projectSearchItems: [],
237 threadSearchItems: [],
238 });
239
240 expect(groups).toHaveLength(1);
241 expect(groups[0]?.items.map((item) => item.value)).toEqual(["thread:project-context-only"]);
242 });
243
244 it("keeps message excerpts searchable without replacing thread metadata", () => {
245 const [item] = buildThreadActionItems({
246 threads: [makeThread({ branch: "feat/search" })],
247 projectTitleById: new Map([[PROJECT_ID, "T3 Code"]]),
248 sortOrder: "updated_at",
249 icon: null,
250 getContentMatch: () => ({
251 source: "assistant",
252 snippet: "The relay reconnect is now bounded.",
253 query: "reconnect",
254 }),
255 runThread: async (_thread) => undefined,
256 });
257
258 expect(item?.searchTerms).toContain("The relay reconnect is now bounded.");
259 expect(item?.threadContentMatch).toEqual({
260 source: "assistant",
261 snippet: "The relay reconnect is now bounded.",
262 query: "reconnect",
263 });
264 expect(item?.description).toBe("T3 Code · #feat/search");
265 });
266
267 it("filters archived threads out of thread search items", () => {
268 const items = buildThreadActionItems({
269 threads: [
270 makeThread({
271 id: ThreadId.make("thread-active"),
272 title: "Active thread",
273 createdAt: "2026-03-02T00:00:00.000Z",
274 updatedAt: "2026-03-19T00:00:00.000Z",
275 }),
276 makeThread({
277 id: ThreadId.make("thread-archived"),
278 title: "Archived thread",
279 archivedAt: "2026-03-20T00:00:00.000Z",
280 updatedAt: "2026-03-20T00:00:00.000Z",
281 }),
282 ],
283 projectTitleById: new Map([[PROJECT_ID, "Project"]]),
284 sortOrder: "updated_at",
285 icon: null,
286 runThread: async (_thread) => undefined,
287 });
288
289 expect(items.map((item) => item.value)).toEqual(["thread:thread-active"]);
290 });
291});
292
293describe("buildBrowseGroups", () => {
294 it("waits for asynchronous browse navigation actions", async () => {
295 let finishNavigation: (() => void) | undefined;
296 const browseTo = vi.fn(
297 () =>
298 new Promise<void>((resolve) => {
299 finishNavigation = resolve;
300 }),
301 );
302 const groups = buildBrowseGroups({
303 browseEntries: [{ name: "Downloads", fullPath: "/Users/test/Downloads" }],
304 browseQuery: "~/",
305 canBrowseUp: false,
306 upIcon: null,
307 directoryIcon: null,
308 browseUp: vi.fn(),
309 browseTo,
310 });
311 const item = groups[0]?.items[0];
312 if (!item || item.kind !== "action") {
313 throw new Error("Expected a browse action");
314 }
315
316 let actionSettled = false;
317 const action = item.run().then(() => {
318 actionSettled = true;
319 });
320 await Promise.resolve();
321
322 expect(browseTo).toHaveBeenCalledWith("Downloads");
323 expect(actionSettled).toBe(false);
324
325 finishNavigation?.();
326 await action;
327 expect(actionSettled).toBe(true);
328 });
329});