Open History to see the last commit
1import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import {
5 matchComposerEnvironmentItems,
6 matchComposerThreadItems,
7 type ComposerThreadCandidate,
8} from "./composerThreadItems.ts";
9
10const environmentId = EnvironmentId.make("env-composer");
11
12function shell(
13 id: string,
14 title: string,
15 overrides: Partial<ComposerThreadCandidate> = {},
16): ComposerThreadCandidate {
17 return {
18 environmentId,
19 id: ThreadId.make(id),
20 title,
21 branch: null,
22 createdAt: "2026-10-01T00:00:00.000Z",
23 updatedAt: "2026-10-01T00:00:00.000Z",
24 latestUserMessageAt: null,
25 opens: null,
26 archivedAt: null,
27 lineage: { rootThreadId: ThreadId.make(id), parentThreadId: null, relationshipToParent: null },
28 ...overrides,
29 };
30}
31
32it("ranks thread mentions like the palette and leaves out subagent threads", () => {
33 const items = matchComposerThreadItems({
34 shells: [
35 shell("newer-mid-word", "Remodel the settings page", {
36 updatedAt: "2026-10-20T00:00:00.000Z",
37 }),
38 shell("word", "Fix model picker"),
39 shell("subagent", "Model review", {
40 lineage: {
41 rootThreadId: ThreadId.make("word"),
42 parentThreadId: ThreadId.make("word"),
43 relationshipToParent: "subagent",
44 },
45 }),
46 shell("other-environment", "Model docs", { environmentId: EnvironmentId.make("env-other") }),
47 ],
48 environmentId,
49 linkedEnvironments: new Map(),
50 excludeThreadId: null,
51 query: "model",
52 now: Date.parse("2026-10-23T00:00:00.000Z"),
53 });
54 expect(items.map((item) => item.thread.threadId)).toEqual(["word", "newer-mid-word"]);
55});
56
57it("offers the composer thread's own used side chats, but not other threads' side chats", () => {
58 const sideChatOf = (parent: string) => ({
59 rootThreadId: ThreadId.make(parent),
60 parentThreadId: ThreadId.make(parent),
61 relationshipToParent: "fork" as const,
62 sideChat: true,
63 });
64 const latestUserMessageAt = "2026-10-22T00:00:00.000Z";
65 const items = matchComposerThreadItems({
66 shells: [
67 shell("mine", "Why is the build slow?", { lineage: sideChatOf("main"), latestUserMessageAt }),
68 shell("unused", "Slow side chat", { lineage: sideChatOf("main") }),
69 shell("theirs", "Why is the test slow?", {
70 lineage: sideChatOf("other"),
71 latestUserMessageAt,
72 }),
73 ],
74 environmentId,
75 linkedEnvironments: new Map(),
76 excludeThreadId: ThreadId.make("main"),
77 query: "slow",
78 now: Date.parse("2026-10-23T00:00:00.000Z"),
79 });
80 expect(items.map(({ thread, description }) => ({ id: thread.threadId, description }))).toEqual([
81 { id: "mine", description: "Side chat" },
82 ]);
83});
84
85it("also offers threads on linked machines, naming the machine, but no other machine's", () => {
86 const vps = EnvironmentId.make("env-vps");
87 const items = matchComposerThreadItems({
88 shells: [
89 shell("here", "Deploy script"),
90 shell("there", "Deploy script", { environmentId: vps }),
91 shell("unlinked", "Deploy notes", { environmentId: EnvironmentId.make("env-unlinked") }),
92 ],
93 environmentId,
94 linkedEnvironments: new Map([[vps, "Hetzner VPS"]]),
95 excludeThreadId: null,
96 query: "deploy",
97 now: Date.parse("2026-10-23T00:00:00.000Z"),
98 });
99 expect(items.map(({ thread, label, description }) => ({ thread, label, description }))).toEqual([
100 { thread: { environmentId, threadId: "here" }, label: "Deploy script", description: "Thread" },
101 {
102 thread: { environmentId: vps, threadId: "there" },
103 label: "Deploy script",
104 description: "Thread on Hetzner VPS",
105 },
106 ]);
107
108 const sameIdItems = matchComposerThreadItems({
109 shells: [
110 shell("same", "Deploy script"),
111 shell("same", "Deploy script", { environmentId: vps }),
112 ],
113 environmentId,
114 linkedEnvironments: new Map([[vps, "Hetzner VPS"]]),
115 excludeThreadId: ThreadId.make("same"),
116 query: "deploy",
117 now: Date.parse("2026-10-23T00:00:00.000Z"),
118 });
119 expect(sameIdItems.map(({ thread, description }) => ({ thread, description }))).toEqual([
120 {
121 thread: { environmentId: vps, threadId: "same" },
122 description: "Thread on Hetzner VPS",
123 },
124 ]);
125});
126
127describe("matchComposerEnvironmentItems", () => {
128 const environments = [
129 { environmentId: EnvironmentId.make("env-here"), label: "Laptop", machine: "laptop" },
130 { environmentId: EnvironmentId.make("env-vps"), label: "Hetzner VPS", machine: "cloud" },
131 { environmentId: EnvironmentId.make("env-mini"), label: "Mac mini", machine: "mac-mini" },
132 ] as const;
133
134 it("offers the other machines matching the query, never the composer's own", () => {
135 expect(
136 matchComposerEnvironmentItems({
137 environments,
138 linkedEnvironments: new Map(),
139 environmentId: EnvironmentId.make("env-here"),
140 query: " vps ",
141 }),
142 ).toEqual([
143 {
144 id: "environment:env-vps",
145 type: "environment",
146 environmentId: "env-vps",
147 machine: "cloud",
148 label: "Hetzner VPS",
149 description: "Machine",
150 },
151 ]);
152 expect(
153 matchComposerEnvironmentItems({
154 environments,
155 linkedEnvironments: new Map(),
156 environmentId: EnvironmentId.make("env-here"),
157 query: "lap",
158 }),
159 ).toEqual([]);
160 expect(
161 matchComposerEnvironmentItems({
162 environments,
163 linkedEnvironments: new Map(),
164 environmentId: EnvironmentId.make("env-here"),
165 query: "",
166 }),
167 ).toEqual([]);
168 });
169
170 it("offers machines the composer's environment links to that this client is not paired with", () => {
171 expect(
172 matchComposerEnvironmentItems({
173 environments: [],
174 linkedEnvironments: new Map([
175 [EnvironmentId.make("env-here"), "Laptop"],
176 [EnvironmentId.make("env-build"), "buildbox"],
177 ]),
178 environmentId: EnvironmentId.make("env-here"),
179 query: "build",
180 }),
181 ).toEqual([
182 {
183 id: "environment:env-build",
184 type: "environment",
185 environmentId: "env-build",
186 machine: "server",
187 label: "buildbox",
188 description: "Machine",
189 },
190 ]);
191 expect(
192 matchComposerEnvironmentItems({
193 environments,
194 linkedEnvironments: new Map([[EnvironmentId.make("env-vps"), "Hetzner VPS"]]),
195 environmentId: EnvironmentId.make("env-here"),
196 query: "vps",
197 }).map((item) => item.machine),
198 ).toEqual(["cloud"]);
199 });
200});