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

CommandPalette.logic.test.ts

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