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

CommandPalette.logic.test.ts

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