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