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

CommandPalette.logic.test.ts

373 lines11.1 KB

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