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

projectGrouping.test.ts

281 lines9.3 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 physical clones in separate groups when requested", () => {
162 const projects = [
163 makeProject("t3code", "/work/t3code"),
164 makeProject("t3code-2", "/work/t3code-2"),
165 makeProject("t3code-3", "/work/t3code-3"),
166 ];
167​
168 const groups = buildProjectGroups({ projects, settings: settings("separate") });
169 expect(groups).toHaveLength(3);
170 expect(groups.flatMap((group) => group.members)).toHaveLength(3);
171 expect(groups.map((group) => group.label)).toEqual(["t3code", "t3code-2", "t3code-3"]);
172 });
173​
174 it("applies a physical-project override without dropping its siblings", () => {
175 const first = makeProject("t3code", "/work/t3code");
176 const second = makeProject("t3code-2", "/work/t3code-2");
177 const third = makeProject("t3code-3", "/work/t3code-3");
178 const groups = buildProjectGroups({
179 projects: [first, second, third],
180 settings: settings("repository", {
181 [derivePhysicalProjectKey(second)]: "separate",
182 }),
183 });
184​
185 expect(groups).toHaveLength(2);
186 expect(groups.flatMap((group) => group.members.map((member) => member.project.id))).toEqual([
187 "t3code",
188 "t3code-3",
189 "t3code-2",
190 ]);
191 });
192​
193 it("dedupes stale registrations at one physical path using the freshest project", () => {
194 const stale = makeProject("stale", "/work/t3code", {
195 repositoryIdentity: null,
196 updatedAt: "2026-07-01T00:00:00.000Z",
197 });
198 const fresh = makeProject("fresh", "/work/t3code/", {
199 updatedAt: "2026-07-02T00:00:00.000Z",
200 });
201​
202 const groups = buildProjectGroups({
203 projects: [stale, fresh],
204 settings: settings("repository"),
205 });
206 expect(groups).toHaveLength(1);
207 expect(groups[0]?.members).toHaveLength(1);
208 expect(groups[0]?.representative.id).toBe("fresh");
209 expect(groups[0]?.memberProjectRefs).toHaveLength(2);
210 });
211​
212 it("uses repository identity from a duplicate registration when the winner lacks it", () => {
213 const identified = makeProject("identified", "/work/t3code", {
214 updatedAt: "2026-07-01T00:00:00.000Z",
215 });
216 const freshUnidentified = makeProject("fresh", "/work/t3code/", {
217 repositoryIdentity: null,
218 updatedAt: "2026-07-02T00:00:00.000Z",
219 });
220 const sibling = makeProject("sibling", "/work/t3code-2");
221​
222 const groups = buildProjectGroups({
223 projects: [identified, freshUnidentified, sibling],
224 settings: settings("repository"),
225 });
226 expect(groups).toHaveLength(1);
227 expect(groups[0]?.members.map((member) => member.project.id)).toEqual(["fresh", "sibling"]);
228 });
229​
230 it("uses the freshest winner's repository identity when stale duplicates disagree", () => {
231 const staleIdentity = {
232 ...repositoryIdentity,
233 canonicalKey: "github.com/t3tools/old-repository",
234 name: "old-repository",
235 displayName: "Old Repository",
236 };
237 const stale = makeProject("stale", "/work/t3code", {
238 repositoryIdentity: staleIdentity,
239 updatedAt: "2026-07-01T00:00:00.000Z",
240 });
241 const fresh = makeProject("fresh", "/work/t3code/", {
242 updatedAt: "2026-07-02T00:00:00.000Z",
243 });
244 const sibling = makeProject("sibling", "/work/t3code-2");
245​
246 const groups = buildProjectGroups({
247 projects: [stale, fresh, sibling],
248 settings: settings("repository"),
249 });
250 expect(groups).toHaveLength(1);
251 expect(groups[0]?.members.map((member) => member.project.id)).toEqual(["fresh", "sibling"]);
252 });
253​
254 it("uses the freshest identity-bearing duplicate when the winner lacks identity", () => {
255 const staleIdentity = {
256 ...repositoryIdentity,
257 canonicalKey: "github.com/t3tools/old-repository",
258 name: "old-repository",
259 displayName: "Old Repository",
260 };
261 const staleIdentified = makeProject("stale-identified", "/work/t3code", {
262 repositoryIdentity: staleIdentity,
263 updatedAt: "2026-07-01T00:00:00.000Z",
264 });
265 const freshIdentified = makeProject("fresh-identified", "/work/t3code/", {
266 updatedAt: "2026-07-02T00:00:00.000Z",
267 });
268 const winner = makeProject("winner", "/work/t3code", {
269 repositoryIdentity: null,
270 updatedAt: "2026-07-03T00:00:00.000Z",
271 });
272 const sibling = makeProject("sibling", "/work/t3code-2");
273​
274 const groups = buildProjectGroups({
275 projects: [staleIdentified, freshIdentified, winner, sibling],
276 settings: settings("repository"),
277 });
278 expect(groups).toHaveLength(1);
279 expect(groups[0]?.members.map((member) => member.project.id)).toEqual(["winner", "sibling"]);
280 });
281});