The best way to control Claude Code, Codex and more from any computer or phone

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