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