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 { makeThreadFixture } from "../test-fixtures";
5import {
6 buildNestedSearchItems,
7 buildThreadActionItems,
8 buildLinkedThreadActionItems,
9 filterPinnedBrowseEntries,
10 filterCommandPaletteGroups,
11 reduceCommandPaletteUiState,
12 createCommandPaletteSearchState,
13 reduceCommandPaletteSearchState,
14 type CommandPaletteUiState,
15} from "./CommandPalette.logic";
16
17describe("linked pull request thread navigation", () => {
18 it("keeps archived relations searchable and routes them through the PR environment", async () => {
19 const environmentId = EnvironmentId.make("remote");
20 const id = ThreadId.make("archived-thread");
21 const runThread = vi.fn(async () => {});
22 const query = "https://github.com/acme/web/pull/42";
23 const linkedThreads = {
24 environmentId,
25 threads: [
26 {
27 id,
28 projectId: ProjectId.make("project"),
29 title: "Completed work",
30 archivedAt: "2026-09-01T00:00:00.000Z",
31 },
32 ],
33 };
34 const state = reduceCommandPaletteUiState(
35 { open: false, mode: "command", openIntent: null },
36 {
37 _tag: "OpenSearch",
38 query,
39 linkedThreads,
40 },
41 );
42 expect(state.openIntent).toEqual({ kind: "search", query, linkedThreads });
43 const items = buildLinkedThreadActionItems({ ...linkedThreads, query, icon: null, runThread });
44 const groups = filterCommandPaletteGroups({
45 activeGroups: [],
46 query,
47 isInSubmenu: false,
48 projectSearchItems: [],
49 settingsSearchItems: [],
50 threadSearchItems: items,
51 });
52 expect(groups.flatMap((group) => group.items)).toEqual(items);
53 expect(items[0]?.description).toBe("Archived thread");
54 await items[0]?.run();
55 expect(runThread).toHaveBeenCalledWith({ environmentId, id });
56 });
57});
58
59describe("reduceCommandPaletteUiState", () => {
60 it("opens PR search from another overlay and replaces an earlier search", () => {
61 const first = reduceCommandPaletteUiState(
62 { open: true, mode: "files", openIntent: null },
63 {
64 _tag: "OpenSearch",
65 query: "https://github.com/acme/web/pull/7",
66 },
67 );
68 expect(first).toEqual({
69 open: true,
70 mode: "command",
71 openIntent: { kind: "search", query: "https://github.com/acme/web/pull/7" },
72 });
73 const second = reduceCommandPaletteUiState(first, {
74 _tag: "OpenSearch",
75 query: "https://github.com/acme/web/pull/8",
76 });
77 expect(second.openIntent).toEqual({
78 kind: "search",
79 query: "https://github.com/acme/web/pull/8",
80 });
81 expect(
82 reduceCommandPaletteUiState(second, { _tag: "SetOpen", open: false }).openIntent,
83 ).toBeNull();
84 });
85});
86
87const LOCAL_ENVIRONMENT_ID = EnvironmentId.make("environment-local");
88const PROJECT_ID = ProjectId.make("project-1");
89
90function makeThread(overrides: Partial<Thread> = {}): Thread {
91 return makeThreadFixture({
92 id: ThreadId.make("thread-1"),
93 environmentId: LOCAL_ENVIRONMENT_ID,
94 projectId: PROJECT_ID,
95 title: "Thread",
96 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },
97 runtimeMode: "full-access",
98 interactionMode: "default",
99 runtime: null,
100 messages: [],
101 proposedPlans: [],
102 createdAt: "2026-03-01T00:00:00.000Z",
103 archivedAt: null,
104 settledOverride: null,
105 settledAt: null,
106 deletedAt: null,
107 updatedAt: "2026-03-01T00:00:00.000Z",
108 latestRun: null,
109 branch: null,
110 worktreePath: null,
111 ...overrides,
112 });
113}
114
115describe("buildThreadActionItems", () => {
116 it("keeps threads apart when two environments share a thread ID", async () => {
117 const local = makeThread();
118 const remote = makeThread({ environmentId: EnvironmentId.make("remote-copy") });
119 const runThread = vi.fn(async () => {});
120 const items = buildThreadActionItems({
121 threads: [local, remote],
122 activeThread: remote,
123 projectByKey: new Map([
124 [`${local.environmentId}:${local.projectId}`, { title: "Local project" }],
125 [`${remote.environmentId}:${remote.projectId}`, { title: "Remote project" }],
126 ]),
127 icon: null,
128 runThread,
129 });
130 expect(new Set(items.map((item) => item.value)).size).toBe(2);
131 const remoteItem = items.find((item) => item.description === "Remote project · Current thread");
132 await remoteItem?.run();
133 expect(runThread).toHaveBeenCalledWith(remote);
134 });
135
136 it("ranks thread title matches ahead of contextual project-name matches", () => {
137 const threadItems = buildThreadActionItems({
138 threads: [
139 makeThread({
140 id: ThreadId.make("thread-context-match"),
141 title: "Fix navbar spacing",
142 updatedAt: "2026-03-20T00:00:00.000Z",
143 }),
144 makeThread({
145 id: ThreadId.make("thread-title-match"),
146 title: "Project kickoff notes",
147 createdAt: "2026-03-02T00:00:00.000Z",
148 updatedAt: "2026-03-19T00:00:00.000Z",
149 }),
150 ],
151 projectByKey: new Map([[`${LOCAL_ENVIRONMENT_ID}:${PROJECT_ID}`, { title: "Project" }]]),
152 icon: null,
153 runThread: async (_thread) => undefined,
154 });
155
156 const groups = filterCommandPaletteGroups({
157 activeGroups: [],
158 query: "project",
159 isInSubmenu: false,
160 projectSearchItems: [],
161 threadSearchItems: threadItems,
162 });
163
164 expect(groups).toHaveLength(1);
165 expect(groups[0]?.value).toBe("threads-search");
166 expect(groups[0]?.items.map((item) => item.value)).toEqual([
167 "thread:environment-local:thread-title-match",
168 "thread:environment-local:thread-context-match",
169 ]);
170 });
171
172 it("searches submenu options from the root and from a submenu with no match", () => {
173 const action = (value: string, title: string) => ({
174 kind: "action" as const,
175 value,
176 searchTerms: [title],
177 title,
178 icon: null,
179 run: async () => undefined,
180 });
181 const projects = [action("new-thread-in:t3code", "t3code"), action("new-thread-in:web", "web")];
182 const newThreadIn = {
183 kind: "submenu" as const,
184 value: "action:new-thread-in",
185 searchTerms: ["new thread"],
186 title: "New thread in...",
187 icon: null,
188 addonIcon: null,
189 groups: [{ value: "projects", label: "Projects", items: projects }],
190 };
191 const addProject = action("action:add-project", "Add project");
192 const rootGroups = [{ value: "actions", label: "Actions", items: [newThreadIn, addProject] }];
193 const search = (query: string, isInSubmenu: boolean) =>
194 filterCommandPaletteGroups({
195 activeGroups: isInSubmenu ? newThreadIn.groups : rootGroups,
196 query,
197 isInSubmenu,
198 allOptions: {
199 rootGroups,
200 nestedItems: buildNestedSearchItems([newThreadIn, addProject], query),
201 },
202 projectSearchItems: [],
203 threadSearchItems: [
204 ...buildThreadActionItems({
205 threads: [makeThread({ title: "Thread about t3code" })],
206 projectByKey: new Map(),
207 icon: null,
208 runThread: async () => undefined,
209 }),
210 ],
211 }).map((group) => [group.label, group.items.map((item) => item.title)]);
212
213 expect(search("new thread in t3code", false)[0]).toEqual([
214 "Suggestions",
215 ["New thread in t3code"],
216 ]);
217 expect(search("new thread", false).map(([label]) => label)).not.toContain("Suggestions");
218 expect(search("t3", true)).toEqual([["Projects", ["t3code"]]]);
219 expect(search("add project", true)).toEqual([["Actions", ["Add project"]]]);
220 });
221
222 it("lists message matches last, once, without moving the title matches above them", () => {
223 const threads = [
224 makeThread({ id: ThreadId.make("title"), title: "Disable Convex schema validation" }),
225 makeThread({ id: ThreadId.make("message"), title: "Fix schema validation" }),
226 ];
227 const items = buildThreadActionItems({
228 threads,
229 projectByKey: new Map(),
230 icon: null,
231 runThread: async () => undefined,
232 });
233 const filter = (threadMessageItems: typeof items) =>
234 filterCommandPaletteGroups({
235 activeGroups: [],
236 query: "convex",
237 isInSubmenu: false,
238 projectSearchItems: [],
239 threadSearchItems: items,
240 threadMessageItems,
241 }).map((group) => [group.label, group.items.map((item) => item.value)]);
242
243 const before = filter([]);
244 // The server matched both; the title match stays where it was.
245 const after = filter([items[0]!, items[1]!].toReversed());
246 expect(before).toEqual([["Threads", ["thread:environment-local:title"]]]);
247 expect(after).toEqual([
248 ["Threads", ["thread:environment-local:title"]],
249 ["In messages", ["thread:environment-local:message"]],
250 ]);
251 });
252
253 it("keeps an active thread first while its title is typed, above settings and actions", () => {
254 const now = Date.parse("2026-10-23T00:00:00.000Z");
255 const action = (value: string, title: string, searchTerms: string[] = [title]) => ({
256 kind: "action" as const,
257 value,
258 searchTerms,
259 title,
260 icon: null,
261 run: async () => undefined,
262 });
263 const threadSearchItems = buildThreadActionItems({
264 threads: [
265 makeThread({
266 title: "Compaction prompt timing and cache age",
267 latestUserMessageAt: "2026-10-11T00:00:00.000Z",
268 }),
269 ],
270 projectByKey: new Map(),
271 icon: null,
272 runThread: async () => undefined,
273 });
274 const firstGroup = (query: string) =>
275 filterCommandPaletteGroups({
276 activeGroups: [
277 { value: "actions", label: "Actions", items: [action("action:copy", "Copy thread ID")] },
278 ],
279 query,
280 isInSubmenu: false,
281 projectSearchItems: [],
282 settingsSearchItems: [
283 action("setting:compact", "Compact idle threads"),
284 action("setting:providers", "Providers", ["Providers", "compatibility"]),
285 ],
286 threadSearchItems,
287 now,
288 })[0]?.label;
289
290 for (const query of ["comp", "compa", "compac", "compact", "compacti", "compaction"]) {
291 expect(firstGroup(query), query).toBe("Threads");
292 }
293 });
294
295 it("orders groups by their best match, so a strong thread is not under a weak action", () => {
296 const action = (value: string, title: string) => ({
297 kind: "action" as const,
298 value,
299 searchTerms: [title],
300 title,
301 icon: null,
302 run: async () => undefined,
303 });
304 const threadSearchItems = buildThreadActionItems({
305 threads: [makeThread({ title: "Upgrade to pnpm 12" })],
306 projectByKey: new Map(),
307 icon: null,
308 runThread: async () => undefined,
309 });
310 const labels = (query: string) =>
311 filterCommandPaletteGroups({
312 activeGroups: [
313 { value: "actions", label: "Actions", items: [action("action:upload", "Upload file")] },
314 ],
315 query,
316 isInSubmenu: false,
317 projectSearchItems: [],
318 threadSearchItems,
319 }).map((group) => group.label);
320
321 expect(labels("upgrade")).toEqual(["Threads"]);
322 expect(labels("upl")).toEqual(["Actions"]);
323 // A short query still finds a command by a keyword it equals.
324 const prGroups = filterCommandPaletteGroups({
325 activeGroups: [
326 {
327 value: "actions",
328 label: "Actions",
329 items: [
330 {
331 kind: "action",
332 value: "action:open-prs",
333 searchTerms: ["Open pull requests", "pr", "prs", "review"],
334 title: "Open pull requests",
335 icon: null,
336 run: async () => undefined,
337 },
338 action("action:project", "Project settings"),
339 ],
340 },
341 ],
342 query: "pr",
343 isInSubmenu: false,
344 projectSearchItems: [],
345 threadSearchItems: [],
346 });
347 expect(prGroups.flatMap((group) => group.items.map((item) => item.value))).toEqual([
348 "action:open-prs",
349 "action:project",
350 ]);
351 expect(labels("up")).toEqual(["Actions", "Threads"]);
352 expect(labels("pnpm up")).toEqual(["Threads"]);
353 });
354
355 it("runs the command for its whole name over a thread with that title, and lists the open thread last", () => {
356 const threads = [
357 makeThread({ id: ThreadId.make("thread-new"), title: "New thread" }),
358 makeThread({ id: ThreadId.make("thread-open"), title: "New thread layout" }),
359 makeThread({ id: ThreadId.make("thread-other"), title: "New thread button" }),
360 ];
361 const threadSearchItems = buildThreadActionItems({
362 threads,
363 activeThread: threads[1]!,
364 projectByKey: new Map(),
365 icon: null,
366 runThread: async () => undefined,
367 });
368 const groups = filterCommandPaletteGroups({
369 activeGroups: [
370 {
371 value: "actions",
372 label: "Actions",
373 items: [
374 {
375 kind: "action",
376 value: "action:new-thread",
377 searchTerms: ["New thread in t3code"],
378 title: "New thread in t3code",
379 icon: null,
380 run: async () => undefined,
381 },
382 ],
383 },
384 ],
385 query: "new thread",
386 isInSubmenu: false,
387 projectSearchItems: [],
388 threadSearchItems,
389 });
390 expect(groups.map((group) => group.label)).toEqual(["Actions", "Threads"]);
391 expect(groups[1]?.items.at(-1)?.title).toBe("New thread layout");
392 });
393
394 it("lists a project above threads that only hold its name when the name is typed", () => {
395 const threadSearchItems = buildThreadActionItems({
396 threads: [makeThread({ title: "Lakebed performance gains" })],
397 projectByKey: new Map(),
398 icon: null,
399 runThread: async () => undefined,
400 });
401 const projectSearchItems = [
402 {
403 kind: "action" as const,
404 value: "project:lakebed",
405 searchTerms: ["Lakebed", "/code/lakebed"],
406 title: "Lakebed",
407 icon: null,
408 run: async () => undefined,
409 },
410 ];
411 const labels = (query: string) =>
412 filterCommandPaletteGroups({
413 activeGroups: [],
414 query,
415 isInSubmenu: false,
416 projectSearchItems,
417 threadSearchItems,
418 }).map((group) => group.label);
419 expect(labels("lakebed")).toEqual(["Projects", "Threads"]);
420 expect(labels("lake")).toEqual(["Threads", "Projects"]);
421 });
422
423 it("keeps a command first over a thread in daily use that matches as well", () => {
424 const now = Date.parse("2026-10-23T00:00:00.000Z");
425 const threadSearchItems = buildThreadActionItems({
426 threads: [
427 makeThread({
428 title: "New onboarding flow",
429 latestUserMessageAt: "2026-10-22T00:00:00.000Z",
430 opens: { openedAt: Date.parse("2026-10-22T00:00:00.000Z"), score: 7, picks: [] },
431 }),
432 ],
433 projectByKey: new Map(),
434 icon: null,
435 runThread: async () => undefined,
436 });
437 const groups = filterCommandPaletteGroups({
438 activeGroups: [
439 {
440 value: "actions",
441 label: "Actions",
442 items: [
443 {
444 kind: "action",
445 value: "action:new-thread",
446 searchTerms: ["new thread", "chat"],
447 title: "New thread",
448 icon: null,
449 run: async () => undefined,
450 },
451 ],
452 },
453 ],
454 query: "new",
455 isInSubmenu: false,
456 projectSearchItems: [],
457 threadSearchItems,
458 now,
459 });
460 expect(groups.map((group) => group.label)).toEqual(["Actions", "Threads"]);
461 });
462
463 it("ranks an order-independent setting title match above a split context match", () => {
464 const settingsSearchItems = [
465 {
466 kind: "action" as const,
467 value: "setting:context-match",
468 searchTerms: ["Pairing settings", "remote backend"],
469 title: "Context match",
470 icon: null,
471 run: async () => undefined,
472 },
473 {
474 kind: "action" as const,
475 value: "setting:remote-pairing",
476 searchTerms: ["Remote pairing", "connections"],
477 title: "Remote pairing",
478 icon: null,
479 run: async () => undefined,
480 },
481 ];
482
483 const groups = filterCommandPaletteGroups({
484 activeGroups: [],
485 query: "pairing remote",
486 isInSubmenu: false,
487 projectSearchItems: [],
488 settingsSearchItems,
489 threadSearchItems: [],
490 });
491
492 expect(groups).toHaveLength(1);
493 expect(groups[0]?.value).toBe("settings-search");
494 expect(groups[0]?.items.map((item) => item.value)).toEqual([
495 "setting:remote-pairing",
496 "setting:context-match",
497 ]);
498 });
499
500 it("searches a styled command title by its plain text", () => {
501 const groups = filterCommandPaletteGroups({
502 activeGroups: [
503 {
504 value: "actions",
505 label: "Actions",
506 items: [
507 {
508 kind: "action",
509 value: "action:new-thread",
510 searchTerms: ["new thread", "chat"],
511 // Styled: not a string, so only `searchTitle` holds its words.
512 title: ["New thread in ", "t3code"],
513 searchTitle: "New thread in t3code",
514 icon: null,
515 run: async () => undefined,
516 },
517 ],
518 },
519 ],
520 query: "> new t3code",
521 isInSubmenu: false,
522 projectSearchItems: [],
523 threadSearchItems: [],
524 });
525 expect(groups.flatMap((group) => group.items.map((item) => item.value))).toEqual([
526 "action:new-thread",
527 ]);
528 });
529
530 it("forgives a typo in a setting name when nothing matches exactly", () => {
531 const groups = filterCommandPaletteGroups({
532 activeGroups: [],
533 query: "keybindngs",
534 isInSubmenu: false,
535 projectSearchItems: [],
536 threadSearchItems: [],
537 settingsSearchItems: [
538 {
539 kind: "action",
540 value: "setting:keybindings",
541 title: "Keybindings",
542 searchTerms: ["Keybindings", "General", "shortcuts"],
543 icon: null,
544 run: async () => undefined,
545 },
546 ],
547 });
548 expect(groups.flatMap((group) => group.items.map((item) => item.value))).toEqual([
549 "setting:keybindings",
550 ]);
551 });
552
553 it("normalizes case independently of the host locale", () => {
554 const toLocaleLowerCase = String.prototype.toLocaleLowerCase;
555 const localeLowerCase = vi
556 .spyOn(String.prototype, "toLocaleLowerCase")
557 .mockImplementation(function (this: string) {
558 return toLocaleLowerCase.call(this, "tr");
559 });
560 try {
561 const groups = filterCommandPaletteGroups({
562 activeGroups: [],
563 query: "GIT",
564 isInSubmenu: false,
565 projectSearchItems: [],
566 threadSearchItems: [],
567 settingsSearchItems: [
568 {
569 kind: "action",
570 value: "setting:version-control",
571 title: "Version control",
572 searchTerms: ["git"],
573 icon: null,
574 run: async () => undefined,
575 },
576 ],
577 });
578 expect(groups.flatMap((group) => group.items.map((item) => item.value))).toEqual([
579 "setting:version-control",
580 ]);
581 } finally {
582 localeLowerCase.mockRestore();
583 }
584 });
585
586 it("surfaces threads when the query is their ID, without outranking title matches", () => {
587 const idThread = makeThread({
588 id: ThreadId.make("thread-alpha-1234"),
589 title: "Unrelated work",
590 updatedAt: "2026-03-05T00:00:00.000Z",
591 });
592 const titleThread = makeThread({
593 id: ThreadId.make("thread-other-9999"),
594 title: "Fix thread-alpha-1234 flakes",
595 updatedAt: "2026-03-04T00:00:00.000Z",
596 });
597 const items = buildThreadActionItems({
598 threads: [idThread, titleThread],
599 projectByKey: new Map([[`${LOCAL_ENVIRONMENT_ID}:${PROJECT_ID}`, { title: "T3 Code" }]]),
600 icon: null,
601 runThread: async (_thread) => undefined,
602 });
603
604 const groups = filterCommandPaletteGroups({
605 activeGroups: [],
606 query: " THREAD-ALPHA-1234 ",
607 isInSubmenu: false,
608 projectSearchItems: [],
609 settingsSearchItems: [],
610 threadSearchItems: items,
611 });
612
613 expect(groups.flatMap((group) => group.items)).toEqual([
614 expect.objectContaining({ value: `thread:${LOCAL_ENVIRONMENT_ID}:${titleThread.id}` }),
615 expect.objectContaining({ value: `thread:${LOCAL_ENVIRONMENT_ID}:${idThread.id}` }),
616 ]);
617 });
618
619 it("hides subagent threads but keeps forks", () => {
620 const parentId = ThreadId.make("parent");
621 const items = buildThreadActionItems({
622 threads: [
623 makeThread({ id: parentId, title: "Parent" }),
624 makeThread({
625 id: ThreadId.make("subagent"),
626 title: "Subagent",
627 lineage: {
628 rootThreadId: parentId,
629 parentThreadId: parentId,
630 relationshipToParent: "subagent",
631 },
632 }),
633 makeThread({
634 id: ThreadId.make("fork"),
635 title: "Fork",
636 lineage: {
637 rootThreadId: parentId,
638 parentThreadId: parentId,
639 relationshipToParent: "fork",
640 },
641 }),
642 ],
643 projectByKey: new Map(),
644 icon: null,
645 runThread: async () => undefined,
646 });
647
648 expect(items.map((item) => item.value).toSorted()).toEqual([
649 "thread:environment-local:fork",
650 "thread:environment-local:parent",
651 ]);
652 });
653
654 it("ranks word-start title matches above newer mid-word matches", () => {
655 const items = buildThreadActionItems({
656 threads: [
657 makeThread({
658 id: ThreadId.make("mid-word"),
659 title: "Fix oauth redirect",
660 updatedAt: "2026-03-20T00:00:00.000Z",
661 }),
662 makeThread({
663 id: ThreadId.make("word-start"),
664 title: "Clean up auth tokens",
665 updatedAt: "2026-03-02T00:00:00.000Z",
666 }),
667 makeThread({
668 id: ThreadId.make("branch-word-start"),
669 title: "Refactor login",
670 branch: "feature/auth-cleanup",
671 updatedAt: "2026-03-01T00:00:00.000Z",
672 }),
673 ],
674 projectByKey: new Map(),
675 icon: null,
676 runThread: async () => undefined,
677 });
678
679 const groups = filterCommandPaletteGroups({
680 activeGroups: [],
681 query: "auth",
682 isInSubmenu: false,
683 projectSearchItems: [],
684 threadSearchItems: items,
685 });
686
687 expect(groups[0]?.items.map((item) => item.value)).toEqual([
688 "thread:environment-local:word-start",
689 "thread:environment-local:branch-word-start",
690 "thread:environment-local:mid-word",
691 ]);
692 });
693
694 it("breaks ties between equal matches by latest user message", () => {
695 const items = buildThreadActionItems({
696 threads: [
697 makeThread({
698 id: ThreadId.make("prefix-stale"),
699 title: "Auth refactor",
700 updatedAt: "2026-03-20T00:00:00.000Z",
701 latestUserMessageAt: "2025-09-01T00:00:00.000Z",
702 }),
703 makeThread({
704 id: ThreadId.make("word-fresh"),
705 title: "Fix auth refresh",
706 updatedAt: "2026-03-10T00:00:00.000Z",
707 latestUserMessageAt: "2026-03-10T00:00:00.000Z",
708 }),
709 ],
710 projectByKey: new Map(),
711 icon: null,
712 runThread: async () => undefined,
713 });
714
715 const groups = filterCommandPaletteGroups({
716 activeGroups: [],
717 query: "auth",
718 isInSubmenu: false,
719 projectSearchItems: [],
720 threadSearchItems: items,
721 now: Date.parse("2026-03-20T00:00:00.000Z"),
722 });
723
724 expect(groups[0]?.items.map((item) => item.value)).toEqual([
725 "thread:environment-local:word-fresh",
726 "thread:environment-local:prefix-stale",
727 ]);
728 });
729});
730
731describe("filterPinnedBrowseEntries", () => {
732 it("matches an existing pinned destination without Windows casing", () => {
733 const windowsEntries = [
734 { name: "Repo", fullPath: "C:\\projects\\Repo" },
735 { name: "work", fullPath: "C:\\projects\\work" },
736 ];
737 expect(
738 filterPinnedBrowseEntries({
739 browseEntries: windowsEntries,
740 filterQuery: "repo",
741 pinnedDirectoryName: "repo",
742 caseSensitive: false,
743 }),
744 ).toEqual({
745 visibleEntries: windowsEntries,
746 exactEntry: windowsEntries[0],
747 });
748 });
749});
750
751it.each([
752 "#10839",
753 "10839",
754 "pingdotgg/t3code#10839",
755 "https://github.com/pingdotgg/t3code/pull/10839",
756])("finds linked threads from PR query %s", (query) => {
757 const items = buildThreadActionItems({
758 threads: [
759 makeThread({
760 title: "Implementation",
761 pullRequests: [
762 {
763 host: "github.com",
764 repository: "pingdotgg/t3code",
765 number: 10839,
766 url: "https://github.com/pingdotgg/t3code/pull/10839",
767 source: "manual",
768 linkedAt: "2026-09-08T00:00:00Z",
769 snapshot: null,
770 stack: null,
771 },
772 ],
773 }),
774 makeThread({ id: ThreadId.make("unrelated"), title: "Other work" }),
775 ],
776 projectByKey: new Map(),
777 icon: null,
778 runThread: async () => undefined,
779 });
780 const groups = filterCommandPaletteGroups({
781 activeGroups: [],
782 query,
783 isInSubmenu: false,
784 projectSearchItems: [],
785 threadSearchItems: items,
786 });
787 expect(groups.flatMap((group) => group.items.map((item) => item.title))).toEqual([
788 "Implementation",
789 ]);
790});
791
792describe("command palette search return", () => {
793 const picker = { addonIcon: null, groups: [] };
794
795 it("restores each parent query when backing out of nested pickers", () => {
796 let state = createCommandPaletteSearchState(">new thread");
797 state = reduceCommandPaletteSearchState(state, { type: "push", view: picker });
798 expect(state.query).toBe("");
799 state = reduceCommandPaletteSearchState(state, { type: "query", query: "remote project" });
800 state = reduceCommandPaletteSearchState(state, {
801 type: "push",
802 view: { ...picker, initialQuery: "~/" },
803 });
804 expect(state.query).toBe("~/");
805
806 state = reduceCommandPaletteSearchState(state, { type: "query", query: "" });
807 state = reduceCommandPaletteSearchState(state, { type: "back" });
808 expect(state.query).toBe("remote project");
809 expect(state.views).toHaveLength(1);
810 state = reduceCommandPaletteSearchState(state, { type: "back" });
811 expect(state.query).toBe(">new thread");
812 expect(state.views).toHaveLength(0);
813 });
814
815 it("cannot return to an old picker after a new search replaces it", () => {
816 let state = reduceCommandPaletteSearchState(createCommandPaletteSearchState("old"), {
817 type: "push",
818 view: picker,
819 });
820 state = reduceCommandPaletteSearchState(state, { type: "reset", query: "fresh" });
821 state = reduceCommandPaletteSearchState(state, { type: "back" });
822 expect(state.query).toBe("fresh");
823 expect(state.views).toHaveLength(0);
824 });
825});
826
827describe("returning from file and content search", () => {
828 const root: CommandPaletteUiState = { open: true, mode: "command", openIntent: null };
829 const query = "https://github.com/acme/web/pull/7";
830 const context = {
831 query,
832 linkedThreadSearch: {
833 kind: "search" as const,
834 query,
835 linkedThreads: {
836 environmentId: EnvironmentId.make("remote"),
837 threads: [
838 {
839 id: ThreadId.make("linked-thread"),
840 projectId: ProjectId.make("project"),
841 title: "Completed work",
842 archivedAt: "2026-09-01T00:00:00.000Z",
843 },
844 ],
845 },
846 },
847 };
848
849 it.each(["files", "content"] as const)(
850 "Escape from %s restores the query and linked-thread results",
851 (mode) => {
852 const child = reduceCommandPaletteUiState(root, { _tag: "OpenChildSearch", mode, context });
853 expect(child.mode).toBe(mode);
854 const returned = reduceCommandPaletteUiState(child, { _tag: "BackToCommand" });
855 expect(returned).toEqual({ ...root, restoredSearch: context });
856 expect(reduceCommandPaletteUiState(returned, { _tag: "BackToCommand" })).toEqual(returned);
857 },
858 );
859
860 it("closing a child search discards its return context before reopening", () => {
861 const child: CommandPaletteUiState = { ...root, mode: "files", parentSearch: context };
862 const closed = reduceCommandPaletteUiState(child, { _tag: "SetOpen", open: false });
863 expect(closed).toEqual({ open: false, mode: "files", openIntent: null });
864 expect(reduceCommandPaletteUiState(closed, { _tag: "BackToCommand" })).toEqual(closed);
865 expect(reduceCommandPaletteUiState(closed, { _tag: "SetOpen", open: true })).toEqual(root);
866 });
867});