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

CommandPalette.logic.test.ts

963 lines31.6 KB

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