Open History to see the last commit
1import { EnvironmentId, ProjectId, ProviderInstanceId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import { deriveLogicalProjectKeyFromSettings } from "./logicalProject";
5import {
6 buildSidebarProjectPickerEntries,
7 buildSidebarProjectSnapshots,
8 resolveSidebarProjectScopeKey,
9 getSidebarProjectSettingsKey,
10} from "./sidebarProjectGrouping";
11import type { Project } from "./types";
12
13const primaryEnvironmentId = EnvironmentId.make("env-primary");
14const remoteEnvironmentId = EnvironmentId.make("env-remote");
15const repositoryIdentity = {
16 canonicalKey: "github.com/example/shared-repo",
17 locator: {
18 source: "git-remote" as const,
19 remoteName: "origin",
20 remoteUrl: "https://github.com/example/shared-repo.git",
21 },
22};
23const defaultGroupingSettings = {
24 sidebarProjectGroupingMode: "repository" as const,
25 sidebarProjectGroupingOverrides: {},
26};
27
28function makeProject(overrides: Partial<Project> = {}): Project {
29 return {
30 id: ProjectId.make("project-1"),
31 environmentId: primaryEnvironmentId,
32 title: "shared-repo",
33 workspaceRoot: "/tmp/shared-repo",
34 repositoryIdentity: null,
35 defaultModelSelection: {
36 instanceId: ProviderInstanceId.make("codex"),
37 model: "gpt-5-codex",
38 },
39 createdAt: "2026-01-01T00:00:00.000Z",
40 updatedAt: "2026-01-01T00:00:00.000Z",
41 scripts: [],
42 ...overrides,
43 };
44}
45
46describe("environment grouping", () => {
47 it("opens physical scratch settings from the merged filter, including a remote thread target", () => {
48 const local = makeProject({
49 title: "No project",
50 workspaceRoot: "/local/scratch",
51 isScratch: true,
52 });
53 const remote = makeProject({
54 title: "No project",
55 id: ProjectId.make("remote-scratch"),
56 environmentId: remoteEnvironmentId,
57 workspaceRoot: "/remote/scratch",
58 isScratch: true,
59 });
60 const input = {
61 projects: [local, remote],
62 settings: defaultGroupingSettings,
63 primaryEnvironmentId,
64 resolveEnvironmentLabel: () => null,
65 };
66 const filters = buildSidebarProjectSnapshots({ ...input, groupScratchProjects: true });
67 const settingsGroups = buildSidebarProjectSnapshots(input);
68 expect(filters).toHaveLength(1);
69 expect(settingsGroups).toHaveLength(2);
70 expect(settingsGroups.map((group) => group.memberProjectRefs)).toEqual([
71 [{ environmentId: local.environmentId, projectId: local.id }],
72 [{ environmentId: remote.environmentId, projectId: remote.id }],
73 ]);
74 expect(getSidebarProjectSettingsKey(filters[0]!, defaultGroupingSettings, input.projects)).toBe(
75 settingsGroups[0]?.projectKey,
76 );
77 expect(
78 getSidebarProjectSettingsKey(filters[0]!, defaultGroupingSettings, input.projects, {
79 environmentId: remote.environmentId,
80 projectId: remote.id,
81 }),
82 ).toBe(settingsGroups[1]?.projectKey);
83 });
84
85 it.each(["repository", "repository_path"] as const)(
86 "migrates a scratch scope selected before config in %s mode",
87 (mode) => {
88 const project = makeProject({
89 workspaceRoot: "/repo/scratch",
90 repositoryIdentity: { ...repositoryIdentity, rootPath: "/repo" },
91 });
92 const settings = { ...defaultGroupingSettings, sidebarProjectGroupingMode: mode };
93 const key = deriveLogicalProjectKeyFromSettings(project, settings);
94 const groups = buildSidebarProjectSnapshots({
95 projects: [{ ...project, isScratch: true }],
96 groupScratchProjects: true,
97 settings,
98 primaryEnvironmentId,
99 resolveEnvironmentLabel: () => null,
100 });
101 expect(
102 resolveSidebarProjectScopeKey({ groups, key, settings, canClearMissingScope: true }),
103 ).toBe(groups[0]?.projectKey);
104 },
105 );
106
107 it("prefers an existing ordinary repository scope over a scratch alias", () => {
108 const scratch = makeProject({ isScratch: true, repositoryIdentity });
109 const ordinary = makeProject({
110 id: ProjectId.make("ordinary"),
111 workspaceRoot: "/ordinary",
112 repositoryIdentity,
113 });
114 const groups = buildSidebarProjectSnapshots({
115 projects: [scratch, ordinary],
116 groupScratchProjects: true,
117 settings: defaultGroupingSettings,
118 primaryEnvironmentId,
119 resolveEnvironmentLabel: () => null,
120 });
121 const key = deriveLogicalProjectKeyFromSettings(ordinary, defaultGroupingSettings);
122 expect(resolveSidebarProjectScopeKey({ groups, key, canClearMissingScope: true })).toBe(key);
123 });
124
125 it("dedupes stale project rows with the same environment and workspace path", () => {
126 const duplicate = makeProject({
127 id: ProjectId.make("project-duplicate"),
128 workspaceRoot: "/tmp/shared-repo/",
129 repositoryIdentity,
130 updatedAt: "2026-01-01T00:00:00.000Z",
131 });
132 const primary = makeProject({
133 id: ProjectId.make("project-primary"),
134 repositoryIdentity,
135 updatedAt: "2026-01-02T00:00:00.000Z",
136 });
137 const remote = makeProject({
138 id: ProjectId.make("project-remote"),
139 environmentId: remoteEnvironmentId,
140 workspaceRoot: "/tmp/shared-repo",
141 repositoryIdentity,
142 });
143
144 const snapshots = buildSidebarProjectSnapshots({
145 projects: [primary, duplicate, remote],
146 settings: defaultGroupingSettings,
147 primaryEnvironmentId,
148 resolveEnvironmentLabel: (environmentId) =>
149 environmentId === remoteEnvironmentId ? "remote" : "primary",
150 });
151
152 expect(snapshots).toHaveLength(1);
153 expect(snapshots[0]?.groupedProjectCount).toBe(2);
154 expect(snapshots[0]?.memberProjects.map((project) => project.id)).toEqual([
155 primary.id,
156 remote.id,
157 ]);
158 });
159
160 it("dedupes stale project rows before logical grouping", () => {
161 const staleWithoutRepositoryIdentity = makeProject({
162 id: ProjectId.make("project-stale"),
163 repositoryIdentity: null,
164 updatedAt: "2026-01-01T00:00:00.000Z",
165 });
166 const canonical = makeProject({
167 id: ProjectId.make("project-canonical"),
168 repositoryIdentity,
169 updatedAt: "2026-01-02T00:00:00.000Z",
170 });
171 const remote = makeProject({
172 id: ProjectId.make("project-remote"),
173 environmentId: remoteEnvironmentId,
174 repositoryIdentity,
175 });
176
177 const snapshots = buildSidebarProjectSnapshots({
178 projects: [staleWithoutRepositoryIdentity, canonical, remote],
179 settings: defaultGroupingSettings,
180 primaryEnvironmentId,
181 resolveEnvironmentLabel: (environmentId) =>
182 environmentId === remoteEnvironmentId ? "remote" : "primary",
183 });
184
185 expect(snapshots).toHaveLength(1);
186 expect(snapshots[0]?.projectKey).toBe(repositoryIdentity.canonicalKey);
187 expect(snapshots[0]?.memberProjects.map((project) => project.id)).toEqual([
188 canonical.id,
189 remote.id,
190 ]);
191 expect(snapshots[0]?.memberProjectRefs).toEqual([
192 {
193 environmentId: primaryEnvironmentId,
194 projectId: staleWithoutRepositoryIdentity.id,
195 },
196 { environmentId: primaryEnvironmentId, projectId: canonical.id },
197 { environmentId: remoteEnvironmentId, projectId: remote.id },
198 ]);
199
200 const [pickerEntry] = buildSidebarProjectPickerEntries({
201 groups: snapshots,
202 preferredProjectRef: {
203 environmentId: primaryEnvironmentId,
204 projectId: staleWithoutRepositoryIdentity.id,
205 },
206 });
207 expect(pickerEntry?.isPreferred).toBe(true);
208 expect(pickerEntry?.targetProject.id).toBe(canonical.id);
209 });
210
211 it("keeps the current environment when available and falls back otherwise", () => {
212 const currentPrimary = makeProject({ repositoryIdentity });
213 const currentRemote = makeProject({
214 id: ProjectId.make("current-remote"),
215 environmentId: remoteEnvironmentId,
216 repositoryIdentity,
217 });
218 const destinationRepositoryIdentity = {
219 canonicalKey: "github.com/example/destination",
220 locator: {
221 source: "git-remote" as const,
222 remoteName: "origin",
223 remoteUrl: "https://github.com/example/destination.git",
224 },
225 };
226 const destinationPrimary = makeProject({
227 id: ProjectId.make("destination-primary"),
228 title: "destination",
229 workspaceRoot: "/tmp/destination",
230 repositoryIdentity: destinationRepositoryIdentity,
231 });
232 const destinationRemote = makeProject({
233 id: ProjectId.make("destination-remote"),
234 environmentId: remoteEnvironmentId,
235 title: "destination",
236 workspaceRoot: "/remote/destination",
237 repositoryIdentity: destinationRepositoryIdentity,
238 });
239 const fallbackPrimary = makeProject({
240 id: ProjectId.make("fallback-primary"),
241 title: "fallback",
242 workspaceRoot: "/tmp/fallback",
243 });
244 const groups = buildSidebarProjectSnapshots({
245 projects: [
246 currentPrimary,
247 currentRemote,
248 destinationPrimary,
249 destinationRemote,
250 fallbackPrimary,
251 ],
252 settings: defaultGroupingSettings,
253 primaryEnvironmentId,
254 resolveEnvironmentLabel: () => null,
255 });
256
257 const entries = buildSidebarProjectPickerEntries({
258 groups,
259 preferredProjectRef: {
260 environmentId: remoteEnvironmentId,
261 projectId: currentRemote.id,
262 },
263 });
264 const destination = entries.find(
265 (entry) => entry.group.projectKey === destinationRepositoryIdentity.canonicalKey,
266 );
267 const fallback = entries.find((entry) => entry.group.displayName === "fallback");
268
269 expect(destination?.targetProject).toMatchObject({
270 environmentId: remoteEnvironmentId,
271 id: destinationRemote.id,
272 });
273 expect(fallback?.targetProject).toMatchObject({
274 environmentId: primaryEnvironmentId,
275 id: fallbackPrimary.id,
276 });
277 });
278});