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

projectGrouping.test.ts

645 lines24.9 KB

1import { EnvironmentId, ProjectId, SourceControlProviderKind } 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 deriveLogicalProjectKeyFromSettings,
9 groupIconFallbacks,
10 pickableProjectGroups,
11 derivePhysicalProjectKey,
12 resolveGroupDefaultProject,
13 resolveScratchProjectScopeKey,
14 sortProjectsByGroupOrder,
15 type ProjectGroupingSettings,
16} from "./projectGrouping.ts";
17​
18const environmentId = EnvironmentId.make("environment");
19​
20describe("load balancing shared project machines", () => {
21 const now = 100_000;
22 const resources = {
23 sampledAt: now,
24 cpuUtilization: 0.2,
25 cpuCount: 8,
26 availableMemoryBytes: 8_000,
27 totalMemoryBytes: 16_000,
28 };
29​
30 it("compares three machines using free capacity and preference", () => {
31 const candidates = [
32 { environmentId: "busy", resources: { ...resources, cpuUtilization: 0.9 }, weight: 1 },
33 { environmentId: "idle", resources, weight: 1 },
34 { environmentId: "preferred", resources: { ...resources, cpuCount: 4 }, weight: 3 },
35 ];
36 expect(chooseLoadBalancedEnvironment(candidates, now)).toBe("preferred");
37 expect(chooseLoadBalancedEnvironment(candidates.slice(0, 2), now)).toBe("idle");
38 });
39​
40 it("rejects stale, unknown, excluded and saturated machines", () => {
41 expect(
42 chooseLoadBalancedEnvironment(
43 [
44 {
45 environmentId: "stale",
46 resources: { ...resources, sampledAt: now - 15_001 },
47 weight: 1,
48 },
49 { environmentId: "unknown", resources: null, weight: 1 },
50 {
51 environmentId: "no-cpu-sample",
52 resources: { ...resources, cpuUtilization: null },
53 weight: 1,
54 },
55 { environmentId: "excluded", resources, weight: 0 },
56 {
57 environmentId: "cpu-full",
58 resources: { ...resources, cpuUtilization: 0.95 },
59 weight: 1,
60 },
61 {
62 environmentId: "memory-full",
63 resources: { ...resources, availableMemoryBytes: 100 },
64 weight: 1,
65 },
66 ],
67 now,
68 ),
69 ).toBeNull();
70 });
71​
72 it("uses client receipt time when host clocks differ", () => {
73 const candidate = {
74 environmentId: "different-clock",
75 resources: { ...resources, sampledAt: now + 60_000 },
76 receivedAt: now,
77 weight: 1,
78 };
79 expect(chooseLoadBalancedEnvironment([candidate], now)).toBe("different-clock");
80 expect(chooseLoadBalancedEnvironment([candidate], now + 15_001)).toBeNull();
81 });
82});
83const repositoryIdentity = {
84 canonicalKey: "github.com/t3tools/t3code",
85 locator: {
86 source: "git-remote" as const,
87 remoteName: "upstream",
88 remoteUrl: "https://github.com/t3tools/t3code.git",
89 },
90 provider: SourceControlProviderKind.make("github"),
91 owner: "t3tools",
92 name: "t3code",
93 displayName: "T3 Code",
94};
95​
96function makeProject(
97 id: string,
98 workspaceRoot: string,
99 overrides: Partial<EnvironmentProject> = {},
100): EnvironmentProject {
101 return {
102 environmentId,
103 id: ProjectId.make(id),
104 title: id,
105 workspaceRoot,
106 repositoryIdentity,
107 defaultModelSelection: null,
108 scripts: [],
109 createdAt: "2026-07-01T00:00:00.000Z",
110 updatedAt: "2026-07-01T00:00:00.000Z",
111 ...overrides,
112 };
114​
115function settings(
116 mode: ProjectGroupingSettings["sidebarProjectGroupingMode"],
117 overrides: ProjectGroupingSettings["sidebarProjectGroupingOverrides"] = {},
118): ProjectGroupingSettings {
119 return {
120 sidebarProjectGroupingMode: mode,
121 sidebarProjectGroupingOverrides: overrides,
122 };
124​
125describe("buildProjectGroups", () => {
126 it.each(["repository", "repository_path", "separate"] as const)(
127 "keeps scratch project ownership separate by default in %s mode",
128 (mode) => {
129 const projects = [
130 makeProject("scratch-local", "/local/scratch", {
131 isScratch: true,
132 repositoryIdentity: null,
133 }),
134 makeProject("scratch-remote", "/remote/scratch", {
135 environmentId: EnvironmentId.make("remote"),
136 isScratch: true,
137 repositoryIdentity: null,
138 }),
139 ];
140 const groups = buildProjectGroups({ projects, settings: settings(mode) });
141 expect(groups.map((group) => group.key)).toEqual(projects.map(derivePhysicalProjectKey));
142 expect(groups.map((group) => group.memberProjectRefs)).toEqual(
143 projects.map((project) => [
144 { environmentId: project.environmentId, projectId: project.id },
145 ]),
146 );
147 expect(
148 buildProjectGroups({ projects, settings: settings(mode), groupScratchProjects: true }),
149 ).toHaveLength(1);
150 },
151 );
152​
153 it.each(["repository", "repository_path", "separate"] as const)(
154 "migrates a pre-config %s scratch selection and retains it after the original host is removed",
155 (mode) => {
156 const local = makeProject("scratch-local", "/local/repo/scratch", {
157 repositoryIdentity: { ...repositoryIdentity, rootPath: "/local/repo" },
158 });
159 const remote = makeProject("scratch-remote", "/remote/scratch", {
160 environmentId: EnvironmentId.make("remote"),
161 repositoryIdentity: null,
162 isScratch: true,
163 });
164 const groupingSettings = settings(mode);
165 const selectedBeforeConfig = deriveLogicalProjectKeyFromSettings(local, groupingSettings);
166 const loaded = [{ ...local, isScratch: true as const }, remote];
167 const migrated = resolveScratchProjectScopeKey(
168 selectedBeforeConfig,
169 loaded,
170 groupingSettings,
171 );
172 const scratchKey = buildProjectGroups({
173 projects: loaded,
174 settings: groupingSettings,
175 groupScratchProjects: true,
176 })[0]!.key;
177 expect(migrated).toBe(scratchKey);
178 expect(resolveScratchProjectScopeKey(migrated, [remote], groupingSettings)).toBe(scratchKey);
179 expect(
180 buildProjectGroups({
181 projects: [remote],
182 settings: groupingSettings,
183 groupScratchProjects: true,
184 })[0]?.memberProjectRefs,
185 ).toEqual([{ environmentId: remote.environmentId, projectId: remote.id }]);
186 },
187 );
188​
189 it("retains a repository filter that still belongs to an ordinary project", () => {
190 const scratch = makeProject("scratch", "/scratch", { isScratch: true });
191 const ordinary = makeProject("ordinary", "/ordinary");
192 const key = deriveLogicalProjectKeyFromSettings(ordinary, settings("repository"));
193 expect(resolveScratchProjectScopeKey(key, [scratch, ordinary])).toBe(key);
194 });
195​
196 it.each(["repository", "repository_path", "separate"] as const)(
197 "coalesces scratch homes in %s mode without merging ordinary same-title projects",
198 (mode) => {
199 const remoteEnvironmentId = EnvironmentId.make("remote");
200 const projects = [
201 makeProject("scratch-local", "/local/scratch", {
202 title: "No project",
203 isScratch: true,
204 repositoryIdentity: null,
205 }),
206 makeProject("scratch-remote", "/remote/scratch", {
207 title: "No project",
208 environmentId: remoteEnvironmentId,
209 isScratch: true,
210 }),
211 makeProject("scratch-duplicate", "/remote/scratch/", {
212 title: "No project",
213 environmentId: remoteEnvironmentId,
214 isScratch: true,
215 repositoryIdentity: null,
216 updatedAt: "2026-07-02T00:00:00.000Z",
217 }),
218 makeProject("ordinary", "/ordinary", { title: "No project" }),
219 makeProject("reserved-collision", "/other", {
220 title: "No project",
221 repositoryIdentity: { ...repositoryIdentity, canonicalKey: "\0no-project" },
222 }),
223 ];
224 const groups = buildProjectGroups({
225 projects,
226 groupScratchProjects: true,
227 settings: settings(mode, { [derivePhysicalProjectKey(projects[0]!)]: "separate" }),
228 preferredEnvironmentId: remoteEnvironmentId,
229 });
230 expect(groups).toHaveLength(3);
231 const scratch = groups.find((group) => group.representative.isScratch)!;
232 expect(scratch.label).toBe("No project");
233 expect(scratch.representative.id).toBe(projects[2]?.id);
234 expect(scratch.members).toHaveLength(2);
235 expect(scratch.memberProjectRefs).toEqual(
236 projects.slice(0, 3).map((project) => ({
237 environmentId: project.environmentId,
238 projectId: project.id,
239 })),
240 );
241 expect(resolveScratchProjectScopeKey(derivePhysicalProjectKey(projects[1]!), projects)).toBe(
242 scratch.key,
243 );
244 expect(resolveScratchProjectScopeKey(derivePhysicalProjectKey(projects[3]!), projects)).toBe(
245 derivePhysicalProjectKey(projects[3]!),
246 );
247 expect(groups.filter((group) => !group.representative.isScratch)).toHaveLength(2);
248 },
249 );
250​
251 it("preserves every physical clone as a selectable member in repository modes", () => {
252 const projects = [
253 makeProject("t3code", "/work/t3code"),
254 makeProject("t3code-2", "/work/t3code-2"),
255 makeProject("t3code-3", "/work/t3code-3"),
256 ];
257​
258 for (const mode of ["repository", "repository_path"] as const) {
259 const groups = buildProjectGroups({ projects, settings: settings(mode) });
260 expect(groups).toHaveLength(1);
261 expect(groups[0]?.members.map((member) => member.project.id)).toEqual([
262 "t3code",
263 "t3code-2",
264 "t3code-3",
265 ]);
266 expect(groups[0]?.memberProjectRefs).toHaveLength(3);
267 }
268 });
269​
270 it("represents a repository group by its root checkout, even when it was added last", () => {
271 const remoteEnvironmentId = EnvironmentId.make("remote");
272 const local = { ...repositoryIdentity, rootPath: "/work/workspace" };
273 const remote = { ...repositoryIdentity, rootPath: "/srv/workspace" };
274 const archivedTool = makeProject("old-tool", "/work/workspace/_archive/old-tool", {
275 repositoryIdentity: local,
276 });
277 const app = makeProject("app-a", "/work/workspace/app-a", { repositoryIdentity: local });
278 const remoteApp = makeProject("remote-app", "/srv/workspace/app-a", {
279 environmentId: remoteEnvironmentId,
280 repositoryIdentity: remote,
281 });
282 const remoteRoot = makeProject("remote-root", "/srv/workspace", {
283 environmentId: remoteEnvironmentId,
284 repositoryIdentity: remote,
285 });
286 const root = makeProject("workspace", "/work/workspace", { repositoryIdentity: local });
287 const representativeOf = (
288 projects: ReadonlyArray<EnvironmentProject>,
289 preferredEnvironmentId: EnvironmentId | null = null,
290 ) =>
291 buildProjectGroups({ projects, settings: settings("repository"), preferredEnvironmentId })[0]
292 ?.representative.id;
293​
294 expect(representativeOf([archivedTool, app, root])).toBe("workspace");
295 expect(representativeOf([archivedTool, app, remoteApp, remoteRoot, root], environmentId)).toBe(
296 "workspace",
297 );
298 expect(
299 representativeOf([archivedTool, app, remoteApp, remoteRoot, root], remoteEnvironmentId),
300 ).toBe("remote-root");
301 // A local root is not worth leaving the current environment for.
302 expect(representativeOf([archivedTool, root, remoteApp], remoteEnvironmentId)).toBe(
303 "remote-app",
304 );
305 expect(representativeOf([archivedTool, app])).toBe("old-tool");
306​
307 // A new thread with no current or last-used project starts from the first project's group.
308 const all = [archivedTool, app, remoteApp, remoteRoot, root];
309 expect(resolveGroupDefaultProject(archivedTool, all, settings("repository")).id).toBe(
310 "workspace",
311 );
312 expect(resolveGroupDefaultProject(remoteApp, all, settings("repository")).id).toBe(
313 "remote-root",
314 );
315 expect(resolveGroupDefaultProject(archivedTool, all, settings("separate")).id).toBe("old-tool");
316 });
317​
318 it("keeps a fork apart from its upstream checkout and labels it by its own remote", () => {
319 const fork = makeProject("fork", "/work/t3code-fork", {
320 repositoryIdentity: {
321 ...repositoryIdentity,
322 origin: {
323 canonicalKey: "github.com/julius/t3code-fork",
324 displayName: "julius/t3code-fork",
325 },
326 },
327 });
328 const forkWorktree = makeProject("fork-2", "/work/t3code-fork-2", {
329 repositoryIdentity: fork.repositoryIdentity,
330 });
331 const projects = [makeProject("t3code", "/work/t3code"), fork, forkWorktree];
332​
333 const groups = buildProjectGroups({ projects, settings: settings("repository") });
334 expect(groups.map((group) => group.key)).toEqual([
335 "github.com/t3tools/t3code",
336 "github.com/julius/t3code-fork",
337 ]);
338 expect(groups[1]?.members.map((member) => member.project.id)).toEqual(["fork", "fork-2"]);
339 expect(groups[1]?.label).toBe("julius/t3code-fork");
340 });
341​
342 // regression: #15049 a rename to the repository name snapped back to the display path
343 it("labels a renamed group by its title even when it matches the repository name", () => {
344 const folderTitles = [
345 makeProject("t3code", "/work/t3code", { title: "t3code" }),
346 makeProject("t3code-2", "/work/t3code-2", { title: "t3code" }),
347 ];
348 const renamed = folderTitles.map((project) => ({ ...project, hasCustomTitle: true }));
349​
350 const label = (projects: ReadonlyArray<EnvironmentProject>) =>
351 buildProjectGroups({ projects, settings: settings("repository") })[0]?.label;
352 expect(label(folderTitles)).toBe("T3 Code");
353 expect(label(renamed)).toBe("t3code");
354 });
355​
356 it("labels a renamed group by its rename after a checkout with a folder title joins it", () => {
357 const checkout = (id: string, root: string, overrides: Partial<EnvironmentProject> = {}) =>
358 makeProject(id, root, {
359 title: "t3code",
360 repositoryIdentity: { ...repositoryIdentity, rootPath: root },
361 ...overrides,
362 });
363 const renamed = checkout("renamed", "/work/t3code", { title: "T3", hasCustomTitle: true });
364 const joined = checkout("joined", "/remote/t3code", {
365 environmentId: EnvironmentId.make("remote"),
366 });
367 const renamedSubfolder = makeProject("web", "/remote/t3code/apps/web", {
368 title: "Web",
369 hasCustomTitle: true,
370 repositoryIdentity: { ...repositoryIdentity, rootPath: "/remote/t3code" },
371 });
372​
373 const label = (projects: ReadonlyArray<EnvironmentProject>) =>
374 buildProjectGroups({ projects, settings: settings("repository") })[0]?.label;
375 expect(label([renamed, joined])).toBe("T3");
376 expect(label([joined, renamedSubfolder])).toBe("T3 Code");
377 // An older server may not send the repository root; like grouping, read that as the root.
378 expect(label([{ ...renamed, repositoryIdentity }, joined])).toBe("T3");
379​
380 const joinedSubfolder = makeProject("web", "/work/t3code/apps/web", {
381 title: "web",
382 repositoryIdentity: { ...repositoryIdentity, rootPath: "/work/t3code" },
383 });
384 const folderGroupLabel = buildProjectGroups({
385 projects: [renamedSubfolder, joinedSubfolder],
386 settings: settings("repository_path"),
387 })[0]?.label;
388 expect(folderGroupLabel).toBe("Web");
389 });
390​
391 it("labels a group by its repository when clones disagree on its casing", () => {
392 // A subfolder project, its repository root, and a clone whose remote URL is
393 // lowercased: no shared title or display name, but one repository.
394 const subfolder = makeProject("design-study", "/work/NewCo/reference/design-study");
395 const root = makeProject("NewCo", "/work/NewCo");
396 const lowercasedClone = makeProject("NewCo", "/remote/NewCo", {
397 environmentId: EnvironmentId.make("remote"),
398 repositoryIdentity: { ...repositoryIdentity, name: "T3CODE", displayName: "t3 code" },
399 });
400​
401 const groups = buildProjectGroups({
402 projects: [subfolder, root, lowercasedClone],
403 settings: settings("repository"),
404 preferredEnvironmentId: environmentId,
405 });
406 expect(groups).toHaveLength(1);
407 expect(groups[0]?.label).toBe("T3 Code");
408​
409 // A subfolder group in repository_path mode keeps its own title.
410 const localIdentity = { ...repositoryIdentity, rootPath: "/work/t3code" };
411 const pathGroups = buildProjectGroups({
412 projects: [
413 makeProject("design", "/work/t3code/design", {
414 title: "Design docs",
415 hasCustomTitle: true,
416 repositoryIdentity: localIdentity,
417 }),
418 makeProject("design", "/remote/t3code/design", {
419 environmentId: EnvironmentId.make("remote"),
420 repositoryIdentity: {
421 ...repositoryIdentity,
422 rootPath: "/remote/t3code",
423 name: "T3CODE",
424 displayName: "t3 code",
425 },
426 }),
427 makeProject("t3code", "/work/t3code", { repositoryIdentity: localIdentity }),
428 ],
429 settings: settings("repository_path"),
430 preferredEnvironmentId: environmentId,
431 });
432 expect(pathGroups.map((group) => group.label)).toEqual(["Design docs", "t3code"]);
433 });
434​
435 it.each([
436 ["/", ["/", "/app", "/tools"]],
437 ["D:\\", ["D:/", "D:/app", "D:/tools"]],
438 ] as const)("keeps folder groups apart in a repository checked out at %s", (rootPath, paths) => {
439 const identity = { ...repositoryIdentity, rootPath };
440 const groups = buildProjectGroups({
441 projects: paths.map((path, index) =>
442 makeProject(`project-${index}`, path, { repositoryIdentity: identity }),
443 ),
444 settings: settings("repository_path"),
445 });
446​
447 const key = repositoryIdentity.canonicalKey;
448 expect(groups.map((group) => group.key)).toEqual([key, `${key}::app`, `${key}::tools`]);
449 });
450​
451 it("keeps a POSIX folder name ending in a backslash as part of the repository root", () => {
452 const identity = { ...repositoryIdentity, rootPath: "/work/repo\\" };
453 const groups = buildProjectGroups({
454 projects: [
455 makeProject("app", "/work/repo\\/app", { repositoryIdentity: identity }),
456 makeProject("sibling", "/work/repo\\other", { repositoryIdentity: identity }),
457 ],
458 settings: settings("repository_path"),
459 });
460​
461 const key = repositoryIdentity.canonicalKey;
462 expect(groups.map((group) => group.key)).toEqual([`${key}::app`, key]);
463 });
464​
465 it("keeps literal POSIX backslashes distinct from repository path separators", () => {
466 const identity = { ...repositoryIdentity, canonicalKey: "host/org/repo", rootPath: "/repo" };
467 const groups = buildProjectGroups({
468 projects: [
469 makeProject("literal", String.raw`/repo/a\b`, { repositoryIdentity: identity }),
470 makeProject("nested", "/repo/a/b", { repositoryIdentity: identity }),
471 makeProject("windows", String.raw`C:\repo\a\b`, {
472 environmentId: EnvironmentId.make("windows"),
473 repositoryIdentity: { ...identity, rootPath: String.raw`C:\repo` },
474 }),
475 ],
476 settings: settings("repository_path"),
477 });
478​
479 expect(groups.map((group) => group.key)).toEqual([
480 String.raw`host/org/repo::a\b`,
481 "host/org/repo::a/b",
482 ]);
483 expect(groups.map((group) => group.members.map(({ project }) => project.id))).toEqual([
484 ["literal"],
485 ["nested", "windows"],
486 ]);
487 });
488​
489 it("applies a physical-project override without dropping its siblings", () => {
490 const first = makeProject("t3code", "/work/t3code");
491 const second = makeProject("t3code-2", "/work/t3code-2");
492 const third = makeProject("t3code-3", "/work/t3code-3");
493 const groups = buildProjectGroups({
494 projects: [first, second, third],
495 settings: settings("repository", {
496 [derivePhysicalProjectKey(second)]: "separate",
497 }),
498 });
499​
500 expect(groups).toHaveLength(2);
501 expect(groups.flatMap((group) => group.members.map((member) => member.project.id))).toEqual([
502 "t3code",
503 "t3code-3",
504 "t3code-2",
505 ]);
506 });
507​
508 it("dedupes stale registrations at one physical path using the freshest project", () => {
509 const stale = makeProject("stale", "/work/t3code", {
510 repositoryIdentity: null,
511 updatedAt: "2026-07-01T00:00:00.000Z",
512 });
513 const fresh = makeProject("fresh", "/work/t3code/", {
514 updatedAt: "2026-07-02T00:00:00.000Z",
515 });
516​
517 const groups = buildProjectGroups({
518 projects: [stale, fresh],
519 settings: settings("repository"),
520 });
521 expect(groups).toHaveLength(1);
522 expect(groups[0]?.members).toHaveLength(1);
523 expect(groups[0]?.representative.id).toBe("fresh");
524 expect(groups[0]?.memberProjectRefs).toHaveLength(2);
525 });
526​
527 it("uses the freshest winner's repository identity when stale duplicates disagree", () => {
528 const staleIdentity = {
529 ...repositoryIdentity,
530 canonicalKey: "github.com/t3tools/old-repository",
531 name: "old-repository",
532 displayName: "Old Repository",
533 };
534 const stale = makeProject("stale", "/work/t3code", {
535 repositoryIdentity: staleIdentity,
536 updatedAt: "2026-07-01T00:00:00.000Z",
537 });
538 const fresh = makeProject("fresh", "/work/t3code/", {
539 updatedAt: "2026-07-02T00:00:00.000Z",
540 });
541 const sibling = makeProject("sibling", "/work/t3code-2");
542​
543 const groups = buildProjectGroups({
544 projects: [stale, fresh, sibling],
545 settings: settings("repository"),
546 });
547 expect(groups).toHaveLength(1);
548 expect(groups[0]?.members.map((member) => member.project.id)).toEqual(["fresh", "sibling"]);
549 });
550​
551 it("uses the freshest identity-bearing duplicate when the winner lacks identity", () => {
552 const staleIdentity = {
553 ...repositoryIdentity,
554 canonicalKey: "github.com/t3tools/old-repository",
555 name: "old-repository",
556 displayName: "Old Repository",
557 };
558 const staleIdentified = makeProject("stale-identified", "/work/t3code", {
559 repositoryIdentity: staleIdentity,
560 updatedAt: "2026-07-01T00:00:00.000Z",
561 });
562 const freshIdentified = makeProject("fresh-identified", "/work/t3code/", {
563 updatedAt: "2026-07-02T00:00:00.000Z",
564 });
565 const winner = makeProject("winner", "/work/t3code", {
566 repositoryIdentity: null,
567 updatedAt: "2026-07-03T00:00:00.000Z",
568 });
569 const sibling = makeProject("sibling", "/work/t3code-2");
570​
571 const groups = buildProjectGroups({
572 projects: [staleIdentified, freshIdentified, winner, sibling],
573 settings: settings("repository"),
574 });
575 expect(groups).toHaveLength(1);
576 expect(groups[0]?.members.map((member) => member.project.id)).toEqual(["winner", "sibling"]);
577 });
578});
579​
580describe("project pins and archives", () => {
581 const at = "2026-07-02T00:00:00.000Z";
582 const remote = EnvironmentId.make("remote");
583 const group = (...projects: EnvironmentProject[]) =>
584 buildProjectGroups({ projects, settings: settings("repository") })[0]!;
585​
586 it("keeps a project visible while its machines disagree", () => {
587 const local = makeProject("local", "/work/t3code");
588 const other = makeProject("remote", "/work/t3code", { environmentId: remote });
589 const partial = group({ ...local, pinnedAt: at, archivedAt: at }, other);
590 expect(partial).toMatchObject({ pinned: true, archived: false });
591 expect(group({ ...local, archivedAt: at }, { ...other, archivedAt: at }).archived).toBe(true);
592 });
593​
594 it("offers pinned projects first and leaves archived ones out", () => {
595 const pickable = pickableProjectGroups([
596 { key: "a", pinned: false, archived: false },
597 { key: "b", pinned: true, archived: true },
598 { key: "c", pinned: true, archived: false },
599 { key: "d", pinned: false, archived: true },
600 ]);
601 expect(pickable.map((entry) => entry.key)).toEqual(["c", "a"]);
602 });
603});
604​
605describe("sortProjectsByGroupOrder", () => {
606 const remote = EnvironmentId.make("remote");
607 const ref = (projectId: string, env = environmentId) => ({
608 environmentId: env,
609 projectId: ProjectId.make(projectId),
610 });
611 const project = (id: string, env: string = environmentId) => ({ id, environmentId: env });
612​
613 it("tells the same project id on two environments apart", () => {
614 const sorted = sortProjectsByGroupOrder(
615 [project("shared"), project("shared", remote)],
616 [
617 { memberProjectRefs: [ref("shared", remote)], pinned: false, archived: false },
618 { memberProjectRefs: [ref("shared")], pinned: false, archived: false },
619 ],
620 );
621 expect(sorted.map((entry) => entry.environmentId)).toEqual([remote, environmentId]);
622 });
623​
624 it("lists pinned projects first and archived ones last, like the sidebar's project menu", () => {
625 const sorted = sortProjectsByGroupOrder(
626 [project("archived"), project("recent"), project("pinned"), project("unlisted")],
627 [
628 { memberProjectRefs: [ref("archived")], pinned: false, archived: true },
629 { memberProjectRefs: [ref("recent")], pinned: false, archived: false },
630 { memberProjectRefs: [ref("pinned")], pinned: true, archived: false },
631 ],
632 );
633 expect(sorted.map((entry) => entry.id)).toEqual(["pinned", "recent", "archived", "unlisted"]);
634 });
635});
636​
637describe("groupIconFallbacks", () => {
638 it("lists the group's other members in order, leaving out its representative", () => {
639 const remote = EnvironmentId.make("remote");
640 const root = makeProject("root", "/work/t3code");
641 const web = makeProject("web", "/work/t3code/apps/web");
642 const remoteRoot = makeProject("root", "/work/t3code", { environmentId: remote });
643 expect(groupIconFallbacks([web, root, remoteRoot], root)).toEqual([web, remoteRoot]);
644 });
645});