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

composerThreadItems.test.ts

274 lines9.1 KB

1import { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 matchComposerEnvironmentItems,
6 matchComposerProjectItems,
7 matchComposerThreadItems,
8 type ComposerProjectCandidate,
9 type ComposerThreadCandidate,
10} from "./composerThreadItems.ts";
11​
12const environmentId = EnvironmentId.make("env-composer");
13​
14it("references projects by their host folder, leaving out other machines, archives and scratch", () => {
15 const project: ComposerProjectCandidate = {
16 id: ProjectId.make("common"),
17 environmentId,
18 title: "Common library",
19 workspaceRoot: "/work/Shared code",
20 };
21 const projects = [
22 project,
23 { ...project, id: ProjectId.make("remote"), environmentId: EnvironmentId.make("other") },
24 { ...project, id: ProjectId.make("archived"), archivedAt: "2026-10-01T00:00:00.000Z" },
25 { ...project, id: ProjectId.make("scratch"), isScratch: true as const },
26 ];
27 const expected = [
28 {
29 id: "project:env-composer:common",
30 type: "path",
31 path: "/work/Shared code",
32 label: "Common library",
33 description: "/work/Shared code",
34 },
35 ];
36 expect(matchComposerProjectItems({ projects, environmentId, query: " COMMON " })).toEqual(
37 expected,
38 );
39 expect(matchComposerProjectItems({ projects, environmentId, query: "shared" })).toEqual(expected);
40 expect(matchComposerProjectItems({ projects, environmentId, query: "" })).toEqual([]);
41 expect(matchComposerProjectItems({ projects, environmentId, query: "missing" })).toEqual([]);
42});
43​
44it("matches a project's folder name, not the parent folders every project shares", () => {
45 const project: ComposerProjectCandidate = {
46 id: ProjectId.make("app"),
47 environmentId,
48 title: "App",
49 workspaceRoot: "/Users/me/code/web-app/",
50 };
51 const windowsProject = {
52 ...project,
53 id: ProjectId.make("tools"),
54 title: "Tools",
55 workspaceRoot: "C:\\Users\\me\\code\\Build tools",
56 };
57 const projects = [project, windowsProject];
58 for (const query of ["use", "users", "code", "/me/"]) {
59 expect(matchComposerProjectItems({ projects, environmentId, query })).toEqual([]);
60 }
61 expect(
62 matchComposerProjectItems({ projects, environmentId, query: "web-app" }).map(
63 (item) => item.label,
64 ),
65 ).toEqual(["App"]);
66 expect(
67 matchComposerProjectItems({ projects, environmentId, query: "build" }).map(
68 (item) => item.label,
69 ),
70 ).toEqual(["Tools"]);
71});
72​
73function shell(
74 id: string,
75 title: string,
76 overrides: Partial<ComposerThreadCandidate> = {},
77): ComposerThreadCandidate {
78 return {
79 environmentId,
80 id: ThreadId.make(id),
81 title,
82 branch: null,
83 createdAt: "2026-10-01T00:00:00.000Z",
84 updatedAt: "2026-10-01T00:00:00.000Z",
85 latestUserMessageAt: null,
86 opens: null,
87 archivedAt: null,
88 lineage: { rootThreadId: ThreadId.make(id), parentThreadId: null, relationshipToParent: null },
89 ...overrides,
90 };
92​
93it("ranks thread mentions like the palette and leaves out subagent threads", () => {
94 const items = matchComposerThreadItems({
95 shells: [
96 shell("newer-mid-word", "Remodel the settings page", {
97 updatedAt: "2026-10-20T00:00:00.000Z",
98 }),
99 shell("word", "Fix model picker"),
100 shell("subagent", "Model review", {
101 lineage: {
102 rootThreadId: ThreadId.make("word"),
103 parentThreadId: ThreadId.make("word"),
104 relationshipToParent: "subagent",
105 },
106 }),
107 shell("other-environment", "Model docs", { environmentId: EnvironmentId.make("env-other") }),
108 ],
109 environmentId,
110 linkedEnvironments: new Map(),
111 excludeThreadId: null,
112 query: "model",
113 now: Date.parse("2026-10-23T00:00:00.000Z"),
114 });
115 expect(items.map((item) => item.thread.threadId)).toEqual(["word", "newer-mid-word"]);
116});
117​
118it("offers the composer thread's own used side chats, but not other threads' side chats", () => {
119 const sideChatOf = (parent: string) => ({
120 rootThreadId: ThreadId.make(parent),
121 parentThreadId: ThreadId.make(parent),
122 relationshipToParent: "fork" as const,
123 sideChat: true,
124 });
125 const latestUserMessageAt = "2026-10-22T00:00:00.000Z";
126 const items = matchComposerThreadItems({
127 shells: [
128 shell("mine", "Why is the build slow?", { lineage: sideChatOf("main"), latestUserMessageAt }),
129 shell("unused", "Slow side chat", { lineage: sideChatOf("main") }),
130 shell("theirs", "Why is the test slow?", {
131 lineage: sideChatOf("other"),
132 latestUserMessageAt,
133 }),
134 ],
135 environmentId,
136 linkedEnvironments: new Map(),
137 excludeThreadId: ThreadId.make("main"),
138 query: "slow",
139 now: Date.parse("2026-10-23T00:00:00.000Z"),
140 });
141 expect(items.map(({ thread, description }) => ({ id: thread.threadId, description }))).toEqual([
142 { id: "mine", description: "Side chat" },
143 ]);
144});
145​
146it("also offers threads on linked machines, naming the machine, but no other machine's", () => {
147 const vps = EnvironmentId.make("env-vps");
148 const items = matchComposerThreadItems({
149 shells: [
150 shell("here", "Deploy script"),
151 shell("there", "Deploy script", { environmentId: vps }),
152 shell("unlinked", "Deploy notes", { environmentId: EnvironmentId.make("env-unlinked") }),
153 ],
154 environmentId,
155 linkedEnvironments: new Map([[vps, "Hetzner VPS"]]),
156 excludeThreadId: null,
157 query: "deploy",
158 now: Date.parse("2026-10-23T00:00:00.000Z"),
159 });
160 expect(items.map(({ thread, label, description }) => ({ thread, label, description }))).toEqual([
161 { thread: { environmentId, threadId: "here" }, label: "Deploy script", description: "Thread" },
162 {
163 thread: { environmentId: vps, threadId: "there" },
164 label: "Deploy script",
165 description: "Thread on Hetzner VPS",
166 },
167 ]);
168​
169 const sameIdItems = matchComposerThreadItems({
170 shells: [
171 shell("same", "Deploy script"),
172 shell("same", "Deploy script", { environmentId: vps }),
173 ],
174 environmentId,
175 linkedEnvironments: new Map([[vps, "Hetzner VPS"]]),
176 excludeThreadId: ThreadId.make("same"),
177 query: "deploy",
178 now: Date.parse("2026-10-23T00:00:00.000Z"),
179 });
180 expect(sameIdItems.map(({ thread, description }) => ({ thread, description }))).toEqual([
181 {
182 thread: { environmentId: vps, threadId: "same" },
183 description: "Thread on Hetzner VPS",
184 },
185 ]);
186});
187​
188describe("matchComposerEnvironmentItems", () => {
189 const environments = [
190 { environmentId: EnvironmentId.make("env-here"), label: "Laptop", machine: "laptop" },
191 { environmentId: EnvironmentId.make("env-vps"), label: "Hetzner VPS", machine: "cloud" },
192 { environmentId: EnvironmentId.make("env-mini"), label: "Mac mini", machine: "mac-mini" },
193 ] as const;
194​
195 it("matches machine names typed in a different Unicode normalization form", () => {
196 const match = (label: string, query: string) =>
197 matchComposerEnvironmentItems({
198 environments: [{ environmentId: EnvironmentId.make("env-cafe"), label, machine: "server" }],
199 linkedEnvironments: new Map(),
200 environmentId: EnvironmentId.make("env-here"),
201 query,
202 }).map((item) => item.label);
203 expect(match("Caf\u00e9", "cafe\u0301")).toEqual(["Caf\u00e9"]);
204 expect(match("Cafe\u0301", "caf\u00e9")).toEqual(["Cafe\u0301"]);
205 expect(match("J\u030c", "j\u030c")).toEqual(["J\u030c"]);
206 });
207​
208 it("offers the other machines matching the query, never the composer's own", () => {
209 expect(
210 matchComposerEnvironmentItems({
211 environments,
212 linkedEnvironments: new Map(),
213 environmentId: EnvironmentId.make("env-here"),
214 query: " vps ",
215 }),
216 ).toEqual([
217 {
218 id: "environment:env-vps",
219 type: "environment",
220 environmentId: "env-vps",
221 machine: "cloud",
222 label: "Hetzner VPS",
223 description: "Machine",
224 },
225 ]);
226 expect(
227 matchComposerEnvironmentItems({
228 environments,
229 linkedEnvironments: new Map(),
230 environmentId: EnvironmentId.make("env-here"),
231 query: "lap",
232 }),
233 ).toEqual([]);
234 expect(
235 matchComposerEnvironmentItems({
236 environments,
237 linkedEnvironments: new Map(),
238 environmentId: EnvironmentId.make("env-here"),
239 query: "",
240 }),
241 ).toEqual([]);
242 });
243​
244 it("offers machines the composer's environment links to that this client is not paired with", () => {
245 expect(
246 matchComposerEnvironmentItems({
247 environments: [],
248 linkedEnvironments: new Map([
249 [EnvironmentId.make("env-here"), "Laptop"],
250 [EnvironmentId.make("env-build"), "buildbox"],
251 ]),
252 environmentId: EnvironmentId.make("env-here"),
253 query: "build",
254 }),
255 ).toEqual([
256 {
257 id: "environment:env-build",
258 type: "environment",
259 environmentId: "env-build",
260 machine: "server",
261 label: "buildbox",
262 description: "Machine",
263 },
264 ]);
265 expect(
266 matchComposerEnvironmentItems({
267 environments,
268 linkedEnvironments: new Map([[EnvironmentId.make("env-vps"), "Hetzner VPS"]]),
269 environmentId: EnvironmentId.make("env-here"),
270 query: "vps",
271 }).map((item) => item.machine),
272 ).toEqual(["cloud"]);
273 });
274});