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