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

CommandPalette.logic.test.ts

234 lines6.8 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2import { EnvironmentId, ProjectId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
3import type { Thread } from "../types";
4import {
5 buildBrowseGroups,
6 buildThreadActionItems,
7 enumerateCommandPaletteItems,
8 filterCommandPaletteGroups,
9 type CommandPaletteGroup,
10} from "./CommandPalette.logic";
11​
12describe("enumerateCommandPaletteItems", () => {
13 it("assigns positional jump shortcuts to the first nine displayed items", () => {
14 const items = Array.from({ length: 10 }, (_, index) => ({
15 kind: "action" as const,
16 value: `project-${index + 1}`,
17 searchTerms: [],
18 title: `Project ${index + 1}`,
19 icon: null,
20 shortcutCommand: "chat.new" as const,
21 run: async () => undefined,
22 }));
23​
24 expect(enumerateCommandPaletteItems(items).map((item) => item.shortcutCommand)).toEqual([
25 "thread.jump.1",
26 "thread.jump.2",
27 "thread.jump.3",
28 "thread.jump.4",
29 "thread.jump.5",
30 "thread.jump.6",
31 "thread.jump.7",
32 "thread.jump.8",
33 "thread.jump.9",
34 undefined,
35 ]);
36 });
37});
38​
39const LOCAL_ENVIRONMENT_ID = EnvironmentId.make("environment-local");
40const PROJECT_ID = ProjectId.make("project-1");
41​
42function makeThread(overrides: Partial<Thread> = {}): Thread {
43 return {
44 id: ThreadId.make("thread-1"),
45 environmentId: LOCAL_ENVIRONMENT_ID,
46 projectId: PROJECT_ID,
47 title: "Thread",
48 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },
49 runtimeMode: "full-access",
50 interactionMode: "default",
51 session: null,
52 messages: [],
53 proposedPlans: [],
54 createdAt: "2026-03-01T00:00:00.000Z",
55 archivedAt: null,
56 settledOverride: null,
57 settledAt: null,
58 deletedAt: null,
59 updatedAt: "2026-03-01T00:00:00.000Z",
60 latestTurn: null,
61 branch: null,
62 worktreePath: null,
63 checkpoints: [],
64 activities: [],
65 ...overrides,
66 };
68​
69describe("buildThreadActionItems", () => {
70 it("orders threads by most recent activity and formats timestamps from updatedAt", () => {
71 vi.useFakeTimers();
72 vi.setSystemTime(new Date("2026-03-25T12:00:00.000Z"));
73​
74 try {
75 const items = buildThreadActionItems({
76 threads: [
77 makeThread({
78 id: ThreadId.make("thread-older"),
79 title: "Older thread",
80 updatedAt: "2026-03-24T12:00:00.000Z",
81 }),
82 makeThread({
83 id: ThreadId.make("thread-newer"),
84 title: "Newer thread",
85 createdAt: "2026-03-20T00:00:00.000Z",
86 updatedAt: "2026-03-20T00:00:00.000Z",
87 }),
88 ],
89 projectTitleById: new Map([[PROJECT_ID, "Project"]]),
90 sortOrder: "updated_at",
91 icon: null,
92 runThread: async (_thread) => undefined,
93 });
94​
95 expect(items.map((item) => item.value)).toEqual([
96 "thread:thread-older",
97 "thread:thread-newer",
98 ]);
99 expect(items[0]?.timestamp).toBe("1d ago");
100 expect(items[1]?.timestamp).toBe("5d ago");
101 } finally {
102 vi.useRealTimers();
103 }
104 });
105​
106 it("ranks thread title matches ahead of contextual project-name matches", () => {
107 const threadItems = buildThreadActionItems({
108 threads: [
109 makeThread({
110 id: ThreadId.make("thread-context-match"),
111 title: "Fix navbar spacing",
112 updatedAt: "2026-03-20T00:00:00.000Z",
113 }),
114 makeThread({
115 id: ThreadId.make("thread-title-match"),
116 title: "Project kickoff notes",
117 createdAt: "2026-03-02T00:00:00.000Z",
118 updatedAt: "2026-03-19T00:00:00.000Z",
119 }),
120 ],
121 projectTitleById: new Map([[PROJECT_ID, "Project"]]),
122 sortOrder: "updated_at",
123 icon: null,
124 runThread: async (_thread) => undefined,
125 });
126​
127 const groups = filterCommandPaletteGroups({
128 activeGroups: [],
129 query: "project",
130 isInSubmenu: false,
131 projectSearchItems: [],
132 threadSearchItems: threadItems,
133 });
134​
135 expect(groups).toHaveLength(1);
136 expect(groups[0]?.value).toBe("threads-search");
137 expect(groups[0]?.items.map((item) => item.value)).toEqual([
138 "thread:thread-title-match",
139 "thread:thread-context-match",
140 ]);
141 });
142​
143 it("preserves thread project-name matches when there is no stronger title match", () => {
144 const group: CommandPaletteGroup = {
145 value: "threads-search",
146 label: "Threads",
147 items: [
148 {
149 kind: "action",
150 value: "thread:project-context-only",
151 searchTerms: ["Fix navbar spacing", "Project"],
152 title: "Fix navbar spacing",
153 description: "Project",
154 icon: null,
155 run: async () => undefined,
156 },
157 ],
158 };
159​
160 const groups = filterCommandPaletteGroups({
161 activeGroups: [group],
162 query: "project",
163 isInSubmenu: false,
164 projectSearchItems: [],
165 threadSearchItems: [],
166 });
167​
168 expect(groups).toHaveLength(1);
169 expect(groups[0]?.items.map((item) => item.value)).toEqual(["thread:project-context-only"]);
170 });
171​
172 it("filters archived threads out of thread search items", () => {
173 const items = buildThreadActionItems({
174 threads: [
175 makeThread({
176 id: ThreadId.make("thread-active"),
177 title: "Active thread",
178 createdAt: "2026-03-02T00:00:00.000Z",
179 updatedAt: "2026-03-19T00:00:00.000Z",
180 }),
181 makeThread({
182 id: ThreadId.make("thread-archived"),
183 title: "Archived thread",
184 archivedAt: "2026-03-20T00:00:00.000Z",
185 updatedAt: "2026-03-20T00:00:00.000Z",
186 }),
187 ],
188 projectTitleById: new Map([[PROJECT_ID, "Project"]]),
189 sortOrder: "updated_at",
190 icon: null,
191 runThread: async (_thread) => undefined,
192 });
193​
194 expect(items.map((item) => item.value)).toEqual(["thread:thread-active"]);
195 });
196});
197​
198describe("buildBrowseGroups", () => {
199 it("waits for asynchronous browse navigation actions", async () => {
200 let finishNavigation: (() => void) | undefined;
201 const browseTo = vi.fn(
202 () =>
203 new Promise<void>((resolve) => {
204 finishNavigation = resolve;
205 }),
206 );
207 const groups = buildBrowseGroups({
208 browseEntries: [{ name: "Downloads", fullPath: "/Users/test/Downloads" }],
209 browseQuery: "~/",
210 canBrowseUp: false,
211 upIcon: null,
212 directoryIcon: null,
213 browseUp: vi.fn(),
214 browseTo,
215 });
216 const item = groups[0]?.items[0];
217 if (!item || item.kind !== "action") {
218 throw new Error("Expected a browse action");
219 }
220​
221 let actionSettled = false;
222 const action = item.run().then(() => {
223 actionSettled = true;
224 });
225 await Promise.resolve();
226​
227 expect(browseTo).toHaveBeenCalledWith("Downloads");
228 expect(actionSettled).toBe(false);
229​
230 finishNavigation?.();
231 await action;
232 expect(actionSettled).toBe(true);
233 });
234});