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

CommandPalette.logic.test.ts

166 lines5.0 KB

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