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

CommandPalette.logic.test.ts

1,000 lines31.3 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2import { EnvironmentId, ProjectId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
3import type { Project, Thread } from "../types";
4import { makeThreadFixture } from "../test-fixtures";
5import {
6 buildBrowseGroups,
7 buildCommandPaletteProjectMetadata,
8 buildCommandPaletteRows,
9 buildProjectActionItems,
10 buildThreadActionItems,
11 buildLinkedThreadActionItems,
12 enumerateCommandPaletteItems,
13 filterPinnedBrowseEntries,
14 filterCommandPaletteGroups,
15 findHighlightedCommandPaletteItem,
16 reduceCommandPaletteUiState,
17 createCommandPaletteSearchState,
18 reduceCommandPaletteSearchState,
19 type CommandPaletteActionItem,
20 type CommandPaletteGroup,
21 type CommandPaletteUiState,
22} from "./CommandPalette.logic";
23​
24describe("linked pull request thread navigation", () => {
25 it("keeps archived relations searchable and routes them through the PR environment", async () => {
26 const environmentId = EnvironmentId.make("remote");
27 const id = ThreadId.make("archived-thread");
28 const runThread = vi.fn(async () => {});
29 const query = "https://github.com/acme/web/pull/42";
30 const linkedThreads = {
31 environmentId,
32 threads: [
33 {
34 id,
35 projectId: ProjectId.make("project"),
36 title: "Completed work",
37 archivedAt: "2026-09-01T00:00:00.000Z",
38 },
39 ],
40 };
41 const state = reduceCommandPaletteUiState(
42 { open: false, mode: "command", openIntent: null },
43 {
44 _tag: "OpenSearch",
45 query,
46 linkedThreads,
47 },
48 );
49 expect(state.openIntent).toEqual({ kind: "search", query, linkedThreads });
50 const items = buildLinkedThreadActionItems({ ...linkedThreads, query, icon: null, runThread });
51 const groups = filterCommandPaletteGroups({
52 activeGroups: [],
53 query,
54 isInSubmenu: false,
55 projectSearchItems: [],
56 settingsSearchItems: [],
57 threadSearchItems: items,
58 });
59 expect(groups.flatMap((group) => group.items)).toEqual(items);
60 expect(items[0]?.description).toBe("Archived thread");
61 await items[0]?.run();
62 expect(runThread).toHaveBeenCalledWith({ environmentId, id });
63 });
64});
65​
66describe("buildCommandPaletteProjectMetadata", () => {
67 const localEnvironmentId = EnvironmentId.make("environment-local");
68 const remoteEnvironmentId = EnvironmentId.make("environment-build-box");
69 const locations = new Map([
70 [localEnvironmentId, { kind: "local" as const, label: "Local", machine: "laptop" as const }],
71 [
72 remoteEnvironmentId,
73 { kind: "remote" as const, label: "Build box", machine: "server" as const },
74 ],
75 ]);
76​
77 it("makes every member environment and path searchable", () => {
78 const metadata = buildCommandPaletteProjectMetadata({
79 projects: [
80 {
81 environmentId: localEnvironmentId,
82 title: "T3 Code",
83 workspaceRoot: "/Users/theo/Projects/t3code",
84 },
85 {
86 environmentId: remoteEnvironmentId,
87 title: "t3code",
88 workspaceRoot: "/srv/t3code",
89 },
90 ],
91 locationByEnvironmentId: locations,
92 });
93​
94 expect(metadata.searchTerms).toEqual([
95 "T3 Code",
96 "/Users/theo/Projects/t3code",
97 "Local",
98 "t3code",
99 "/srv/t3code",
100 "Build box",
101 ]);
102 expect(metadata.environmentLabels).toEqual(["Local", "Build box"]);
103​
104 const [filteredGroup] = filterCommandPaletteGroups({
105 activeGroups: [],
106 query: "build box",
107 isInSubmenu: false,
108 projectSearchItems: [
109 {
110 kind: "action",
111 value: "project:t3code",
112 title: "T3 Code",
113 searchTerms: metadata.searchTerms,
114 icon: null,
115 run: async () => undefined,
116 },
117 ],
118 threadSearchItems: [],
119 });
120 expect(filteredGroup?.items).toHaveLength(1);
121 });
122​
123 it("deduplicates grouped checkouts by environment", () => {
124 const metadata = buildCommandPaletteProjectMetadata({
125 projects: [
126 {
127 environmentId: remoteEnvironmentId,
128 title: "T3 Code",
129 workspaceRoot: "/srv/t3code",
130 },
131 {
132 environmentId: remoteEnvironmentId,
133 title: "T3 Code worktree",
134 workspaceRoot: "/srv/t3code-feature",
135 },
136 ],
137 locationByEnvironmentId: locations,
138 });
139​
140 expect(metadata.environmentLabels).toEqual(["Build box"]);
141 });
142​
143 it("deduplicates distinct environments with the same label", () => {
144 const secondRemoteEnvironmentId = EnvironmentId.make("environment-build-box-2");
145 const metadata = buildCommandPaletteProjectMetadata({
146 projects: [
147 {
148 environmentId: remoteEnvironmentId,
149 title: "T3 Code",
150 workspaceRoot: "/srv/t3code",
151 },
152 {
153 environmentId: secondRemoteEnvironmentId,
154 title: "T3 Code mirror",
155 workspaceRoot: "/srv/mirror/t3code",
156 },
157 ],
158 locationByEnvironmentId: new Map([
159 [remoteEnvironmentId, { label: "Build box" }],
160 [secondRemoteEnvironmentId, { label: "Build box" }],
161 ]),
162 });
163​
164 expect(metadata.environmentLabels).toEqual(["Build box"]);
165 });
166​
167 it("uses a human-readable fallback when presentation data is unavailable", () => {
168 const metadata = buildCommandPaletteProjectMetadata({
169 projects: [
170 {
171 environmentId: remoteEnvironmentId,
172 title: "T3 Code",
173 workspaceRoot: "/srv/t3code",
174 },
175 ],
176 locationByEnvironmentId: new Map(),
177 });
178​
179 expect(metadata.searchTerms).toContain("Remote");
180 expect(metadata.environmentLabels).toEqual(["Remote"]);
181 });
182});
183​
184describe("reduceCommandPaletteUiState", () => {
185 const closedState = { open: false, mode: "command", openIntent: null } as const;
186​
187 it("toggles each overlay mode open and closed", () => {
188 const filesOpen = reduceCommandPaletteUiState(closedState, {
189 _tag: "ToggleMode",
190 mode: "files",
191 });
192 expect(filesOpen).toEqual({ open: true, mode: "files", openIntent: null });
193​
194 const contentOpen = reduceCommandPaletteUiState(filesOpen, {
195 _tag: "ToggleMode",
196 mode: "content",
197 });
198 expect(contentOpen).toEqual({ open: true, mode: "content", openIntent: null });
199​
200 expect(
201 reduceCommandPaletteUiState(contentOpen, { _tag: "ToggleMode", mode: "content" }),
202 ).toEqual({ open: false, mode: "content", openIntent: null });
203 });
204​
205 it("switches between open modes without closing", () => {
206 const filesOpen = reduceCommandPaletteUiState(closedState, {
207 _tag: "ToggleMode",
208 mode: "files",
209 });
210 expect(reduceCommandPaletteUiState(filesOpen, { _tag: "ToggleMode", mode: "command" })).toEqual(
211 {
212 open: true,
213 mode: "command",
214 openIntent: null,
215 },
216 );
217 });
218​
219 it("opens PR search from another overlay and replaces an earlier search", () => {
220 const first = reduceCommandPaletteUiState(
221 { open: true, mode: "files", openIntent: null },
222 {
223 _tag: "OpenSearch",
224 query: "https://github.com/acme/web/pull/7",
225 },
226 );
227 expect(first).toEqual({
228 open: true,
229 mode: "command",
230 openIntent: { kind: "search", query: "https://github.com/acme/web/pull/7" },
231 });
232 const second = reduceCommandPaletteUiState(first, {
233 _tag: "OpenSearch",
234 query: "https://github.com/acme/web/pull/8",
235 });
236 expect(second.openIntent).toEqual({
237 kind: "search",
238 query: "https://github.com/acme/web/pull/8",
239 });
240 expect(
241 reduceCommandPaletteUiState(second, { _tag: "SetOpen", open: false }).openIntent,
242 ).toBeNull();
243 });
244​
245 it("routes open intents to command mode", () => {
246 const filesOpen = reduceCommandPaletteUiState(closedState, {
247 _tag: "ToggleMode",
248 mode: "files",
249 });
250 expect(reduceCommandPaletteUiState(filesOpen, { _tag: "OpenAddProject" })).toEqual({
251 open: true,
252 mode: "command",
253 openIntent: { kind: "add-project" },
254 });
255 expect(reduceCommandPaletteUiState(filesOpen, { _tag: "OpenNewThreadIn" })).toEqual({
256 open: true,
257 mode: "command",
258 openIntent: { kind: "new-thread-in" },
259 });
260 });
261​
262 it("preserves the mode on close and resets it on open", () => {
263 const filesOpen = reduceCommandPaletteUiState(closedState, {
264 _tag: "ToggleMode",
265 mode: "files",
266 });
267​
268 expect(reduceCommandPaletteUiState(filesOpen, { _tag: "SetOpen", open: false })).toEqual({
269 open: false,
270 mode: "files",
271 openIntent: null,
272 });
273 expect(reduceCommandPaletteUiState(filesOpen, { _tag: "SetOpen", open: true })).toEqual({
274 open: true,
275 mode: "command",
276 openIntent: null,
277 });
278 });
279});
280​
281describe("enumerateCommandPaletteItems", () => {
282 it("assigns positional jump shortcuts to the first nine displayed items", () => {
283 const items = Array.from({ length: 10 }, (_, index) => ({
284 kind: "action" as const,
285 value: `project-${index + 1}`,
286 searchTerms: [],
287 title: `Project ${index + 1}`,
288 icon: null,
289 shortcutCommand: "chat.new" as const,
290 run: async () => undefined,
291 }));
292​
293 expect(enumerateCommandPaletteItems(items).map((item) => item.shortcutCommand)).toEqual([
294 "thread.jump.1",
295 "thread.jump.2",
296 "thread.jump.3",
297 "thread.jump.4",
298 "thread.jump.5",
299 "thread.jump.6",
300 "thread.jump.7",
301 "thread.jump.8",
302 "thread.jump.9",
303 undefined,
304 ]);
305 });
306});
307​
308const LOCAL_ENVIRONMENT_ID = EnvironmentId.make("environment-local");
309const PROJECT_ID = ProjectId.make("project-1");
310​
311function makeProject(overrides: Partial<Project> = {}): Project {
312 return {
313 id: PROJECT_ID,
314 environmentId: LOCAL_ENVIRONMENT_ID,
315 title: "Project",
316 workspaceRoot: "/workspace/project",
317 defaultModelSelection: null,
318 scripts: [],
319 createdAt: "2026-03-01T00:00:00.000Z",
320 updatedAt: "2026-03-01T00:00:00.000Z",
321 ...overrides,
322 };
324​
325function makeThread(overrides: Partial<Thread> = {}): Thread {
326 return makeThreadFixture({
327 id: ThreadId.make("thread-1"),
328 environmentId: LOCAL_ENVIRONMENT_ID,
329 projectId: PROJECT_ID,
330 title: "Thread",
331 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },
332 runtimeMode: "full-access",
333 interactionMode: "default",
334 runtime: null,
335 messages: [],
336 proposedPlans: [],
337 createdAt: "2026-03-01T00:00:00.000Z",
338 archivedAt: null,
339 settledOverride: null,
340 settledAt: null,
341 deletedAt: null,
342 updatedAt: "2026-03-01T00:00:00.000Z",
343 latestRun: null,
344 branch: null,
345 worktreePath: null,
346 ...overrides,
347 });
349​
350describe("buildProjectActionItems", () => {
351 it("shows the grouped display name but keeps the real title for icons", () => {
352 const project = makeProject({ title: "fleet", workspaceRoot: "/Users/theo/Code/p/fleet" });
353 const iconTitles: string[] = [];
354 const [item] = buildProjectActionItems({
355 projects: [{ ...project, displayName: "t3dotgg/fleet" }],
356 valuePrefix: "project",
357 icon: (candidate) => {
358 iconTitles.push(candidate.title);
359 return null;
360 },
361 runProject: async () => undefined,
362 });
363​
364 expect(item?.title).toBe("t3dotgg/fleet");
365 expect(item?.searchTerms).toEqual(
366 expect.arrayContaining(["t3dotgg/fleet", "fleet", "/Users/theo/Code/p/fleet"]),
367 );
368 expect(iconTitles).toEqual(["fleet"]);
369 });
370});
371​
372describe("buildThreadActionItems", () => {
373 it("orders threads by most recent activity and formats timestamps from updatedAt", () => {
374 vi.useFakeTimers();
375 vi.setSystemTime(new Date("2026-03-25T12:00:00.000Z"));
376​
377 try {
378 const items = buildThreadActionItems({
379 threads: [
380 makeThread({
381 id: ThreadId.make("thread-older"),
382 title: "Older thread",
383 updatedAt: "2026-03-24T12:00:00.000Z",
384 }),
385 makeThread({
386 id: ThreadId.make("thread-newer"),
387 title: "Newer thread",
388 createdAt: "2026-03-20T00:00:00.000Z",
389 updatedAt: "2026-03-20T00:00:00.000Z",
390 }),
391 ],
392 projectTitleById: new Map([[PROJECT_ID, "Project"]]),
393 sortOrder: "updated_at",
394 icon: null,
395 runThread: async (_thread) => undefined,
396 });
397​
398 expect(items.map((item) => item.value)).toEqual([
399 "thread:thread-older",
400 "thread:thread-newer",
401 ]);
402 expect(items[0]?.timestamp).toBe("1d ago");
403 expect(items[1]?.timestamp).toBe("5d ago");
404 } finally {
405 vi.useRealTimers();
406 }
407 });
408​
409 it("ranks thread title matches ahead of contextual project-name matches", () => {
410 const threadItems = buildThreadActionItems({
411 threads: [
412 makeThread({
413 id: ThreadId.make("thread-context-match"),
414 title: "Fix navbar spacing",
415 updatedAt: "2026-03-20T00:00:00.000Z",
416 }),
417 makeThread({
418 id: ThreadId.make("thread-title-match"),
419 title: "Project kickoff notes",
420 createdAt: "2026-03-02T00:00:00.000Z",
421 updatedAt: "2026-03-19T00:00:00.000Z",
422 }),
423 ],
424 projectTitleById: new Map([[PROJECT_ID, "Project"]]),
425 sortOrder: "updated_at",
426 icon: null,
427 runThread: async (_thread) => undefined,
428 });
429​
430 const groups = filterCommandPaletteGroups({
431 activeGroups: [],
432 query: "project",
433 isInSubmenu: false,
434 projectSearchItems: [],
435 threadSearchItems: threadItems,
436 });
437​
438 expect(groups).toHaveLength(1);
439 expect(groups[0]?.value).toBe("threads-search");
440 expect(groups[0]?.items.map((item) => item.value)).toEqual([
441 "thread:thread-title-match",
442 "thread:thread-context-match",
443 ]);
444 });
445​
446 it("orders title matches by recent activity before older prefix matches", () => {
447 const threads = [
448 makeThread({
449 id: ThreadId.make("old-prefix"),
450 title: "Convex InvalidCursor in Convex threads query",
451 createdAt: "2026-01-01T00:00:00.000Z",
452 updatedAt: "2026-01-02T00:00:00.000Z",
453 }),
454 makeThread({
455 id: ThreadId.make("recent-title"),
456 title: "Disable Convex schema validation",
457 createdAt: "2025-12-01T00:00:00.000Z",
458 updatedAt: "2026-03-24T00:00:00.000Z",
459 }),
460 makeThread({
461 id: ThreadId.make("recent-content"),
462 title: "Fix schema validation",
463 createdAt: "2026-03-25T00:00:00.000Z",
464 updatedAt: "2026-03-25T00:00:00.000Z",
465 }),
466 ];
467 const items = buildThreadActionItems({
468 threads,
469 projectTitleById: new Map([[PROJECT_ID, "T3 Code"]]),
470 sortOrder: "created_at",
471 icon: null,
472 getContentMatch: (thread) =>
473 thread.id === ThreadId.make("recent-content")
474 ? { source: "user", snippet: "Please check Convex", query: "convex" }
475 : undefined,
476 runThread: async () => undefined,
477 });
478​
479 const groups = filterCommandPaletteGroups({
480 activeGroups: [],
481 query: "convex",
482 isInSubmenu: false,
483 projectSearchItems: [],
484 threadSearchItems: items,
485 });
486​
487 expect(groups[0]?.items.map((item) => item.value)).toEqual([
488 "thread:recent-title",
489 "thread:old-prefix",
490 "thread:recent-content",
491 ]);
492 });
493​
494 it("preserves thread project-name matches when there is no stronger title match", () => {
495 const group: CommandPaletteGroup = {
496 value: "threads-search",
497 label: "Threads",
498 items: [
499 {
500 kind: "action",
501 value: "thread:project-context-only",
502 searchTerms: ["Fix navbar spacing", "Project"],
503 title: "Fix navbar spacing",
504 description: "Project",
505 icon: null,
506 run: async () => undefined,
507 },
508 ],
509 };
510​
511 const groups = filterCommandPaletteGroups({
512 activeGroups: [group],
513 query: "project",
514 isInSubmenu: false,
515 projectSearchItems: [],
516 threadSearchItems: [],
517 });
518​
519 expect(groups).toHaveLength(1);
520 expect(groups[0]?.items.map((item) => item.value)).toEqual(["thread:project-context-only"]);
521 });
522​
523 it("ranks an order-independent setting title match above a split context match", () => {
524 const settingsSearchItems = [
525 {
526 kind: "action" as const,
527 value: "setting:context-match",
528 searchTerms: ["Pairing settings", "remote backend"],
529 title: "Context match",
530 icon: null,
531 run: async () => undefined,
532 },
533 {
534 kind: "action" as const,
535 value: "setting:remote-pairing",
536 searchTerms: ["Remote pairing", "connections"],
537 title: "Remote pairing",
538 icon: null,
539 run: async () => undefined,
540 },
541 ];
542​
543 const groups = filterCommandPaletteGroups({
544 activeGroups: [],
545 query: "pairing remote",
546 isInSubmenu: false,
547 projectSearchItems: [],
548 settingsSearchItems,
549 threadSearchItems: [],
550 });
551​
552 expect(groups).toHaveLength(1);
553 expect(groups[0]?.value).toBe("settings-search");
554 expect(groups[0]?.items.map((item) => item.value)).toEqual([
555 "setting:remote-pairing",
556 "setting:context-match",
557 ]);
558 });
559​
560 it("keeps accent-insensitive setting results", () => {
561 const groups = filterCommandPaletteGroups({
562 activeGroups: [],
563 query: "thè\u{1ab0}mes",
564 isInSubmenu: false,
565 projectSearchItems: [],
566 settingsSearchItems: [
567 {
568 kind: "action",
569 value: "setting:theme",
570 searchTerms: ["Themes", "Appearance"],
571 title: "Themes",
572 icon: null,
573 run: async () => undefined,
574 },
575 ],
576 threadSearchItems: [],
577 });
578​
579 expect(groups[0]?.items.map((item) => item.value)).toEqual(["setting:theme"]);
580 });
581​
582 it("normalizes case independently of the host locale", () => {
583 const toLocaleLowerCase = String.prototype.toLocaleLowerCase;
584 const localeLowerCase = vi
585 .spyOn(String.prototype, "toLocaleLowerCase")
586 .mockImplementation(function (this: string) {
587 return toLocaleLowerCase.call(this, "tr");
588 });
589 try {
590 const groups = filterCommandPaletteGroups({
591 activeGroups: [],
592 query: "GIT",
593 isInSubmenu: false,
594 projectSearchItems: [],
595 threadSearchItems: [],
596 settingsSearchItems: [
597 {
598 kind: "action",
599 value: "setting:version-control",
600 title: "Version control",
601 searchTerms: ["git"],
602 icon: null,
603 run: async () => undefined,
604 },
605 ],
606 });
607 expect(groups.flatMap((group) => group.items.map((item) => item.value))).toEqual([
608 "setting:version-control",
609 ]);
610 } finally {
611 localeLowerCase.mockRestore();
612 }
613 });
614​
615 it("keeps message excerpts searchable without replacing thread metadata", () => {
616 const [item] = buildThreadActionItems({
617 threads: [makeThread({ branch: "feat/search" })],
618 projectTitleById: new Map([[PROJECT_ID, "T3 Code"]]),
619 sortOrder: "updated_at",
620 icon: null,
621 getContentMatch: () => ({
622 source: "assistant",
623 snippet: "The relay reconnect is now bounded.",
624 query: "reconnect",
625 }),
626 runThread: async (_thread) => undefined,
627 });
628​
629 expect(item?.searchTerms).toContain("The relay reconnect is now bounded.");
630 expect(item?.threadContentMatch).toEqual({
631 source: "assistant",
632 snippet: "The relay reconnect is now bounded.",
633 query: "reconnect",
634 });
635 expect(item?.description).toBe("T3 Code · #feat/search");
636 });
637​
638 it("surfaces threads when the query is their ID, without outranking title matches", () => {
639 const idThread = makeThread({
640 id: ThreadId.make("thread-alpha-1234"),
641 title: "Unrelated work",
642 updatedAt: "2026-03-05T00:00:00.000Z",
643 });
644 const titleThread = makeThread({
645 id: ThreadId.make("thread-other-9999"),
646 title: "Fix thread-alpha-1234 flakes",
647 updatedAt: "2026-03-04T00:00:00.000Z",
648 });
649 const items = buildThreadActionItems({
650 threads: [idThread, titleThread],
651 projectTitleById: new Map([[PROJECT_ID, "T3 Code"]]),
652 sortOrder: "updated_at",
653 icon: null,
654 runThread: async (_thread) => undefined,
655 });
656​
657 const groups = filterCommandPaletteGroups({
658 activeGroups: [],
659 query: " THREAD-ALPHA-1234 ",
660 isInSubmenu: false,
661 projectSearchItems: [],
662 settingsSearchItems: [],
663 threadSearchItems: items,
664 });
665​
666 expect(groups.flatMap((group) => group.items)).toEqual([
667 expect.objectContaining({ value: `thread:${titleThread.id}` }),
668 expect.objectContaining({ value: `thread:${idThread.id}` }),
669 ]);
670 });
671​
672 it("prefers renderDescription when provided", () => {
673 const [item] = buildThreadActionItems({
674 threads: [makeThread({ branch: "feat/search", worktreePath: "/tmp/wt" })],
675 projectTitleById: new Map([[PROJECT_ID, "T3 Code"]]),
676 sortOrder: "updated_at",
677 icon: null,
678 renderDescription: (thread, { projectTitle }) =>
679 `${projectTitle}:${thread.branch}:${thread.worktreePath ? "wt" : "local"}`,
680 runThread: async (_thread) => undefined,
681 });
682​
683 expect(item?.description).toBe("T3 Code:feat/search:wt");
684 });
685​
686 it("filters archived threads out of thread search items", () => {
687 const items = buildThreadActionItems({
688 threads: [
689 makeThread({
690 id: ThreadId.make("thread-active"),
691 title: "Active thread",
692 createdAt: "2026-03-02T00:00:00.000Z",
693 updatedAt: "2026-03-19T00:00:00.000Z",
694 }),
695 makeThread({
696 id: ThreadId.make("thread-archived"),
697 title: "Archived thread",
698 archivedAt: "2026-03-20T00:00:00.000Z",
699 updatedAt: "2026-03-20T00:00:00.000Z",
700 }),
701 ],
702 projectTitleById: new Map([[PROJECT_ID, "Project"]]),
703 sortOrder: "updated_at",
704 icon: null,
705 runThread: async (_thread) => undefined,
706 });
707​
708 expect(items.map((item) => item.value)).toEqual(["thread:thread-active"]);
709 });
710});
711​
712describe("buildBrowseGroups", () => {
713 it("waits for asynchronous browse navigation actions", async () => {
714 let finishNavigation: (() => void) | undefined;
715 const browseTo = vi.fn(
716 () =>
717 new Promise<void>((resolve) => {
718 finishNavigation = resolve;
719 }),
720 );
721 const groups = buildBrowseGroups({
722 browseEntries: [{ name: "Downloads", fullPath: "/Users/test/Downloads" }],
723 browseQuery: "~/",
724 canBrowseUp: false,
725 upIcon: null,
726 directoryIcon: null,
727 browseUp: vi.fn(),
728 browseTo,
729 });
730 const item = groups[0]?.items[0];
731 if (!item || item.kind !== "action") {
732 throw new Error("Expected a browse action");
733 }
734​
735 let actionSettled = false;
736 const action = item.run().then(() => {
737 actionSettled = true;
738 });
739 await Promise.resolve();
740​
741 expect(browseTo).toHaveBeenCalledWith("Downloads");
742 expect(actionSettled).toBe(false);
743​
744 finishNavigation?.();
745 await action;
746 expect(actionSettled).toBe(true);
747 });
748});
749​
750describe("filterPinnedBrowseEntries", () => {
751 const entries = [
752 { name: "repo", fullPath: "/projects/repo" },
753 { name: "work", fullPath: "/projects/work" },
754 ];
755​
756 it("shows sibling folders without losing an existing pinned destination", () => {
757 expect(
758 filterPinnedBrowseEntries({
759 browseEntries: entries,
760 filterQuery: "repo",
761 pinnedDirectoryName: "repo",
762 caseSensitive: true,
763 }),
764 ).toEqual({ visibleEntries: entries, exactEntry: entries[0] });
765 });
766​
767 it("matches an existing pinned destination without Windows casing", () => {
768 const windowsEntries = [
769 { name: "Repo", fullPath: "C:\\projects\\Repo" },
770 { name: "work", fullPath: "C:\\projects\\work" },
771 ];
772 expect(
773 filterPinnedBrowseEntries({
774 browseEntries: windowsEntries,
775 filterQuery: "repo",
776 pinnedDirectoryName: "repo",
777 caseSensitive: false,
778 }),
779 ).toEqual({
780 visibleEntries: windowsEntries,
781 exactEntry: windowsEntries[0],
782 });
783 });
784});
785​
786it.each([
787 "#10839",
788 "10839",
789 "pingdotgg/t3code#10839",
790 "https://github.com/pingdotgg/t3code/pull/10839",
791])("finds linked threads from PR query %s", (query) => {
792 const items = buildThreadActionItems({
793 threads: [
794 makeThread({
795 title: "Implementation",
796 pullRequests: [
797 {
798 host: "github.com",
799 repository: "pingdotgg/t3code",
800 number: 10839,
801 url: "https://github.com/pingdotgg/t3code/pull/10839",
802 source: "manual",
803 linkedAt: "2026-09-08T00:00:00Z",
804 snapshot: null,
805 stack: null,
806 },
807 ],
808 }),
809 makeThread({ id: ThreadId.make("unrelated"), title: "Other work" }),
810 ],
811 projectTitleById: new Map(),
812 sortOrder: "updated_at",
813 icon: null,
814 runThread: async () => undefined,
815 });
816 const groups = filterCommandPaletteGroups({
817 activeGroups: [],
818 query,
819 isInSubmenu: false,
820 projectSearchItems: [],
821 threadSearchItems: items,
822 });
823 expect(groups.flatMap((group) => group.items.map((item) => item.title))).toEqual([
824 "Implementation",
825 ]);
826});
827​
828describe("filterCommandPaletteGroups", () => {
829 it("sorts secondary settings results after other matches", () => {
830 const item = (value: string, title: string, secondary?: boolean) =>
831 ({
832 kind: "action",
833 value,
834 title,
835 searchTerms: [title, "General"],
836 icon: null,
837 run: async () => undefined,
838 ...(secondary ? { secondary } : {}),
839 }) satisfies CommandPaletteActionItem;
840 const [group] = filterCommandPaletteGroups({
841 activeGroups: [],
842 query: "model",
843 isInSubmenu: false,
844 projectSearchItems: [],
845 settingsSearchItems: [
846 item("setting:keybinding-modelPicker.toggle", "Model Picker: Toggle", true),
847 item("setting:default-model", "Default model"),
848 ],
849 threadSearchItems: [],
850 });
851 expect(group?.items.map((entry) => entry.value)).toEqual([
852 "setting:default-model",
853 "setting:keybinding-modelPicker.toggle",
854 ]);
855 });
856});
857​
858describe("virtualized command palette rows", () => {
859 const action = (value: string, disabled = false): CommandPaletteActionItem => ({
860 kind: "action",
861 value,
862 searchTerms: [],
863 title: value,
864 icon: null,
865 ...(disabled ? { disabled } : {}),
866 run: async () => {},
867 });
868 const groups: CommandPaletteGroup[] = [
869 { value: "actions", label: "Actions", items: [action("new-thread"), action("offline", true)] },
870 { value: "threads", label: "Threads", items: [action("thread-a"), action("thread-b")] },
871 ];
872​
873 it("keeps group order and headings while indexing only enabled items", () => {
874 const { rows, itemValues, rowIndexByItemIndex } = buildCommandPaletteRows(groups);
875​
876 expect(rows.map((row) => (row.kind === "label" ? `# ${row.label}` : row.key))).toEqual([
877 "# Actions",
878 "actions:new-thread",
879 "actions:offline",
880 "# Threads",
881 "threads:thread-a",
882 "threads:thread-b",
883 ]);
884 expect(itemValues).toEqual(["new-thread", "thread-a", "thread-b"]);
885 expect(rowIndexByItemIndex).toEqual([1, 4, 5]);
886 expect(rows.flatMap((row) => (row.kind === "item" ? [row.itemIndex] : []))).toEqual([
887 0,
888 null,
889 1,
890 2,
891 ]);
892 });
893​
894 it("resolves Enter to the highlighted item without needing its row mounted", () => {
895 expect(findHighlightedCommandPaletteItem(groups, "thread-b")?.value).toBe("thread-b");
896 expect(findHighlightedCommandPaletteItem(groups, "offline")).toBeNull();
897 expect(findHighlightedCommandPaletteItem(groups, null)).toBeNull();
898 });
899});
900​
901describe("command palette search return", () => {
902 const picker = { addonIcon: null, groups: [] };
903​
904 it("restores each parent query when backing out of nested pickers", () => {
905 let state = createCommandPaletteSearchState(">new thread");
906 state = reduceCommandPaletteSearchState(state, { type: "push", view: picker });
907 expect(state.query).toBe("");
908 state = reduceCommandPaletteSearchState(state, { type: "query", query: "remote project" });
909 state = reduceCommandPaletteSearchState(state, {
910 type: "push",
911 view: { ...picker, initialQuery: "~/" },
912 });
913 expect(state.query).toBe("~/");
914​
915 state = reduceCommandPaletteSearchState(state, { type: "query", query: "" });
916 state = reduceCommandPaletteSearchState(state, { type: "back" });
917 expect(state.query).toBe("remote project");
918 expect(state.views).toHaveLength(1);
919 state = reduceCommandPaletteSearchState(state, { type: "back" });
920 expect(state.query).toBe(">new thread");
921 expect(state.views).toHaveLength(0);
922 });
923​
924 it("cannot return to an old picker after a new search replaces it", () => {
925 let state = reduceCommandPaletteSearchState(createCommandPaletteSearchState("old"), {
926 type: "push",
927 view: picker,
928 });
929 state = reduceCommandPaletteSearchState(state, { type: "reset", query: "fresh" });
930 state = reduceCommandPaletteSearchState(state, { type: "back" });
931 expect(state.query).toBe("fresh");
932 expect(state.views).toHaveLength(0);
933 });
934});
935​
936describe("returning from file and content search", () => {
937 const root: CommandPaletteUiState = { open: true, mode: "command", openIntent: null };
938 const query = "https://github.com/acme/web/pull/7";
939 const context = {
940 query,
941 linkedThreadSearch: {
942 kind: "search" as const,
943 query,
944 linkedThreads: {
945 environmentId: EnvironmentId.make("remote"),
946 threads: [
947 {
948 id: ThreadId.make("linked-thread"),
949 projectId: ProjectId.make("project"),
950 title: "Completed work",
951 archivedAt: "2026-09-01T00:00:00.000Z",
952 },
953 ],
954 },
955 },
956 };
957​
958 it.each(["files", "content"] as const)(
959 "Escape from %s restores the query and linked-thread results",
960 (mode) => {
961 const child = reduceCommandPaletteUiState(root, { _tag: "OpenChildSearch", mode, context });
962 expect(child.mode).toBe(mode);
963 const returned = reduceCommandPaletteUiState(child, { _tag: "BackToCommand" });
964 expect(returned).toEqual({ ...root, restoredSearch: context });
965 expect(reduceCommandPaletteUiState(returned, { _tag: "BackToCommand" })).toEqual(returned);
966 },
967 );
968​
969 it("closing a child search discards its return context before reopening", () => {
970 const child: CommandPaletteUiState = { ...root, mode: "files", parentSearch: context };
971 const closed = reduceCommandPaletteUiState(child, { _tag: "SetOpen", open: false });
972 expect(closed).toEqual({ open: false, mode: "files", openIntent: null });
973 expect(reduceCommandPaletteUiState(closed, { _tag: "BackToCommand" })).toEqual(closed);
974 expect(reduceCommandPaletteUiState(closed, { _tag: "SetOpen", open: true })).toEqual(root);
975 });
976​
977 it("a fresh overlay shortcut has no parent search to return to", () => {
978 const child = reduceCommandPaletteUiState(root, {
979 _tag: "OpenChildSearch",
980 mode: "files",
981 context,
982 });
983 const fresh = reduceCommandPaletteUiState(child, { _tag: "ToggleMode", mode: "content" });
984 expect(fresh).toEqual({ open: true, mode: "content", openIntent: null });
985 expect(reduceCommandPaletteUiState(fresh, { _tag: "BackToCommand" })).toEqual(root);
986 });
987​
988 it("a direct search intent replaces restored query and linked-thread context", () => {
989 const child = reduceCommandPaletteUiState(root, {
990 _tag: "OpenChildSearch",
991 mode: "content",
992 context,
993 });
994 const returned = reduceCommandPaletteUiState(child, { _tag: "BackToCommand" });
995 expect(reduceCommandPaletteUiState(returned, { _tag: "OpenSearch", query: "fresh" })).toEqual({
996 ...root,
997 openIntent: { kind: "search", query: "fresh" },
998 });
999 });
1000});