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

projectGrouping.test.ts

320 lines10.8 KB

1import { EnvironmentId, ProjectId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import type { EnvironmentProject } from "./models.ts";
5import { chooseLoadBalancedEnvironment } from "../load-balancing.ts";
6import {
7 buildProjectGroups,
8 derivePhysicalProjectKey,
9 type ProjectGroupingSettings,
10} from "./projectGrouping.ts";
11​
12const environmentId = EnvironmentId.make("environment");
13​
14describe("load balancing shared project machines", () => {
15 const now = 100_000;
16 const resources = {
17 sampledAt: now,
18 cpuUtilization: 0.2,
19 cpuCount: 8,
20 availableMemoryBytes: 8_000,
21 totalMemoryBytes: 16_000,
22 };
23​
24 it("compares three machines using free capacity and preference", () => {
25 const candidates = [
26 { environmentId: "busy", resources: { ...resources, cpuUtilization: 0.9 }, weight: 1 },
27 { environmentId: "idle", resources, weight: 1 },
28 { environmentId: "preferred", resources: { ...resources, cpuCount: 4 }, weight: 3 },
29 ];
30 expect(chooseLoadBalancedEnvironment(candidates, now)).toBe("preferred");
31 expect(chooseLoadBalancedEnvironment(candidates.slice(0, 2), now)).toBe("idle");
32 });
33​
34 it("rejects stale, unknown, excluded and saturated machines", () => {
35 expect(
36 chooseLoadBalancedEnvironment(
37 [
38 {
39 environmentId: "stale",
40 resources: { ...resources, sampledAt: now - 15_001 },
41 weight: 1,
42 },
43 { environmentId: "unknown", resources: null, weight: 1 },
44 {
45 environmentId: "no-cpu-sample",
46 resources: { ...resources, cpuUtilization: null },
47 weight: 1,
48 },
49 { environmentId: "excluded", resources, weight: 0 },
50 {
51 environmentId: "cpu-full",
52 resources: { ...resources, cpuUtilization: 0.95 },
53 weight: 1,
54 },
55 {
56 environmentId: "memory-full",
57 resources: { ...resources, availableMemoryBytes: 100 },
58 weight: 1,
59 },
60 ],
61 now,
62 ),
63 ).toBeNull();
64 });
65​
66 it("uses client receipt time when host clocks differ", () => {
67 const candidate = {
68 environmentId: "different-clock",
69 resources: { ...resources, sampledAt: now + 60_000 },
70 receivedAt: now,
71 weight: 1,
72 };
73 expect(chooseLoadBalancedEnvironment([candidate], now)).toBe("different-clock");
74 expect(chooseLoadBalancedEnvironment([candidate], now + 15_001)).toBeNull();
75 });
76});
77const repositoryIdentity = {
78 canonicalKey: "github.com/t3tools/t3code",
79 locator: {
80 source: "git-remote" as const,
81 remoteName: "upstream",
82 remoteUrl: "https://github.com/t3tools/t3code.git",
83 },
84 provider: "github",
85 owner: "t3tools",
86 name: "t3code",
87 displayName: "T3 Code",
88};
89​
90function makeProject(
91 id: string,
92 workspaceRoot: string,
93 overrides: Partial<EnvironmentProject> = {},
94): EnvironmentProject {
95 return {
96 environmentId,
97 id: ProjectId.make(id),
98 title: id,
99 workspaceRoot,
100 repositoryIdentity,
101 defaultModelSelection: null,
102 scripts: [],
103 createdAt: "2026-07-01T00:00:00.000Z",
104 updatedAt: "2026-07-01T00:00:00.000Z",
105 ...overrides,
106 };
108​
109function settings(
110 mode: ProjectGroupingSettings["sidebarProjectGroupingMode"],
111 overrides: ProjectGroupingSettings["sidebarProjectGroupingOverrides"] = {},
112): ProjectGroupingSettings {
113 return {
114 sidebarProjectGroupingMode: mode,
115 sidebarProjectGroupingOverrides: overrides,
116 };
118​
119describe("buildProjectGroups", () => {
120 it("preserves every physical clone as a selectable member in repository modes", () => {
121 const projects = [
122 makeProject("t3code", "/work/t3code"),
123 makeProject("t3code-2", "/work/t3code-2"),
124 makeProject("t3code-3", "/work/t3code-3"),
125 ];
126​
127 for (const mode of ["repository", "repository_path"] as const) {
128 const groups = buildProjectGroups({ projects, settings: settings(mode) });
129 expect(groups).toHaveLength(1);
130 expect(groups[0]?.members.map((member) => member.project.id)).toEqual([
131 "t3code",
132 "t3code-2",
133 "t3code-3",
134 ]);
135 expect(groups[0]?.memberProjectRefs).toHaveLength(3);
136 }
137 });
138​
139 it("uses a shared custom title as the repository group's label", () => {
140 const projects = [
141 makeProject("first", "/work/t3code", { title: "Custom project" }),
142 makeProject("second", "/work/t3code-2", { title: "Custom project" }),
143 ];
144​
145 expect(buildProjectGroups({ projects, settings: settings("repository") })[0]?.label).toBe(
146 "Custom project",
147 );
148 });
149​
150 it("keeps the repository label when shared titles match its repository name", () => {
151 const projects = [
152 makeProject("first", "/work/t3code", { title: "t3code" }),
153 makeProject("second", "/work/t3code-2", { title: "t3code" }),
154 ];
155​
156 expect(buildProjectGroups({ projects, settings: settings("repository") })[0]?.label).toBe(
157 "T3 Code",
158 );
159 });
160​
161 it("keeps a fork apart from its upstream checkout and labels it by its own remote", () => {
162 const fork = makeProject("fork", "/work/t3code-fork", {
163 repositoryIdentity: {
164 ...repositoryIdentity,
165 origin: {
166 canonicalKey: "github.com/julius/t3code-fork",
167 displayName: "julius/t3code-fork",
168 },
169 },
170 });
171 const forkWorktree = makeProject("fork-2", "/work/t3code-fork-2", {
172 repositoryIdentity: fork.repositoryIdentity,
173 });
174 const projects = [makeProject("t3code", "/work/t3code"), fork, forkWorktree];
175​
176 const groups = buildProjectGroups({ projects, settings: settings("repository") });
177 expect(groups.map((group) => group.key)).toEqual([
178 "github.com/t3tools/t3code",
179 "github.com/julius/t3code-fork",
180 ]);
181 expect(groups[1]?.members.map((member) => member.project.id)).toEqual(["fork", "fork-2"]);
182 expect(groups[1]?.label).toBe("julius/t3code-fork");
183 });
184​
185 it("labels a fork by its canonical key when its origin has no display name", () => {
186 const identity = {
187 ...repositoryIdentity,
188 origin: { canonicalKey: "internal-host" },
189 };
190 const projects = [
191 makeProject("fork", "/work/fork", { repositoryIdentity: identity }),
192 makeProject("fork-2", "/work/fork-2", { repositoryIdentity: identity }),
193 ];
194​
195 const groups = buildProjectGroups({ projects, settings: settings("repository") });
196 expect(groups).toHaveLength(1);
197 expect(groups[0]?.label).toBe("internal-host");
198 });
199​
200 it("keeps physical clones in separate groups when requested", () => {
201 const projects = [
202 makeProject("t3code", "/work/t3code"),
203 makeProject("t3code-2", "/work/t3code-2"),
204 makeProject("t3code-3", "/work/t3code-3"),
205 ];
206​
207 const groups = buildProjectGroups({ projects, settings: settings("separate") });
208 expect(groups).toHaveLength(3);
209 expect(groups.flatMap((group) => group.members)).toHaveLength(3);
210 expect(groups.map((group) => group.label)).toEqual(["t3code", "t3code-2", "t3code-3"]);
211 });
212​
213 it("applies a physical-project override without dropping its siblings", () => {
214 const first = makeProject("t3code", "/work/t3code");
215 const second = makeProject("t3code-2", "/work/t3code-2");
216 const third = makeProject("t3code-3", "/work/t3code-3");
217 const groups = buildProjectGroups({
218 projects: [first, second, third],
219 settings: settings("repository", {
220 [derivePhysicalProjectKey(second)]: "separate",
221 }),
222 });
223​
224 expect(groups).toHaveLength(2);
225 expect(groups.flatMap((group) => group.members.map((member) => member.project.id))).toEqual([
226 "t3code",
227 "t3code-3",
228 "t3code-2",
229 ]);
230 });
231​
232 it("dedupes stale registrations at one physical path using the freshest project", () => {
233 const stale = makeProject("stale", "/work/t3code", {
234 repositoryIdentity: null,
235 updatedAt: "2026-07-01T00:00:00.000Z",
236 });
237 const fresh = makeProject("fresh", "/work/t3code/", {
238 updatedAt: "2026-07-02T00:00:00.000Z",
239 });
240​
241 const groups = buildProjectGroups({
242 projects: [stale, fresh],
243 settings: settings("repository"),
244 });
245 expect(groups).toHaveLength(1);
246 expect(groups[0]?.members).toHaveLength(1);
247 expect(groups[0]?.representative.id).toBe("fresh");
248 expect(groups[0]?.memberProjectRefs).toHaveLength(2);
249 });
250​
251 it("uses repository identity from a duplicate registration when the winner lacks it", () => {
252 const identified = makeProject("identified", "/work/t3code", {
253 updatedAt: "2026-07-01T00:00:00.000Z",
254 });
255 const freshUnidentified = makeProject("fresh", "/work/t3code/", {
256 repositoryIdentity: null,
257 updatedAt: "2026-07-02T00:00:00.000Z",
258 });
259 const sibling = makeProject("sibling", "/work/t3code-2");
260​
261 const groups = buildProjectGroups({
262 projects: [identified, freshUnidentified, sibling],
263 settings: settings("repository"),
264 });
265 expect(groups).toHaveLength(1);
266 expect(groups[0]?.members.map((member) => member.project.id)).toEqual(["fresh", "sibling"]);
267 });
268​
269 it("uses the freshest winner's repository identity when stale duplicates disagree", () => {
270 const staleIdentity = {
271 ...repositoryIdentity,
272 canonicalKey: "github.com/t3tools/old-repository",
273 name: "old-repository",
274 displayName: "Old Repository",
275 };
276 const stale = makeProject("stale", "/work/t3code", {
277 repositoryIdentity: staleIdentity,
278 updatedAt: "2026-07-01T00:00:00.000Z",
279 });
280 const fresh = makeProject("fresh", "/work/t3code/", {
281 updatedAt: "2026-07-02T00:00:00.000Z",
282 });
283 const sibling = makeProject("sibling", "/work/t3code-2");
284​
285 const groups = buildProjectGroups({
286 projects: [stale, fresh, sibling],
287 settings: settings("repository"),
288 });
289 expect(groups).toHaveLength(1);
290 expect(groups[0]?.members.map((member) => member.project.id)).toEqual(["fresh", "sibling"]);
291 });
292​
293 it("uses the freshest identity-bearing duplicate when the winner lacks identity", () => {
294 const staleIdentity = {
295 ...repositoryIdentity,
296 canonicalKey: "github.com/t3tools/old-repository",
297 name: "old-repository",
298 displayName: "Old Repository",
299 };
300 const staleIdentified = makeProject("stale-identified", "/work/t3code", {
301 repositoryIdentity: staleIdentity,
302 updatedAt: "2026-07-01T00:00:00.000Z",
303 });
304 const freshIdentified = makeProject("fresh-identified", "/work/t3code/", {
305 updatedAt: "2026-07-02T00:00:00.000Z",
306 });
307 const winner = makeProject("winner", "/work/t3code", {
308 repositoryIdentity: null,
309 updatedAt: "2026-07-03T00:00:00.000Z",
310 });
311 const sibling = makeProject("sibling", "/work/t3code-2");
312​
313 const groups = buildProjectGroups({
314 projects: [staleIdentified, freshIdentified, winner, sibling],
315 settings: settings("repository"),
316 });
317 expect(groups).toHaveLength(1);
318 expect(groups[0]?.members.map((member) => member.project.id)).toEqual(["winner", "sibling"]);
319 });
320});