Open History to see the last commit
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
44function shell(
45 id: string,
46 title: string,
47 overrides: Partial<ComposerThreadCandidate> = {},
48): ComposerThreadCandidate {
49 return {
50 environmentId,
51 id: ThreadId.make(id),
52 title,
53 branch: null,
54 createdAt: "2026-10-01T00:00:00.000Z",
55 updatedAt: "2026-10-01T00:00:00.000Z",
56 latestUserMessageAt: null,
57 opens: null,
58 archivedAt: null,
59 lineage: { rootThreadId: ThreadId.make(id), parentThreadId: null, relationshipToParent: null },
60 ...overrides,
61 };
62}
63
64it("ranks thread mentions like the palette and leaves out subagent threads", () => {
65 const items = matchComposerThreadItems({
66 shells: [
67 shell("newer-mid-word", "Remodel the settings page", {
68 updatedAt: "2026-10-20T00:00:00.000Z",
69 }),
70 shell("word", "Fix model picker"),
71 shell("subagent", "Model review", {
72 lineage: {
73 rootThreadId: ThreadId.make("word"),
74 parentThreadId: ThreadId.make("word"),
75 relationshipToParent: "subagent",
76 },
77 }),
78 shell("other-environment", "Model docs", { environmentId: EnvironmentId.make("env-other") }),
79 ],
80 environmentId,
81 linkedEnvironments: new Map(),
82 excludeThreadId: null,
83 query: "model",
84 now: Date.parse("2026-10-23T00:00:00.000Z"),
85 });
86 expect(items.map((item) => item.thread.threadId)).toEqual(["word", "newer-mid-word"]);
87});
88
89it("offers the composer thread's own used side chats, but not other threads' side chats", () => {
90 const sideChatOf = (parent: string) => ({
91 rootThreadId: ThreadId.make(parent),
92 parentThreadId: ThreadId.make(parent),
93 relationshipToParent: "fork" as const,
94 sideChat: true,
95 });
96 const latestUserMessageAt = "2026-10-22T00:00:00.000Z";
97 const items = matchComposerThreadItems({
98 shells: [
99 shell("mine", "Why is the build slow?", { lineage: sideChatOf("main"), latestUserMessageAt }),
100 shell("unused", "Slow side chat", { lineage: sideChatOf("main") }),
101 shell("theirs", "Why is the test slow?", {
102 lineage: sideChatOf("other"),
103 latestUserMessageAt,
104 }),
105 ],
106 environmentId,
107 linkedEnvironments: new Map(),
108 excludeThreadId: ThreadId.make("main"),
109 query: "slow",
110 now: Date.parse("2026-10-23T00:00:00.000Z"),
111 });
112 expect(items.map(({ thread, description }) => ({ id: thread.threadId, description }))).toEqual([
113 { id: "mine", description: "Side chat" },
114 ]);
115});
116
117it("also offers threads on linked machines, naming the machine, but no other machine's", () => {
118 const vps = EnvironmentId.make("env-vps");
119 const items = matchComposerThreadItems({
120 shells: [
121 shell("here", "Deploy script"),
122 shell("there", "Deploy script", { environmentId: vps }),
123 shell("unlinked", "Deploy notes", { environmentId: EnvironmentId.make("env-unlinked") }),
124 ],
125 environmentId,
126 linkedEnvironments: new Map([[vps, "Hetzner VPS"]]),
127 excludeThreadId: null,
128 query: "deploy",
129 now: Date.parse("2026-10-23T00:00:00.000Z"),
130 });
131 expect(items.map(({ thread, label, description }) => ({ thread, label, description }))).toEqual([
132 { thread: { environmentId, threadId: "here" }, label: "Deploy script", description: "Thread" },
133 {
134 thread: { environmentId: vps, threadId: "there" },
135 label: "Deploy script",
136 description: "Thread on Hetzner VPS",
137 },
138 ]);
139
140 const sameIdItems = matchComposerThreadItems({
141 shells: [
142 shell("same", "Deploy script"),
143 shell("same", "Deploy script", { environmentId: vps }),
144 ],
145 environmentId,
146 linkedEnvironments: new Map([[vps, "Hetzner VPS"]]),
147 excludeThreadId: ThreadId.make("same"),
148 query: "deploy",
149 now: Date.parse("2026-10-23T00:00:00.000Z"),
150 });
151 expect(sameIdItems.map(({ thread, description }) => ({ thread, description }))).toEqual([
152 {
153 thread: { environmentId: vps, threadId: "same" },
154 description: "Thread on Hetzner VPS",
155 },
156 ]);
157});
158
159describe("matchComposerEnvironmentItems", () => {
160 const environments = [
161 { environmentId: EnvironmentId.make("env-here"), label: "Laptop", machine: "laptop" },
162 { environmentId: EnvironmentId.make("env-vps"), label: "Hetzner VPS", machine: "cloud" },
163 { environmentId: EnvironmentId.make("env-mini"), label: "Mac mini", machine: "mac-mini" },
164 ] as const;
165
166 it("offers the other machines matching the query, never the composer's own", () => {
167 expect(
168 matchComposerEnvironmentItems({
169 environments,
170 linkedEnvironments: new Map(),
171 environmentId: EnvironmentId.make("env-here"),
172 query: " vps ",
173 }),
174 ).toEqual([
175 {
176 id: "environment:env-vps",
177 type: "environment",
178 environmentId: "env-vps",
179 machine: "cloud",
180 label: "Hetzner VPS",
181 description: "Machine",
182 },
183 ]);
184 expect(
185 matchComposerEnvironmentItems({
186 environments,
187 linkedEnvironments: new Map(),
188 environmentId: EnvironmentId.make("env-here"),
189 query: "lap",
190 }),
191 ).toEqual([]);
192 expect(
193 matchComposerEnvironmentItems({
194 environments,
195 linkedEnvironments: new Map(),
196 environmentId: EnvironmentId.make("env-here"),
197 query: "",
198 }),
199 ).toEqual([]);
200 });
201
202 it("offers machines the composer's environment links to that this client is not paired with", () => {
203 expect(
204 matchComposerEnvironmentItems({
205 environments: [],
206 linkedEnvironments: new Map([
207 [EnvironmentId.make("env-here"), "Laptop"],
208 [EnvironmentId.make("env-build"), "buildbox"],
209 ]),
210 environmentId: EnvironmentId.make("env-here"),
211 query: "build",
212 }),
213 ).toEqual([
214 {
215 id: "environment:env-build",
216 type: "environment",
217 environmentId: "env-build",
218 machine: "server",
219 label: "buildbox",
220 description: "Machine",
221 },
222 ]);
223 expect(
224 matchComposerEnvironmentItems({
225 environments,
226 linkedEnvironments: new Map([[EnvironmentId.make("env-vps"), "Hetzner VPS"]]),
227 environmentId: EnvironmentId.make("env-here"),
228 query: "vps",
229 }).map((item) => item.machine),
230 ).toEqual(["cloud"]);
231 });
232});