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