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