Open History to see the last commit
1import { describe, expect, it, vi } from "vite-plus/test";
2import { EnvironmentId, ProjectId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
3import type { Thread } from "../types";
4import { makeThreadFixture } from "../test-fixtures";
5import {
6 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 });
112}
113
114describe("buildThreadActionItems", () => {
115 it("ranks thread title matches ahead of contextual project-name matches", () => {
116 const threadItems = buildThreadActionItems({
117 threads: [
118 makeThread({
119 id: ThreadId.make("thread-context-match"),
120 title: "Fix navbar spacing",
121 updatedAt: "2026-03-20T00:00:00.000Z",
122 }),
123 makeThread({
124 id: ThreadId.make("thread-title-match"),
125 title: "Project kickoff notes",
126 createdAt: "2026-03-02T00:00:00.000Z",
127 updatedAt: "2026-03-19T00:00:00.000Z",
128 }),
129 ],
130 projectTitleById: new Map([[PROJECT_ID, "Project"]]),
131 sortOrder: "updated_at",
132 icon: null,
133 runThread: async (_thread) => undefined,
134 });
135
136 const groups = filterCommandPaletteGroups({
137 activeGroups: [],
138 query: "project",
139 isInSubmenu: false,
140 projectSearchItems: [],
141 threadSearchItems: threadItems,
142 });
143
144 expect(groups).toHaveLength(1);
145 expect(groups[0]?.value).toBe("threads-search");
146 expect(groups[0]?.items.map((item) => item.value)).toEqual([
147 "thread:thread-title-match",
148 "thread:thread-context-match",
149 ]);
150 });
151
152 it("orders title matches by recent activity before older prefix matches", () => {
153 const threads = [
154 makeThread({
155 id: ThreadId.make("old-prefix"),
156 title: "Convex InvalidCursor in Convex threads query",
157 createdAt: "2026-01-01T00:00:00.000Z",
158 updatedAt: "2026-01-02T00:00:00.000Z",
159 }),
160 makeThread({
161 id: ThreadId.make("recent-title"),
162 title: "Disable Convex schema validation",
163 createdAt: "2025-12-01T00:00:00.000Z",
164 updatedAt: "2026-03-24T00:00:00.000Z",
165 }),
166 makeThread({
167 id: ThreadId.make("recent-content"),
168 title: "Fix schema validation",
169 createdAt: "2026-03-25T00:00:00.000Z",
170 updatedAt: "2026-03-25T00:00:00.000Z",
171 }),
172 ];
173 const items = buildThreadActionItems({
174 threads,
175 projectTitleById: new Map([[PROJECT_ID, "T3 Code"]]),
176 sortOrder: "created_at",
177 icon: null,
178 getContentMatch: (thread) =>
179 thread.id === ThreadId.make("recent-content")
180 ? { source: "user", snippet: "Please check Convex", query: "convex" }
181 : undefined,
182 runThread: async () => undefined,
183 });
184
185 const groups = filterCommandPaletteGroups({
186 activeGroups: [],
187 query: "convex",
188 isInSubmenu: false,
189 projectSearchItems: [],
190 threadSearchItems: items,
191 });
192
193 expect(groups[0]?.items.map((item) => item.value)).toEqual([
194 "thread:recent-title",
195 "thread:old-prefix",
196 "thread:recent-content",
197 ]);
198 });
199
200 it("ranks an order-independent setting title match above a split context match", () => {
201 const settingsSearchItems = [
202 {
203 kind: "action" as const,
204 value: "setting:context-match",
205 searchTerms: ["Pairing settings", "remote backend"],
206 title: "Context match",
207 icon: null,
208 run: async () => undefined,
209 },
210 {
211 kind: "action" as const,
212 value: "setting:remote-pairing",
213 searchTerms: ["Remote pairing", "connections"],
214 title: "Remote pairing",
215 icon: null,
216 run: async () => undefined,
217 },
218 ];
219
220 const groups = filterCommandPaletteGroups({
221 activeGroups: [],
222 query: "pairing remote",
223 isInSubmenu: false,
224 projectSearchItems: [],
225 settingsSearchItems,
226 threadSearchItems: [],
227 });
228
229 expect(groups).toHaveLength(1);
230 expect(groups[0]?.value).toBe("settings-search");
231 expect(groups[0]?.items.map((item) => item.value)).toEqual([
232 "setting:remote-pairing",
233 "setting:context-match",
234 ]);
235 });
236
237 it("normalizes case independently of the host locale", () => {
238 const toLocaleLowerCase = String.prototype.toLocaleLowerCase;
239 const localeLowerCase = vi
240 .spyOn(String.prototype, "toLocaleLowerCase")
241 .mockImplementation(function (this: string) {
242 return toLocaleLowerCase.call(this, "tr");
243 });
244 try {
245 const groups = filterCommandPaletteGroups({
246 activeGroups: [],
247 query: "GIT",
248 isInSubmenu: false,
249 projectSearchItems: [],
250 threadSearchItems: [],
251 settingsSearchItems: [
252 {
253 kind: "action",
254 value: "setting:version-control",
255 title: "Version control",
256 searchTerms: ["git"],
257 icon: null,
258 run: async () => undefined,
259 },
260 ],
261 });
262 expect(groups.flatMap((group) => group.items.map((item) => item.value))).toEqual([
263 "setting:version-control",
264 ]);
265 } finally {
266 localeLowerCase.mockRestore();
267 }
268 });
269
270 it("surfaces threads when the query is their ID, without outranking title matches", () => {
271 const idThread = makeThread({
272 id: ThreadId.make("thread-alpha-1234"),
273 title: "Unrelated work",
274 updatedAt: "2026-03-05T00:00:00.000Z",
275 });
276 const titleThread = makeThread({
277 id: ThreadId.make("thread-other-9999"),
278 title: "Fix thread-alpha-1234 flakes",
279 updatedAt: "2026-03-04T00:00:00.000Z",
280 });
281 const items = buildThreadActionItems({
282 threads: [idThread, titleThread],
283 projectTitleById: new Map([[PROJECT_ID, "T3 Code"]]),
284 sortOrder: "updated_at",
285 icon: null,
286 runThread: async (_thread) => undefined,
287 });
288
289 const groups = filterCommandPaletteGroups({
290 activeGroups: [],
291 query: " THREAD-ALPHA-1234 ",
292 isInSubmenu: false,
293 projectSearchItems: [],
294 settingsSearchItems: [],
295 threadSearchItems: items,
296 });
297
298 expect(groups.flatMap((group) => group.items)).toEqual([
299 expect.objectContaining({ value: `thread:${titleThread.id}` }),
300 expect.objectContaining({ value: `thread:${idThread.id}` }),
301 ]);
302 });
303});
304
305describe("filterPinnedBrowseEntries", () => {
306 it("matches an existing pinned destination without Windows casing", () => {
307 const windowsEntries = [
308 { name: "Repo", fullPath: "C:\\projects\\Repo" },
309 { name: "work", fullPath: "C:\\projects\\work" },
310 ];
311 expect(
312 filterPinnedBrowseEntries({
313 browseEntries: windowsEntries,
314 filterQuery: "repo",
315 pinnedDirectoryName: "repo",
316 caseSensitive: false,
317 }),
318 ).toEqual({
319 visibleEntries: windowsEntries,
320 exactEntry: windowsEntries[0],
321 });
322 });
323});
324
325it.each([
326 "#10839",
327 "10839",
328 "pingdotgg/t3code#10839",
329 "https://github.com/pingdotgg/t3code/pull/10839",
330])("finds linked threads from PR query %s", (query) => {
331 const items = buildThreadActionItems({
332 threads: [
333 makeThread({
334 title: "Implementation",
335 pullRequests: [
336 {
337 host: "github.com",
338 repository: "pingdotgg/t3code",
339 number: 10839,
340 url: "https://github.com/pingdotgg/t3code/pull/10839",
341 source: "manual",
342 linkedAt: "2026-09-08T00:00:00Z",
343 snapshot: null,
344 stack: null,
345 },
346 ],
347 }),
348 makeThread({ id: ThreadId.make("unrelated"), title: "Other work" }),
349 ],
350 projectTitleById: new Map(),
351 sortOrder: "updated_at",
352 icon: null,
353 runThread: async () => undefined,
354 });
355 const groups = filterCommandPaletteGroups({
356 activeGroups: [],
357 query,
358 isInSubmenu: false,
359 projectSearchItems: [],
360 threadSearchItems: items,
361 });
362 expect(groups.flatMap((group) => group.items.map((item) => item.title))).toEqual([
363 "Implementation",
364 ]);
365});
366
367describe("command palette search return", () => {
368 const picker = { addonIcon: null, groups: [] };
369
370 it("restores each parent query when backing out of nested pickers", () => {
371 let state = createCommandPaletteSearchState(">new thread");
372 state = reduceCommandPaletteSearchState(state, { type: "push", view: picker });
373 expect(state.query).toBe("");
374 state = reduceCommandPaletteSearchState(state, { type: "query", query: "remote project" });
375 state = reduceCommandPaletteSearchState(state, {
376 type: "push",
377 view: { ...picker, initialQuery: "~/" },
378 });
379 expect(state.query).toBe("~/");
380
381 state = reduceCommandPaletteSearchState(state, { type: "query", query: "" });
382 state = reduceCommandPaletteSearchState(state, { type: "back" });
383 expect(state.query).toBe("remote project");
384 expect(state.views).toHaveLength(1);
385 state = reduceCommandPaletteSearchState(state, { type: "back" });
386 expect(state.query).toBe(">new thread");
387 expect(state.views).toHaveLength(0);
388 });
389
390 it("cannot return to an old picker after a new search replaces it", () => {
391 let state = reduceCommandPaletteSearchState(createCommandPaletteSearchState("old"), {
392 type: "push",
393 view: picker,
394 });
395 state = reduceCommandPaletteSearchState(state, { type: "reset", query: "fresh" });
396 state = reduceCommandPaletteSearchState(state, { type: "back" });
397 expect(state.query).toBe("fresh");
398 expect(state.views).toHaveLength(0);
399 });
400});
401
402describe("returning from file and content search", () => {
403 const root: CommandPaletteUiState = { open: true, mode: "command", openIntent: null };
404 const query = "https://github.com/acme/web/pull/7";
405 const context = {
406 query,
407 linkedThreadSearch: {
408 kind: "search" as const,
409 query,
410 linkedThreads: {
411 environmentId: EnvironmentId.make("remote"),
412 threads: [
413 {
414 id: ThreadId.make("linked-thread"),
415 projectId: ProjectId.make("project"),
416 title: "Completed work",
417 archivedAt: "2026-09-01T00:00:00.000Z",
418 },
419 ],
420 },
421 },
422 };
423
424 it.each(["files", "content"] as const)(
425 "Escape from %s restores the query and linked-thread results",
426 (mode) => {
427 const child = reduceCommandPaletteUiState(root, { _tag: "OpenChildSearch", mode, context });
428 expect(child.mode).toBe(mode);
429 const returned = reduceCommandPaletteUiState(child, { _tag: "BackToCommand" });
430 expect(returned).toEqual({ ...root, restoredSearch: context });
431 expect(reduceCommandPaletteUiState(returned, { _tag: "BackToCommand" })).toEqual(returned);
432 },
433 );
434
435 it("closing a child search discards its return context before reopening", () => {
436 const child: CommandPaletteUiState = { ...root, mode: "files", parentSearch: context };
437 const closed = reduceCommandPaletteUiState(child, { _tag: "SetOpen", open: false });
438 expect(closed).toEqual({ open: false, mode: "files", openIntent: null });
439 expect(reduceCommandPaletteUiState(closed, { _tag: "BackToCommand" })).toEqual(closed);
440 expect(reduceCommandPaletteUiState(closed, { _tag: "SetOpen", open: true })).toEqual(root);
441 });
442});