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

CommandPalette.logic.test.ts

1,005 lines33.1 KB

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